blob: 65c01798d843052fb0ef6007606aa57b5865e931 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020031#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010033#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020035#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020036#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020037#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Takashi Iwaid66fee52011-08-02 15:39:31 +020039#define CREATE_TRACE_POINTS
40#include "hda_trace.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * vendor / preset table
44 */
45
46struct hda_vendor_id {
47 unsigned int id;
48 const char *name;
49};
50
51/* codec vendor labels */
52static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020054 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020055 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010057 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010058 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020059 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010060 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020061 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010062 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020063 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020065 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010066 { 0x17e8, "Chrontel" },
67 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000068 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010070 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020071 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 {} /* terminator */
73};
74
Takashi Iwai1289e9e2008-11-27 15:47:11 +010075static DEFINE_MUTEX(preset_mutex);
76static LIST_HEAD(hda_preset_tables);
77
78int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
79{
80 mutex_lock(&preset_mutex);
81 list_add_tail(&preset->list, &hda_preset_tables);
82 mutex_unlock(&preset_mutex);
83 return 0;
84}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010085EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010086
87int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
88{
89 mutex_lock(&preset_mutex);
90 list_del(&preset->list);
91 mutex_unlock(&preset_mutex);
92 return 0;
93}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010094EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Takashi Iwaicb53c622007-08-10 17:21:45 +020096#ifdef CONFIG_SND_HDA_POWER_SAVE
97static void hda_power_work(struct work_struct *work);
98static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020099#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200100#else
101static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200102#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200103#endif
104
Takashi Iwaid5191e52009-11-16 14:58:17 +0100105/**
106 * snd_hda_get_jack_location - Give a location string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack location, e.g. "Rear", "Front", etc.
111 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400112const char *snd_hda_get_jack_location(u32 cfg)
113{
114 static char *bases[7] = {
115 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
116 };
117 static unsigned char specials_idx[] = {
118 0x07, 0x08,
119 0x17, 0x18, 0x19,
120 0x37, 0x38
121 };
122 static char *specials[] = {
123 "Rear Panel", "Drive Bar",
124 "Riser", "HDMI", "ATAPI",
125 "Mobile-In", "Mobile-Out"
126 };
127 int i;
128 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
129 if ((cfg & 0x0f) < 7)
130 return bases[cfg & 0x0f];
131 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
132 if (cfg == specials_idx[i])
133 return specials[i];
134 }
135 return "UNKNOWN";
136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100137EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138
Takashi Iwaid5191e52009-11-16 14:58:17 +0100139/**
140 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
141 * @cfg: pin default config value
142 *
143 * Parse the pin default config value and returns the string of the
144 * jack connectivity, i.e. external or internal connection.
145 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146const char *snd_hda_get_jack_connectivity(u32 cfg)
147{
148 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
149
150 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100152EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153
Takashi Iwaid5191e52009-11-16 14:58:17 +0100154/**
155 * snd_hda_get_jack_type - Give a type string of the jack
156 * @cfg: pin default config value
157 *
158 * Parse the pin default config value and returns the string of the
159 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
160 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400161const char *snd_hda_get_jack_type(u32 cfg)
162{
163 static char *jack_types[16] = {
164 "Line Out", "Speaker", "HP Out", "CD",
165 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
166 "Line In", "Aux", "Mic", "Telephony",
167 "SPDIF In", "Digitial In", "Reserved", "Other"
168 };
169
170 return jack_types[(cfg & AC_DEFCFG_DEVICE)
171 >> AC_DEFCFG_DEVICE_SHIFT];
172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100173EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400174
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100175/*
176 * Compose a 32bit command word to be sent to the HD-audio controller
177 */
178static inline unsigned int
179make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
180 unsigned int verb, unsigned int parm)
181{
182 u32 val;
183
Takashi Iwai82e1b802009-07-17 12:47:34 +0200184 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
185 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800186 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
187 codec->addr, direct, nid, verb, parm);
188 return ~0;
189 }
190
191 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100192 val |= (u32)direct << 27;
193 val |= (u32)nid << 20;
194 val |= verb << 8;
195 val |= parm;
196 return val;
197}
198
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199/*
200 * Send and receive a verb
201 */
202static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
203 unsigned int *res)
204{
205 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207
Wu Fengguang6430aee2009-07-17 16:49:19 +0800208 if (cmd == ~0)
209 return -1;
210
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 if (res)
212 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200214 snd_hda_power_up(codec);
215 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200216 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200217 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200218 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800219 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200220 trace_hda_get_response(codec, *res);
221 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200222 mutex_unlock(&bus->cmd_mutex);
223 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200224 if (res && *res == -1 && bus->rirb_error) {
225 if (bus->response_reset) {
226 snd_printd("hda_codec: resetting BUS due to "
227 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200228 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200229 bus->ops.bus_reset(bus);
230 }
231 goto again;
232 }
233 /* clear reset-flag when the communication gets recovered */
234 if (!err)
235 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200236 return err;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/**
240 * snd_hda_codec_read - send a command and get the response
241 * @codec: the HDA codec
242 * @nid: NID to send the command
243 * @direct: direct flag
244 * @verb: the verb to send
245 * @parm: the parameter for the verb
246 *
247 * Send a single command and read the corresponding response.
248 *
249 * Returns the obtained response value, or -1 for an error.
250 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200251unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
252 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned int verb, unsigned int parm)
254{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200255 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
256 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700257 if (codec_exec_verb(codec, cmd, &res))
258 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return res;
260}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100261EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/**
264 * snd_hda_codec_write - send a single command without waiting for response
265 * @codec: the HDA codec
266 * @nid: NID to send the command
267 * @direct: direct flag
268 * @verb: the verb to send
269 * @parm: the parameter for the verb
270 *
271 * Send a single command without waiting for response.
272 *
273 * Returns 0 if successful, or a negative error code.
274 */
275int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
276 unsigned int verb, unsigned int parm)
277{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200278 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100279 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200280 return codec_exec_verb(codec, cmd,
281 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100283EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/**
286 * snd_hda_sequence_write - sequence writes
287 * @codec: the HDA codec
288 * @seq: VERB array to send
289 *
290 * Send the commands sequentially from the given array.
291 * The array must be terminated with NID=0.
292 */
293void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
294{
295 for (; seq->nid; seq++)
296 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_get_sub_nodes - get the range of sub nodes
302 * @codec: the HDA codec
303 * @nid: NID to parse
304 * @start_id: the pointer to store the start NID
305 *
306 * Parse the NID and store the start NID of its sub-nodes.
307 * Returns the number of sub-nodes.
308 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200309int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
310 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 unsigned int parm;
313
314 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200315 if (parm == -1)
316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *start_id = (parm >> 16) & 0x7fff;
318 return (int)(parm & 0x7fff);
319}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100320EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200322/* look up the cached results */
323static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
324{
325 int i, len;
326 for (i = 0; i < array->used; ) {
327 hda_nid_t *p = snd_array_elem(array, i);
328 if (nid == *p)
329 return p;
330 len = p[1];
331 i += len + 2;
332 }
333 return NULL;
334}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200337 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * @codec: the HDA codec
339 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200340 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
342 * Parses the connection list of the given widget and stores the list
343 * of NIDs.
344 *
345 * Returns the number of connections, or a negative error code.
346 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200347int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
348 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200349{
350 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200351 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200352 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200353 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200356 again:
357 /* if the connection-list is already cached, read it */
358 p = lookup_conn_list(array, nid);
359 if (p) {
360 if (listp)
361 *listp = p + 2;
362 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200363 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200364 if (snd_BUG_ON(added))
365 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200366
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200367 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200368 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200369 if (len < 0)
370 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200371 err = snd_hda_override_conn_list(codec, nid, len, list);
372 if (err < 0)
373 return err;
374 added = true;
375 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200376}
Takashi Iwaidce20792011-06-24 14:10:28 +0200377EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
378
379/**
380 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * @codec: the HDA codec
382 * @nid: NID to parse
383 * @conn_list: connection list array
384 * @max_conns: max. number of connections to store
385 *
386 * Parses the connection list of the given widget and stores the list
387 * of NIDs.
388 *
389 * Returns the number of connections, or a negative error code.
390 */
391int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200392 hda_nid_t *conn_list, int max_conns)
393{
Takashi Iwaidce20792011-06-24 14:10:28 +0200394 const hda_nid_t *list;
395 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200396
Takashi Iwaidce20792011-06-24 14:10:28 +0200397 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200398 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200399 if (len > max_conns) {
400 snd_printk(KERN_ERR "hda_codec: "
401 "Too many connections %d for NID 0x%x\n",
402 len, nid);
403 return -EINVAL;
404 }
405 memcpy(conn_list, list, len * sizeof(hda_nid_t));
406 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407}
408EXPORT_SYMBOL_HDA(snd_hda_get_connections);
409
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200410/**
411 * snd_hda_get_raw_connections - copy connection list without cache
412 * @codec: the HDA codec
413 * @nid: NID to parse
414 * @conn_list: connection list array
415 * @max_conns: max. number of connections to store
416 *
417 * Like snd_hda_get_connections(), copy the connection list but without
418 * checking through the connection-list cache.
419 * Currently called only from hda_proc.c, so not exported.
420 */
421int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
422 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100425 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200427 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100428 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Takashi Iwaida3cec32008-08-08 17:12:14 +0200430 if (snd_BUG_ON(!conn_list || max_conns <= 0))
431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200433 wcaps = get_wcaps(codec, nid);
434 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200435 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
436 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
439 if (parm & AC_CLIST_LONG) {
440 /* long form */
441 shift = 16;
442 num_elems = 2;
443 } else {
444 /* short form */
445 shift = 8;
446 num_elems = 4;
447 }
448 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 mask = (1 << (shift-1)) - 1;
450
Takashi Iwai0ba21762007-04-16 11:29:14 +0200451 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0; /* no connection */
453
454 if (conn_len == 1) {
455 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200456 parm = snd_hda_codec_read(codec, nid, 0,
457 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200458 if (parm == -1 && codec->bus->rirb_error)
459 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 conn_list[0] = parm & mask;
461 return 1;
462 }
463
464 /* multi connection */
465 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100466 prev_nid = 0;
467 for (i = 0; i < conn_len; i++) {
468 int range_val;
469 hda_nid_t val, n;
470
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200471 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100472 parm = snd_hda_codec_read(codec, nid, 0,
473 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200474 if (parm == -1 && codec->bus->rirb_error)
475 return -EIO;
476 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200477 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100478 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200479 if (val == 0) {
480 snd_printk(KERN_WARNING "hda_codec: "
481 "invalid CONNECT_LIST verb %x[%i]:%x\n",
482 nid, i, parm);
483 return 0;
484 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100485 parm >>= shift;
486 if (range_val) {
487 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200488 if (!prev_nid || prev_nid >= val) {
489 snd_printk(KERN_WARNING "hda_codec: "
490 "invalid dep_range_val %x:%x\n",
491 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100492 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100494 for (n = prev_nid + 1; n <= val; n++) {
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++] = n;
502 }
503 } else {
504 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200505 snd_printk(KERN_ERR "hda_codec: "
506 "Too many connections %d for NID 0x%x\n",
507 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 return -EINVAL;
509 }
510 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100512 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 return conns;
515}
516
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200517static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
518{
519 hda_nid_t *p = snd_array_new(array);
520 if (!p)
521 return false;
522 *p = nid;
523 return true;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200527 * snd_hda_override_conn_list - add/modify the connection-list to cache
528 * @codec: the HDA codec
529 * @nid: NID to parse
530 * @len: number of connection list entries
531 * @list: the list of connection entries
532 *
533 * Add or modify the given connection-list to the cache. If the corresponding
534 * cache already exists, invalidate it and append a new one.
535 *
536 * Returns zero or a negative error code.
537 */
538int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
539 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200541 struct snd_array *array = &codec->conn_lists;
542 hda_nid_t *p;
543 int i, old_used;
544
545 p = lookup_conn_list(array, nid);
546 if (p)
547 *p = -1; /* invalidate the old entry */
548
549 old_used = array->used;
550 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
551 goto error_add;
552 for (i = 0; i < len; i++)
553 if (!add_conn_list(array, list[i]))
554 goto error_add;
555 return 0;
556
557 error_add:
558 array->used = old_used;
559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200561EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
562
563/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200564 * snd_hda_get_conn_index - get the connection index of the given NID
565 * @codec: the HDA codec
566 * @mux: NID containing the list
567 * @nid: NID to select
568 * @recursive: 1 when searching NID recursively, otherwise 0
569 *
570 * Parses the connection list of the widget @mux and checks whether the
571 * widget @nid is present. If it is, return the connection index.
572 * Otherwise it returns -1.
573 */
574int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
575 hda_nid_t nid, int recursive)
576{
577 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
578 int i, nums;
579
580 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
581 for (i = 0; i < nums; i++)
582 if (conn[i] == nid)
583 return i;
584 if (!recursive)
585 return -1;
586 if (recursive > 5) {
587 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
588 return -1;
589 }
590 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200591 for (i = 0; i < nums; i++) {
592 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
593 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
594 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200595 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
596 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200597 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200598 return -1;
599}
600EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/**
603 * snd_hda_queue_unsol_event - add an unsolicited event to queue
604 * @bus: the BUS
605 * @res: unsolicited event (lower 32bit of RIRB entry)
606 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
607 *
608 * Adds the given event to the queue. The events are processed in
609 * the workqueue asynchronously. Call this function in the interrupt
610 * hanlder when RIRB receives an unsolicited event.
611 *
612 * Returns 0 if successful, or a negative error code.
613 */
614int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
615{
616 struct hda_bus_unsolicited *unsol;
617 unsigned int wp;
618
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200619 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200620 unsol = bus->unsol;
621 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623
624 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
625 unsol->wp = wp;
626
627 wp <<= 1;
628 unsol->queue[wp] = res;
629 unsol->queue[wp + 1] = res_ex;
630
Takashi Iwai6acaed32009-01-12 10:09:24 +0100631 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return 0;
634}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100635EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800638 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
David Howellsc4028952006-11-22 14:57:56 +0000640static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
David Howellsc4028952006-11-22 14:57:56 +0000642 struct hda_bus_unsolicited *unsol =
643 container_of(work, struct hda_bus_unsolicited, work);
644 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct hda_codec *codec;
646 unsigned int rp, caddr, res;
647
648 while (unsol->rp != unsol->wp) {
649 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
650 unsol->rp = rp;
651 rp <<= 1;
652 res = unsol->queue[rp];
653 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 continue;
656 codec = bus->caddr_tbl[caddr & 0x0f];
657 if (codec && codec->patch_ops.unsol_event)
658 codec->patch_ops.unsol_event(codec, res);
659 }
660}
661
662/*
663 * initialize unsolicited queue
664 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200665static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct hda_bus_unsolicited *unsol;
668
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100669 if (bus->unsol) /* already initialized */
670 return 0;
671
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200672 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200673 if (!unsol) {
674 snd_printk(KERN_ERR "hda_codec: "
675 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENOMEM;
677 }
David Howellsc4028952006-11-22 14:57:56 +0000678 INIT_WORK(&unsol->work, process_unsol_events);
679 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 bus->unsol = unsol;
681 return 0;
682}
683
684/*
685 * destructor
686 */
687static void snd_hda_codec_free(struct hda_codec *codec);
688
689static int snd_hda_bus_free(struct hda_bus *bus)
690{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200691 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Takashi Iwai0ba21762007-04-16 11:29:14 +0200693 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100695 if (bus->workq)
696 flush_workqueue(bus->workq);
697 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 snd_hda_codec_free(codec);
701 }
702 if (bus->ops.private_free)
703 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100704 if (bus->workq)
705 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 kfree(bus);
707 return 0;
708}
709
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100710static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d3532008-11-21 09:08:06 +0100713 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return snd_hda_bus_free(bus);
715}
716
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200717#ifdef CONFIG_SND_HDA_HWDEP
718static int snd_hda_bus_dev_register(struct snd_device *device)
719{
720 struct hda_bus *bus = device->device_data;
721 struct hda_codec *codec;
722 list_for_each_entry(codec, &bus->codec_list, list) {
723 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100724 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200725 }
726 return 0;
727}
728#else
729#define snd_hda_bus_dev_register NULL
730#endif
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/**
733 * snd_hda_bus_new - create a HDA bus
734 * @card: the card entry
735 * @temp: the template for hda_bus information
736 * @busp: the pointer to store the created bus instance
737 *
738 * Returns 0 if successful, or a negative error code.
739 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100740int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200741 const struct hda_bus_template *temp,
742 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct hda_bus *bus;
745 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100746 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200747 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .dev_free = snd_hda_bus_dev_free,
749 };
750
Takashi Iwaida3cec32008-08-08 17:12:14 +0200751 if (snd_BUG_ON(!temp))
752 return -EINVAL;
753 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (busp)
757 *busp = NULL;
758
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200759 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (bus == NULL) {
761 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
762 return -ENOMEM;
763 }
764
765 bus->card = card;
766 bus->private_data = temp->private_data;
767 bus->pci = temp->pci;
768 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100769 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bus->ops = temp->ops;
771
Ingo Molnar62932df2006-01-16 16:34:20 +0100772 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200773 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 INIT_LIST_HEAD(&bus->codec_list);
775
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100776 snprintf(bus->workq_name, sizeof(bus->workq_name),
777 "hd-audio%d", card->number);
778 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100779 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100780 snd_printk(KERN_ERR "cannot create workqueue %s\n",
781 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100782 kfree(bus);
783 return -ENOMEM;
784 }
785
Takashi Iwai0ba21762007-04-16 11:29:14 +0200786 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
787 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 snd_hda_bus_free(bus);
789 return err;
790 }
791 if (busp)
792 *busp = bus;
793 return 0;
794}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100795EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Takashi Iwai82467612007-07-27 19:15:54 +0200797#ifdef CONFIG_SND_HDA_GENERIC
798#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200799 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200800#else
801#define is_generic_config(codec) 0
802#endif
803
Takashi Iwai645f10c2008-11-28 15:07:37 +0100804#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100805#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
806#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100807#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100808#endif
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * find a matching codec preset
812 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200813static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200814find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816 struct hda_codec_preset_list *tbl;
817 const struct hda_codec_preset *preset;
818 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Takashi Iwai82467612007-07-27 19:15:54 +0200820 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100821 return NULL; /* use the generic parser */
822
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100823 again:
824 mutex_lock(&preset_mutex);
825 list_for_each_entry(tbl, &hda_preset_tables, list) {
826 if (!try_module_get(tbl->owner)) {
827 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
828 continue;
829 }
830 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100832 if (preset->afg && preset->afg != codec->afg)
833 continue;
834 if (preset->mfg && preset->mfg != codec->mfg)
835 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200836 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200838 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200839 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100840 preset->rev == codec->revision_id)) {
841 mutex_unlock(&preset_mutex);
842 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100846 module_put(tbl->owner);
847 }
848 mutex_unlock(&preset_mutex);
849
850 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
851 char name[32];
852 if (!mod_requested)
853 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
854 codec->vendor_id);
855 else
856 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
857 (codec->vendor_id >> 16) & 0xffff);
858 request_module(name);
859 mod_requested++;
860 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 return NULL;
863}
864
865/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200866 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200868static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 const struct hda_vendor_id *c;
871 const char *vendor = NULL;
872 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200873 char tmp[16];
874
875 if (codec->vendor_name)
876 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 for (c = hda_vendor_ids; c->id; c++) {
879 if (c->id == vendor_id) {
880 vendor = c->name;
881 break;
882 }
883 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200884 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 sprintf(tmp, "Generic %04x", vendor_id);
886 vendor = tmp;
887 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200888 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
889 if (!codec->vendor_name)
890 return -ENOMEM;
891
892 get_chip_name:
893 if (codec->chip_name)
894 return 0;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200897 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
898 else {
899 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
900 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
901 }
902 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200903 return -ENOMEM;
904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200908 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100910static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200912 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 hda_nid_t nid;
914
915 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
916 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200917 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200918 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200919 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200920 case AC_GRP_AUDIO_FUNCTION:
921 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200922 codec->afg_function_id = function_id & 0xff;
923 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200924 break;
925 case AC_GRP_MODEM_FUNCTION:
926 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200927 codec->mfg_function_id = function_id & 0xff;
928 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200929 break;
930 default:
931 break;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100937 * read widget caps for each widget and store in cache
938 */
939static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
940{
941 int i;
942 hda_nid_t nid;
943
944 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
945 &codec->start_nid);
946 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200947 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100948 return -ENOMEM;
949 nid = codec->start_nid;
950 for (i = 0; i < codec->num_nodes; i++, nid++)
951 codec->wcaps[i] = snd_hda_param_read(codec, nid,
952 AC_PAR_AUDIO_WIDGET_CAP);
953 return 0;
954}
955
Takashi Iwai3be14142009-02-20 14:11:16 +0100956/* read all pin default configurations and save codec->init_pins */
957static int read_pin_defaults(struct hda_codec *codec)
958{
959 int i;
960 hda_nid_t nid = codec->start_nid;
961
962 for (i = 0; i < codec->num_nodes; i++, nid++) {
963 struct hda_pincfg *pin;
964 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200965 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100966 if (wid_type != AC_WID_PIN)
967 continue;
968 pin = snd_array_new(&codec->init_pins);
969 if (!pin)
970 return -ENOMEM;
971 pin->nid = nid;
972 pin->cfg = snd_hda_codec_read(codec, nid, 0,
973 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200974 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
975 AC_VERB_GET_PIN_WIDGET_CONTROL,
976 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100977 }
978 return 0;
979}
980
981/* look up the given pin config list and return the item matching with NID */
982static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
983 struct snd_array *array,
984 hda_nid_t nid)
985{
986 int i;
987 for (i = 0; i < array->used; i++) {
988 struct hda_pincfg *pin = snd_array_elem(array, i);
989 if (pin->nid == nid)
990 return pin;
991 }
992 return NULL;
993}
994
995/* write a config value for the given NID */
996static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
997 unsigned int cfg)
998{
999 int i;
1000 for (i = 0; i < 4; i++) {
1001 snd_hda_codec_write(codec, nid, 0,
1002 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1003 cfg & 0xff);
1004 cfg >>= 8;
1005 }
1006}
1007
1008/* set the current pin config value for the given NID.
1009 * the value is cached, and read via snd_hda_codec_get_pincfg()
1010 */
1011int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1012 hda_nid_t nid, unsigned int cfg)
1013{
1014 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001015 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001016
Takashi Iwaib82855a2009-12-27 11:24:56 +01001017 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1018 return -EINVAL;
1019
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001020 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001021 pin = look_up_pincfg(codec, list, nid);
1022 if (!pin) {
1023 pin = snd_array_new(list);
1024 if (!pin)
1025 return -ENOMEM;
1026 pin->nid = nid;
1027 }
1028 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001029
1030 /* change only when needed; e.g. if the pincfg is already present
1031 * in user_pins[], don't write it
1032 */
1033 cfg = snd_hda_codec_get_pincfg(codec, nid);
1034 if (oldcfg != cfg)
1035 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001036 return 0;
1037}
1038
Takashi Iwaid5191e52009-11-16 14:58:17 +01001039/**
1040 * snd_hda_codec_set_pincfg - Override a pin default configuration
1041 * @codec: the HDA codec
1042 * @nid: NID to set the pin config
1043 * @cfg: the pin default config value
1044 *
1045 * Override a pin default configuration value in the cache.
1046 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1047 * priority than the real hardware value.
1048 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001049int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1050 hda_nid_t nid, unsigned int cfg)
1051{
Takashi Iwai346ff702009-02-23 09:42:57 +01001052 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001053}
1054EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1055
Takashi Iwaid5191e52009-11-16 14:58:17 +01001056/**
1057 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1058 * @codec: the HDA codec
1059 * @nid: NID to get the pin config
1060 *
1061 * Get the current pin config value of the given pin NID.
1062 * If the pincfg value is cached or overridden via sysfs or driver,
1063 * returns the cached value.
1064 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001065unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1066{
1067 struct hda_pincfg *pin;
1068
Takashi Iwai3be14142009-02-20 14:11:16 +01001069#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001070 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 if (pin)
1072 return pin->cfg;
1073#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001074 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1075 if (pin)
1076 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001077 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1078 if (pin)
1079 return pin->cfg;
1080 return 0;
1081}
1082EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1083
1084/* restore all current pin configs */
1085static void restore_pincfgs(struct hda_codec *codec)
1086{
1087 int i;
1088 for (i = 0; i < codec->init_pins.used; i++) {
1089 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1090 set_pincfg(codec, pin->nid,
1091 snd_hda_codec_get_pincfg(codec, pin->nid));
1092 }
1093}
Takashi Iwai54d17402005-11-21 16:33:22 +01001094
Takashi Iwai92ee6162009-12-27 11:18:59 +01001095/**
1096 * snd_hda_shutup_pins - Shut up all pins
1097 * @codec: the HDA codec
1098 *
1099 * Clear all pin controls to shup up before suspend for avoiding click noise.
1100 * The controls aren't cached so that they can be resumed properly.
1101 */
1102void snd_hda_shutup_pins(struct hda_codec *codec)
1103{
1104 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001105 /* don't shut up pins when unloading the driver; otherwise it breaks
1106 * the default pin setup at the next load of the driver
1107 */
1108 if (codec->bus->shutdown)
1109 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001110 for (i = 0; i < codec->init_pins.used; i++) {
1111 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1112 /* use read here for syncing after issuing each verb */
1113 snd_hda_codec_read(codec, pin->nid, 0,
1114 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1115 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001116 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001117}
1118EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1119
Takashi Iwai2a439522011-07-26 09:52:50 +02001120#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001121/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1122static void restore_shutup_pins(struct hda_codec *codec)
1123{
1124 int i;
1125 if (!codec->pins_shutup)
1126 return;
1127 if (codec->bus->shutdown)
1128 return;
1129 for (i = 0; i < codec->init_pins.used; i++) {
1130 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1131 snd_hda_codec_write(codec, pin->nid, 0,
1132 AC_VERB_SET_PIN_WIDGET_CONTROL,
1133 pin->ctrl);
1134 }
1135 codec->pins_shutup = 0;
1136}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001137#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001138
Takashi Iwai01751f52007-08-10 16:59:39 +02001139static void init_hda_cache(struct hda_cache_rec *cache,
1140 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001141static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001142
Takashi Iwai3be14142009-02-20 14:11:16 +01001143/* restore the initial pin cfgs and release all pincfg lists */
1144static void restore_init_pincfgs(struct hda_codec *codec)
1145{
Takashi Iwai346ff702009-02-23 09:42:57 +01001146 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001147 * so that only the values in init_pins are restored
1148 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001149 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001150#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001151 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001152#endif
1153 restore_pincfgs(codec);
1154 snd_array_free(&codec->init_pins);
1155}
1156
Takashi Iwai54d17402005-11-21 16:33:22 +01001157/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001158 * audio-converter setup caches
1159 */
1160struct hda_cvt_setup {
1161 hda_nid_t nid;
1162 u8 stream_tag;
1163 u8 channel_id;
1164 u16 format_id;
1165 unsigned char active; /* cvt is currently used */
1166 unsigned char dirty; /* setups should be cleared */
1167};
1168
1169/* get or create a cache entry for the given audio converter NID */
1170static struct hda_cvt_setup *
1171get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 struct hda_cvt_setup *p;
1174 int i;
1175
1176 for (i = 0; i < codec->cvt_setups.used; i++) {
1177 p = snd_array_elem(&codec->cvt_setups, i);
1178 if (p->nid == nid)
1179 return p;
1180 }
1181 p = snd_array_new(&codec->cvt_setups);
1182 if (p)
1183 p->nid = nid;
1184 return p;
1185}
1186
1187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * codec destructor
1189 */
1190static void snd_hda_codec_free(struct hda_codec *codec)
1191{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001192 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001194 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001195#ifdef CONFIG_SND_HDA_POWER_SAVE
1196 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001197 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001200 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001201 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001202 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001203 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 codec->bus->caddr_tbl[codec->addr] = NULL;
1205 if (codec->patch_ops.free)
1206 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001207 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001208 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001209 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001210 kfree(codec->vendor_name);
1211 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001212 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001213 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 kfree(codec);
1215}
1216
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001217static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1218 unsigned int power_state);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/**
1221 * snd_hda_codec_new - create a HDA codec
1222 * @bus: the bus to assign
1223 * @codec_addr: the codec address
1224 * @codecp: the pointer to store the generated codec
1225 *
1226 * Returns 0 if successful, or a negative error code.
1227 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001228int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1229 unsigned int codec_addr,
1230 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001233 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int err;
1235
Takashi Iwaida3cec32008-08-08 17:12:14 +02001236 if (snd_BUG_ON(!bus))
1237 return -EINVAL;
1238 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1239 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001242 snd_printk(KERN_ERR "hda_codec: "
1243 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EBUSY;
1245 }
1246
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001247 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (codec == NULL) {
1249 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1250 return -ENOMEM;
1251 }
1252
1253 codec->bus = bus;
1254 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001255 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001256 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001257 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001258 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001259 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1260 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001261 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001262 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001263 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001264 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001265 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001266 if (codec->bus->modelname) {
1267 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1268 if (!codec->modelname) {
1269 snd_hda_codec_free(codec);
1270 return -ENODEV;
1271 }
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Takashi Iwaicb53c622007-08-10 17:21:45 +02001274#ifdef CONFIG_SND_HDA_POWER_SAVE
1275 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1276 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1277 * the caller has to power down appropriatley after initialization
1278 * phase.
1279 */
1280 hda_keep_power_on(codec);
1281#endif
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 list_add_tail(&codec->list, &bus->codec_list);
1284 bus->caddr_tbl[codec_addr] = codec;
1285
Takashi Iwai0ba21762007-04-16 11:29:14 +02001286 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1287 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001288 if (codec->vendor_id == -1)
1289 /* read again, hopefully the access method was corrected
1290 * in the last read...
1291 */
1292 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001294 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_SUBSYSTEM_ID);
1296 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001299 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001301 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001302 err = -ENODEV;
1303 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1307 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001308 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001310 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001311 err = read_pin_defaults(codec);
1312 if (err < 0)
1313 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001314
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317 codec->subsystem_id =
1318 snd_hda_codec_read(codec, nid, 0,
1319 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001320 }
1321
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001322 /* power-up all before initialization */
1323 hda_set_power_state(codec,
1324 codec->afg ? codec->afg : codec->mfg,
1325 AC_PWRST_D0);
1326
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001327 snd_hda_codec_proc_new(codec);
1328
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001329 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330
1331 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1332 codec->subsystem_id, codec->revision_id);
1333 snd_component_add(codec->bus->card, component);
1334
1335 if (codecp)
1336 *codecp = codec;
1337 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001338
1339 error:
1340 snd_hda_codec_free(codec);
1341 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001342}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001343EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001344
Takashi Iwaid5191e52009-11-16 14:58:17 +01001345/**
1346 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1347 * @codec: the HDA codec
1348 *
1349 * Start parsing of the given codec tree and (re-)initialize the whole
1350 * patch instance.
1351 *
1352 * Returns 0 if successful or a negative error code.
1353 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001354int snd_hda_codec_configure(struct hda_codec *codec)
1355{
1356 int err;
1357
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001358 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001359 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001360 err = get_codec_name(codec);
1361 if (err < 0)
1362 return err;
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Takashi Iwai82467612007-07-27 19:15:54 +02001365 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001367 goto patched;
1368 }
Takashi Iwai82467612007-07-27 19:15:54 +02001369 if (codec->preset && codec->preset->patch) {
1370 err = codec->preset->patch(codec);
1371 goto patched;
1372 }
1373
1374 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001375 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001376 if (err < 0)
1377 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001378
1379 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001380 if (!err && codec->patch_ops.unsol_event)
1381 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001382 /* audio codec should override the mixer name */
1383 if (!err && (codec->afg || !*codec->bus->card->mixername))
1384 snprintf(codec->bus->card->mixername,
1385 sizeof(codec->bus->card->mixername),
1386 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001389EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/**
1392 * snd_hda_codec_setup_stream - set up the codec for streaming
1393 * @codec: the CODEC to set up
1394 * @nid: the NID to set up
1395 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1396 * @channel_id: channel id to pass, zero based.
1397 * @format: stream format.
1398 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001399void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1400 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int channel_id, int format)
1402{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001403 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001404 struct hda_cvt_setup *p;
1405 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001406 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001407 int i;
1408
Takashi Iwai0ba21762007-04-16 11:29:14 +02001409 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001410 return;
1411
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 snd_printdd("hda_codec_setup_stream: "
1413 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001415 p = get_hda_cvt_setup(codec, nid);
1416 if (!p)
1417 return;
1418 /* update the stream-id if changed */
1419 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1420 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1421 newval = (stream_tag << 4) | channel_id;
1422 if (oldval != newval)
1423 snd_hda_codec_write(codec, nid, 0,
1424 AC_VERB_SET_CHANNEL_STREAMID,
1425 newval);
1426 p->stream_tag = stream_tag;
1427 p->channel_id = channel_id;
1428 }
1429 /* update the format-id if changed */
1430 if (p->format_id != format) {
1431 oldval = snd_hda_codec_read(codec, nid, 0,
1432 AC_VERB_GET_STREAM_FORMAT, 0);
1433 if (oldval != format) {
1434 msleep(1);
1435 snd_hda_codec_write(codec, nid, 0,
1436 AC_VERB_SET_STREAM_FORMAT,
1437 format);
1438 }
1439 p->format_id = format;
1440 }
1441 p->active = 1;
1442 p->dirty = 0;
1443
1444 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001445 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001446 list_for_each_entry(c, &codec->bus->codec_list, list) {
1447 for (i = 0; i < c->cvt_setups.used; i++) {
1448 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001449 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001450 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001451 p->dirty = 1;
1452 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001455EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Takashi Iwaif0cea792010-08-13 11:56:53 +02001457static void really_cleanup_stream(struct hda_codec *codec,
1458 struct hda_cvt_setup *q);
1459
Takashi Iwaid5191e52009-11-16 14:58:17 +01001460/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001461 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001462 * @codec: the CODEC to clean up
1463 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001465 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001466void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1467 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001468{
Takashi Iwaieb541332010-08-06 13:48:11 +02001469 struct hda_cvt_setup *p;
1470
Takashi Iwai888afa12008-03-18 09:57:50 +01001471 if (!nid)
1472 return;
1473
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001474 if (codec->no_sticky_stream)
1475 do_now = 1;
1476
Takashi Iwai888afa12008-03-18 09:57:50 +01001477 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001478 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001479 if (p) {
1480 /* here we just clear the active flag when do_now isn't set;
1481 * actual clean-ups will be done later in
1482 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1483 */
1484 if (do_now)
1485 really_cleanup_stream(codec, p);
1486 else
1487 p->active = 0;
1488 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001489}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001490EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001491
Takashi Iwaieb541332010-08-06 13:48:11 +02001492static void really_cleanup_stream(struct hda_codec *codec,
1493 struct hda_cvt_setup *q)
1494{
1495 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001496 if (q->stream_tag || q->channel_id)
1497 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1498 if (q->format_id)
1499 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1500);
Takashi Iwaieb541332010-08-06 13:48:11 +02001501 memset(q, 0, sizeof(*q));
1502 q->nid = nid;
1503}
1504
1505/* clean up the all conflicting obsolete streams */
1506static void purify_inactive_streams(struct hda_codec *codec)
1507{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001508 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001509 int i;
1510
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001511 list_for_each_entry(c, &codec->bus->codec_list, list) {
1512 for (i = 0; i < c->cvt_setups.used; i++) {
1513 struct hda_cvt_setup *p;
1514 p = snd_array_elem(&c->cvt_setups, i);
1515 if (p->dirty)
1516 really_cleanup_stream(c, p);
1517 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001518 }
1519}
1520
Takashi Iwai2a439522011-07-26 09:52:50 +02001521#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001522/* clean up all streams; called from suspend */
1523static void hda_cleanup_all_streams(struct hda_codec *codec)
1524{
1525 int i;
1526
1527 for (i = 0; i < codec->cvt_setups.used; i++) {
1528 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1529 if (p->stream_tag)
1530 really_cleanup_stream(codec, p);
1531 }
1532}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001533#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/*
1536 * amp access functions
1537 */
1538
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001539/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001540#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001541#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001542#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1543#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001545#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001548static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001549 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Takashi Iwai01751f52007-08-10 16:59:39 +02001551 memset(cache, 0, sizeof(*cache));
1552 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001553 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001554}
1555
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001556static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001557{
Takashi Iwai603c4012008-07-30 15:01:44 +02001558 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001562static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwai01751f52007-08-10 16:59:39 +02001564 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1565 u16 cur = cache->hash[idx];
1566 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001569 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (info->key == key)
1571 return info;
1572 cur = info->next;
1573 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001574 return NULL;
1575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001577/* query the hash. allocate an entry if not found. */
1578static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1579 u32 key)
1580{
1581 struct hda_cache_head *info = get_hash(cache, key);
1582 if (!info) {
1583 u16 idx, cur;
1584 /* add a new hash entry */
1585 info = snd_array_new(&cache->buf);
1586 if (!info)
1587 return NULL;
1588 cur = snd_array_index(&cache->buf, info);
1589 info->key = key;
1590 info->val = 0;
1591 idx = key % (u16)ARRAY_SIZE(cache->hash);
1592 info->next = cache->hash[idx];
1593 cache->hash[idx] = cur;
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return info;
1596}
1597
Takashi Iwai01751f52007-08-10 16:59:39 +02001598/* query and allocate an amp hash entry */
1599static inline struct hda_amp_info *
1600get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1601{
1602 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1603}
1604
Takashi Iwaid5191e52009-11-16 14:58:17 +01001605/**
1606 * query_amp_caps - query AMP capabilities
1607 * @codec: the HD-auio codec
1608 * @nid: the NID to query
1609 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1610 *
1611 * Query AMP capabilities for the given widget and direction.
1612 * Returns the obtained capability bits.
1613 *
1614 * When cap bits have been already read, this doesn't read again but
1615 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001617u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001619 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Takashi Iwai0ba21762007-04-16 11:29:14 +02001621 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1622 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001624 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001627 info->amp_caps = snd_hda_param_read(codec, nid,
1628 direction == HDA_OUTPUT ?
1629 AC_PAR_AMP_OUT_CAP :
1630 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001631 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001632 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 return info->amp_caps;
1635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001636EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Takashi Iwaid5191e52009-11-16 14:58:17 +01001638/**
1639 * snd_hda_override_amp_caps - Override the AMP capabilities
1640 * @codec: the CODEC to clean up
1641 * @nid: the NID to clean up
1642 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1643 * @caps: the capability bits to set
1644 *
1645 * Override the cached AMP caps bits value by the given one.
1646 * This function is useful if the driver needs to adjust the AMP ranges,
1647 * e.g. limit to 0dB, etc.
1648 *
1649 * Returns zero if successful or a negative error code.
1650 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001651int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1652 unsigned int caps)
1653{
1654 struct hda_amp_info *info;
1655
1656 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1657 if (!info)
1658 return -EINVAL;
1659 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001660 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001661 return 0;
1662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001663EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001664
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001665static unsigned int
1666query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1667 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001668{
1669 struct hda_amp_info *info;
1670
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001671 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001672 if (!info)
1673 return 0;
1674 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001675 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001676 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001677 }
1678 return info->amp_caps;
1679}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001680
1681static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1682{
1683 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1684}
1685
Takashi Iwaid5191e52009-11-16 14:58:17 +01001686/**
1687 * snd_hda_query_pin_caps - Query PIN capabilities
1688 * @codec: the HD-auio codec
1689 * @nid: the NID to query
1690 *
1691 * Query PIN capabilities for the given widget.
1692 * Returns the obtained capability bits.
1693 *
1694 * When cap bits have been already read, this doesn't read again but
1695 * returns the cached value.
1696 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001697u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1698{
1699 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1700 read_pin_cap);
1701}
Takashi Iwai1327a322009-03-23 13:07:47 +01001702EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1703
Wu Fengguang864f92b2009-11-18 12:38:02 +08001704/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001705 * snd_hda_override_pin_caps - Override the pin capabilities
1706 * @codec: the CODEC
1707 * @nid: the NID to override
1708 * @caps: the capability bits to set
1709 *
1710 * Override the cached PIN capabilitiy bits value by the given one.
1711 *
1712 * Returns zero if successful or a negative error code.
1713 */
1714int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1715 unsigned int caps)
1716{
1717 struct hda_amp_info *info;
1718 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1719 if (!info)
1720 return -ENOMEM;
1721 info->amp_caps = caps;
1722 info->head.val |= INFO_AMP_CAPS;
1723 return 0;
1724}
1725EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727/*
1728 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001729 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001731static unsigned int get_vol_mute(struct hda_codec *codec,
1732 struct hda_amp_info *info, hda_nid_t nid,
1733 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 u32 val, parm;
1736
Takashi Iwai01751f52007-08-10 16:59:39 +02001737 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001738 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1741 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1742 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001743 val = snd_hda_codec_read(codec, nid, 0,
1744 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001746 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001747 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001751 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001753static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001754 hda_nid_t nid, int ch, int direction, int index,
1755 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 u32 parm;
1758
1759 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1760 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1761 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1762 parm |= val;
1763 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001764 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Takashi Iwaid5191e52009-11-16 14:58:17 +01001767/**
1768 * snd_hda_codec_amp_read - Read AMP value
1769 * @codec: HD-audio codec
1770 * @nid: NID to read the AMP value
1771 * @ch: channel (left=0 or right=1)
1772 * @direction: #HDA_INPUT or #HDA_OUTPUT
1773 * @index: the index value (only for input direction)
1774 *
1775 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Takashi Iwai834be882006-03-01 14:16:17 +01001777int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1778 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001780 struct hda_amp_info *info;
1781 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1782 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001784 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001786EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Takashi Iwaid5191e52009-11-16 14:58:17 +01001788/**
1789 * snd_hda_codec_amp_update - update the AMP value
1790 * @codec: HD-audio codec
1791 * @nid: NID to read the AMP value
1792 * @ch: channel (left=0 or right=1)
1793 * @direction: #HDA_INPUT or #HDA_OUTPUT
1794 * @idx: the index value (only for input direction)
1795 * @mask: bit mask to set
1796 * @val: the bits value to set
1797 *
1798 * Update the AMP value with a bit mask.
1799 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001800 */
Takashi Iwai834be882006-03-01 14:16:17 +01001801int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1802 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001805
Takashi Iwai0ba21762007-04-16 11:29:14 +02001806 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1807 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001809 if (snd_BUG_ON(mask & ~0xff))
1810 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001811 val &= mask;
1812 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001813 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001815 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 1;
1817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001818EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Takashi Iwaid5191e52009-11-16 14:58:17 +01001820/**
1821 * snd_hda_codec_amp_stereo - update the AMP stereo values
1822 * @codec: HD-audio codec
1823 * @nid: NID to read the AMP value
1824 * @direction: #HDA_INPUT or #HDA_OUTPUT
1825 * @idx: the index value (only for input direction)
1826 * @mask: bit mask to set
1827 * @val: the bits value to set
1828 *
1829 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1830 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001831 */
1832int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1833 int direction, int idx, int mask, int val)
1834{
1835 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001836
1837 if (snd_BUG_ON(mask & ~0xff))
1838 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001839 for (ch = 0; ch < 2; ch++)
1840 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1841 idx, mask, val);
1842 return ret;
1843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001844EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001845
Takashi Iwai2a439522011-07-26 09:52:50 +02001846#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001847/**
1848 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1849 * @codec: HD-audio codec
1850 *
1851 * Resume the all amp commands from the cache.
1852 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001853void snd_hda_codec_resume_amp(struct hda_codec *codec)
1854{
Takashi Iwai603c4012008-07-30 15:01:44 +02001855 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001856 int i;
1857
Takashi Iwai603c4012008-07-30 15:01:44 +02001858 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001859 u32 key = buffer->head.key;
1860 hda_nid_t nid;
1861 unsigned int idx, dir, ch;
1862 if (!key)
1863 continue;
1864 nid = key & 0xff;
1865 idx = (key >> 16) & 0xff;
1866 dir = (key >> 24) & 0xff;
1867 for (ch = 0; ch < 2; ch++) {
1868 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1869 continue;
1870 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1871 buffer->vol[ch]);
1872 }
1873 }
1874}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001875EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001876#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001878static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1879 unsigned int ofs)
1880{
1881 u32 caps = query_amp_caps(codec, nid, dir);
1882 /* get num steps */
1883 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1884 if (ofs < caps)
1885 caps -= ofs;
1886 return caps;
1887}
1888
Takashi Iwaid5191e52009-11-16 14:58:17 +01001889/**
1890 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1891 *
1892 * The control element is supposed to have the private_value field
1893 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1894 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001895int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 u16 nid = get_amp_nid(kcontrol);
1900 u8 chs = get_amp_channels(kcontrol);
1901 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001902 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001904 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1905 uinfo->count = chs == 3 ? 2 : 1;
1906 uinfo->value.integer.min = 0;
1907 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1908 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001909 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001910 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1911 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return -EINVAL;
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001916EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001918
1919static inline unsigned int
1920read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1921 int ch, int dir, int idx, unsigned int ofs)
1922{
1923 unsigned int val;
1924 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1925 val &= HDA_AMP_VOLMASK;
1926 if (val >= ofs)
1927 val -= ofs;
1928 else
1929 val = 0;
1930 return val;
1931}
1932
1933static inline int
1934update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1935 int ch, int dir, int idx, unsigned int ofs,
1936 unsigned int val)
1937{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001938 unsigned int maxval;
1939
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001940 if (val > 0)
1941 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001942 /* ofs = 0: raw max value */
1943 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001944 if (val > maxval)
1945 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001946 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1947 HDA_AMP_VOLMASK, val);
1948}
1949
Takashi Iwaid5191e52009-11-16 14:58:17 +01001950/**
1951 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1952 *
1953 * The control element is supposed to have the private_value field
1954 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1955 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001956int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1960 hda_nid_t nid = get_amp_nid(kcontrol);
1961 int chs = get_amp_channels(kcontrol);
1962 int dir = get_amp_direction(kcontrol);
1963 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001964 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 long *valp = ucontrol->value.integer.value;
1966
1967 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001968 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001970 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return 0;
1972}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001973EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Takashi Iwaid5191e52009-11-16 14:58:17 +01001975/**
1976 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1977 *
1978 * The control element is supposed to have the private_value field
1979 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1980 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001981int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1985 hda_nid_t nid = get_amp_nid(kcontrol);
1986 int chs = get_amp_channels(kcontrol);
1987 int dir = get_amp_direction(kcontrol);
1988 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001989 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 long *valp = ucontrol->value.integer.value;
1991 int change = 0;
1992
Takashi Iwaicb53c622007-08-10 17:21:45 +02001993 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001994 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001995 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001996 valp++;
1997 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001998 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002000 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return change;
2002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002003EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Takashi Iwaid5191e52009-11-16 14:58:17 +01002005/**
2006 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2007 *
2008 * The control element is supposed to have the private_value field
2009 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2010 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002011int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2012 unsigned int size, unsigned int __user *_tlv)
2013{
2014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2015 hda_nid_t nid = get_amp_nid(kcontrol);
2016 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002017 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002018 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002019 u32 caps, val1, val2;
2020
2021 if (size < 4 * sizeof(unsigned int))
2022 return -ENOMEM;
2023 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002024 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2025 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002026 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002028 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002029 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002030 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002031 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2032 return -EFAULT;
2033 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2034 return -EFAULT;
2035 if (put_user(val1, _tlv + 2))
2036 return -EFAULT;
2037 if (put_user(val2, _tlv + 3))
2038 return -EFAULT;
2039 return 0;
2040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002041EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002042
Takashi Iwaid5191e52009-11-16 14:58:17 +01002043/**
2044 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2045 * @codec: HD-audio codec
2046 * @nid: NID of a reference widget
2047 * @dir: #HDA_INPUT or #HDA_OUTPUT
2048 * @tlv: TLV data to be stored, at least 4 elements
2049 *
2050 * Set (static) TLV data for a virtual master volume using the AMP caps
2051 * obtained from the reference NID.
2052 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002053 */
2054void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2055 unsigned int *tlv)
2056{
2057 u32 caps;
2058 int nums, step;
2059
2060 caps = query_amp_caps(codec, nid, dir);
2061 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2062 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2063 step = (step + 1) * 25;
2064 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2065 tlv[1] = 2 * sizeof(unsigned int);
2066 tlv[2] = -nums * step;
2067 tlv[3] = step;
2068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002069EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002070
2071/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002072static struct snd_kcontrol *
2073_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2074 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002075{
2076 struct snd_ctl_elem_id id;
2077 memset(&id, 0, sizeof(id));
2078 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002079 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002080 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2081 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002082 strcpy(id.name, name);
2083 return snd_ctl_find_id(codec->bus->card, &id);
2084}
2085
Takashi Iwaid5191e52009-11-16 14:58:17 +01002086/**
2087 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2088 * @codec: HD-audio codec
2089 * @name: ctl id name string
2090 *
2091 * Get the control element with the given id string and IFACE_MIXER.
2092 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002093struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2094 const char *name)
2095{
2096 return _snd_hda_find_mixer_ctl(codec, name, 0);
2097}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002098EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002099
Takashi Iwai1afe2062010-12-23 10:17:52 +01002100static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2101{
2102 int idx;
2103 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2104 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2105 return idx;
2106 }
2107 return -EBUSY;
2108}
2109
Takashi Iwaid5191e52009-11-16 14:58:17 +01002110/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002111 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002112 * @codec: HD-audio codec
2113 * @nid: corresponding NID (optional)
2114 * @kctl: the control element to assign
2115 *
2116 * Add the given control element to an array inside the codec instance.
2117 * All control elements belonging to a codec are supposed to be added
2118 * by this function so that a proper clean-up works at the free or
2119 * reconfiguration time.
2120 *
2121 * If non-zero @nid is passed, the NID is assigned to the control element.
2122 * The assignment is shown in the codec proc file.
2123 *
2124 * snd_hda_ctl_add() checks the control subdev id field whether
2125 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002126 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2127 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002128 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002129int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2130 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002131{
2132 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002133 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002134 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002135
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002136 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002137 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002138 if (nid == 0)
2139 nid = get_amp_nid_(kctl->private_value);
2140 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002141 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2142 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002144 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002145 err = snd_ctl_add(codec->bus->card, kctl);
2146 if (err < 0)
2147 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002148 item = snd_array_new(&codec->mixers);
2149 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002150 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002151 item->kctl = kctl;
2152 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002153 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002154 return 0;
2155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002156EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002157
Takashi Iwaid5191e52009-11-16 14:58:17 +01002158/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002159 * snd_hda_add_nid - Assign a NID to a control element
2160 * @codec: HD-audio codec
2161 * @nid: corresponding NID (optional)
2162 * @kctl: the control element to assign
2163 * @index: index to kctl
2164 *
2165 * Add the given control element to an array inside the codec instance.
2166 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2167 * NID:KCTL mapping - for example "Capture Source" selector.
2168 */
2169int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2170 unsigned int index, hda_nid_t nid)
2171{
2172 struct hda_nid_item *item;
2173
2174 if (nid > 0) {
2175 item = snd_array_new(&codec->nids);
2176 if (!item)
2177 return -ENOMEM;
2178 item->kctl = kctl;
2179 item->index = index;
2180 item->nid = nid;
2181 return 0;
2182 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002183 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2184 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002185 return -EINVAL;
2186}
2187EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2188
2189/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002190 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2191 * @codec: HD-audio codec
2192 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002193void snd_hda_ctls_clear(struct hda_codec *codec)
2194{
2195 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002196 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002197 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002198 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002199 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002200 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002201}
2202
Takashi Iwaia65d6292009-02-23 16:57:04 +01002203/* pseudo device locking
2204 * toggle card->shutdown to allow/disallow the device access (as a hack)
2205 */
2206static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002207{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002208 spin_lock(&card->files_lock);
2209 if (card->shutdown) {
2210 spin_unlock(&card->files_lock);
2211 return -EINVAL;
2212 }
2213 card->shutdown = 1;
2214 spin_unlock(&card->files_lock);
2215 return 0;
2216}
2217
2218static void hda_unlock_devices(struct snd_card *card)
2219{
2220 spin_lock(&card->files_lock);
2221 card->shutdown = 0;
2222 spin_unlock(&card->files_lock);
2223}
2224
Takashi Iwaid5191e52009-11-16 14:58:17 +01002225/**
2226 * snd_hda_codec_reset - Clear all objects assigned to the codec
2227 * @codec: HD-audio codec
2228 *
2229 * This frees the all PCM and control elements assigned to the codec, and
2230 * clears the caches and restores the pin default configurations.
2231 *
2232 * When a device is being used, it returns -EBSY. If successfully freed,
2233 * returns zero.
2234 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002235int snd_hda_codec_reset(struct hda_codec *codec)
2236{
2237 struct snd_card *card = codec->bus->card;
2238 int i, pcm;
2239
2240 if (hda_lock_devices(card) < 0)
2241 return -EBUSY;
2242 /* check whether the codec isn't used by any mixer or PCM streams */
2243 if (!list_empty(&card->ctl_files)) {
2244 hda_unlock_devices(card);
2245 return -EBUSY;
2246 }
2247 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2248 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2249 if (!cpcm->pcm)
2250 continue;
2251 if (cpcm->pcm->streams[0].substream_opened ||
2252 cpcm->pcm->streams[1].substream_opened) {
2253 hda_unlock_devices(card);
2254 return -EBUSY;
2255 }
2256 }
2257
2258 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002259
2260#ifdef CONFIG_SND_HDA_POWER_SAVE
2261 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002262 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002263#endif
2264 snd_hda_ctls_clear(codec);
2265 /* relase PCMs */
2266 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002267 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002268 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002269 clear_bit(codec->pcm_info[i].device,
2270 codec->bus->pcm_dev_bits);
2271 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002272 }
2273 if (codec->patch_ops.free)
2274 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002275 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002276 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002277 codec->spec = NULL;
2278 free_hda_cache(&codec->amp_cache);
2279 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002280 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2281 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002282 /* free only driver_pins so that init_pins + user_pins are restored */
2283 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002284 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002285 codec->num_pcms = 0;
2286 codec->pcm_info = NULL;
2287 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002288 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2289 codec->slave_dig_outs = NULL;
2290 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002291 module_put(codec->owner);
2292 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002293
2294 /* allow device access again */
2295 hda_unlock_devices(card);
2296 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002297}
2298
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002299typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2300
2301/* apply the function to all matching slave ctls in the mixer list */
2302static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002303 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002304{
2305 struct hda_nid_item *items;
2306 const char * const *s;
2307 int i, err;
2308
2309 items = codec->mixers.list;
2310 for (i = 0; i < codec->mixers.used; i++) {
2311 struct snd_kcontrol *sctl = items[i].kctl;
2312 if (!sctl || !sctl->id.name ||
2313 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2314 continue;
2315 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002316 char tmpname[sizeof(sctl->id.name)];
2317 const char *name = *s;
2318 if (suffix) {
2319 snprintf(tmpname, sizeof(tmpname), "%s %s",
2320 name, suffix);
2321 name = tmpname;
2322 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002323 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002324 err = func(data, sctl);
2325 if (err)
2326 return err;
2327 break;
2328 }
2329 }
2330 }
2331 return 0;
2332}
2333
2334static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2335{
2336 return 1;
2337}
2338
Takashi Iwaid5191e52009-11-16 14:58:17 +01002339/**
2340 * snd_hda_add_vmaster - create a virtual master control and add slaves
2341 * @codec: HD-audio codec
2342 * @name: vmaster control name
2343 * @tlv: TLV data (optional)
2344 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002345 * @suffix: suffix string to each slave name (optional)
Takashi Iwaid5191e52009-11-16 14:58:17 +01002346 *
2347 * Create a virtual master control with the given name. The TLV data
2348 * must be either NULL or a valid data.
2349 *
2350 * @slaves is a NULL-terminated array of strings, each of which is a
2351 * slave control name. All controls with these names are assigned to
2352 * the new virtual master control.
2353 *
2354 * This function returns zero if successful or a negative error code.
2355 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002356int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002357 unsigned int *tlv, const char * const *slaves,
2358 const char *suffix)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002359{
2360 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002361 int err;
2362
Takashi Iwai9322ca52012-02-03 14:28:01 +01002363 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002364 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002365 snd_printdd("No slave found for %s\n", name);
2366 return 0;
2367 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002368 kctl = snd_ctl_make_virtual_master(name, tlv);
2369 if (!kctl)
2370 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002371 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002372 if (err < 0)
2373 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002374
Takashi Iwai9322ca52012-02-03 14:28:01 +01002375 err = map_slaves(codec, slaves, suffix,
2376 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002377 if (err < 0)
2378 return err;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002379 return 0;
2380}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002381EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002382
Takashi Iwaid5191e52009-11-16 14:58:17 +01002383/**
2384 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2385 *
2386 * The control element is supposed to have the private_value field
2387 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2388 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002389int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
2392 int chs = get_amp_channels(kcontrol);
2393
2394 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2395 uinfo->count = chs == 3 ? 2 : 1;
2396 uinfo->value.integer.min = 0;
2397 uinfo->value.integer.max = 1;
2398 return 0;
2399}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002400EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Takashi Iwaid5191e52009-11-16 14:58:17 +01002402/**
2403 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2404 *
2405 * The control element is supposed to have the private_value field
2406 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2407 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002408int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 hda_nid_t nid = get_amp_nid(kcontrol);
2413 int chs = get_amp_channels(kcontrol);
2414 int dir = get_amp_direction(kcontrol);
2415 int idx = get_amp_index(kcontrol);
2416 long *valp = ucontrol->value.integer.value;
2417
2418 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002419 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002420 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002422 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002423 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return 0;
2425}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002426EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Takashi Iwaid5191e52009-11-16 14:58:17 +01002428/**
2429 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2430 *
2431 * The control element is supposed to have the private_value field
2432 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2433 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002434int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438 hda_nid_t nid = get_amp_nid(kcontrol);
2439 int chs = get_amp_channels(kcontrol);
2440 int dir = get_amp_direction(kcontrol);
2441 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 long *valp = ucontrol->value.integer.value;
2443 int change = 0;
2444
Takashi Iwaicb53c622007-08-10 17:21:45 +02002445 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002446 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002447 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002448 HDA_AMP_MUTE,
2449 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002450 valp++;
2451 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002452 if (chs & 2)
2453 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002454 HDA_AMP_MUTE,
2455 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002456 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002457 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return change;
2459}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002460EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Takashi Iwai67d634c2009-11-16 15:35:59 +01002462#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002463/**
2464 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2465 *
2466 * This function calls snd_hda_enable_beep_device(), which behaves differently
2467 * depending on beep_mode option.
2468 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002469int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol)
2471{
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 long *valp = ucontrol->value.integer.value;
2474
2475 snd_hda_enable_beep_device(codec, *valp);
2476 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2477}
2478EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002479#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
Takashi Iwai985be542005-11-02 18:26:49 +01002482 * bound volume controls
2483 *
2484 * bind multiple volumes (# indices, from 0)
2485 */
2486
2487#define AMP_VAL_IDX_SHIFT 19
2488#define AMP_VAL_IDX_MASK (0x0f<<19)
2489
Takashi Iwaid5191e52009-11-16 14:58:17 +01002490/**
2491 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2492 *
2493 * The control element is supposed to have the private_value field
2494 * set up via HDA_BIND_MUTE*() macros.
2495 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002496int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002498{
2499 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2500 unsigned long pval;
2501 int err;
2502
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002503 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002504 pval = kcontrol->private_value;
2505 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2506 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2507 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002508 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002509 return err;
2510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002511EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002512
Takashi Iwaid5191e52009-11-16 14:58:17 +01002513/**
2514 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2515 *
2516 * The control element is supposed to have the private_value field
2517 * set up via HDA_BIND_MUTE*() macros.
2518 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002519int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002521{
2522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2523 unsigned long pval;
2524 int i, indices, err = 0, change = 0;
2525
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002526 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002527 pval = kcontrol->private_value;
2528 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2529 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002530 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2531 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002532 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2533 if (err < 0)
2534 break;
2535 change |= err;
2536 }
2537 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002538 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002539 return err < 0 ? err : change;
2540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002541EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002542
Takashi Iwaid5191e52009-11-16 14:58:17 +01002543/**
2544 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2545 *
2546 * The control element is supposed to have the private_value field
2547 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002548 */
2549int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_info *uinfo)
2551{
2552 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2553 struct hda_bind_ctls *c;
2554 int err;
2555
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002556 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002557 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002558 kcontrol->private_value = *c->values;
2559 err = c->ops->info(kcontrol, uinfo);
2560 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002561 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002562 return err;
2563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002564EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002565
Takashi Iwaid5191e52009-11-16 14:58:17 +01002566/**
2567 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2568 *
2569 * The control element is supposed to have the private_value field
2570 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2571 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002572int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
2574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 struct hda_bind_ctls *c;
2577 int err;
2578
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002579 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002580 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002581 kcontrol->private_value = *c->values;
2582 err = c->ops->get(kcontrol, ucontrol);
2583 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002584 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002585 return err;
2586}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002587EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002588
Takashi Iwaid5191e52009-11-16 14:58:17 +01002589/**
2590 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2591 *
2592 * The control element is supposed to have the private_value field
2593 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2594 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002595int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2597{
2598 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2599 struct hda_bind_ctls *c;
2600 unsigned long *vals;
2601 int err = 0, change = 0;
2602
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002603 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002604 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002605 for (vals = c->values; *vals; vals++) {
2606 kcontrol->private_value = *vals;
2607 err = c->ops->put(kcontrol, ucontrol);
2608 if (err < 0)
2609 break;
2610 change |= err;
2611 }
2612 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002613 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002614 return err < 0 ? err : change;
2615}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002616EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002617
Takashi Iwaid5191e52009-11-16 14:58:17 +01002618/**
2619 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2620 *
2621 * The control element is supposed to have the private_value field
2622 * set up via HDA_BIND_VOL() macro.
2623 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002624int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2625 unsigned int size, unsigned int __user *tlv)
2626{
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2628 struct hda_bind_ctls *c;
2629 int err;
2630
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002631 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002632 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002633 kcontrol->private_value = *c->values;
2634 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2635 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002636 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002637 return err;
2638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002639EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002640
2641struct hda_ctl_ops snd_hda_bind_vol = {
2642 .info = snd_hda_mixer_amp_volume_info,
2643 .get = snd_hda_mixer_amp_volume_get,
2644 .put = snd_hda_mixer_amp_volume_put,
2645 .tlv = snd_hda_mixer_amp_tlv
2646};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002647EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002648
2649struct hda_ctl_ops snd_hda_bind_sw = {
2650 .info = snd_hda_mixer_amp_switch_info,
2651 .get = snd_hda_mixer_amp_switch_get,
2652 .put = snd_hda_mixer_amp_switch_put,
2653 .tlv = snd_hda_mixer_amp_tlv
2654};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002655EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002656
2657/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 * SPDIF out controls
2659 */
2660
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2665 uinfo->count = 1;
2666 return 0;
2667}
2668
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2673 IEC958_AES0_NONAUDIO |
2674 IEC958_AES0_CON_EMPHASIS_5015 |
2675 IEC958_AES0_CON_NOT_COPYRIGHT;
2676 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2677 IEC958_AES1_CON_ORIGINAL;
2678 return 0;
2679}
2680
Takashi Iwai0ba21762007-04-16 11:29:14 +02002681static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2685 IEC958_AES0_NONAUDIO |
2686 IEC958_AES0_PRO_EMPHASIS_5015;
2687 return 0;
2688}
2689
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002694 int idx = kcontrol->private_value;
2695 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Stephen Warren7c935972011-06-01 11:14:17 -06002697 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2698 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2699 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2700 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 return 0;
2703}
2704
2705/* convert from SPDIF status bits to HDA SPDIF bits
2706 * bit 0 (DigEn) is always set zero (to be filled later)
2707 */
2708static unsigned short convert_from_spdif_status(unsigned int sbits)
2709{
2710 unsigned short val = 0;
2711
2712 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002713 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2718 IEC958_AES0_PRO_EMPHASIS_5015)
2719 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2722 IEC958_AES0_CON_EMPHASIS_5015)
2723 val |= AC_DIG1_EMPHASIS;
2724 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2725 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002727 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2729 }
2730 return val;
2731}
2732
2733/* convert to SPDIF status bits from HDA SPDIF bits
2734 */
2735static unsigned int convert_to_spdif_status(unsigned short val)
2736{
2737 unsigned int sbits = 0;
2738
Takashi Iwai0ba21762007-04-16 11:29:14 +02002739 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002741 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sbits |= IEC958_AES0_PROFESSIONAL;
2743 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002744 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2746 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002747 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002751 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2753 sbits |= val & (0x7f << 8);
2754 }
2755 return sbits;
2756}
2757
Takashi Iwai2f728532008-09-25 16:32:41 +02002758/* set digital convert verbs both for the given NID and its slaves */
2759static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2760 int verb, int val)
2761{
Takashi Iwaidda14412011-05-02 11:29:30 +02002762 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002763
Takashi Iwai9e976972008-11-25 08:17:20 +01002764 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002765 d = codec->slave_dig_outs;
2766 if (!d)
2767 return;
2768 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002769 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002770}
2771
2772static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2773 int dig1, int dig2)
2774{
2775 if (dig1 != -1)
2776 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2777 if (dig2 != -1)
2778 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2779}
2780
Takashi Iwai0ba21762007-04-16 11:29:14 +02002781static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002785 int idx = kcontrol->private_value;
2786 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2787 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 unsigned short val;
2789 int change;
2790
Ingo Molnar62932df2006-01-16 16:34:20 +01002791 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002792 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2794 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2795 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002796 val = convert_from_spdif_status(spdif->status);
2797 val |= spdif->ctls & 1;
2798 change = spdif->ctls != val;
2799 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002800 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002801 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002802 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return change;
2804}
2805
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002806#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Takashi Iwai0ba21762007-04-16 11:29:14 +02002808static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002812 int idx = kcontrol->private_value;
2813 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Stephen Warren7c935972011-06-01 11:14:17 -06002815 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 0;
2817}
2818
Stephen Warren74b654c2011-06-01 11:14:18 -06002819static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2820 int dig1, int dig2)
2821{
2822 set_dig_out_convert(codec, nid, dig1, dig2);
2823 /* unmute amp switch (if any) */
2824 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2825 (dig1 & AC_DIG1_ENABLE))
2826 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2827 HDA_AMP_MUTE, 0);
2828}
2829
Takashi Iwai0ba21762007-04-16 11:29:14 +02002830static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002834 int idx = kcontrol->private_value;
2835 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2836 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 unsigned short val;
2838 int change;
2839
Ingo Molnar62932df2006-01-16 16:34:20 +01002840 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002841 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002843 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002844 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002845 spdif->ctls = val;
2846 if (change && nid != (u16)-1)
2847 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002848 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return change;
2850}
2851
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002852static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 {
2854 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2855 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002856 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 .info = snd_hda_spdif_mask_info,
2858 .get = snd_hda_spdif_cmask_get,
2859 },
2860 {
2861 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002863 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 .info = snd_hda_spdif_mask_info,
2865 .get = snd_hda_spdif_pmask_get,
2866 },
2867 {
2868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002869 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 .info = snd_hda_spdif_mask_info,
2871 .get = snd_hda_spdif_default_get,
2872 .put = snd_hda_spdif_default_put,
2873 },
2874 {
2875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002876 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 .info = snd_hda_spdif_out_switch_info,
2878 .get = snd_hda_spdif_out_switch_get,
2879 .put = snd_hda_spdif_out_switch_put,
2880 },
2881 { } /* end */
2882};
2883
2884/**
2885 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2886 * @codec: the HDA codec
2887 * @nid: audio out widget NID
2888 *
2889 * Creates controls related with the SPDIF output.
2890 * Called from each patch supporting the SPDIF out.
2891 *
2892 * Returns 0 if successful, or a negative error code.
2893 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002894int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2895 hda_nid_t associated_nid,
2896 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002899 struct snd_kcontrol *kctl;
2900 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002901 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002902 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Takashi Iwai1afe2062010-12-23 10:17:52 +01002904 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2905 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002906 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2907 return -EBUSY;
2908 }
Stephen Warren7c935972011-06-01 11:14:17 -06002909 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2911 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002912 if (!kctl)
2913 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002914 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002915 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002916 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002917 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return err;
2919 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002920 spdif->nid = cvt_nid;
2921 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06002922 AC_VERB_GET_DIGI_CONVERT_1, 0);
2923 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
2925}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002926EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Stephen Warren7c935972011-06-01 11:14:17 -06002928struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2929 hda_nid_t nid)
2930{
2931 int i;
2932 for (i = 0; i < codec->spdif_out.used; i++) {
2933 struct hda_spdif_out *spdif =
2934 snd_array_elem(&codec->spdif_out, i);
2935 if (spdif->nid == nid)
2936 return spdif;
2937 }
2938 return NULL;
2939}
2940EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2941
Stephen Warren74b654c2011-06-01 11:14:18 -06002942void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2943{
2944 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2945
2946 mutex_lock(&codec->spdif_mutex);
2947 spdif->nid = (u16)-1;
2948 mutex_unlock(&codec->spdif_mutex);
2949}
2950EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2951
2952void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2953{
2954 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2955 unsigned short val;
2956
2957 mutex_lock(&codec->spdif_mutex);
2958 if (spdif->nid != nid) {
2959 spdif->nid = nid;
2960 val = spdif->ctls;
2961 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2962 }
2963 mutex_unlock(&codec->spdif_mutex);
2964}
2965EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002968 * SPDIF sharing with analog output
2969 */
2970static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2971 struct snd_ctl_elem_value *ucontrol)
2972{
2973 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2974 ucontrol->value.integer.value[0] = mout->share_spdif;
2975 return 0;
2976}
2977
2978static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2979 struct snd_ctl_elem_value *ucontrol)
2980{
2981 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2982 mout->share_spdif = !!ucontrol->value.integer.value[0];
2983 return 0;
2984}
2985
2986static struct snd_kcontrol_new spdif_share_sw = {
2987 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2988 .name = "IEC958 Default PCM Playback Switch",
2989 .info = snd_ctl_boolean_mono_info,
2990 .get = spdif_share_sw_get,
2991 .put = spdif_share_sw_put,
2992};
2993
Takashi Iwaid5191e52009-11-16 14:58:17 +01002994/**
2995 * snd_hda_create_spdif_share_sw - create Default PCM switch
2996 * @codec: the HDA codec
2997 * @mout: multi-out instance
2998 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002999int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3000 struct hda_multi_out *mout)
3001{
3002 if (!mout->dig_out_nid)
3003 return 0;
3004 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003005 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3006 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003007}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003008EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003009
3010/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 * SPDIF input
3012 */
3013
3014#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3015
Takashi Iwai0ba21762007-04-16 11:29:14 +02003016static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3017 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3020
3021 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3022 return 0;
3023}
3024
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
3028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3029 hda_nid_t nid = kcontrol->private_value;
3030 unsigned int val = !!ucontrol->value.integer.value[0];
3031 int change;
3032
Ingo Molnar62932df2006-01-16 16:34:20 +01003033 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003035 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003037 snd_hda_codec_write_cache(codec, nid, 0,
3038 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003040 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return change;
3042}
3043
Takashi Iwai0ba21762007-04-16 11:29:14 +02003044static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3048 hda_nid_t nid = kcontrol->private_value;
3049 unsigned short val;
3050 unsigned int sbits;
3051
Andrew Paprocki3982d172007-12-19 12:13:44 +01003052 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 sbits = convert_to_spdif_status(val);
3054 ucontrol->value.iec958.status[0] = sbits;
3055 ucontrol->value.iec958.status[1] = sbits >> 8;
3056 ucontrol->value.iec958.status[2] = sbits >> 16;
3057 ucontrol->value.iec958.status[3] = sbits >> 24;
3058 return 0;
3059}
3060
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003061static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 {
3063 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003064 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .info = snd_hda_spdif_in_switch_info,
3066 .get = snd_hda_spdif_in_switch_get,
3067 .put = snd_hda_spdif_in_switch_put,
3068 },
3069 {
3070 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3071 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003072 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 .info = snd_hda_spdif_mask_info,
3074 .get = snd_hda_spdif_in_status_get,
3075 },
3076 { } /* end */
3077};
3078
3079/**
3080 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3081 * @codec: the HDA codec
3082 * @nid: audio in widget NID
3083 *
3084 * Creates controls related with the SPDIF input.
3085 * Called from each patch supporting the SPDIF in.
3086 *
3087 * Returns 0 if successful, or a negative error code.
3088 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003089int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003092 struct snd_kcontrol *kctl;
3093 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003094 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Takashi Iwai1afe2062010-12-23 10:17:52 +01003096 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3097 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003098 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3099 return -EBUSY;
3100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3102 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003103 if (!kctl)
3104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003106 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return err;
3109 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003111 snd_hda_codec_read(codec, nid, 0,
3112 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003113 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return 0;
3115}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003116EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Takashi Iwai2a439522011-07-26 09:52:50 +02003118#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003119/*
3120 * command cache
3121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003123/* build a 32bit cache key with the widget id and the command parameter */
3124#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3125#define get_cmd_cache_nid(key) ((key) & 0xff)
3126#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3127
3128/**
3129 * snd_hda_codec_write_cache - send a single command with caching
3130 * @codec: the HDA codec
3131 * @nid: NID to send the command
3132 * @direct: direct flag
3133 * @verb: the verb to send
3134 * @parm: the parameter for the verb
3135 *
3136 * Send a single command without waiting for response.
3137 *
3138 * Returns 0 if successful, or a negative error code.
3139 */
3140int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3141 int direct, unsigned int verb, unsigned int parm)
3142{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003143 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3144 struct hda_cache_head *c;
3145 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003146
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003147 if (err < 0)
3148 return err;
3149 /* parm may contain the verb stuff for get/set amp */
3150 verb = verb | (parm >> 8);
3151 parm &= 0xff;
3152 key = build_cmd_cache_key(nid, verb);
3153 mutex_lock(&codec->bus->cmd_mutex);
3154 c = get_alloc_hash(&codec->cmd_cache, key);
3155 if (c)
3156 c->val = parm;
3157 mutex_unlock(&codec->bus->cmd_mutex);
3158 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003160EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003161
Takashi Iwaid5191e52009-11-16 14:58:17 +01003162/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003163 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3164 * @codec: the HDA codec
3165 * @nid: NID to send the command
3166 * @direct: direct flag
3167 * @verb: the verb to send
3168 * @parm: the parameter for the verb
3169 *
3170 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3171 * command if the parameter is already identical with the cached value.
3172 * If not, it sends the command and refreshes the cache.
3173 *
3174 * Returns 0 if successful, or a negative error code.
3175 */
3176int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3177 int direct, unsigned int verb, unsigned int parm)
3178{
3179 struct hda_cache_head *c;
3180 u32 key;
3181
3182 /* parm may contain the verb stuff for get/set amp */
3183 verb = verb | (parm >> 8);
3184 parm &= 0xff;
3185 key = build_cmd_cache_key(nid, verb);
3186 mutex_lock(&codec->bus->cmd_mutex);
3187 c = get_hash(&codec->cmd_cache, key);
3188 if (c && c->val == parm) {
3189 mutex_unlock(&codec->bus->cmd_mutex);
3190 return 0;
3191 }
3192 mutex_unlock(&codec->bus->cmd_mutex);
3193 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3194}
3195EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3196
3197/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003198 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3199 * @codec: HD-audio codec
3200 *
3201 * Execute all verbs recorded in the command caches to resume.
3202 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003203void snd_hda_codec_resume_cache(struct hda_codec *codec)
3204{
Takashi Iwai603c4012008-07-30 15:01:44 +02003205 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003206 int i;
3207
Takashi Iwai603c4012008-07-30 15:01:44 +02003208 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003209 u32 key = buffer->key;
3210 if (!key)
3211 continue;
3212 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3213 get_cmd_cache_cmd(key), buffer->val);
3214 }
3215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003216EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003217
3218/**
3219 * snd_hda_sequence_write_cache - sequence writes with caching
3220 * @codec: the HDA codec
3221 * @seq: VERB array to send
3222 *
3223 * Send the commands sequentially from the given array.
3224 * Thte commands are recorded on cache for power-save and resume.
3225 * The array must be terminated with NID=0.
3226 */
3227void snd_hda_sequence_write_cache(struct hda_codec *codec,
3228 const struct hda_verb *seq)
3229{
3230 for (; seq->nid; seq++)
3231 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3232 seq->param);
3233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003234EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003235#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003236
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003237void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3238 unsigned int power_state,
3239 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003240{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003241 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003242 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003243
Takashi Iwaicb53c622007-08-10 17:21:45 +02003244 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003245 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003246 if (!(wcaps & AC_WCAP_POWER))
3247 continue;
3248 /* don't power down the widget if it controls eapd and
3249 * EAPD_BTLENABLE is set.
3250 */
3251 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3252 get_wcaps_type(wcaps) == AC_WID_PIN &&
3253 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3254 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003255 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003256 if (eapd & 0x02)
3257 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003258 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003259 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3260 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003261 }
3262
Takashi Iwaicb53c622007-08-10 17:21:45 +02003263 if (power_state == AC_PWRST_D0) {
3264 unsigned long end_time;
3265 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003266 /* wait until the codec reachs to D0 */
3267 end_time = jiffies + msecs_to_jiffies(500);
3268 do {
3269 state = snd_hda_codec_read(codec, fg, 0,
3270 AC_VERB_GET_POWER_STATE, 0);
3271 if (state == power_state)
3272 break;
3273 msleep(1);
3274 } while (time_after_eq(end_time, jiffies));
3275 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003276}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003277EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3278
3279/*
3280 * set power state of the codec
3281 */
3282static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3283 unsigned int power_state)
3284{
3285 if (codec->patch_ops.set_power_state) {
3286 codec->patch_ops.set_power_state(codec, fg, power_state);
3287 return;
3288 }
3289
3290 /* this delay seems necessary to avoid click noise at power-down */
3291 if (power_state == AC_PWRST_D3)
3292 msleep(100);
3293 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3294 power_state);
3295 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3296}
Takashi Iwai54d17402005-11-21 16:33:22 +01003297
Takashi Iwai11aeff02008-07-30 15:01:46 +02003298#ifdef CONFIG_SND_HDA_HWDEP
3299/* execute additional init verbs */
3300static void hda_exec_init_verbs(struct hda_codec *codec)
3301{
3302 if (codec->init_verbs.list)
3303 snd_hda_sequence_write(codec, codec->init_verbs.list);
3304}
3305#else
3306static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3307#endif
3308
Takashi Iwai2a439522011-07-26 09:52:50 +02003309#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003310/*
3311 * call suspend and power-down; used both from PM and power-save
3312 */
3313static void hda_call_codec_suspend(struct hda_codec *codec)
3314{
3315 if (codec->patch_ops.suspend)
3316 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003317 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003318 hda_set_power_state(codec,
3319 codec->afg ? codec->afg : codec->mfg,
3320 AC_PWRST_D3);
3321#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003322 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003323 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003324 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003325 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003326 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003327#endif
3328}
3329
3330/*
3331 * kick up codec; used both from PM and power-save
3332 */
3333static void hda_call_codec_resume(struct hda_codec *codec)
3334{
3335 hda_set_power_state(codec,
3336 codec->afg ? codec->afg : codec->mfg,
3337 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003338 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003339 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003340 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003341 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003342 if (codec->patch_ops.resume)
3343 codec->patch_ops.resume(codec);
3344 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003345 if (codec->patch_ops.init)
3346 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003347 snd_hda_codec_resume_amp(codec);
3348 snd_hda_codec_resume_cache(codec);
3349 }
3350}
Takashi Iwai2a439522011-07-26 09:52:50 +02003351#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003352
Takashi Iwai54d17402005-11-21 16:33:22 +01003353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354/**
3355 * snd_hda_build_controls - build mixer controls
3356 * @bus: the BUS
3357 *
3358 * Creates mixer controls for each codec included in the bus.
3359 *
3360 * Returns 0 if successful, otherwise a negative error code.
3361 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003362int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003364 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Takashi Iwai0ba21762007-04-16 11:29:14 +02003366 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003367 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003368 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003369 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003370 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003371 err = snd_hda_codec_reset(codec);
3372 if (err < 0) {
3373 printk(KERN_ERR
3374 "hda_codec: cannot revert codec\n");
3375 return err;
3376 }
3377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003379 return 0;
3380}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003381EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003383int snd_hda_codec_build_controls(struct hda_codec *codec)
3384{
3385 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003386 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003387 /* continue to initialize... */
3388 if (codec->patch_ops.init)
3389 err = codec->patch_ops.init(codec);
3390 if (!err && codec->patch_ops.build_controls)
3391 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003392 if (err < 0)
3393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return 0;
3395}
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397/*
3398 * stream formats
3399 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003400struct hda_rate_tbl {
3401 unsigned int hz;
3402 unsigned int alsa_bits;
3403 unsigned int hda_fmt;
3404};
3405
Takashi Iwai92f10b32010-08-03 14:21:00 +02003406/* rate = base * mult / div */
3407#define HDA_RATE(base, mult, div) \
3408 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3409 (((div) - 1) << AC_FMT_DIV_SHIFT))
3410
Takashi Iwaibefdf312005-08-22 13:57:55 +02003411static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003413
3414 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003415 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3416 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3417 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3418 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3419 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3420 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3421 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3422 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3423 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3424 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3425 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003426#define AC_PAR_PCM_RATE_BITS 11
3427 /* up to bits 10, 384kHZ isn't supported properly */
3428
3429 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003430 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003431
Takashi Iwaibefdf312005-08-22 13:57:55 +02003432 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433};
3434
3435/**
3436 * snd_hda_calc_stream_format - calculate format bitset
3437 * @rate: the sample rate
3438 * @channels: the number of channels
3439 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3440 * @maxbps: the max. bps
3441 *
3442 * Calculate the format bitset from the given rate, channels and th PCM format.
3443 *
3444 * Return zero if invalid.
3445 */
3446unsigned int snd_hda_calc_stream_format(unsigned int rate,
3447 unsigned int channels,
3448 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003449 unsigned int maxbps,
3450 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 int i;
3453 unsigned int val = 0;
3454
Takashi Iwaibefdf312005-08-22 13:57:55 +02003455 for (i = 0; rate_bits[i].hz; i++)
3456 if (rate_bits[i].hz == rate) {
3457 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 break;
3459 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 snd_printdd("invalid rate %d\n", rate);
3462 return 0;
3463 }
3464
3465 if (channels == 0 || channels > 8) {
3466 snd_printdd("invalid channels %d\n", channels);
3467 return 0;
3468 }
3469 val |= channels - 1;
3470
3471 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003472 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003473 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003474 break;
3475 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003476 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 case 20:
3479 case 24:
3480 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003481 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003482 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003484 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003486 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 break;
3488 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489 snd_printdd("invalid format width %d\n",
3490 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return 0;
3492 }
3493
Anssi Hannula32c168c2010-08-03 13:28:57 +03003494 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003495 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return val;
3498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003499EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003501static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3502{
3503 unsigned int val = 0;
3504 if (nid != codec->afg &&
3505 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3506 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3507 if (!val || val == -1)
3508 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3509 if (!val || val == -1)
3510 return 0;
3511 return val;
3512}
3513
3514static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3515{
3516 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3517 get_pcm_param);
3518}
3519
3520static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3521{
3522 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3523 if (!streams || streams == -1)
3524 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3525 if (!streams || streams == -1)
3526 return 0;
3527 return streams;
3528}
3529
3530static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3531{
3532 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3533 get_stream_param);
3534}
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536/**
3537 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3538 * @codec: the HDA codec
3539 * @nid: NID to query
3540 * @ratesp: the pointer to store the detected rate bitflags
3541 * @formatsp: the pointer to store the detected formats
3542 * @bpsp: the pointer to store the detected format widths
3543 *
3544 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3545 * or @bsps argument is ignored.
3546 *
3547 * Returns 0 if successful, otherwise a negative error code.
3548 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003549int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3551{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003552 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003554 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003555 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 if (ratesp) {
3558 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003559 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003561 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003563 if (rates == 0) {
3564 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3565 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3566 nid, val,
3567 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3568 return -EIO;
3569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 *ratesp = rates;
3571 }
3572
3573 if (formatsp || bpsp) {
3574 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003575 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003577 streams = query_stream_param(codec, nid);
3578 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 bps = 0;
3582 if (streams & AC_SUPFMT_PCM) {
3583 if (val & AC_SUPPCM_BITS_8) {
3584 formats |= SNDRV_PCM_FMTBIT_U8;
3585 bps = 8;
3586 }
3587 if (val & AC_SUPPCM_BITS_16) {
3588 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3589 bps = 16;
3590 }
3591 if (wcaps & AC_WCAP_DIGITAL) {
3592 if (val & AC_SUPPCM_BITS_32)
3593 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3594 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3595 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3596 if (val & AC_SUPPCM_BITS_24)
3597 bps = 24;
3598 else if (val & AC_SUPPCM_BITS_20)
3599 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003600 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3601 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3603 if (val & AC_SUPPCM_BITS_32)
3604 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 else if (val & AC_SUPPCM_BITS_24)
3606 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02003607 else if (val & AC_SUPPCM_BITS_20)
3608 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003611 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003613 if (!bps)
3614 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003615 }
3616 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003617 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 /* temporary hack: we have still no proper support
3619 * for the direct AC3 stream...
3620 */
3621 formats |= SNDRV_PCM_FMTBIT_U8;
3622 bps = 8;
3623 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003624 if (formats == 0) {
3625 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3626 "(nid=0x%x, val=0x%x, ovrd=%i, "
3627 "streams=0x%x)\n",
3628 nid, val,
3629 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3630 streams);
3631 return -EIO;
3632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (formatsp)
3634 *formatsp = formats;
3635 if (bpsp)
3636 *bpsp = bps;
3637 }
3638
3639 return 0;
3640}
Stephen Warren384a48d2011-06-01 11:14:21 -06003641EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003644 * snd_hda_is_supported_format - Check the validity of the format
3645 * @codec: HD-audio codec
3646 * @nid: NID to check
3647 * @format: the HD-audio format value to check
3648 *
3649 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 *
3651 * Returns 1 if supported, 0 if not.
3652 */
3653int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3654 unsigned int format)
3655{
3656 int i;
3657 unsigned int val = 0, rate, stream;
3658
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003659 val = query_pcm_param(codec, nid);
3660 if (!val)
3661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003664 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003665 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 if (val & (1 << i))
3667 break;
3668 return 0;
3669 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003670 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return 0;
3672
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003673 stream = query_stream_param(codec, nid);
3674 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return 0;
3676
3677 if (stream & AC_SUPFMT_PCM) {
3678 switch (format & 0xf0) {
3679 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003680 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return 0;
3682 break;
3683 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003684 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return 0;
3686 break;
3687 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003688 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return 0;
3690 break;
3691 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003692 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return 0;
3694 break;
3695 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003696 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return 0;
3698 break;
3699 default:
3700 return 0;
3701 }
3702 } else {
3703 /* FIXME: check for float32 and AC3? */
3704 }
3705
3706 return 1;
3707}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003708EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710/*
3711 * PCM stuff
3712 */
3713static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3714 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003715 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
3717 return 0;
3718}
3719
3720static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3721 struct hda_codec *codec,
3722 unsigned int stream_tag,
3723 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003724 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
3726 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3727 return 0;
3728}
3729
3730static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3731 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003732 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Takashi Iwai888afa12008-03-18 09:57:50 +01003734 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return 0;
3736}
3737
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003738static int set_pcm_default_values(struct hda_codec *codec,
3739 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003741 int err;
3742
Takashi Iwai0ba21762007-04-16 11:29:14 +02003743 /* query support PCM information from the given NID */
3744 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003745 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003746 info->rates ? NULL : &info->rates,
3747 info->formats ? NULL : &info->formats,
3748 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003749 if (err < 0)
3750 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 }
3752 if (info->ops.open == NULL)
3753 info->ops.open = hda_pcm_default_open_close;
3754 if (info->ops.close == NULL)
3755 info->ops.close = hda_pcm_default_open_close;
3756 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003757 if (snd_BUG_ON(!info->nid))
3758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 info->ops.prepare = hda_pcm_default_prepare;
3760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003762 if (snd_BUG_ON(!info->nid))
3763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 info->ops.cleanup = hda_pcm_default_cleanup;
3765 }
3766 return 0;
3767}
3768
Takashi Iwaieb541332010-08-06 13:48:11 +02003769/*
3770 * codec prepare/cleanup entries
3771 */
3772int snd_hda_codec_prepare(struct hda_codec *codec,
3773 struct hda_pcm_stream *hinfo,
3774 unsigned int stream,
3775 unsigned int format,
3776 struct snd_pcm_substream *substream)
3777{
3778 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003779 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003780 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3781 if (ret >= 0)
3782 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003783 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003784 return ret;
3785}
3786EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3787
3788void snd_hda_codec_cleanup(struct hda_codec *codec,
3789 struct hda_pcm_stream *hinfo,
3790 struct snd_pcm_substream *substream)
3791{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003792 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003793 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003794 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003795}
3796EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3797
Takashi Iwaid5191e52009-11-16 14:58:17 +01003798/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003799const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3800 "Audio", "SPDIF", "HDMI", "Modem"
3801};
3802
Takashi Iwai176d5332008-07-30 15:01:44 +02003803/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003804 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003805 *
3806 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003807 */
3808static int get_empty_pcm_device(struct hda_bus *bus, int type)
3809{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003810 /* audio device indices; not linear to keep compatibility */
3811 static int audio_idx[HDA_PCM_NTYPES][5] = {
3812 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3813 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003814 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003815 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003816 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003817 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003818
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003819 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003820 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3821 return -EINVAL;
3822 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003823
3824 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3825 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3826 return audio_idx[type][i];
3827
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003828 /* non-fixed slots starting from 10 */
3829 for (i = 10; i < 32; i++) {
3830 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3831 return i;
3832 }
3833
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003834 snd_printk(KERN_WARNING "Too many %s devices\n",
3835 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003836 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003837}
3838
3839/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003840 * attach a new PCM stream
3841 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003842static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003843{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003844 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003845 struct hda_pcm_stream *info;
3846 int stream, err;
3847
Takashi Iwaib91f0802008-11-04 08:43:08 +01003848 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003849 return -EINVAL;
3850 for (stream = 0; stream < 2; stream++) {
3851 info = &pcm->stream[stream];
3852 if (info->substreams) {
3853 err = set_pcm_default_values(codec, info);
3854 if (err < 0)
3855 return err;
3856 }
3857 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003858 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003859}
3860
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003861/* assign all PCMs of the given codec */
3862int snd_hda_codec_build_pcms(struct hda_codec *codec)
3863{
3864 unsigned int pcm;
3865 int err;
3866
3867 if (!codec->num_pcms) {
3868 if (!codec->patch_ops.build_pcms)
3869 return 0;
3870 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003871 if (err < 0) {
3872 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003873 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003874 err = snd_hda_codec_reset(codec);
3875 if (err < 0) {
3876 printk(KERN_ERR
3877 "hda_codec: cannot revert codec\n");
3878 return err;
3879 }
3880 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003881 }
3882 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3883 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3884 int dev;
3885
3886 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003887 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003888
3889 if (!cpcm->pcm) {
3890 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3891 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003892 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003893 cpcm->device = dev;
3894 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003895 if (err < 0) {
3896 printk(KERN_ERR "hda_codec: cannot attach "
3897 "PCM stream %d for codec #%d\n",
3898 dev, codec->addr);
3899 continue; /* no fatal error */
3900 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003901 }
3902 }
3903 return 0;
3904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/**
3907 * snd_hda_build_pcms - build PCM information
3908 * @bus: the BUS
3909 *
3910 * Create PCM information for each codec included in the bus.
3911 *
3912 * The build_pcms codec patch is requested to set up codec->num_pcms and
3913 * codec->pcm_info properly. The array is referred by the top-level driver
3914 * to create its PCM instances.
3915 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3916 * callback.
3917 *
3918 * At least, substreams, channels_min and channels_max must be filled for
3919 * each stream. substreams = 0 indicates that the stream doesn't exist.
3920 * When rates and/or formats are zero, the supported values are queried
3921 * from the given nid. The nid is used also by the default ops.prepare
3922 * and ops.cleanup callbacks.
3923 *
3924 * The driver needs to call ops.open in its open callback. Similarly,
3925 * ops.close is supposed to be called in the close callback.
3926 * ops.prepare should be called in the prepare or hw_params callback
3927 * with the proper parameters for set up.
3928 * ops.cleanup should be called in hw_free for clean up of streams.
3929 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003930 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003932int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003934 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Takashi Iwai0ba21762007-04-16 11:29:14 +02003936 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003937 int err = snd_hda_codec_build_pcms(codec);
3938 if (err < 0)
3939 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
3941 return 0;
3942}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003943EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945/**
3946 * snd_hda_check_board_config - compare the current codec with the config table
3947 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003948 * @num_configs: number of config enums
3949 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 * @tbl: configuration table, terminated by null entries
3951 *
3952 * Compares the modelname or PCI subsystem id of the current codec with the
3953 * given configuration table. If a matching entry is found, returns its
3954 * config value (supposed to be 0 or positive).
3955 *
3956 * If no entries are matching, the function returns a negative value.
3957 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003958int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003959 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003960 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003962 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003963 int i;
3964 for (i = 0; i < num_configs; i++) {
3965 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003966 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003967 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3968 "selected\n", models[i]);
3969 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971 }
3972 }
3973
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003974 if (!codec->bus->pci || !tbl)
3975 return -1;
3976
3977 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3978 if (!tbl)
3979 return -1;
3980 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003981#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003982 char tmp[10];
3983 const char *model = NULL;
3984 if (models)
3985 model = models[tbl->value];
3986 if (!model) {
3987 sprintf(tmp, "#%d", tbl->value);
3988 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003990 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3991 "for config %x:%x (%s)\n",
3992 model, tbl->subvendor, tbl->subdevice,
3993 (tbl->name ? tbl->name : "Unknown device"));
3994#endif
3995 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 }
3997 return -1;
3998}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003999EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004002 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004003 subsystem ID with the
4004 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004005
4006 This is important for Gateway notebooks with SB450 HDA Audio
4007 where the vendor ID of the PCI device is:
4008 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4009 and the vendor/subvendor are found only at the codec.
4010
4011 * @codec: the HDA codec
4012 * @num_configs: number of config enums
4013 * @models: array of model name strings
4014 * @tbl: configuration table, terminated by null entries
4015 *
4016 * Compares the modelname or PCI subsystem id of the current codec with the
4017 * given configuration table. If a matching entry is found, returns its
4018 * config value (supposed to be 0 or positive).
4019 *
4020 * If no entries are matching, the function returns a negative value.
4021 */
4022int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004023 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004024 const struct snd_pci_quirk *tbl)
4025{
4026 const struct snd_pci_quirk *q;
4027
4028 /* Search for codec ID */
4029 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004030 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4031 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4032 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004033 break;
4034 }
4035
4036 if (!q->subvendor)
4037 return -1;
4038
4039 tbl = q;
4040
4041 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004042#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004043 char tmp[10];
4044 const char *model = NULL;
4045 if (models)
4046 model = models[tbl->value];
4047 if (!model) {
4048 sprintf(tmp, "#%d", tbl->value);
4049 model = tmp;
4050 }
4051 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4052 "for config %x:%x (%s)\n",
4053 model, tbl->subvendor, tbl->subdevice,
4054 (tbl->name ? tbl->name : "Unknown device"));
4055#endif
4056 return tbl->value;
4057 }
4058 return -1;
4059}
4060EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4061
4062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 * snd_hda_add_new_ctls - create controls from the array
4064 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004065 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 *
4067 * This helper function creates and add new controls in the given array.
4068 * The array must be terminated with an empty entry as terminator.
4069 *
4070 * Returns 0 if successful, or a negative error code.
4071 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004072int snd_hda_add_new_ctls(struct hda_codec *codec,
4073 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004075 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004078 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004079 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004080 if (knew->iface == -1) /* skip this codec private value */
4081 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004082 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004083 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004084 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004085 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004086 if (addr > 0)
4087 kctl->id.device = addr;
4088 if (idx > 0)
4089 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004090 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004091 if (!err)
4092 break;
4093 /* try first with another device index corresponding to
4094 * the codec addr; if it still fails (or it's the
4095 * primary codec), then try another control index
4096 */
4097 if (!addr && codec->addr)
4098 addr = codec->addr;
4099 else if (!idx && !knew->index) {
4100 idx = find_empty_mixer_ctl_idx(codec,
4101 knew->name);
4102 if (idx <= 0)
4103 return err;
4104 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004105 return err;
4106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
4108 return 0;
4109}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004110EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Takashi Iwaicb53c622007-08-10 17:21:45 +02004112#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004113static void hda_power_work(struct work_struct *work)
4114{
4115 struct hda_codec *codec =
4116 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004117 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004118
Maxim Levitsky2e492462007-09-03 15:26:57 +02004119 if (!codec->power_on || codec->power_count) {
4120 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004121 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004122 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004123
Takashi Iwaid66fee52011-08-02 15:39:31 +02004124 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004125 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004126 if (bus->ops.pm_notify)
4127 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004128}
4129
4130static void hda_keep_power_on(struct hda_codec *codec)
4131{
4132 codec->power_count++;
4133 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004134 codec->power_jiffies = jiffies;
4135}
4136
Takashi Iwaid5191e52009-11-16 14:58:17 +01004137/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004138void snd_hda_update_power_acct(struct hda_codec *codec)
4139{
4140 unsigned long delta = jiffies - codec->power_jiffies;
4141 if (codec->power_on)
4142 codec->power_on_acct += delta;
4143 else
4144 codec->power_off_acct += delta;
4145 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004146}
4147
Takashi Iwaid5191e52009-11-16 14:58:17 +01004148/**
4149 * snd_hda_power_up - Power-up the codec
4150 * @codec: HD-audio codec
4151 *
4152 * Increment the power-up counter and power up the hardware really when
4153 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004154 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004155void snd_hda_power_up(struct hda_codec *codec)
4156{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004157 struct hda_bus *bus = codec->bus;
4158
Takashi Iwaicb53c622007-08-10 17:21:45 +02004159 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004160 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004161 return;
4162
Takashi Iwaid66fee52011-08-02 15:39:31 +02004163 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004164 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004165 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004166 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004167 if (bus->ops.pm_notify)
4168 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004169 hda_call_codec_resume(codec);
4170 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004171 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004173EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004174
4175#define power_save(codec) \
4176 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004177
Takashi Iwaid5191e52009-11-16 14:58:17 +01004178/**
4179 * snd_hda_power_down - Power-down the codec
4180 * @codec: HD-audio codec
4181 *
4182 * Decrement the power-up counter and schedules the power-off work if
4183 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004184 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004185void snd_hda_power_down(struct hda_codec *codec)
4186{
4187 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004188 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004189 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004190 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004191 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004192 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004193 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004194 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004195}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004196EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004197
Takashi Iwaid5191e52009-11-16 14:58:17 +01004198/**
4199 * snd_hda_check_amp_list_power - Check the amp list and update the power
4200 * @codec: HD-audio codec
4201 * @check: the object containing an AMP list and the status
4202 * @nid: NID to check / update
4203 *
4204 * Check whether the given NID is in the amp list. If it's in the list,
4205 * check the current AMP status, and update the the power-status according
4206 * to the mute status.
4207 *
4208 * This function is supposed to be set or called from the check_power_status
4209 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004210 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004211int snd_hda_check_amp_list_power(struct hda_codec *codec,
4212 struct hda_loopback_check *check,
4213 hda_nid_t nid)
4214{
Takashi Iwai031024e2011-05-02 11:29:30 +02004215 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004216 int ch, v;
4217
4218 if (!check->amplist)
4219 return 0;
4220 for (p = check->amplist; p->nid; p++) {
4221 if (p->nid == nid)
4222 break;
4223 }
4224 if (!p->nid)
4225 return 0; /* nothing changed */
4226
4227 for (p = check->amplist; p->nid; p++) {
4228 for (ch = 0; ch < 2; ch++) {
4229 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4230 p->idx);
4231 if (!(v & HDA_AMP_MUTE) && v > 0) {
4232 if (!check->power_on) {
4233 check->power_on = 1;
4234 snd_hda_power_up(codec);
4235 }
4236 return 1;
4237 }
4238 }
4239 }
4240 if (check->power_on) {
4241 check->power_on = 0;
4242 snd_hda_power_down(codec);
4243 }
4244 return 0;
4245}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004246EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004249/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004250 * Channel mode helper
4251 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004252
4253/**
4254 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4255 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004256int snd_hda_ch_mode_info(struct hda_codec *codec,
4257 struct snd_ctl_elem_info *uinfo,
4258 const struct hda_channel_mode *chmode,
4259 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004260{
4261 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4262 uinfo->count = 1;
4263 uinfo->value.enumerated.items = num_chmodes;
4264 if (uinfo->value.enumerated.item >= num_chmodes)
4265 uinfo->value.enumerated.item = num_chmodes - 1;
4266 sprintf(uinfo->value.enumerated.name, "%dch",
4267 chmode[uinfo->value.enumerated.item].channels);
4268 return 0;
4269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004270EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004271
Takashi Iwaid5191e52009-11-16 14:58:17 +01004272/**
4273 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4274 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004275int snd_hda_ch_mode_get(struct hda_codec *codec,
4276 struct snd_ctl_elem_value *ucontrol,
4277 const struct hda_channel_mode *chmode,
4278 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004279 int max_channels)
4280{
4281 int i;
4282
4283 for (i = 0; i < num_chmodes; i++) {
4284 if (max_channels == chmode[i].channels) {
4285 ucontrol->value.enumerated.item[0] = i;
4286 break;
4287 }
4288 }
4289 return 0;
4290}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004291EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004292
Takashi Iwaid5191e52009-11-16 14:58:17 +01004293/**
4294 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004296int snd_hda_ch_mode_put(struct hda_codec *codec,
4297 struct snd_ctl_elem_value *ucontrol,
4298 const struct hda_channel_mode *chmode,
4299 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004300 int *max_channelsp)
4301{
4302 unsigned int mode;
4303
4304 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004305 if (mode >= num_chmodes)
4306 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004307 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004308 return 0;
4309 /* change the current channel setting */
4310 *max_channelsp = chmode[mode].channels;
4311 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004312 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004313 return 1;
4314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004315EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317/*
4318 * input MUX helper
4319 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004320
4321/**
4322 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004324int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4325 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 unsigned int index;
4328
4329 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4330 uinfo->count = 1;
4331 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004332 if (!imux->num_items)
4333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 index = uinfo->value.enumerated.item;
4335 if (index >= imux->num_items)
4336 index = imux->num_items - 1;
4337 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4338 return 0;
4339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004340EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Takashi Iwaid5191e52009-11-16 14:58:17 +01004342/**
4343 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4344 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004345int snd_hda_input_mux_put(struct hda_codec *codec,
4346 const struct hda_input_mux *imux,
4347 struct snd_ctl_elem_value *ucontrol,
4348 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 unsigned int *cur_val)
4350{
4351 unsigned int idx;
4352
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004353 if (!imux->num_items)
4354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 idx = ucontrol->value.enumerated.item[0];
4356 if (idx >= imux->num_items)
4357 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004358 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004360 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4361 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 *cur_val = idx;
4363 return 1;
4364}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004365EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
4367
4368/*
4369 * Multi-channel / digital-out PCM helper functions
4370 */
4371
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004372/* setup SPDIF output stream */
4373static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4374 unsigned int stream_tag, unsigned int format)
4375{
Stephen Warren7c935972011-06-01 11:14:17 -06004376 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4377
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004378 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004379 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004380 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004381 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004382 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004383 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004384 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004385 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004386 for (d = codec->slave_dig_outs; *d; d++)
4387 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4388 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004389 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004390 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004391 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004392 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004393 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004394}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004395
Takashi Iwai2f728532008-09-25 16:32:41 +02004396static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4397{
4398 snd_hda_codec_cleanup_stream(codec, nid);
4399 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004400 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004401 for (d = codec->slave_dig_outs; *d; d++)
4402 snd_hda_codec_cleanup_stream(codec, *d);
4403 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004404}
4405
Takashi Iwaid5191e52009-11-16 14:58:17 +01004406/**
4407 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4408 * @bus: HD-audio bus
4409 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004410void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4411{
4412 struct hda_codec *codec;
4413
4414 if (!bus)
4415 return;
4416 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004417 if (hda_codec_is_power_on(codec) &&
4418 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004419 codec->patch_ops.reboot_notify(codec);
4420 }
4421}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004422EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004423
Takashi Iwaid5191e52009-11-16 14:58:17 +01004424/**
4425 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004427int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4428 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Ingo Molnar62932df2006-01-16 16:34:20 +01004430 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004431 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4432 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004433 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004435 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return 0;
4437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004438EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Takashi Iwaid5191e52009-11-16 14:58:17 +01004440/**
4441 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4442 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004443int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4444 struct hda_multi_out *mout,
4445 unsigned int stream_tag,
4446 unsigned int format,
4447 struct snd_pcm_substream *substream)
4448{
4449 mutex_lock(&codec->spdif_mutex);
4450 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4451 mutex_unlock(&codec->spdif_mutex);
4452 return 0;
4453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004454EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004455
Takashi Iwaid5191e52009-11-16 14:58:17 +01004456/**
4457 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4458 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004459int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4460 struct hda_multi_out *mout)
4461{
4462 mutex_lock(&codec->spdif_mutex);
4463 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4464 mutex_unlock(&codec->spdif_mutex);
4465 return 0;
4466}
4467EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4468
Takashi Iwaid5191e52009-11-16 14:58:17 +01004469/**
4470 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004472int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4473 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
Ingo Molnar62932df2006-01-16 16:34:20 +01004475 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004477 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return 0;
4479}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004480EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Takashi Iwaid5191e52009-11-16 14:58:17 +01004482/**
4483 * snd_hda_multi_out_analog_open - open analog outputs
4484 *
4485 * Open analog outputs and set up the hw-constraints.
4486 * If the digital outputs can be opened as slave, open the digital
4487 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004489int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4490 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004491 struct snd_pcm_substream *substream,
4492 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Takashi Iwai9a081602008-02-12 18:37:26 +01004494 struct snd_pcm_runtime *runtime = substream->runtime;
4495 runtime->hw.channels_max = mout->max_channels;
4496 if (mout->dig_out_nid) {
4497 if (!mout->analog_rates) {
4498 mout->analog_rates = hinfo->rates;
4499 mout->analog_formats = hinfo->formats;
4500 mout->analog_maxbps = hinfo->maxbps;
4501 } else {
4502 runtime->hw.rates = mout->analog_rates;
4503 runtime->hw.formats = mout->analog_formats;
4504 hinfo->maxbps = mout->analog_maxbps;
4505 }
4506 if (!mout->spdif_rates) {
4507 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4508 &mout->spdif_rates,
4509 &mout->spdif_formats,
4510 &mout->spdif_maxbps);
4511 }
4512 mutex_lock(&codec->spdif_mutex);
4513 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004514 if ((runtime->hw.rates & mout->spdif_rates) &&
4515 (runtime->hw.formats & mout->spdif_formats)) {
4516 runtime->hw.rates &= mout->spdif_rates;
4517 runtime->hw.formats &= mout->spdif_formats;
4518 if (mout->spdif_maxbps < hinfo->maxbps)
4519 hinfo->maxbps = mout->spdif_maxbps;
4520 } else {
4521 mout->share_spdif = 0;
4522 /* FIXME: need notify? */
4523 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004524 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004525 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4528 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4529}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004530EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Takashi Iwaid5191e52009-11-16 14:58:17 +01004532/**
4533 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4534 *
4535 * Set up the i/o for analog out.
4536 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004538int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4539 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 unsigned int stream_tag,
4541 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004542 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Takashi Iwaidda14412011-05-02 11:29:30 +02004544 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004546 struct hda_spdif_out *spdif =
4547 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 int i;
4549
Ingo Molnar62932df2006-01-16 16:34:20 +01004550 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004551 if (mout->dig_out_nid && mout->share_spdif &&
4552 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004554 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4555 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004556 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004558 setup_dig_out_stream(codec, mout->dig_out_nid,
4559 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 } else {
4561 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004562 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
4564 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004565 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004568 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4569 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004570 if (!mout->no_share_stream &&
4571 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004573 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4574 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004575 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004576 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4577 if (!mout->no_share_stream && mout->hp_out_nid[i])
4578 snd_hda_codec_setup_stream(codec,
4579 mout->hp_out_nid[i],
4580 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004581 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004582 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004583 snd_hda_codec_setup_stream(codec,
4584 mout->extra_out_nid[i],
4585 stream_tag, 0, format);
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 /* surrounds */
4588 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004589 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4591 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004592 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004593 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4594 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 }
4596 return 0;
4597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004598EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Takashi Iwaid5191e52009-11-16 14:58:17 +01004600/**
4601 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004603int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4604 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Takashi Iwaidda14412011-05-02 11:29:30 +02004606 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 int i;
4608
4609 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004610 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004612 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004613 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4614 if (mout->hp_out_nid[i])
4615 snd_hda_codec_cleanup_stream(codec,
4616 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004617 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4618 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004619 snd_hda_codec_cleanup_stream(codec,
4620 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004621 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004623 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 mout->dig_out_used = 0;
4625 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004626 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 return 0;
4628}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004629EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004631/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004632 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004633 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004634
Takashi Iwaidda14412011-05-02 11:29:30 +02004635static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004636{
4637 for (; *list; list++)
4638 if (*list == nid)
4639 return 1;
4640 return 0;
4641}
4642
Steve Longerbeam81937d32007-05-08 15:33:03 +02004643
4644/*
4645 * Sort an associated group of pins according to their sequence numbers.
4646 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004647static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004648 int num_pins)
4649{
4650 int i, j;
4651 short seq;
4652 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004653
Steve Longerbeam81937d32007-05-08 15:33:03 +02004654 for (i = 0; i < num_pins; i++) {
4655 for (j = i + 1; j < num_pins; j++) {
4656 if (sequences[i] > sequences[j]) {
4657 seq = sequences[i];
4658 sequences[i] = sequences[j];
4659 sequences[j] = seq;
4660 nid = pins[i];
4661 pins[i] = pins[j];
4662 pins[j] = nid;
4663 }
4664 }
4665 }
4666}
4667
4668
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004669/* add the found input-pin to the cfg->inputs[] table */
4670static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4671 int type)
4672{
4673 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4674 cfg->inputs[cfg->num_inputs].pin = nid;
4675 cfg->inputs[cfg->num_inputs].type = type;
4676 cfg->num_inputs++;
4677 }
4678}
4679
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004680/* sort inputs in the order of AUTO_PIN_* type */
4681static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4682{
4683 int i, j;
4684
4685 for (i = 0; i < cfg->num_inputs; i++) {
4686 for (j = i + 1; j < cfg->num_inputs; j++) {
4687 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4688 struct auto_pin_cfg_item tmp;
4689 tmp = cfg->inputs[i];
4690 cfg->inputs[i] = cfg->inputs[j];
4691 cfg->inputs[j] = tmp;
4692 }
4693 }
4694 }
4695}
4696
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004697/* Reorder the surround channels
4698 * ALSA sequence is front/surr/clfe/side
4699 * HDA sequence is:
4700 * 4-ch: front/surr => OK as it is
4701 * 6-ch: front/clfe/surr
4702 * 8-ch: front/clfe/rear/side|fc
4703 */
4704static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4705{
4706 hda_nid_t nid;
4707
4708 switch (nums) {
4709 case 3:
4710 case 4:
4711 nid = pins[1];
4712 pins[1] = pins[2];
4713 pins[2] = nid;
4714 break;
4715 }
4716}
4717
Takashi Iwai82bc9552006-03-21 11:24:42 +01004718/*
4719 * Parse all pin widgets and store the useful pin nids to cfg
4720 *
4721 * The number of line-outs or any primary output is stored in line_outs,
4722 * and the corresponding output pins are assigned to line_out_pins[],
4723 * in the order of front, rear, CLFE, side, ...
4724 *
4725 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004726 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004727 * is detected, one of speaker of HP pins is assigned as the primary
4728 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4729 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004730 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004731 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004732 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4733 * respectively.
4734 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004735int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4736 struct auto_pin_cfg *cfg,
4737 const hda_nid_t *ignore_nids,
4738 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004739{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004740 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004741 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004742 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4743 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004744 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004745 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004746
4747 memset(cfg, 0, sizeof(*cfg));
4748
Steve Longerbeam81937d32007-05-08 15:33:03 +02004749 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4750 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004751 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004752 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004753
Takashi Iwai2f451d22011-11-10 12:36:46 +01004754 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004755 end_nid = codec->start_nid + codec->num_nodes;
4756 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004757 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004758 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004759 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004760 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004761
4762 /* read all default configuration for pin complex */
4763 if (wid_type != AC_WID_PIN)
4764 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004765 /* ignore the given nids (e.g. pc-beep returns error) */
4766 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4767 continue;
4768
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004769 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01004770 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4771 AC_DEFCFG_MISC_NO_PRESENCE))
4772 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004773 conn = get_defcfg_connect(def_conf);
4774 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004775 continue;
4776 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004777 dev = get_defcfg_device(def_conf);
4778
4779 /* workaround for buggy BIOS setups */
4780 if (dev == AC_JACK_LINE_OUT) {
4781 if (conn == AC_JACK_PORT_FIXED)
4782 dev = AC_JACK_SPEAKER;
4783 }
4784
4785 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004786 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004787 seq = get_defcfg_sequence(def_conf);
4788 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004789
4790 if (!(wid_caps & AC_WCAP_STEREO))
4791 if (!cfg->mono_out_pin)
4792 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004793 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004794 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004795 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004796 assoc_line_out = assoc;
4797 else if (assoc_line_out != assoc)
4798 continue;
4799 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4800 continue;
4801 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004802 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004803 cfg->line_outs++;
4804 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004805 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004806 seq = get_defcfg_sequence(def_conf);
4807 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004808 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4809 continue;
4810 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004811 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004812 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004813 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004814 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004815 seq = get_defcfg_sequence(def_conf);
4816 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004817 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4818 continue;
4819 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004820 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004821 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004822 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004823 case AC_JACK_MIC_IN:
4824 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004825 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004826 case AC_JACK_LINE_IN:
4827 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004828 break;
4829 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004830 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004831 break;
4832 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004833 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004834 break;
4835 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004836 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004837 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4838 continue;
4839 cfg->dig_out_pins[cfg->dig_outs] = nid;
4840 cfg->dig_out_type[cfg->dig_outs] =
4841 (loc == AC_JACK_LOC_HDMI) ?
4842 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4843 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004844 break;
4845 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004846 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004847 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004848 if (loc == AC_JACK_LOC_HDMI)
4849 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4850 else
4851 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004852 break;
4853 }
4854 }
4855
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004856 /* FIX-UP:
4857 * If no line-out is defined but multiple HPs are found,
4858 * some of them might be the real line-outs.
4859 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004860 if (!cfg->line_outs && cfg->hp_outs > 1 &&
4861 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004862 int i = 0;
4863 while (i < cfg->hp_outs) {
4864 /* The real HPs should have the sequence 0x0f */
4865 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4866 i++;
4867 continue;
4868 }
4869 /* Move it to the line-out table */
4870 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4871 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4872 cfg->line_outs++;
4873 cfg->hp_outs--;
4874 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4875 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004876 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004877 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4878 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004879 memset(cfg->hp_pins + cfg->hp_outs, 0,
4880 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004881 if (!cfg->hp_outs)
4882 cfg->line_out_type = AUTO_PIN_HP_OUT;
4883
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004884 }
4885
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004886 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004887 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4888 cfg->line_outs);
4889 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4890 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004891 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4892 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004893
Steve Longerbeam81937d32007-05-08 15:33:03 +02004894 /*
4895 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4896 * as a primary output
4897 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004898 if (!cfg->line_outs &&
4899 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02004900 if (cfg->speaker_outs) {
4901 cfg->line_outs = cfg->speaker_outs;
4902 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4903 sizeof(cfg->speaker_pins));
4904 cfg->speaker_outs = 0;
4905 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4906 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4907 } else if (cfg->hp_outs) {
4908 cfg->line_outs = cfg->hp_outs;
4909 memcpy(cfg->line_out_pins, cfg->hp_pins,
4910 sizeof(cfg->hp_pins));
4911 cfg->hp_outs = 0;
4912 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4913 cfg->line_out_type = AUTO_PIN_HP_OUT;
4914 }
4915 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004916
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004917 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
4918 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
4919 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004920
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004921 sort_autocfg_input_pins(cfg);
4922
Takashi Iwai82bc9552006-03-21 11:24:42 +01004923 /*
4924 * debug prints of the parsed results
4925 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004926 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 +01004927 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4928 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004929 cfg->line_out_pins[4],
4930 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4931 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4932 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004933 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4934 cfg->speaker_outs, cfg->speaker_pins[0],
4935 cfg->speaker_pins[1], cfg->speaker_pins[2],
4936 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004937 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4938 cfg->hp_outs, cfg->hp_pins[0],
4939 cfg->hp_pins[1], cfg->hp_pins[2],
4940 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004941 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004942 if (cfg->dig_outs)
4943 snd_printd(" dig-out=0x%x/0x%x\n",
4944 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004945 snd_printd(" inputs:");
4946 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004947 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004948 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004949 cfg->inputs[i].pin);
4950 }
4951 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004952 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004953 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004954
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004955 return 0;
4956}
Takashi Iwaid025feb2011-08-23 15:24:39 +02004957EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004958
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004959int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004960{
4961 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004962 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004963 if (conn == AC_JACK_PORT_NONE)
4964 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004965 /* Windows may claim the internal mic to be BOTH, too */
4966 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004967 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004968 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004969 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004970 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004971 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004972 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004973 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004974 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004975 return INPUT_PIN_ATTR_FRONT;
4976 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004977}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004978EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004979
Takashi Iwai990061c2010-09-09 22:08:44 +02004980/**
4981 * hda_get_input_pin_label - Give a label for the given input pin
4982 *
4983 * When check_location is true, the function checks the pin location
4984 * for mic and line-in pins, and set an appropriate prefix like "Front",
4985 * "Rear", "Internal".
4986 */
4987
Takashi Iwai04f5ade2011-10-27 20:47:07 +02004988static const char *hda_get_input_pin_label(struct hda_codec *codec,
4989 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02004990{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004991 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004992 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004993 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4994 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004995 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004996
Takashi Iwai10a20af2010-09-09 16:28:02 +02004997 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004998
4999 switch (get_defcfg_device(def_conf)) {
5000 case AC_JACK_MIC_IN:
5001 if (!check_location)
5002 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005003 attr = snd_hda_get_input_pin_attr(def_conf);
5004 if (!attr)
5005 return "None";
5006 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005007 case AC_JACK_LINE_IN:
5008 if (!check_location)
5009 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005010 attr = snd_hda_get_input_pin_attr(def_conf);
5011 if (!attr)
5012 return "None";
5013 if (attr == INPUT_PIN_ATTR_DOCK)
5014 return "Dock Line";
5015 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005016 case AC_JACK_AUX:
5017 return "Aux";
5018 case AC_JACK_CD:
5019 return "CD";
5020 case AC_JACK_SPDIF_IN:
5021 return "SPDIF In";
5022 case AC_JACK_DIG_OTHER_IN:
5023 return "Digital In";
5024 default:
5025 return "Misc";
5026 }
5027}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005028
Takashi Iwaia1c98512010-09-09 21:36:27 +02005029/* Check whether the location prefix needs to be added to the label.
5030 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5031 * have to put "Front" prefix to each label. In such a case, returns false.
5032 */
5033static int check_mic_location_need(struct hda_codec *codec,
5034 const struct auto_pin_cfg *cfg,
5035 int input)
5036{
5037 unsigned int defc;
5038 int i, attr, attr2;
5039
5040 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005041 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005042 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005043 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005044 return 1;
5045
5046 attr = 0;
5047 for (i = 0; i < cfg->num_inputs; i++) {
5048 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005049 attr2 = snd_hda_get_input_pin_attr(defc);
5050 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005051 if (attr && attr != attr2)
5052 return 1; /* different locations found */
5053 attr = attr2;
5054 }
5055 }
5056 return 0;
5057}
5058
Takashi Iwai990061c2010-09-09 22:08:44 +02005059/**
5060 * hda_get_autocfg_input_label - Get a label for the given input
5061 *
5062 * Get a label for the given input pin defined by the autocfg item.
5063 * Unlike hda_get_input_pin_label(), this function checks all inputs
5064 * defined in autocfg and avoids the redundant mic/line prefix as much as
5065 * possible.
5066 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005067const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5068 const struct auto_pin_cfg *cfg,
5069 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005070{
5071 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005072 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005073
Takashi Iwai10a20af2010-09-09 16:28:02 +02005074 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5075 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5076 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005077 if (has_multiple_pins && type == AUTO_PIN_MIC)
5078 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005079 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5080 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005081}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005082EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5083
Takashi Iwai358b6e62011-11-21 14:34:20 +01005084/* return the position of NID in the list, or -1 if not found */
5085static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5086{
5087 int i;
5088 for (i = 0; i < nums; i++)
5089 if (list[i] == nid)
5090 return i;
5091 return -1;
5092}
5093
Takashi Iwai201e06f2011-11-16 15:33:26 +01005094/* get a unique suffix or an index number */
5095static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5096 int num_pins, int *indexp)
5097{
5098 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005099 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005100 };
5101 int i;
5102
Takashi Iwai358b6e62011-11-21 14:34:20 +01005103 i = find_idx_in_nid_list(nid, pins, num_pins);
5104 if (i < 0)
5105 return NULL;
5106 if (num_pins == 1)
5107 return "";
5108 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5109 if (indexp)
5110 *indexp = i;
5111 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005112 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005113 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005114}
5115
5116static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5117 const struct auto_pin_cfg *cfg,
5118 const char *name, char *label, int maxlen,
5119 int *indexp)
5120{
5121 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5122 int attr = snd_hda_get_input_pin_attr(def_conf);
5123 const char *pfx = "", *sfx = "";
5124
5125 /* handle as a speaker if it's a fixed line-out */
5126 if (!strcmp(name, "Line-Out") && attr == INPUT_PIN_ATTR_INT)
5127 name = "Speaker";
5128 /* check the location */
5129 switch (attr) {
5130 case INPUT_PIN_ATTR_DOCK:
5131 pfx = "Dock ";
5132 break;
5133 case INPUT_PIN_ATTR_FRONT:
5134 pfx = "Front ";
5135 break;
5136 }
5137 if (cfg) {
5138 /* try to give a unique suffix if needed */
5139 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5140 indexp);
5141 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005142 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5143 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005144 if (!sfx) {
5145 /* don't add channel suffix for Headphone controls */
5146 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5147 cfg->hp_outs);
5148 if (idx >= 0)
5149 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005150 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005151 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005152 }
5153 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5154 return 1;
5155}
5156
Takashi Iwai990061c2010-09-09 22:08:44 +02005157/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005158 * snd_hda_get_pin_label - Get a label for the given I/O pin
5159 *
5160 * Get a label for the given pin. This function works for both input and
5161 * output pins. When @cfg is given as non-NULL, the function tries to get
5162 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005163 *
5164 * This function tries to give a unique label string for the pin as much as
5165 * possible. For example, when the multiple line-outs are present, it adds
5166 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5167 * If no unique name with a suffix is available and @indexp is non-NULL, the
5168 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005169 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005170int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5171 const struct auto_pin_cfg *cfg,
5172 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005173{
5174 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005175 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005176 int i;
5177
Takashi Iwai201e06f2011-11-16 15:33:26 +01005178 if (indexp)
5179 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005180 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005181 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005182
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005183 switch (get_defcfg_device(def_conf)) {
5184 case AC_JACK_LINE_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005185 return fill_audio_out_name(codec, nid, cfg, "Line-Out",
5186 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005187 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005188 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5189 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005190 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005191 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5192 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005193 case AC_JACK_SPDIF_OUT:
5194 case AC_JACK_DIG_OTHER_OUT:
5195 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005196 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005197 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005198 name = "SPDIF";
5199 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005200 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5201 cfg->dig_outs);
5202 if (i >= 0)
5203 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005204 }
5205 break;
5206 default:
5207 if (cfg) {
5208 for (i = 0; i < cfg->num_inputs; i++) {
5209 if (cfg->inputs[i].pin != nid)
5210 continue;
5211 name = hda_get_autocfg_input_label(codec, cfg, i);
5212 if (name)
5213 break;
5214 }
5215 }
5216 if (!name)
5217 name = hda_get_input_pin_label(codec, nid, true);
5218 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005219 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005220 if (!name)
5221 return 0;
5222 strlcpy(label, name, maxlen);
5223 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005224}
5225EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5226
Takashi Iwai990061c2010-09-09 22:08:44 +02005227/**
5228 * snd_hda_add_imux_item - Add an item to input_mux
5229 *
5230 * When the same label is used already in the existing items, the number
5231 * suffix is appended to the label. This label index number is stored
5232 * to type_idx when non-NULL pointer is given.
5233 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005234int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5235 int index, int *type_idx)
5236{
5237 int i, label_idx = 0;
5238 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5239 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5240 return -EINVAL;
5241 }
5242 for (i = 0; i < imux->num_items; i++) {
5243 if (!strncmp(label, imux->items[i].label, strlen(label)))
5244 label_idx++;
5245 }
5246 if (type_idx)
5247 *type_idx = label_idx;
5248 if (label_idx > 0)
5249 snprintf(imux->items[imux->num_items].label,
5250 sizeof(imux->items[imux->num_items].label),
5251 "%s %d", label, label_idx);
5252 else
5253 strlcpy(imux->items[imux->num_items].label, label,
5254 sizeof(imux->items[imux->num_items].label));
5255 imux->items[imux->num_items].index = index;
5256 imux->num_items++;
5257 return 0;
5258}
5259EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005260
Takashi Iwai4a471b72005-12-07 13:56:29 +01005261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262#ifdef CONFIG_PM
5263/*
5264 * power management
5265 */
5266
5267/**
5268 * snd_hda_suspend - suspend the codecs
5269 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 *
5271 * Returns 0 if successful.
5272 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005273int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005275 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
Takashi Iwai0ba21762007-04-16 11:29:14 +02005277 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005278 if (hda_codec_is_power_on(codec))
5279 hda_call_codec_suspend(codec);
5280 if (codec->patch_ops.post_suspend)
5281 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
5283 return 0;
5284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005285EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
5287/**
5288 * snd_hda_resume - resume the codecs
5289 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 *
5291 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005292 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005293 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005294 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 */
5296int snd_hda_resume(struct hda_bus *bus)
5297{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005298 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Takashi Iwai0ba21762007-04-16 11:29:14 +02005300 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005301 if (codec->patch_ops.pre_resume)
5302 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005303 if (snd_hda_codec_needs_resume(codec))
5304 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return 0;
5307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005308EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005309#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005310
5311/*
5312 * generic arrays
5313 */
5314
Takashi Iwaid5191e52009-11-16 14:58:17 +01005315/**
5316 * snd_array_new - get a new element from the given array
5317 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005318 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005319 * Get a new element from the given array. If it exceeds the
5320 * pre-allocated array size, re-allocate the array.
5321 *
5322 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005323 */
5324void *snd_array_new(struct snd_array *array)
5325{
5326 if (array->used >= array->alloced) {
5327 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005328 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005329 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005330 void *nlist;
5331 if (snd_BUG_ON(num >= 4096))
5332 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005333 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005334 if (!nlist)
5335 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005336 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005337 array->list = nlist;
5338 array->alloced = num;
5339 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005340 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005342EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005343
Takashi Iwaid5191e52009-11-16 14:58:17 +01005344/**
5345 * snd_array_free - free the given array elements
5346 * @array: the array object
5347 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005348void snd_array_free(struct snd_array *array)
5349{
5350 kfree(array->list);
5351 array->used = 0;
5352 array->alloced = 0;
5353 array->list = NULL;
5354}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005355EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005356
Takashi Iwaid5191e52009-11-16 14:58:17 +01005357/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005358 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5359 * @pcm: PCM caps bits
5360 * @buf: the string buffer to write
5361 * @buflen: the max buffer length
5362 *
5363 * used by hda_proc.c and hda_eld.c
5364 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005365void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5366{
5367 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5368 int i, j;
5369
5370 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5371 if (pcm & (AC_SUPPCM_BITS_8 << i))
5372 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5373
5374 buf[j] = '\0'; /* necessary when j == 0 */
5375}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005376EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005377
5378MODULE_DESCRIPTION("HDA codec core");
5379MODULE_LICENSE("GPL");