blob: d0deab1ed510cbd5f02ad1b4a75258196a213c9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700246 if (codec_exec_verb(codec, cmd, &res))
247 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return res;
249}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100250EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/**
253 * snd_hda_codec_write - send a single command without waiting for response
254 * @codec: the HDA codec
255 * @nid: NID to send the command
256 * @direct: direct flag
257 * @verb: the verb to send
258 * @parm: the parameter for the verb
259 *
260 * Send a single command without waiting for response.
261 *
262 * Returns 0 if successful, or a negative error code.
263 */
264int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
265 unsigned int verb, unsigned int parm)
266{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200267 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100268 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200269 return codec_exec_verb(codec, cmd,
270 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100272EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/**
275 * snd_hda_sequence_write - sequence writes
276 * @codec: the HDA codec
277 * @seq: VERB array to send
278 *
279 * Send the commands sequentially from the given array.
280 * The array must be terminated with NID=0.
281 */
282void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
283{
284 for (; seq->nid; seq++)
285 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100287EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * snd_hda_get_sub_nodes - get the range of sub nodes
291 * @codec: the HDA codec
292 * @nid: NID to parse
293 * @start_id: the pointer to store the start NID
294 *
295 * Parse the NID and store the start NID of its sub-nodes.
296 * Returns the number of sub-nodes.
297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200298int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned int parm;
302
303 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200304 if (parm == -1)
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *start_id = (parm >> 16) & 0x7fff;
307 return (int)(parm & 0x7fff);
308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100309EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200311static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
312 hda_nid_t *conn_list, int max_conns);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200313
314/* look up the cached results */
315static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
316{
317 int i, len;
318 for (i = 0; i < array->used; ) {
319 hda_nid_t *p = snd_array_elem(array, i);
320 if (nid == *p)
321 return p;
322 len = p[1];
323 i += len + 2;
324 }
325 return NULL;
326}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200329 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * @codec: the HDA codec
331 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * Parses the connection list of the given widget and stores the list
335 * of NIDs.
336 *
337 * Returns the number of connections, or a negative error code.
338 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200339int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
340 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200341{
342 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200343 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200345 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200346 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200348 again:
349 /* if the connection-list is already cached, read it */
350 p = lookup_conn_list(array, nid);
351 if (p) {
352 if (listp)
353 *listp = p + 2;
354 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200356 if (snd_BUG_ON(added))
357 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200359 /* read the connection and add to the cache */
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200360 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
361 if (len < 0)
362 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 err = snd_hda_override_conn_list(codec, nid, len, list);
364 if (err < 0)
365 return err;
366 added = true;
367 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200368}
Takashi Iwaidce20792011-06-24 14:10:28 +0200369EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
370
371/**
372 * snd_hda_get_connections - copy connection list
373 * @codec: the HDA codec
374 * @nid: NID to parse
375 * @conn_list: connection list array
376 * @max_conns: max. number of connections to store
377 *
378 * Parses the connection list of the given widget and stores the list
379 * of NIDs.
380 *
381 * Returns the number of connections, or a negative error code.
382 */
383int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
384 hda_nid_t *conn_list, int max_conns)
385{
386 const hda_nid_t *list;
387 int len = snd_hda_get_conn_list(codec, nid, &list);
388
389 if (len <= 0)
390 return len;
391 if (len > max_conns) {
392 snd_printk(KERN_ERR "hda_codec: "
393 "Too many connections %d for NID 0x%x\n",
394 len, nid);
395 return -EINVAL;
396 }
397 memcpy(conn_list, list, len * sizeof(hda_nid_t));
398 return len;
399}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200400EXPORT_SYMBOL_HDA(snd_hda_get_connections);
401
402static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
403 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100406 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200408 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100409 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Takashi Iwaida3cec32008-08-08 17:12:14 +0200411 if (snd_BUG_ON(!conn_list || max_conns <= 0))
412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200414 wcaps = get_wcaps(codec, nid);
415 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200416 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
417 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
420 if (parm & AC_CLIST_LONG) {
421 /* long form */
422 shift = 16;
423 num_elems = 2;
424 } else {
425 /* short form */
426 shift = 8;
427 num_elems = 4;
428 }
429 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 mask = (1 << (shift-1)) - 1;
431
Takashi Iwai0ba21762007-04-16 11:29:14 +0200432 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0; /* no connection */
434
435 if (conn_len == 1) {
436 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200437 parm = snd_hda_codec_read(codec, nid, 0,
438 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200439 if (parm == -1 && codec->bus->rirb_error)
440 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 conn_list[0] = parm & mask;
442 return 1;
443 }
444
445 /* multi connection */
446 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100447 prev_nid = 0;
448 for (i = 0; i < conn_len; i++) {
449 int range_val;
450 hda_nid_t val, n;
451
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200452 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100453 parm = snd_hda_codec_read(codec, nid, 0,
454 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200455 if (parm == -1 && codec->bus->rirb_error)
456 return -EIO;
457 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200458 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200460 if (val == 0) {
461 snd_printk(KERN_WARNING "hda_codec: "
462 "invalid CONNECT_LIST verb %x[%i]:%x\n",
463 nid, i, parm);
464 return 0;
465 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100466 parm >>= shift;
467 if (range_val) {
468 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200469 if (!prev_nid || prev_nid >= val) {
470 snd_printk(KERN_WARNING "hda_codec: "
471 "invalid dep_range_val %x:%x\n",
472 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100475 for (n = prev_nid + 1; n <= val; n++) {
476 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200477 snd_printk(KERN_ERR "hda_codec: "
478 "Too many connections %d for NID 0x%x\n",
479 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100480 return -EINVAL;
481 }
482 conn_list[conns++] = n;
483 }
484 } else {
485 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200486 snd_printk(KERN_ERR "hda_codec: "
487 "Too many connections %d for NID 0x%x\n",
488 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100489 return -EINVAL;
490 }
491 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 return conns;
496}
497
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200498static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
499{
500 hda_nid_t *p = snd_array_new(array);
501 if (!p)
502 return false;
503 *p = nid;
504 return true;
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200508 * snd_hda_override_conn_list - add/modify the connection-list to cache
509 * @codec: the HDA codec
510 * @nid: NID to parse
511 * @len: number of connection list entries
512 * @list: the list of connection entries
513 *
514 * Add or modify the given connection-list to the cache. If the corresponding
515 * cache already exists, invalidate it and append a new one.
516 *
517 * Returns zero or a negative error code.
518 */
519int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
520 const hda_nid_t *list)
521{
522 struct snd_array *array = &codec->conn_lists;
523 hda_nid_t *p;
524 int i, old_used;
525
526 p = lookup_conn_list(array, nid);
527 if (p)
528 *p = -1; /* invalidate the old entry */
529
530 old_used = array->used;
531 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
532 goto error_add;
533 for (i = 0; i < len; i++)
534 if (!add_conn_list(array, list[i]))
535 goto error_add;
536 return 0;
537
538 error_add:
539 array->used = old_used;
540 return -ENOMEM;
541}
542EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
543
544/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200545 * snd_hda_get_conn_index - get the connection index of the given NID
546 * @codec: the HDA codec
547 * @mux: NID containing the list
548 * @nid: NID to select
549 * @recursive: 1 when searching NID recursively, otherwise 0
550 *
551 * Parses the connection list of the widget @mux and checks whether the
552 * widget @nid is present. If it is, return the connection index.
553 * Otherwise it returns -1.
554 */
555int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
556 hda_nid_t nid, int recursive)
557{
558 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
559 int i, nums;
560
561 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
562 for (i = 0; i < nums; i++)
563 if (conn[i] == nid)
564 return i;
565 if (!recursive)
566 return -1;
567 if (recursive > 5) {
568 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
569 return -1;
570 }
571 recursive++;
572 for (i = 0; i < nums; i++)
573 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
574 return i;
575 return -1;
576}
577EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
578
579/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * snd_hda_queue_unsol_event - add an unsolicited event to queue
581 * @bus: the BUS
582 * @res: unsolicited event (lower 32bit of RIRB entry)
583 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
584 *
585 * Adds the given event to the queue. The events are processed in
586 * the workqueue asynchronously. Call this function in the interrupt
587 * hanlder when RIRB receives an unsolicited event.
588 *
589 * Returns 0 if successful, or a negative error code.
590 */
591int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
592{
593 struct hda_bus_unsolicited *unsol;
594 unsigned int wp;
595
Takashi Iwai0ba21762007-04-16 11:29:14 +0200596 unsol = bus->unsol;
597 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599
600 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
601 unsol->wp = wp;
602
603 wp <<= 1;
604 unsol->queue[wp] = res;
605 unsol->queue[wp + 1] = res_ex;
606
Takashi Iwai6acaed32009-01-12 10:09:24 +0100607 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return 0;
610}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100611EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800614 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
David Howellsc4028952006-11-22 14:57:56 +0000616static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
David Howellsc4028952006-11-22 14:57:56 +0000618 struct hda_bus_unsolicited *unsol =
619 container_of(work, struct hda_bus_unsolicited, work);
620 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct hda_codec *codec;
622 unsigned int rp, caddr, res;
623
624 while (unsol->rp != unsol->wp) {
625 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->rp = rp;
627 rp <<= 1;
628 res = unsol->queue[rp];
629 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200630 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 continue;
632 codec = bus->caddr_tbl[caddr & 0x0f];
633 if (codec && codec->patch_ops.unsol_event)
634 codec->patch_ops.unsol_event(codec, res);
635 }
636}
637
638/*
639 * initialize unsolicited queue
640 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200641static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct hda_bus_unsolicited *unsol;
644
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100645 if (bus->unsol) /* already initialized */
646 return 0;
647
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200648 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200649 if (!unsol) {
650 snd_printk(KERN_ERR "hda_codec: "
651 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -ENOMEM;
653 }
David Howellsc4028952006-11-22 14:57:56 +0000654 INIT_WORK(&unsol->work, process_unsol_events);
655 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 bus->unsol = unsol;
657 return 0;
658}
659
660/*
661 * destructor
662 */
663static void snd_hda_codec_free(struct hda_codec *codec);
664
665static int snd_hda_bus_free(struct hda_bus *bus)
666{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200667 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Takashi Iwai0ba21762007-04-16 11:29:14 +0200669 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100671 if (bus->workq)
672 flush_workqueue(bus->workq);
673 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 snd_hda_codec_free(codec);
677 }
678 if (bus->ops.private_free)
679 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100680 if (bus->workq)
681 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 kfree(bus);
683 return 0;
684}
685
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100686static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100689 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return snd_hda_bus_free(bus);
691}
692
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200693#ifdef CONFIG_SND_HDA_HWDEP
694static int snd_hda_bus_dev_register(struct snd_device *device)
695{
696 struct hda_bus *bus = device->device_data;
697 struct hda_codec *codec;
698 list_for_each_entry(codec, &bus->codec_list, list) {
699 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100700 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200701 }
702 return 0;
703}
704#else
705#define snd_hda_bus_dev_register NULL
706#endif
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
709 * snd_hda_bus_new - create a HDA bus
710 * @card: the card entry
711 * @temp: the template for hda_bus information
712 * @busp: the pointer to store the created bus instance
713 *
714 * Returns 0 if successful, or a negative error code.
715 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100716int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200717 const struct hda_bus_template *temp,
718 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct hda_bus *bus;
721 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100722 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200723 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .dev_free = snd_hda_bus_dev_free,
725 };
726
Takashi Iwaida3cec32008-08-08 17:12:14 +0200727 if (snd_BUG_ON(!temp))
728 return -EINVAL;
729 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
730 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (busp)
733 *busp = NULL;
734
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200735 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (bus == NULL) {
737 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
738 return -ENOMEM;
739 }
740
741 bus->card = card;
742 bus->private_data = temp->private_data;
743 bus->pci = temp->pci;
744 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100745 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 bus->ops = temp->ops;
747
Ingo Molnar62932df2006-01-16 16:34:20 +0100748 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200749 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 INIT_LIST_HEAD(&bus->codec_list);
751
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100752 snprintf(bus->workq_name, sizeof(bus->workq_name),
753 "hd-audio%d", card->number);
754 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100755 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100756 snd_printk(KERN_ERR "cannot create workqueue %s\n",
757 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100758 kfree(bus);
759 return -ENOMEM;
760 }
761
Takashi Iwai0ba21762007-04-16 11:29:14 +0200762 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
763 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 snd_hda_bus_free(bus);
765 return err;
766 }
767 if (busp)
768 *busp = bus;
769 return 0;
770}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100771EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Takashi Iwai82467612007-07-27 19:15:54 +0200773#ifdef CONFIG_SND_HDA_GENERIC
774#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200775 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200776#else
777#define is_generic_config(codec) 0
778#endif
779
Takashi Iwai645f10c2008-11-28 15:07:37 +0100780#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100781#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
782#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100783#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100784#endif
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * find a matching codec preset
788 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200789static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200790find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100792 struct hda_codec_preset_list *tbl;
793 const struct hda_codec_preset *preset;
794 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai82467612007-07-27 19:15:54 +0200796 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100797 return NULL; /* use the generic parser */
798
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100799 again:
800 mutex_lock(&preset_mutex);
801 list_for_each_entry(tbl, &hda_preset_tables, list) {
802 if (!try_module_get(tbl->owner)) {
803 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
804 continue;
805 }
806 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100808 if (preset->afg && preset->afg != codec->afg)
809 continue;
810 if (preset->mfg && preset->mfg != codec->mfg)
811 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200812 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200814 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200815 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816 preset->rev == codec->revision_id)) {
817 mutex_unlock(&preset_mutex);
818 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100822 module_put(tbl->owner);
823 }
824 mutex_unlock(&preset_mutex);
825
826 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
827 char name[32];
828 if (!mod_requested)
829 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
830 codec->vendor_id);
831 else
832 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
833 (codec->vendor_id >> 16) & 0xffff);
834 request_module(name);
835 mod_requested++;
836 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 return NULL;
839}
840
841/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200842 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200844static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 const struct hda_vendor_id *c;
847 const char *vendor = NULL;
848 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200849 char tmp[16];
850
851 if (codec->vendor_name)
852 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 for (c = hda_vendor_ids; c->id; c++) {
855 if (c->id == vendor_id) {
856 vendor = c->name;
857 break;
858 }
859 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200860 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 sprintf(tmp, "Generic %04x", vendor_id);
862 vendor = tmp;
863 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200864 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
865 if (!codec->vendor_name)
866 return -ENOMEM;
867
868 get_chip_name:
869 if (codec->chip_name)
870 return 0;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200873 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
874 else {
875 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
876 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
877 }
878 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200879 return -ENOMEM;
880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200884 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100886static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200888 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 hda_nid_t nid;
890
891 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
892 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200893 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200894 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200895 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200896 case AC_GRP_AUDIO_FUNCTION:
897 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200898 codec->afg_function_id = function_id & 0xff;
899 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200900 break;
901 case AC_GRP_MODEM_FUNCTION:
902 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200903 codec->mfg_function_id = function_id & 0xff;
904 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200905 break;
906 default:
907 break;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100913 * read widget caps for each widget and store in cache
914 */
915static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
916{
917 int i;
918 hda_nid_t nid;
919
920 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
921 &codec->start_nid);
922 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200923 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100924 return -ENOMEM;
925 nid = codec->start_nid;
926 for (i = 0; i < codec->num_nodes; i++, nid++)
927 codec->wcaps[i] = snd_hda_param_read(codec, nid,
928 AC_PAR_AUDIO_WIDGET_CAP);
929 return 0;
930}
931
Takashi Iwai3be14142009-02-20 14:11:16 +0100932/* read all pin default configurations and save codec->init_pins */
933static int read_pin_defaults(struct hda_codec *codec)
934{
935 int i;
936 hda_nid_t nid = codec->start_nid;
937
938 for (i = 0; i < codec->num_nodes; i++, nid++) {
939 struct hda_pincfg *pin;
940 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200941 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100942 if (wid_type != AC_WID_PIN)
943 continue;
944 pin = snd_array_new(&codec->init_pins);
945 if (!pin)
946 return -ENOMEM;
947 pin->nid = nid;
948 pin->cfg = snd_hda_codec_read(codec, nid, 0,
949 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200950 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
951 AC_VERB_GET_PIN_WIDGET_CONTROL,
952 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100953 }
954 return 0;
955}
956
957/* look up the given pin config list and return the item matching with NID */
958static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
959 struct snd_array *array,
960 hda_nid_t nid)
961{
962 int i;
963 for (i = 0; i < array->used; i++) {
964 struct hda_pincfg *pin = snd_array_elem(array, i);
965 if (pin->nid == nid)
966 return pin;
967 }
968 return NULL;
969}
970
971/* write a config value for the given NID */
972static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
973 unsigned int cfg)
974{
975 int i;
976 for (i = 0; i < 4; i++) {
977 snd_hda_codec_write(codec, nid, 0,
978 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
979 cfg & 0xff);
980 cfg >>= 8;
981 }
982}
983
984/* set the current pin config value for the given NID.
985 * the value is cached, and read via snd_hda_codec_get_pincfg()
986 */
987int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
988 hda_nid_t nid, unsigned int cfg)
989{
990 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100991 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100992
Takashi Iwaib82855a2009-12-27 11:24:56 +0100993 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
994 return -EINVAL;
995
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100996 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100997 pin = look_up_pincfg(codec, list, nid);
998 if (!pin) {
999 pin = snd_array_new(list);
1000 if (!pin)
1001 return -ENOMEM;
1002 pin->nid = nid;
1003 }
1004 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001005
1006 /* change only when needed; e.g. if the pincfg is already present
1007 * in user_pins[], don't write it
1008 */
1009 cfg = snd_hda_codec_get_pincfg(codec, nid);
1010 if (oldcfg != cfg)
1011 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001012 return 0;
1013}
1014
Takashi Iwaid5191e52009-11-16 14:58:17 +01001015/**
1016 * snd_hda_codec_set_pincfg - Override a pin default configuration
1017 * @codec: the HDA codec
1018 * @nid: NID to set the pin config
1019 * @cfg: the pin default config value
1020 *
1021 * Override a pin default configuration value in the cache.
1022 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1023 * priority than the real hardware value.
1024 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001025int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1026 hda_nid_t nid, unsigned int cfg)
1027{
Takashi Iwai346ff702009-02-23 09:42:57 +01001028 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001029}
1030EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1031
Takashi Iwaid5191e52009-11-16 14:58:17 +01001032/**
1033 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to get the pin config
1036 *
1037 * Get the current pin config value of the given pin NID.
1038 * If the pincfg value is cached or overridden via sysfs or driver,
1039 * returns the cached value.
1040 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001041unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1042{
1043 struct hda_pincfg *pin;
1044
Takashi Iwai3be14142009-02-20 14:11:16 +01001045#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001046 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001047 if (pin)
1048 return pin->cfg;
1049#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001050 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1051 if (pin)
1052 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001053 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1054 if (pin)
1055 return pin->cfg;
1056 return 0;
1057}
1058EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1059
1060/* restore all current pin configs */
1061static void restore_pincfgs(struct hda_codec *codec)
1062{
1063 int i;
1064 for (i = 0; i < codec->init_pins.used; i++) {
1065 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1066 set_pincfg(codec, pin->nid,
1067 snd_hda_codec_get_pincfg(codec, pin->nid));
1068 }
1069}
Takashi Iwai54d17402005-11-21 16:33:22 +01001070
Takashi Iwai92ee6162009-12-27 11:18:59 +01001071/**
1072 * snd_hda_shutup_pins - Shut up all pins
1073 * @codec: the HDA codec
1074 *
1075 * Clear all pin controls to shup up before suspend for avoiding click noise.
1076 * The controls aren't cached so that they can be resumed properly.
1077 */
1078void snd_hda_shutup_pins(struct hda_codec *codec)
1079{
1080 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001081 /* don't shut up pins when unloading the driver; otherwise it breaks
1082 * the default pin setup at the next load of the driver
1083 */
1084 if (codec->bus->shutdown)
1085 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001086 for (i = 0; i < codec->init_pins.used; i++) {
1087 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1088 /* use read here for syncing after issuing each verb */
1089 snd_hda_codec_read(codec, pin->nid, 0,
1090 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1091 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001092 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001093}
1094EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1095
Mike Waychison1c7276c2011-04-20 12:04:36 -07001096#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001097/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1098static void restore_shutup_pins(struct hda_codec *codec)
1099{
1100 int i;
1101 if (!codec->pins_shutup)
1102 return;
1103 if (codec->bus->shutdown)
1104 return;
1105 for (i = 0; i < codec->init_pins.used; i++) {
1106 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1107 snd_hda_codec_write(codec, pin->nid, 0,
1108 AC_VERB_SET_PIN_WIDGET_CONTROL,
1109 pin->ctrl);
1110 }
1111 codec->pins_shutup = 0;
1112}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001113#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001114
Takashi Iwai01751f52007-08-10 16:59:39 +02001115static void init_hda_cache(struct hda_cache_rec *cache,
1116 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001117static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001118
Takashi Iwai3be14142009-02-20 14:11:16 +01001119/* restore the initial pin cfgs and release all pincfg lists */
1120static void restore_init_pincfgs(struct hda_codec *codec)
1121{
Takashi Iwai346ff702009-02-23 09:42:57 +01001122 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001123 * so that only the values in init_pins are restored
1124 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001125 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001126#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001127 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001128#endif
1129 restore_pincfgs(codec);
1130 snd_array_free(&codec->init_pins);
1131}
1132
Takashi Iwai54d17402005-11-21 16:33:22 +01001133/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001134 * audio-converter setup caches
1135 */
1136struct hda_cvt_setup {
1137 hda_nid_t nid;
1138 u8 stream_tag;
1139 u8 channel_id;
1140 u16 format_id;
1141 unsigned char active; /* cvt is currently used */
1142 unsigned char dirty; /* setups should be cleared */
1143};
1144
1145/* get or create a cache entry for the given audio converter NID */
1146static struct hda_cvt_setup *
1147get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1148{
1149 struct hda_cvt_setup *p;
1150 int i;
1151
1152 for (i = 0; i < codec->cvt_setups.used; i++) {
1153 p = snd_array_elem(&codec->cvt_setups, i);
1154 if (p->nid == nid)
1155 return p;
1156 }
1157 p = snd_array_new(&codec->cvt_setups);
1158 if (p)
1159 p->nid = nid;
1160 return p;
1161}
1162
1163/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * codec destructor
1165 */
1166static void snd_hda_codec_free(struct hda_codec *codec)
1167{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001168 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001170 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001171#ifdef CONFIG_SND_HDA_POWER_SAVE
1172 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001173 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001176 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001177 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001178 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001179 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 codec->bus->caddr_tbl[codec->addr] = NULL;
1181 if (codec->patch_ops.free)
1182 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001183 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001184 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001185 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001186 kfree(codec->vendor_name);
1187 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001188 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001189 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 kfree(codec);
1191}
1192
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001193static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1194 unsigned int power_state);
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/**
1197 * snd_hda_codec_new - create a HDA codec
1198 * @bus: the bus to assign
1199 * @codec_addr: the codec address
1200 * @codecp: the pointer to store the generated codec
1201 *
1202 * Returns 0 if successful, or a negative error code.
1203 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001204int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1205 unsigned int codec_addr,
1206 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001209 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int err;
1211
Takashi Iwaida3cec32008-08-08 17:12:14 +02001212 if (snd_BUG_ON(!bus))
1213 return -EINVAL;
1214 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001218 snd_printk(KERN_ERR "hda_codec: "
1219 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EBUSY;
1221 }
1222
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001223 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (codec == NULL) {
1225 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1226 return -ENOMEM;
1227 }
1228
1229 codec->bus = bus;
1230 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001231 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001232 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001233 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001234 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001235 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1236 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001237 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001238 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001239 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001240 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001241 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001242 if (codec->bus->modelname) {
1243 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1244 if (!codec->modelname) {
1245 snd_hda_codec_free(codec);
1246 return -ENODEV;
1247 }
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Takashi Iwaicb53c622007-08-10 17:21:45 +02001250#ifdef CONFIG_SND_HDA_POWER_SAVE
1251 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1252 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1253 * the caller has to power down appropriatley after initialization
1254 * phase.
1255 */
1256 hda_keep_power_on(codec);
1257#endif
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 list_add_tail(&codec->list, &bus->codec_list);
1260 bus->caddr_tbl[codec_addr] = codec;
1261
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1263 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001264 if (codec->vendor_id == -1)
1265 /* read again, hopefully the access method was corrected
1266 * in the last read...
1267 */
1268 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1269 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001270 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1271 AC_PAR_SUBSYSTEM_ID);
1272 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1273 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001275 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001276 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001277 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001278 err = -ENODEV;
1279 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Takashi Iwai3be14142009-02-20 14:11:16 +01001282 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1283 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001284 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001285 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001286 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001287 err = read_pin_defaults(codec);
1288 if (err < 0)
1289 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001290
Takashi Iwai0ba21762007-04-16 11:29:14 +02001291 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001292 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001293 codec->subsystem_id =
1294 snd_hda_codec_read(codec, nid, 0,
1295 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001296 }
1297
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001298 /* power-up all before initialization */
1299 hda_set_power_state(codec,
1300 codec->afg ? codec->afg : codec->mfg,
1301 AC_PWRST_D0);
1302
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001303 snd_hda_codec_proc_new(codec);
1304
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001305 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001306
1307 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1308 codec->subsystem_id, codec->revision_id);
1309 snd_component_add(codec->bus->card, component);
1310
1311 if (codecp)
1312 *codecp = codec;
1313 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001314
1315 error:
1316 snd_hda_codec_free(codec);
1317 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001318}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001319EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001320
Takashi Iwaid5191e52009-11-16 14:58:17 +01001321/**
1322 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1323 * @codec: the HDA codec
1324 *
1325 * Start parsing of the given codec tree and (re-)initialize the whole
1326 * patch instance.
1327 *
1328 * Returns 0 if successful or a negative error code.
1329 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330int snd_hda_codec_configure(struct hda_codec *codec)
1331{
1332 int err;
1333
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001334 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001335 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001336 err = get_codec_name(codec);
1337 if (err < 0)
1338 return err;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Takashi Iwai82467612007-07-27 19:15:54 +02001341 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001343 goto patched;
1344 }
Takashi Iwai82467612007-07-27 19:15:54 +02001345 if (codec->preset && codec->preset->patch) {
1346 err = codec->preset->patch(codec);
1347 goto patched;
1348 }
1349
1350 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001351 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001352 if (err < 0)
1353 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001354
1355 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001356 if (!err && codec->patch_ops.unsol_event)
1357 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001358 /* audio codec should override the mixer name */
1359 if (!err && (codec->afg || !*codec->bus->card->mixername))
1360 snprintf(codec->bus->card->mixername,
1361 sizeof(codec->bus->card->mixername),
1362 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001365EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367/**
1368 * snd_hda_codec_setup_stream - set up the codec for streaming
1369 * @codec: the CODEC to set up
1370 * @nid: the NID to set up
1371 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1372 * @channel_id: channel id to pass, zero based.
1373 * @format: stream format.
1374 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001375void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1376 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int channel_id, int format)
1378{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001379 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001380 struct hda_cvt_setup *p;
1381 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001382 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001383 int i;
1384
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001386 return;
1387
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 snd_printdd("hda_codec_setup_stream: "
1389 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001391 p = get_hda_cvt_setup(codec, nid);
1392 if (!p)
1393 return;
1394 /* update the stream-id if changed */
1395 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1396 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1397 newval = (stream_tag << 4) | channel_id;
1398 if (oldval != newval)
1399 snd_hda_codec_write(codec, nid, 0,
1400 AC_VERB_SET_CHANNEL_STREAMID,
1401 newval);
1402 p->stream_tag = stream_tag;
1403 p->channel_id = channel_id;
1404 }
1405 /* update the format-id if changed */
1406 if (p->format_id != format) {
1407 oldval = snd_hda_codec_read(codec, nid, 0,
1408 AC_VERB_GET_STREAM_FORMAT, 0);
1409 if (oldval != format) {
1410 msleep(1);
1411 snd_hda_codec_write(codec, nid, 0,
1412 AC_VERB_SET_STREAM_FORMAT,
1413 format);
1414 }
1415 p->format_id = format;
1416 }
1417 p->active = 1;
1418 p->dirty = 0;
1419
1420 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001421 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001422 list_for_each_entry(c, &codec->bus->codec_list, list) {
1423 for (i = 0; i < c->cvt_setups.used; i++) {
1424 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001425 if (!p->active && p->stream_tag == stream_tag &&
1426 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001427 p->dirty = 1;
1428 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001431EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Takashi Iwaif0cea792010-08-13 11:56:53 +02001433static void really_cleanup_stream(struct hda_codec *codec,
1434 struct hda_cvt_setup *q);
1435
Takashi Iwaid5191e52009-11-16 14:58:17 +01001436/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001437 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001438 * @codec: the CODEC to clean up
1439 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001440 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001441 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001442void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1443 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001444{
Takashi Iwaieb541332010-08-06 13:48:11 +02001445 struct hda_cvt_setup *p;
1446
Takashi Iwai888afa12008-03-18 09:57:50 +01001447 if (!nid)
1448 return;
1449
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001450 if (codec->no_sticky_stream)
1451 do_now = 1;
1452
Takashi Iwai888afa12008-03-18 09:57:50 +01001453 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001454 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001455 if (p) {
1456 /* here we just clear the active flag when do_now isn't set;
1457 * actual clean-ups will be done later in
1458 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1459 */
1460 if (do_now)
1461 really_cleanup_stream(codec, p);
1462 else
1463 p->active = 0;
1464 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001465}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001466EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001467
Takashi Iwaieb541332010-08-06 13:48:11 +02001468static void really_cleanup_stream(struct hda_codec *codec,
1469 struct hda_cvt_setup *q)
1470{
1471 hda_nid_t nid = q->nid;
1472 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1473 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1474 memset(q, 0, sizeof(*q));
1475 q->nid = nid;
1476}
1477
1478/* clean up the all conflicting obsolete streams */
1479static void purify_inactive_streams(struct hda_codec *codec)
1480{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001481 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001482 int i;
1483
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001484 list_for_each_entry(c, &codec->bus->codec_list, list) {
1485 for (i = 0; i < c->cvt_setups.used; i++) {
1486 struct hda_cvt_setup *p;
1487 p = snd_array_elem(&c->cvt_setups, i);
1488 if (p->dirty)
1489 really_cleanup_stream(c, p);
1490 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001491 }
1492}
1493
Mike Waychison1c7276c2011-04-20 12:04:36 -07001494#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001495/* clean up all streams; called from suspend */
1496static void hda_cleanup_all_streams(struct hda_codec *codec)
1497{
1498 int i;
1499
1500 for (i = 0; i < codec->cvt_setups.used; i++) {
1501 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1502 if (p->stream_tag)
1503 really_cleanup_stream(codec, p);
1504 }
1505}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001506#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/*
1509 * amp access functions
1510 */
1511
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001512/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001513#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001514#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001515#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1516#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001518#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001521static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001522 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwai01751f52007-08-10 16:59:39 +02001524 memset(cache, 0, sizeof(*cache));
1525 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001526 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001527}
1528
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001529static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001530{
Takashi Iwai603c4012008-07-30 15:01:44 +02001531 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001535static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Takashi Iwai01751f52007-08-10 16:59:39 +02001537 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1538 u16 cur = cache->hash[idx];
1539 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001542 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (info->key == key)
1544 return info;
1545 cur = info->next;
1546 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001547 return NULL;
1548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001550/* query the hash. allocate an entry if not found. */
1551static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1552 u32 key)
1553{
1554 struct hda_cache_head *info = get_hash(cache, key);
1555 if (!info) {
1556 u16 idx, cur;
1557 /* add a new hash entry */
1558 info = snd_array_new(&cache->buf);
1559 if (!info)
1560 return NULL;
1561 cur = snd_array_index(&cache->buf, info);
1562 info->key = key;
1563 info->val = 0;
1564 idx = key % (u16)ARRAY_SIZE(cache->hash);
1565 info->next = cache->hash[idx];
1566 cache->hash[idx] = cur;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return info;
1569}
1570
Takashi Iwai01751f52007-08-10 16:59:39 +02001571/* query and allocate an amp hash entry */
1572static inline struct hda_amp_info *
1573get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1574{
1575 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1576}
1577
Takashi Iwaid5191e52009-11-16 14:58:17 +01001578/**
1579 * query_amp_caps - query AMP capabilities
1580 * @codec: the HD-auio codec
1581 * @nid: the NID to query
1582 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1583 *
1584 * Query AMP capabilities for the given widget and direction.
1585 * Returns the obtained capability bits.
1586 *
1587 * When cap bits have been already read, this doesn't read again but
1588 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001590u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001592 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Takashi Iwai0ba21762007-04-16 11:29:14 +02001594 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1595 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001597 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001598 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001600 info->amp_caps = snd_hda_param_read(codec, nid,
1601 direction == HDA_OUTPUT ?
1602 AC_PAR_AMP_OUT_CAP :
1603 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001604 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001605 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 return info->amp_caps;
1608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001609EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwaid5191e52009-11-16 14:58:17 +01001611/**
1612 * snd_hda_override_amp_caps - Override the AMP capabilities
1613 * @codec: the CODEC to clean up
1614 * @nid: the NID to clean up
1615 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1616 * @caps: the capability bits to set
1617 *
1618 * Override the cached AMP caps bits value by the given one.
1619 * This function is useful if the driver needs to adjust the AMP ranges,
1620 * e.g. limit to 0dB, etc.
1621 *
1622 * Returns zero if successful or a negative error code.
1623 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001624int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1625 unsigned int caps)
1626{
1627 struct hda_amp_info *info;
1628
1629 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1630 if (!info)
1631 return -EINVAL;
1632 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001633 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001634 return 0;
1635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001636EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001637
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001638static unsigned int
1639query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1640 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001641{
1642 struct hda_amp_info *info;
1643
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001644 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001645 if (!info)
1646 return 0;
1647 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001648 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001649 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001650 }
1651 return info->amp_caps;
1652}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001653
1654static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1655{
1656 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1657}
1658
Takashi Iwaid5191e52009-11-16 14:58:17 +01001659/**
1660 * snd_hda_query_pin_caps - Query PIN capabilities
1661 * @codec: the HD-auio codec
1662 * @nid: the NID to query
1663 *
1664 * Query PIN capabilities for the given widget.
1665 * Returns the obtained capability bits.
1666 *
1667 * When cap bits have been already read, this doesn't read again but
1668 * returns the cached value.
1669 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001670u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1671{
1672 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1673 read_pin_cap);
1674}
Takashi Iwai1327a322009-03-23 13:07:47 +01001675EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1676
Wu Fengguang864f92b2009-11-18 12:38:02 +08001677/**
1678 * snd_hda_pin_sense - execute pin sense measurement
1679 * @codec: the CODEC to sense
1680 * @nid: the pin NID to sense
1681 *
1682 * Execute necessary pin sense measurement and return its Presence Detect,
1683 * Impedance, ELD Valid etc. status bits.
1684 */
1685u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1686{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001687 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001688
Takashi Iwai729d55b2009-12-25 22:49:01 +01001689 if (!codec->no_trigger_sense) {
1690 pincap = snd_hda_query_pin_caps(codec, nid);
1691 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001692 snd_hda_codec_read(codec, nid, 0,
1693 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001694 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001695 return snd_hda_codec_read(codec, nid, 0,
1696 AC_VERB_GET_PIN_SENSE, 0);
1697}
1698EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1699
1700/**
1701 * snd_hda_jack_detect - query pin Presence Detect status
1702 * @codec: the CODEC to sense
1703 * @nid: the pin NID to sense
1704 *
1705 * Query and return the pin's Presence Detect status.
1706 */
1707int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1708{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001709 u32 sense = snd_hda_pin_sense(codec, nid);
1710 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001711}
1712EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001716 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001718static unsigned int get_vol_mute(struct hda_codec *codec,
1719 struct hda_amp_info *info, hda_nid_t nid,
1720 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 u32 val, parm;
1723
Takashi Iwai01751f52007-08-10 16:59:39 +02001724 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001725 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1728 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1729 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001730 val = snd_hda_codec_read(codec, nid, 0,
1731 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001733 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001734 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001738 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001740static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001741 hda_nid_t nid, int ch, int direction, int index,
1742 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 u32 parm;
1745
1746 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1747 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1748 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1749 parm |= val;
1750 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001751 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Takashi Iwaid5191e52009-11-16 14:58:17 +01001754/**
1755 * snd_hda_codec_amp_read - Read AMP value
1756 * @codec: HD-audio codec
1757 * @nid: NID to read the AMP value
1758 * @ch: channel (left=0 or right=1)
1759 * @direction: #HDA_INPUT or #HDA_OUTPUT
1760 * @index: the index value (only for input direction)
1761 *
1762 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
Takashi Iwai834be882006-03-01 14:16:17 +01001764int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1765 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001767 struct hda_amp_info *info;
1768 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1769 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001771 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001773EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Takashi Iwaid5191e52009-11-16 14:58:17 +01001775/**
1776 * snd_hda_codec_amp_update - update the AMP value
1777 * @codec: HD-audio codec
1778 * @nid: NID to read the AMP value
1779 * @ch: channel (left=0 or right=1)
1780 * @direction: #HDA_INPUT or #HDA_OUTPUT
1781 * @idx: the index value (only for input direction)
1782 * @mask: bit mask to set
1783 * @val: the bits value to set
1784 *
1785 * Update the AMP value with a bit mask.
1786 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001787 */
Takashi Iwai834be882006-03-01 14:16:17 +01001788int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1789 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001791 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001792
Takashi Iwai0ba21762007-04-16 11:29:14 +02001793 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1794 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001796 if (snd_BUG_ON(mask & ~0xff))
1797 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001798 val &= mask;
1799 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001800 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001802 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 1;
1804}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001805EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Takashi Iwaid5191e52009-11-16 14:58:17 +01001807/**
1808 * snd_hda_codec_amp_stereo - update the AMP stereo values
1809 * @codec: HD-audio codec
1810 * @nid: NID to read the AMP value
1811 * @direction: #HDA_INPUT or #HDA_OUTPUT
1812 * @idx: the index value (only for input direction)
1813 * @mask: bit mask to set
1814 * @val: the bits value to set
1815 *
1816 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1817 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001818 */
1819int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1820 int direction, int idx, int mask, int val)
1821{
1822 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001823
1824 if (snd_BUG_ON(mask & ~0xff))
1825 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001826 for (ch = 0; ch < 2; ch++)
1827 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1828 idx, mask, val);
1829 return ret;
1830}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001831EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001832
Takashi Iwaicb53c622007-08-10 17:21:45 +02001833#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001834/**
1835 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1836 * @codec: HD-audio codec
1837 *
1838 * Resume the all amp commands from the cache.
1839 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001840void snd_hda_codec_resume_amp(struct hda_codec *codec)
1841{
Takashi Iwai603c4012008-07-30 15:01:44 +02001842 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001843 int i;
1844
Takashi Iwai603c4012008-07-30 15:01:44 +02001845 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001846 u32 key = buffer->head.key;
1847 hda_nid_t nid;
1848 unsigned int idx, dir, ch;
1849 if (!key)
1850 continue;
1851 nid = key & 0xff;
1852 idx = (key >> 16) & 0xff;
1853 dir = (key >> 24) & 0xff;
1854 for (ch = 0; ch < 2; ch++) {
1855 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1856 continue;
1857 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1858 buffer->vol[ch]);
1859 }
1860 }
1861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001862EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001863#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001865static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1866 unsigned int ofs)
1867{
1868 u32 caps = query_amp_caps(codec, nid, dir);
1869 /* get num steps */
1870 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1871 if (ofs < caps)
1872 caps -= ofs;
1873 return caps;
1874}
1875
Takashi Iwaid5191e52009-11-16 14:58:17 +01001876/**
1877 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1878 *
1879 * The control element is supposed to have the private_value field
1880 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1881 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001882int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1886 u16 nid = get_amp_nid(kcontrol);
1887 u8 chs = get_amp_channels(kcontrol);
1888 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001889 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001891 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1892 uinfo->count = chs == 3 ? 2 : 1;
1893 uinfo->value.integer.min = 0;
1894 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1895 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001896 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001897 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1898 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return -EINVAL;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001903EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001905
1906static inline unsigned int
1907read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1908 int ch, int dir, int idx, unsigned int ofs)
1909{
1910 unsigned int val;
1911 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1912 val &= HDA_AMP_VOLMASK;
1913 if (val >= ofs)
1914 val -= ofs;
1915 else
1916 val = 0;
1917 return val;
1918}
1919
1920static inline int
1921update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1922 int ch, int dir, int idx, unsigned int ofs,
1923 unsigned int val)
1924{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001925 unsigned int maxval;
1926
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001927 if (val > 0)
1928 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001929 /* ofs = 0: raw max value */
1930 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001931 if (val > maxval)
1932 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001933 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1934 HDA_AMP_VOLMASK, val);
1935}
1936
Takashi Iwaid5191e52009-11-16 14:58:17 +01001937/**
1938 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1939 *
1940 * The control element is supposed to have the private_value field
1941 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1942 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001943int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1944 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1947 hda_nid_t nid = get_amp_nid(kcontrol);
1948 int chs = get_amp_channels(kcontrol);
1949 int dir = get_amp_direction(kcontrol);
1950 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001951 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 long *valp = ucontrol->value.integer.value;
1953
1954 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001955 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001957 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001960EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Takashi Iwaid5191e52009-11-16 14:58:17 +01001962/**
1963 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1964 *
1965 * The control element is supposed to have the private_value field
1966 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1967 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001968int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1969 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1972 hda_nid_t nid = get_amp_nid(kcontrol);
1973 int chs = get_amp_channels(kcontrol);
1974 int dir = get_amp_direction(kcontrol);
1975 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001976 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 long *valp = ucontrol->value.integer.value;
1978 int change = 0;
1979
Takashi Iwaicb53c622007-08-10 17:21:45 +02001980 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001981 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001982 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001983 valp++;
1984 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001985 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001986 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001987 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return change;
1989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001990EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Takashi Iwaid5191e52009-11-16 14:58:17 +01001992/**
1993 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1994 *
1995 * The control element is supposed to have the private_value field
1996 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1997 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001998int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1999 unsigned int size, unsigned int __user *_tlv)
2000{
2001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2002 hda_nid_t nid = get_amp_nid(kcontrol);
2003 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002004 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002005 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002006 u32 caps, val1, val2;
2007
2008 if (size < 4 * sizeof(unsigned int))
2009 return -ENOMEM;
2010 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002011 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2012 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002013 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002015 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002016 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002017 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002018 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2019 return -EFAULT;
2020 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2021 return -EFAULT;
2022 if (put_user(val1, _tlv + 2))
2023 return -EFAULT;
2024 if (put_user(val2, _tlv + 3))
2025 return -EFAULT;
2026 return 0;
2027}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002028EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002029
Takashi Iwaid5191e52009-11-16 14:58:17 +01002030/**
2031 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2032 * @codec: HD-audio codec
2033 * @nid: NID of a reference widget
2034 * @dir: #HDA_INPUT or #HDA_OUTPUT
2035 * @tlv: TLV data to be stored, at least 4 elements
2036 *
2037 * Set (static) TLV data for a virtual master volume using the AMP caps
2038 * obtained from the reference NID.
2039 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002040 */
2041void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2042 unsigned int *tlv)
2043{
2044 u32 caps;
2045 int nums, step;
2046
2047 caps = query_amp_caps(codec, nid, dir);
2048 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2049 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2050 step = (step + 1) * 25;
2051 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2052 tlv[1] = 2 * sizeof(unsigned int);
2053 tlv[2] = -nums * step;
2054 tlv[3] = step;
2055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002056EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002057
2058/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002059static struct snd_kcontrol *
2060_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2061 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002062{
2063 struct snd_ctl_elem_id id;
2064 memset(&id, 0, sizeof(id));
2065 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002066 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002067 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2068 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002069 strcpy(id.name, name);
2070 return snd_ctl_find_id(codec->bus->card, &id);
2071}
2072
Takashi Iwaid5191e52009-11-16 14:58:17 +01002073/**
2074 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2075 * @codec: HD-audio codec
2076 * @name: ctl id name string
2077 *
2078 * Get the control element with the given id string and IFACE_MIXER.
2079 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002080struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2081 const char *name)
2082{
2083 return _snd_hda_find_mixer_ctl(codec, name, 0);
2084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002085EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002086
Takashi Iwai1afe2062010-12-23 10:17:52 +01002087static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2088{
2089 int idx;
2090 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2091 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2092 return idx;
2093 }
2094 return -EBUSY;
2095}
2096
Takashi Iwaid5191e52009-11-16 14:58:17 +01002097/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002098 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002099 * @codec: HD-audio codec
2100 * @nid: corresponding NID (optional)
2101 * @kctl: the control element to assign
2102 *
2103 * Add the given control element to an array inside the codec instance.
2104 * All control elements belonging to a codec are supposed to be added
2105 * by this function so that a proper clean-up works at the free or
2106 * reconfiguration time.
2107 *
2108 * If non-zero @nid is passed, the NID is assigned to the control element.
2109 * The assignment is shown in the codec proc file.
2110 *
2111 * snd_hda_ctl_add() checks the control subdev id field whether
2112 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002113 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2114 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002116int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2117 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002118{
2119 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002120 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002121 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002122
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002123 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002124 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002125 if (nid == 0)
2126 nid = get_amp_nid_(kctl->private_value);
2127 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002128 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2129 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002130 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002131 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002132 err = snd_ctl_add(codec->bus->card, kctl);
2133 if (err < 0)
2134 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002135 item = snd_array_new(&codec->mixers);
2136 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002137 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002138 item->kctl = kctl;
2139 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002140 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002141 return 0;
2142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002143EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002146 * snd_hda_add_nid - Assign a NID to a control element
2147 * @codec: HD-audio codec
2148 * @nid: corresponding NID (optional)
2149 * @kctl: the control element to assign
2150 * @index: index to kctl
2151 *
2152 * Add the given control element to an array inside the codec instance.
2153 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2154 * NID:KCTL mapping - for example "Capture Source" selector.
2155 */
2156int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2157 unsigned int index, hda_nid_t nid)
2158{
2159 struct hda_nid_item *item;
2160
2161 if (nid > 0) {
2162 item = snd_array_new(&codec->nids);
2163 if (!item)
2164 return -ENOMEM;
2165 item->kctl = kctl;
2166 item->index = index;
2167 item->nid = nid;
2168 return 0;
2169 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002170 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2171 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002172 return -EINVAL;
2173}
2174EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2175
2176/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002177 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2178 * @codec: HD-audio codec
2179 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002180void snd_hda_ctls_clear(struct hda_codec *codec)
2181{
2182 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002183 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002184 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002185 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002186 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002187 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002188}
2189
Takashi Iwaia65d6292009-02-23 16:57:04 +01002190/* pseudo device locking
2191 * toggle card->shutdown to allow/disallow the device access (as a hack)
2192 */
2193static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002194{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002195 spin_lock(&card->files_lock);
2196 if (card->shutdown) {
2197 spin_unlock(&card->files_lock);
2198 return -EINVAL;
2199 }
2200 card->shutdown = 1;
2201 spin_unlock(&card->files_lock);
2202 return 0;
2203}
2204
2205static void hda_unlock_devices(struct snd_card *card)
2206{
2207 spin_lock(&card->files_lock);
2208 card->shutdown = 0;
2209 spin_unlock(&card->files_lock);
2210}
2211
Takashi Iwaid5191e52009-11-16 14:58:17 +01002212/**
2213 * snd_hda_codec_reset - Clear all objects assigned to the codec
2214 * @codec: HD-audio codec
2215 *
2216 * This frees the all PCM and control elements assigned to the codec, and
2217 * clears the caches and restores the pin default configurations.
2218 *
2219 * When a device is being used, it returns -EBSY. If successfully freed,
2220 * returns zero.
2221 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002222int snd_hda_codec_reset(struct hda_codec *codec)
2223{
2224 struct snd_card *card = codec->bus->card;
2225 int i, pcm;
2226
2227 if (hda_lock_devices(card) < 0)
2228 return -EBUSY;
2229 /* check whether the codec isn't used by any mixer or PCM streams */
2230 if (!list_empty(&card->ctl_files)) {
2231 hda_unlock_devices(card);
2232 return -EBUSY;
2233 }
2234 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2235 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2236 if (!cpcm->pcm)
2237 continue;
2238 if (cpcm->pcm->streams[0].substream_opened ||
2239 cpcm->pcm->streams[1].substream_opened) {
2240 hda_unlock_devices(card);
2241 return -EBUSY;
2242 }
2243 }
2244
2245 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002246
2247#ifdef CONFIG_SND_HDA_POWER_SAVE
2248 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002249 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002250#endif
2251 snd_hda_ctls_clear(codec);
2252 /* relase PCMs */
2253 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002254 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002255 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002256 clear_bit(codec->pcm_info[i].device,
2257 codec->bus->pcm_dev_bits);
2258 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002259 }
2260 if (codec->patch_ops.free)
2261 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002262 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002263 codec->spec = NULL;
2264 free_hda_cache(&codec->amp_cache);
2265 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002266 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2267 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002268 /* free only driver_pins so that init_pins + user_pins are restored */
2269 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002270 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002271 codec->num_pcms = 0;
2272 codec->pcm_info = NULL;
2273 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002274 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2275 codec->slave_dig_outs = NULL;
2276 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002277 module_put(codec->owner);
2278 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002279
2280 /* allow device access again */
2281 hda_unlock_devices(card);
2282 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002283}
2284
Takashi Iwaid5191e52009-11-16 14:58:17 +01002285/**
2286 * snd_hda_add_vmaster - create a virtual master control and add slaves
2287 * @codec: HD-audio codec
2288 * @name: vmaster control name
2289 * @tlv: TLV data (optional)
2290 * @slaves: slave control names (optional)
2291 *
2292 * Create a virtual master control with the given name. The TLV data
2293 * must be either NULL or a valid data.
2294 *
2295 * @slaves is a NULL-terminated array of strings, each of which is a
2296 * slave control name. All controls with these names are assigned to
2297 * the new virtual master control.
2298 *
2299 * This function returns zero if successful or a negative error code.
2300 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002301int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002302 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002303{
2304 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002305 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002306 int err;
2307
Takashi Iwai2f085542008-02-22 18:43:50 +01002308 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2309 ;
2310 if (!*s) {
2311 snd_printdd("No slave found for %s\n", name);
2312 return 0;
2313 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002314 kctl = snd_ctl_make_virtual_master(name, tlv);
2315 if (!kctl)
2316 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002317 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002318 if (err < 0)
2319 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002320
Takashi Iwai2134ea42008-01-10 16:53:55 +01002321 for (s = slaves; *s; s++) {
2322 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002323 int i = 0;
2324 for (;;) {
2325 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2326 if (!sctl) {
2327 if (!i)
2328 snd_printdd("Cannot find slave %s, "
2329 "skipped\n", *s);
2330 break;
2331 }
2332 err = snd_ctl_add_slave(kctl, sctl);
2333 if (err < 0)
2334 return err;
2335 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002336 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002337 }
2338 return 0;
2339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002340EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002341
Takashi Iwaid5191e52009-11-16 14:58:17 +01002342/**
2343 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2344 *
2345 * The control element is supposed to have the private_value field
2346 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2347 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002348int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 int chs = get_amp_channels(kcontrol);
2352
2353 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2354 uinfo->count = chs == 3 ? 2 : 1;
2355 uinfo->value.integer.min = 0;
2356 uinfo->value.integer.max = 1;
2357 return 0;
2358}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002359EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Takashi Iwaid5191e52009-11-16 14:58:17 +01002361/**
2362 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2363 *
2364 * The control element is supposed to have the private_value field
2365 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2366 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002367int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2371 hda_nid_t nid = get_amp_nid(kcontrol);
2372 int chs = get_amp_channels(kcontrol);
2373 int dir = get_amp_direction(kcontrol);
2374 int idx = get_amp_index(kcontrol);
2375 long *valp = ucontrol->value.integer.value;
2376
2377 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002378 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002379 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002381 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002382 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return 0;
2384}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002385EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Takashi Iwaid5191e52009-11-16 14:58:17 +01002387/**
2388 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2389 *
2390 * The control element is supposed to have the private_value field
2391 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2392 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002393int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2397 hda_nid_t nid = get_amp_nid(kcontrol);
2398 int chs = get_amp_channels(kcontrol);
2399 int dir = get_amp_direction(kcontrol);
2400 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 long *valp = ucontrol->value.integer.value;
2402 int change = 0;
2403
Takashi Iwaicb53c622007-08-10 17:21:45 +02002404 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002405 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002406 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002407 HDA_AMP_MUTE,
2408 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002409 valp++;
2410 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002411 if (chs & 2)
2412 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002413 HDA_AMP_MUTE,
2414 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002415 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002416 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return change;
2418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002419EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Takashi Iwai67d634c2009-11-16 15:35:59 +01002421#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002422/**
2423 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2424 *
2425 * This function calls snd_hda_enable_beep_device(), which behaves differently
2426 * depending on beep_mode option.
2427 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002428int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2432 long *valp = ucontrol->value.integer.value;
2433
2434 snd_hda_enable_beep_device(codec, *valp);
2435 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2436}
2437EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002438#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/*
Takashi Iwai985be542005-11-02 18:26:49 +01002441 * bound volume controls
2442 *
2443 * bind multiple volumes (# indices, from 0)
2444 */
2445
2446#define AMP_VAL_IDX_SHIFT 19
2447#define AMP_VAL_IDX_MASK (0x0f<<19)
2448
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449/**
2450 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2451 *
2452 * The control element is supposed to have the private_value field
2453 * set up via HDA_BIND_MUTE*() macros.
2454 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002455int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002457{
2458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2459 unsigned long pval;
2460 int err;
2461
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002462 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002463 pval = kcontrol->private_value;
2464 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2465 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2466 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002467 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002468 return err;
2469}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002470EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002471
Takashi Iwaid5191e52009-11-16 14:58:17 +01002472/**
2473 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2474 *
2475 * The control element is supposed to have the private_value field
2476 * set up via HDA_BIND_MUTE*() macros.
2477 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002478int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002480{
2481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2482 unsigned long pval;
2483 int i, indices, err = 0, change = 0;
2484
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002485 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002486 pval = kcontrol->private_value;
2487 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2488 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002489 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2490 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002491 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2492 if (err < 0)
2493 break;
2494 change |= err;
2495 }
2496 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002497 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002498 return err < 0 ? err : change;
2499}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002500EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002501
Takashi Iwaid5191e52009-11-16 14:58:17 +01002502/**
2503 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2504 *
2505 * The control element is supposed to have the private_value field
2506 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002507 */
2508int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2509 struct snd_ctl_elem_info *uinfo)
2510{
2511 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2512 struct hda_bind_ctls *c;
2513 int err;
2514
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002515 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002516 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002517 kcontrol->private_value = *c->values;
2518 err = c->ops->info(kcontrol, uinfo);
2519 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002520 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002521 return err;
2522}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002523EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002524
Takashi Iwaid5191e52009-11-16 14:58:17 +01002525/**
2526 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2527 *
2528 * The control element is supposed to have the private_value field
2529 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2530 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002531int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533{
2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 struct hda_bind_ctls *c;
2536 int err;
2537
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002538 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002539 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002540 kcontrol->private_value = *c->values;
2541 err = c->ops->get(kcontrol, ucontrol);
2542 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002543 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002544 return err;
2545}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002546EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002547
Takashi Iwaid5191e52009-11-16 14:58:17 +01002548/**
2549 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2550 *
2551 * The control element is supposed to have the private_value field
2552 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2553 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002554int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
2557 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2558 struct hda_bind_ctls *c;
2559 unsigned long *vals;
2560 int err = 0, change = 0;
2561
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002562 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002563 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002564 for (vals = c->values; *vals; vals++) {
2565 kcontrol->private_value = *vals;
2566 err = c->ops->put(kcontrol, ucontrol);
2567 if (err < 0)
2568 break;
2569 change |= err;
2570 }
2571 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002572 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002573 return err < 0 ? err : change;
2574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002575EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002576
Takashi Iwaid5191e52009-11-16 14:58:17 +01002577/**
2578 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2579 *
2580 * The control element is supposed to have the private_value field
2581 * set up via HDA_BIND_VOL() macro.
2582 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002583int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2584 unsigned int size, unsigned int __user *tlv)
2585{
2586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2587 struct hda_bind_ctls *c;
2588 int err;
2589
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002590 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002591 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002592 kcontrol->private_value = *c->values;
2593 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2594 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002595 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002596 return err;
2597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002598EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002599
2600struct hda_ctl_ops snd_hda_bind_vol = {
2601 .info = snd_hda_mixer_amp_volume_info,
2602 .get = snd_hda_mixer_amp_volume_get,
2603 .put = snd_hda_mixer_amp_volume_put,
2604 .tlv = snd_hda_mixer_amp_tlv
2605};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002606EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002607
2608struct hda_ctl_ops snd_hda_bind_sw = {
2609 .info = snd_hda_mixer_amp_switch_info,
2610 .get = snd_hda_mixer_amp_switch_get,
2611 .put = snd_hda_mixer_amp_switch_put,
2612 .tlv = snd_hda_mixer_amp_tlv
2613};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002614EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002615
2616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 * SPDIF out controls
2618 */
2619
Takashi Iwai0ba21762007-04-16 11:29:14 +02002620static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2624 uinfo->count = 1;
2625 return 0;
2626}
2627
Takashi Iwai0ba21762007-04-16 11:29:14 +02002628static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2632 IEC958_AES0_NONAUDIO |
2633 IEC958_AES0_CON_EMPHASIS_5015 |
2634 IEC958_AES0_CON_NOT_COPYRIGHT;
2635 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2636 IEC958_AES1_CON_ORIGINAL;
2637 return 0;
2638}
2639
Takashi Iwai0ba21762007-04-16 11:29:14 +02002640static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2641 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2644 IEC958_AES0_NONAUDIO |
2645 IEC958_AES0_PRO_EMPHASIS_5015;
2646 return 0;
2647}
2648
Takashi Iwai0ba21762007-04-16 11:29:14 +02002649static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002653 int idx = kcontrol->private_value;
2654 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Stephen Warren7c935972011-06-01 11:14:17 -06002656 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2657 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2658 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2659 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 return 0;
2662}
2663
2664/* convert from SPDIF status bits to HDA SPDIF bits
2665 * bit 0 (DigEn) is always set zero (to be filled later)
2666 */
2667static unsigned short convert_from_spdif_status(unsigned int sbits)
2668{
2669 unsigned short val = 0;
2670
2671 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002674 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002676 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2677 IEC958_AES0_PRO_EMPHASIS_5015)
2678 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002680 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2681 IEC958_AES0_CON_EMPHASIS_5015)
2682 val |= AC_DIG1_EMPHASIS;
2683 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2684 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2688 }
2689 return val;
2690}
2691
2692/* convert to SPDIF status bits from HDA SPDIF bits
2693 */
2694static unsigned int convert_to_spdif_status(unsigned short val)
2695{
2696 unsigned int sbits = 0;
2697
Takashi Iwai0ba21762007-04-16 11:29:14 +02002698 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002700 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 sbits |= IEC958_AES0_PROFESSIONAL;
2702 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002703 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2705 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002706 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2712 sbits |= val & (0x7f << 8);
2713 }
2714 return sbits;
2715}
2716
Takashi Iwai2f728532008-09-25 16:32:41 +02002717/* set digital convert verbs both for the given NID and its slaves */
2718static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2719 int verb, int val)
2720{
Takashi Iwaidda14412011-05-02 11:29:30 +02002721 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002722
Takashi Iwai9e976972008-11-25 08:17:20 +01002723 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002724 d = codec->slave_dig_outs;
2725 if (!d)
2726 return;
2727 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002728 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002729}
2730
2731static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2732 int dig1, int dig2)
2733{
2734 if (dig1 != -1)
2735 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2736 if (dig2 != -1)
2737 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2738}
2739
Takashi Iwai0ba21762007-04-16 11:29:14 +02002740static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002744 int idx = kcontrol->private_value;
2745 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2746 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 unsigned short val;
2748 int change;
2749
Ingo Molnar62932df2006-01-16 16:34:20 +01002750 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002751 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2753 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2754 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002755 val = convert_from_spdif_status(spdif->status);
2756 val |= spdif->ctls & 1;
2757 change = spdif->ctls != val;
2758 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002759 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002760 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002761 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return change;
2763}
2764
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002765#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Takashi Iwai0ba21762007-04-16 11:29:14 +02002767static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002771 int idx = kcontrol->private_value;
2772 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Stephen Warren7c935972011-06-01 11:14:17 -06002774 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776}
2777
Stephen Warren74b654c2011-06-01 11:14:18 -06002778static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2779 int dig1, int dig2)
2780{
2781 set_dig_out_convert(codec, nid, dig1, dig2);
2782 /* unmute amp switch (if any) */
2783 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2784 (dig1 & AC_DIG1_ENABLE))
2785 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2786 HDA_AMP_MUTE, 0);
2787}
2788
Takashi Iwai0ba21762007-04-16 11:29:14 +02002789static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002793 int idx = kcontrol->private_value;
2794 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2795 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 unsigned short val;
2797 int change;
2798
Ingo Molnar62932df2006-01-16 16:34:20 +01002799 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002800 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002802 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002803 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002804 spdif->ctls = val;
2805 if (change && nid != (u16)-1)
2806 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002807 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return change;
2809}
2810
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002811static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 {
2813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002815 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 .info = snd_hda_spdif_mask_info,
2817 .get = snd_hda_spdif_cmask_get,
2818 },
2819 {
2820 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2821 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002822 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 .info = snd_hda_spdif_mask_info,
2824 .get = snd_hda_spdif_pmask_get,
2825 },
2826 {
2827 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002828 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 .info = snd_hda_spdif_mask_info,
2830 .get = snd_hda_spdif_default_get,
2831 .put = snd_hda_spdif_default_put,
2832 },
2833 {
2834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002835 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 .info = snd_hda_spdif_out_switch_info,
2837 .get = snd_hda_spdif_out_switch_get,
2838 .put = snd_hda_spdif_out_switch_put,
2839 },
2840 { } /* end */
2841};
2842
2843/**
2844 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2845 * @codec: the HDA codec
2846 * @nid: audio out widget NID
2847 *
2848 * Creates controls related with the SPDIF output.
2849 * Called from each patch supporting the SPDIF out.
2850 *
2851 * Returns 0 if successful, or a negative error code.
2852 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002853int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2854 hda_nid_t associated_nid,
2855 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002858 struct snd_kcontrol *kctl;
2859 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002860 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002861 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Takashi Iwai1afe2062010-12-23 10:17:52 +01002863 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2864 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002865 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2866 return -EBUSY;
2867 }
Stephen Warren7c935972011-06-01 11:14:17 -06002868 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2870 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002871 if (!kctl)
2872 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002873 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002874 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002875 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002876 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return err;
2878 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002879 spdif->nid = cvt_nid;
2880 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06002881 AC_VERB_GET_DIGI_CONVERT_1, 0);
2882 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return 0;
2884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002885EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Stephen Warren7c935972011-06-01 11:14:17 -06002887struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2888 hda_nid_t nid)
2889{
2890 int i;
2891 for (i = 0; i < codec->spdif_out.used; i++) {
2892 struct hda_spdif_out *spdif =
2893 snd_array_elem(&codec->spdif_out, i);
2894 if (spdif->nid == nid)
2895 return spdif;
2896 }
2897 return NULL;
2898}
2899EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2900
Stephen Warren74b654c2011-06-01 11:14:18 -06002901void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2902{
2903 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2904
2905 mutex_lock(&codec->spdif_mutex);
2906 spdif->nid = (u16)-1;
2907 mutex_unlock(&codec->spdif_mutex);
2908}
2909EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2910
2911void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2912{
2913 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2914 unsigned short val;
2915
2916 mutex_lock(&codec->spdif_mutex);
2917 if (spdif->nid != nid) {
2918 spdif->nid = nid;
2919 val = spdif->ctls;
2920 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2921 }
2922 mutex_unlock(&codec->spdif_mutex);
2923}
2924EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002927 * SPDIF sharing with analog output
2928 */
2929static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2931{
2932 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2933 ucontrol->value.integer.value[0] = mout->share_spdif;
2934 return 0;
2935}
2936
2937static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2941 mout->share_spdif = !!ucontrol->value.integer.value[0];
2942 return 0;
2943}
2944
2945static struct snd_kcontrol_new spdif_share_sw = {
2946 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2947 .name = "IEC958 Default PCM Playback Switch",
2948 .info = snd_ctl_boolean_mono_info,
2949 .get = spdif_share_sw_get,
2950 .put = spdif_share_sw_put,
2951};
2952
Takashi Iwaid5191e52009-11-16 14:58:17 +01002953/**
2954 * snd_hda_create_spdif_share_sw - create Default PCM switch
2955 * @codec: the HDA codec
2956 * @mout: multi-out instance
2957 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002958int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2959 struct hda_multi_out *mout)
2960{
2961 if (!mout->dig_out_nid)
2962 return 0;
2963 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002964 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2965 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002966}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002967EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002968
2969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * SPDIF input
2971 */
2972
2973#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2974
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979
2980 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2981 return 0;
2982}
2983
Takashi Iwai0ba21762007-04-16 11:29:14 +02002984static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2988 hda_nid_t nid = kcontrol->private_value;
2989 unsigned int val = !!ucontrol->value.integer.value[0];
2990 int change;
2991
Ingo Molnar62932df2006-01-16 16:34:20 +01002992 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002994 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002996 snd_hda_codec_write_cache(codec, nid, 0,
2997 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002999 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return change;
3001}
3002
Takashi Iwai0ba21762007-04-16 11:29:14 +02003003static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3007 hda_nid_t nid = kcontrol->private_value;
3008 unsigned short val;
3009 unsigned int sbits;
3010
Andrew Paprocki3982d172007-12-19 12:13:44 +01003011 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sbits = convert_to_spdif_status(val);
3013 ucontrol->value.iec958.status[0] = sbits;
3014 ucontrol->value.iec958.status[1] = sbits >> 8;
3015 ucontrol->value.iec958.status[2] = sbits >> 16;
3016 ucontrol->value.iec958.status[3] = sbits >> 24;
3017 return 0;
3018}
3019
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003020static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 {
3022 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003023 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .info = snd_hda_spdif_in_switch_info,
3025 .get = snd_hda_spdif_in_switch_get,
3026 .put = snd_hda_spdif_in_switch_put,
3027 },
3028 {
3029 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003031 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 .info = snd_hda_spdif_mask_info,
3033 .get = snd_hda_spdif_in_status_get,
3034 },
3035 { } /* end */
3036};
3037
3038/**
3039 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3040 * @codec: the HDA codec
3041 * @nid: audio in widget NID
3042 *
3043 * Creates controls related with the SPDIF input.
3044 * Called from each patch supporting the SPDIF in.
3045 *
3046 * Returns 0 if successful, or a negative error code.
3047 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003048int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003051 struct snd_kcontrol *kctl;
3052 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003053 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Takashi Iwai1afe2062010-12-23 10:17:52 +01003055 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3056 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003057 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3058 return -EBUSY;
3059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3061 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003062 if (!kctl)
3063 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003065 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return err;
3068 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003070 snd_hda_codec_read(codec, nid, 0,
3071 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return 0;
3074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003075EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Takashi Iwaicb53c622007-08-10 17:21:45 +02003077#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003078/*
3079 * command cache
3080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003082/* build a 32bit cache key with the widget id and the command parameter */
3083#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3084#define get_cmd_cache_nid(key) ((key) & 0xff)
3085#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3086
3087/**
3088 * snd_hda_codec_write_cache - send a single command with caching
3089 * @codec: the HDA codec
3090 * @nid: NID to send the command
3091 * @direct: direct flag
3092 * @verb: the verb to send
3093 * @parm: the parameter for the verb
3094 *
3095 * Send a single command without waiting for response.
3096 *
3097 * Returns 0 if successful, or a negative error code.
3098 */
3099int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3100 int direct, unsigned int verb, unsigned int parm)
3101{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003102 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3103 struct hda_cache_head *c;
3104 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003105
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003106 if (err < 0)
3107 return err;
3108 /* parm may contain the verb stuff for get/set amp */
3109 verb = verb | (parm >> 8);
3110 parm &= 0xff;
3111 key = build_cmd_cache_key(nid, verb);
3112 mutex_lock(&codec->bus->cmd_mutex);
3113 c = get_alloc_hash(&codec->cmd_cache, key);
3114 if (c)
3115 c->val = parm;
3116 mutex_unlock(&codec->bus->cmd_mutex);
3117 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003119EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003120
Takashi Iwaid5191e52009-11-16 14:58:17 +01003121/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003122 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3123 * @codec: the HDA codec
3124 * @nid: NID to send the command
3125 * @direct: direct flag
3126 * @verb: the verb to send
3127 * @parm: the parameter for the verb
3128 *
3129 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3130 * command if the parameter is already identical with the cached value.
3131 * If not, it sends the command and refreshes the cache.
3132 *
3133 * Returns 0 if successful, or a negative error code.
3134 */
3135int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3136 int direct, unsigned int verb, unsigned int parm)
3137{
3138 struct hda_cache_head *c;
3139 u32 key;
3140
3141 /* parm may contain the verb stuff for get/set amp */
3142 verb = verb | (parm >> 8);
3143 parm &= 0xff;
3144 key = build_cmd_cache_key(nid, verb);
3145 mutex_lock(&codec->bus->cmd_mutex);
3146 c = get_hash(&codec->cmd_cache, key);
3147 if (c && c->val == parm) {
3148 mutex_unlock(&codec->bus->cmd_mutex);
3149 return 0;
3150 }
3151 mutex_unlock(&codec->bus->cmd_mutex);
3152 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3153}
3154EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3155
3156/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003157 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3158 * @codec: HD-audio codec
3159 *
3160 * Execute all verbs recorded in the command caches to resume.
3161 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003162void snd_hda_codec_resume_cache(struct hda_codec *codec)
3163{
Takashi Iwai603c4012008-07-30 15:01:44 +02003164 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003165 int i;
3166
Takashi Iwai603c4012008-07-30 15:01:44 +02003167 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003168 u32 key = buffer->key;
3169 if (!key)
3170 continue;
3171 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3172 get_cmd_cache_cmd(key), buffer->val);
3173 }
3174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003175EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003176
3177/**
3178 * snd_hda_sequence_write_cache - sequence writes with caching
3179 * @codec: the HDA codec
3180 * @seq: VERB array to send
3181 *
3182 * Send the commands sequentially from the given array.
3183 * Thte commands are recorded on cache for power-save and resume.
3184 * The array must be terminated with NID=0.
3185 */
3186void snd_hda_sequence_write_cache(struct hda_codec *codec,
3187 const struct hda_verb *seq)
3188{
3189 for (; seq->nid; seq++)
3190 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3191 seq->param);
3192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003193EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003194#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003195
Takashi Iwai54d17402005-11-21 16:33:22 +01003196/*
3197 * set power state of the codec
3198 */
3199static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3200 unsigned int power_state)
3201{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003202 hda_nid_t nid;
3203 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003204
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003205 /* this delay seems necessary to avoid click noise at power-down */
3206 if (power_state == AC_PWRST_D3)
3207 msleep(100);
3208 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003209 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003210 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003211 if (power_state == AC_PWRST_D0 &&
3212 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003213 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003214
Takashi Iwaicb53c622007-08-10 17:21:45 +02003215 nid = codec->start_nid;
3216 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003217 unsigned int wcaps = get_wcaps(codec, nid);
3218 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003219 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003220 if (power_state == AC_PWRST_D3 &&
3221 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003222 unsigned int pincap;
3223 /*
3224 * don't power down the widget if it controls
3225 * eapd and EAPD_BTLENABLE is set.
3226 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003227 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003228 if (pincap & AC_PINCAP_EAPD) {
3229 int eapd = snd_hda_codec_read(codec,
3230 nid, 0,
3231 AC_VERB_GET_EAPD_BTLENABLE, 0);
3232 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003233 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003234 continue;
3235 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003236 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003237 snd_hda_codec_write(codec, nid, 0,
3238 AC_VERB_SET_POWER_STATE,
3239 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003240 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003241 }
3242
Takashi Iwaicb53c622007-08-10 17:21:45 +02003243 if (power_state == AC_PWRST_D0) {
3244 unsigned long end_time;
3245 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003246 /* wait until the codec reachs to D0 */
3247 end_time = jiffies + msecs_to_jiffies(500);
3248 do {
3249 state = snd_hda_codec_read(codec, fg, 0,
3250 AC_VERB_GET_POWER_STATE, 0);
3251 if (state == power_state)
3252 break;
3253 msleep(1);
3254 } while (time_after_eq(end_time, jiffies));
3255 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003256}
3257
Takashi Iwai11aeff02008-07-30 15:01:46 +02003258#ifdef CONFIG_SND_HDA_HWDEP
3259/* execute additional init verbs */
3260static void hda_exec_init_verbs(struct hda_codec *codec)
3261{
3262 if (codec->init_verbs.list)
3263 snd_hda_sequence_write(codec, codec->init_verbs.list);
3264}
3265#else
3266static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3267#endif
3268
Takashi Iwaicb53c622007-08-10 17:21:45 +02003269#ifdef SND_HDA_NEEDS_RESUME
3270/*
3271 * call suspend and power-down; used both from PM and power-save
3272 */
3273static void hda_call_codec_suspend(struct hda_codec *codec)
3274{
3275 if (codec->patch_ops.suspend)
3276 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003277 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003278 hda_set_power_state(codec,
3279 codec->afg ? codec->afg : codec->mfg,
3280 AC_PWRST_D3);
3281#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003282 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003283 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003284 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003285 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003286 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003287#endif
3288}
3289
3290/*
3291 * kick up codec; used both from PM and power-save
3292 */
3293static void hda_call_codec_resume(struct hda_codec *codec)
3294{
3295 hda_set_power_state(codec,
3296 codec->afg ? codec->afg : codec->mfg,
3297 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003298 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003299 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003300 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003301 if (codec->patch_ops.resume)
3302 codec->patch_ops.resume(codec);
3303 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003304 if (codec->patch_ops.init)
3305 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003306 snd_hda_codec_resume_amp(codec);
3307 snd_hda_codec_resume_cache(codec);
3308 }
3309}
3310#endif /* SND_HDA_NEEDS_RESUME */
3311
Takashi Iwai54d17402005-11-21 16:33:22 +01003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313/**
3314 * snd_hda_build_controls - build mixer controls
3315 * @bus: the BUS
3316 *
3317 * Creates mixer controls for each codec included in the bus.
3318 *
3319 * Returns 0 if successful, otherwise a negative error code.
3320 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003321int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003323 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Takashi Iwai0ba21762007-04-16 11:29:14 +02003325 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003326 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003327 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003328 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003329 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003330 err = snd_hda_codec_reset(codec);
3331 if (err < 0) {
3332 printk(KERN_ERR
3333 "hda_codec: cannot revert codec\n");
3334 return err;
3335 }
3336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003338 return 0;
3339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003340EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003342int snd_hda_codec_build_controls(struct hda_codec *codec)
3343{
3344 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003345 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003346 /* continue to initialize... */
3347 if (codec->patch_ops.init)
3348 err = codec->patch_ops.init(codec);
3349 if (!err && codec->patch_ops.build_controls)
3350 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003351 if (err < 0)
3352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return 0;
3354}
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356/*
3357 * stream formats
3358 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003359struct hda_rate_tbl {
3360 unsigned int hz;
3361 unsigned int alsa_bits;
3362 unsigned int hda_fmt;
3363};
3364
Takashi Iwai92f10b32010-08-03 14:21:00 +02003365/* rate = base * mult / div */
3366#define HDA_RATE(base, mult, div) \
3367 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3368 (((div) - 1) << AC_FMT_DIV_SHIFT))
3369
Takashi Iwaibefdf312005-08-22 13:57:55 +02003370static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003372
3373 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003374 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3375 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3376 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3377 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3378 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3379 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3380 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3381 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3382 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3383 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3384 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003385#define AC_PAR_PCM_RATE_BITS 11
3386 /* up to bits 10, 384kHZ isn't supported properly */
3387
3388 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003389 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003390
Takashi Iwaibefdf312005-08-22 13:57:55 +02003391 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392};
3393
3394/**
3395 * snd_hda_calc_stream_format - calculate format bitset
3396 * @rate: the sample rate
3397 * @channels: the number of channels
3398 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3399 * @maxbps: the max. bps
3400 *
3401 * Calculate the format bitset from the given rate, channels and th PCM format.
3402 *
3403 * Return zero if invalid.
3404 */
3405unsigned int snd_hda_calc_stream_format(unsigned int rate,
3406 unsigned int channels,
3407 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003408 unsigned int maxbps,
3409 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 int i;
3412 unsigned int val = 0;
3413
Takashi Iwaibefdf312005-08-22 13:57:55 +02003414 for (i = 0; rate_bits[i].hz; i++)
3415 if (rate_bits[i].hz == rate) {
3416 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 break;
3418 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003419 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 snd_printdd("invalid rate %d\n", rate);
3421 return 0;
3422 }
3423
3424 if (channels == 0 || channels > 8) {
3425 snd_printdd("invalid channels %d\n", channels);
3426 return 0;
3427 }
3428 val |= channels - 1;
3429
3430 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003431 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003432 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003433 break;
3434 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003435 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 case 20:
3438 case 24:
3439 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003440 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003441 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003443 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003445 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 break;
3447 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003448 snd_printdd("invalid format width %d\n",
3449 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return 0;
3451 }
3452
Anssi Hannula32c168c2010-08-03 13:28:57 +03003453 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003454 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return val;
3457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003458EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003460static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3461{
3462 unsigned int val = 0;
3463 if (nid != codec->afg &&
3464 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3465 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3466 if (!val || val == -1)
3467 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3468 if (!val || val == -1)
3469 return 0;
3470 return val;
3471}
3472
3473static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3474{
3475 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3476 get_pcm_param);
3477}
3478
3479static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3480{
3481 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3482 if (!streams || streams == -1)
3483 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3484 if (!streams || streams == -1)
3485 return 0;
3486 return streams;
3487}
3488
3489static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3490{
3491 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3492 get_stream_param);
3493}
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495/**
3496 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3497 * @codec: the HDA codec
3498 * @nid: NID to query
3499 * @ratesp: the pointer to store the detected rate bitflags
3500 * @formatsp: the pointer to store the detected formats
3501 * @bpsp: the pointer to store the detected format widths
3502 *
3503 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3504 * or @bsps argument is ignored.
3505 *
3506 * Returns 0 if successful, otherwise a negative error code.
3507 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003508int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3510{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003511 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003513 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003514 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 if (ratesp) {
3517 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003518 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003520 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003522 if (rates == 0) {
3523 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3524 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3525 nid, val,
3526 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3527 return -EIO;
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 *ratesp = rates;
3530 }
3531
3532 if (formatsp || bpsp) {
3533 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003534 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003536 streams = query_stream_param(codec, nid);
3537 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 bps = 0;
3541 if (streams & AC_SUPFMT_PCM) {
3542 if (val & AC_SUPPCM_BITS_8) {
3543 formats |= SNDRV_PCM_FMTBIT_U8;
3544 bps = 8;
3545 }
3546 if (val & AC_SUPPCM_BITS_16) {
3547 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3548 bps = 16;
3549 }
3550 if (wcaps & AC_WCAP_DIGITAL) {
3551 if (val & AC_SUPPCM_BITS_32)
3552 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3553 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3554 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3555 if (val & AC_SUPPCM_BITS_24)
3556 bps = 24;
3557 else if (val & AC_SUPPCM_BITS_20)
3558 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3560 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3562 if (val & AC_SUPPCM_BITS_32)
3563 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 else if (val & AC_SUPPCM_BITS_24)
3565 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003566 else if (val & AC_SUPPCM_BITS_20)
3567 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
3569 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003570 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003572 if (!bps)
3573 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003574 }
3575 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003576 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 /* temporary hack: we have still no proper support
3578 * for the direct AC3 stream...
3579 */
3580 formats |= SNDRV_PCM_FMTBIT_U8;
3581 bps = 8;
3582 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003583 if (formats == 0) {
3584 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3585 "(nid=0x%x, val=0x%x, ovrd=%i, "
3586 "streams=0x%x)\n",
3587 nid, val,
3588 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3589 streams);
3590 return -EIO;
3591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (formatsp)
3593 *formatsp = formats;
3594 if (bpsp)
3595 *bpsp = bps;
3596 }
3597
3598 return 0;
3599}
Stephen Warren384a48d2011-06-01 11:14:21 -06003600EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003603 * snd_hda_is_supported_format - Check the validity of the format
3604 * @codec: HD-audio codec
3605 * @nid: NID to check
3606 * @format: the HD-audio format value to check
3607 *
3608 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 *
3610 * Returns 1 if supported, 0 if not.
3611 */
3612int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3613 unsigned int format)
3614{
3615 int i;
3616 unsigned int val = 0, rate, stream;
3617
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003618 val = query_pcm_param(codec, nid);
3619 if (!val)
3620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003623 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003624 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 if (val & (1 << i))
3626 break;
3627 return 0;
3628 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003629 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return 0;
3631
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003632 stream = query_stream_param(codec, nid);
3633 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 return 0;
3635
3636 if (stream & AC_SUPFMT_PCM) {
3637 switch (format & 0xf0) {
3638 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003639 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
3641 break;
3642 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003643 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return 0;
3645 break;
3646 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003647 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return 0;
3649 break;
3650 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003651 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return 0;
3653 break;
3654 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003655 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return 0;
3657 break;
3658 default:
3659 return 0;
3660 }
3661 } else {
3662 /* FIXME: check for float32 and AC3? */
3663 }
3664
3665 return 1;
3666}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003667EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669/*
3670 * PCM stuff
3671 */
3672static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3673 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003674 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
3676 return 0;
3677}
3678
3679static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3680 struct hda_codec *codec,
3681 unsigned int stream_tag,
3682 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003683 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
3685 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3686 return 0;
3687}
3688
3689static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3690 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003691 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
Takashi Iwai888afa12008-03-18 09:57:50 +01003693 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return 0;
3695}
3696
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003697static int set_pcm_default_values(struct hda_codec *codec,
3698 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003700 int err;
3701
Takashi Iwai0ba21762007-04-16 11:29:14 +02003702 /* query support PCM information from the given NID */
3703 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003704 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003705 info->rates ? NULL : &info->rates,
3706 info->formats ? NULL : &info->formats,
3707 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003708 if (err < 0)
3709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711 if (info->ops.open == NULL)
3712 info->ops.open = hda_pcm_default_open_close;
3713 if (info->ops.close == NULL)
3714 info->ops.close = hda_pcm_default_open_close;
3715 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003716 if (snd_BUG_ON(!info->nid))
3717 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 info->ops.prepare = hda_pcm_default_prepare;
3719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003721 if (snd_BUG_ON(!info->nid))
3722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 info->ops.cleanup = hda_pcm_default_cleanup;
3724 }
3725 return 0;
3726}
3727
Takashi Iwaieb541332010-08-06 13:48:11 +02003728/*
3729 * codec prepare/cleanup entries
3730 */
3731int snd_hda_codec_prepare(struct hda_codec *codec,
3732 struct hda_pcm_stream *hinfo,
3733 unsigned int stream,
3734 unsigned int format,
3735 struct snd_pcm_substream *substream)
3736{
3737 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003738 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003739 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3740 if (ret >= 0)
3741 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003742 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003743 return ret;
3744}
3745EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3746
3747void snd_hda_codec_cleanup(struct hda_codec *codec,
3748 struct hda_pcm_stream *hinfo,
3749 struct snd_pcm_substream *substream)
3750{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003751 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003752 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003753 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003754}
3755EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3756
Takashi Iwaid5191e52009-11-16 14:58:17 +01003757/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003758const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3759 "Audio", "SPDIF", "HDMI", "Modem"
3760};
3761
Takashi Iwai176d5332008-07-30 15:01:44 +02003762/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003763 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003764 *
3765 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003766 */
3767static int get_empty_pcm_device(struct hda_bus *bus, int type)
3768{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003769 /* audio device indices; not linear to keep compatibility */
3770 static int audio_idx[HDA_PCM_NTYPES][5] = {
3771 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3772 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003773 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003774 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003775 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003776 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003777
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003778 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003779 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3780 return -EINVAL;
3781 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003782
3783 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3784 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3785 return audio_idx[type][i];
3786
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003787 snd_printk(KERN_WARNING "Too many %s devices\n",
3788 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003789 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003790}
3791
3792/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003793 * attach a new PCM stream
3794 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003795static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003796{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003797 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003798 struct hda_pcm_stream *info;
3799 int stream, err;
3800
Takashi Iwaib91f0802008-11-04 08:43:08 +01003801 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003802 return -EINVAL;
3803 for (stream = 0; stream < 2; stream++) {
3804 info = &pcm->stream[stream];
3805 if (info->substreams) {
3806 err = set_pcm_default_values(codec, info);
3807 if (err < 0)
3808 return err;
3809 }
3810 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003811 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003812}
3813
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003814/* assign all PCMs of the given codec */
3815int snd_hda_codec_build_pcms(struct hda_codec *codec)
3816{
3817 unsigned int pcm;
3818 int err;
3819
3820 if (!codec->num_pcms) {
3821 if (!codec->patch_ops.build_pcms)
3822 return 0;
3823 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003824 if (err < 0) {
3825 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003826 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003827 err = snd_hda_codec_reset(codec);
3828 if (err < 0) {
3829 printk(KERN_ERR
3830 "hda_codec: cannot revert codec\n");
3831 return err;
3832 }
3833 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003834 }
3835 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3836 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3837 int dev;
3838
3839 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003840 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003841
3842 if (!cpcm->pcm) {
3843 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3844 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003845 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003846 cpcm->device = dev;
3847 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003848 if (err < 0) {
3849 printk(KERN_ERR "hda_codec: cannot attach "
3850 "PCM stream %d for codec #%d\n",
3851 dev, codec->addr);
3852 continue; /* no fatal error */
3853 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003854 }
3855 }
3856 return 0;
3857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/**
3860 * snd_hda_build_pcms - build PCM information
3861 * @bus: the BUS
3862 *
3863 * Create PCM information for each codec included in the bus.
3864 *
3865 * The build_pcms codec patch is requested to set up codec->num_pcms and
3866 * codec->pcm_info properly. The array is referred by the top-level driver
3867 * to create its PCM instances.
3868 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3869 * callback.
3870 *
3871 * At least, substreams, channels_min and channels_max must be filled for
3872 * each stream. substreams = 0 indicates that the stream doesn't exist.
3873 * When rates and/or formats are zero, the supported values are queried
3874 * from the given nid. The nid is used also by the default ops.prepare
3875 * and ops.cleanup callbacks.
3876 *
3877 * The driver needs to call ops.open in its open callback. Similarly,
3878 * ops.close is supposed to be called in the close callback.
3879 * ops.prepare should be called in the prepare or hw_params callback
3880 * with the proper parameters for set up.
3881 * ops.cleanup should be called in hw_free for clean up of streams.
3882 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003883 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003885int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003887 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Takashi Iwai0ba21762007-04-16 11:29:14 +02003889 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003890 int err = snd_hda_codec_build_pcms(codec);
3891 if (err < 0)
3892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894 return 0;
3895}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003896EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898/**
3899 * snd_hda_check_board_config - compare the current codec with the config table
3900 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003901 * @num_configs: number of config enums
3902 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 * @tbl: configuration table, terminated by null entries
3904 *
3905 * Compares the modelname or PCI subsystem id of the current codec with the
3906 * given configuration table. If a matching entry is found, returns its
3907 * config value (supposed to be 0 or positive).
3908 *
3909 * If no entries are matching, the function returns a negative value.
3910 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003911int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003912 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003913 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003915 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003916 int i;
3917 for (i = 0; i < num_configs; i++) {
3918 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003919 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003920 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3921 "selected\n", models[i]);
3922 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
3924 }
3925 }
3926
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003927 if (!codec->bus->pci || !tbl)
3928 return -1;
3929
3930 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3931 if (!tbl)
3932 return -1;
3933 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003934#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003935 char tmp[10];
3936 const char *model = NULL;
3937 if (models)
3938 model = models[tbl->value];
3939 if (!model) {
3940 sprintf(tmp, "#%d", tbl->value);
3941 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003943 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3944 "for config %x:%x (%s)\n",
3945 model, tbl->subvendor, tbl->subdevice,
3946 (tbl->name ? tbl->name : "Unknown device"));
3947#endif
3948 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950 return -1;
3951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003952EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003955 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003956 subsystem ID with the
3957 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003958
3959 This is important for Gateway notebooks with SB450 HDA Audio
3960 where the vendor ID of the PCI device is:
3961 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3962 and the vendor/subvendor are found only at the codec.
3963
3964 * @codec: the HDA codec
3965 * @num_configs: number of config enums
3966 * @models: array of model name strings
3967 * @tbl: configuration table, terminated by null entries
3968 *
3969 * Compares the modelname or PCI subsystem id of the current codec with the
3970 * given configuration table. If a matching entry is found, returns its
3971 * config value (supposed to be 0 or positive).
3972 *
3973 * If no entries are matching, the function returns a negative value.
3974 */
3975int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003976 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003977 const struct snd_pci_quirk *tbl)
3978{
3979 const struct snd_pci_quirk *q;
3980
3981 /* Search for codec ID */
3982 for (q = tbl; q->subvendor; q++) {
3983 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3984
3985 if (vendorid == codec->subsystem_id)
3986 break;
3987 }
3988
3989 if (!q->subvendor)
3990 return -1;
3991
3992 tbl = q;
3993
3994 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003995#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003996 char tmp[10];
3997 const char *model = NULL;
3998 if (models)
3999 model = models[tbl->value];
4000 if (!model) {
4001 sprintf(tmp, "#%d", tbl->value);
4002 model = tmp;
4003 }
4004 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4005 "for config %x:%x (%s)\n",
4006 model, tbl->subvendor, tbl->subdevice,
4007 (tbl->name ? tbl->name : "Unknown device"));
4008#endif
4009 return tbl->value;
4010 }
4011 return -1;
4012}
4013EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4014
4015/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 * snd_hda_add_new_ctls - create controls from the array
4017 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004018 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 *
4020 * This helper function creates and add new controls in the given array.
4021 * The array must be terminated with an empty entry as terminator.
4022 *
4023 * Returns 0 if successful, or a negative error code.
4024 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004025int snd_hda_add_new_ctls(struct hda_codec *codec,
4026 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004031 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004032 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004033 if (knew->iface == -1) /* skip this codec private value */
4034 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004035 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004036 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004037 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004038 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004039 if (addr > 0)
4040 kctl->id.device = addr;
4041 if (idx > 0)
4042 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004043 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004044 if (!err)
4045 break;
4046 /* try first with another device index corresponding to
4047 * the codec addr; if it still fails (or it's the
4048 * primary codec), then try another control index
4049 */
4050 if (!addr && codec->addr)
4051 addr = codec->addr;
4052 else if (!idx && !knew->index) {
4053 idx = find_empty_mixer_ctl_idx(codec,
4054 knew->name);
4055 if (idx <= 0)
4056 return err;
4057 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004058 return err;
4059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061 return 0;
4062}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004063EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Takashi Iwaicb53c622007-08-10 17:21:45 +02004065#ifdef CONFIG_SND_HDA_POWER_SAVE
4066static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
4067 unsigned int power_state);
4068
4069static void hda_power_work(struct work_struct *work)
4070{
4071 struct hda_codec *codec =
4072 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004073 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004074
Maxim Levitsky2e492462007-09-03 15:26:57 +02004075 if (!codec->power_on || codec->power_count) {
4076 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004077 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004078 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004079
4080 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004081 if (bus->ops.pm_notify)
4082 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004083}
4084
4085static void hda_keep_power_on(struct hda_codec *codec)
4086{
4087 codec->power_count++;
4088 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004089 codec->power_jiffies = jiffies;
4090}
4091
Takashi Iwaid5191e52009-11-16 14:58:17 +01004092/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004093void snd_hda_update_power_acct(struct hda_codec *codec)
4094{
4095 unsigned long delta = jiffies - codec->power_jiffies;
4096 if (codec->power_on)
4097 codec->power_on_acct += delta;
4098 else
4099 codec->power_off_acct += delta;
4100 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004101}
4102
Takashi Iwaid5191e52009-11-16 14:58:17 +01004103/**
4104 * snd_hda_power_up - Power-up the codec
4105 * @codec: HD-audio codec
4106 *
4107 * Increment the power-up counter and power up the hardware really when
4108 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004109 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004110void snd_hda_power_up(struct hda_codec *codec)
4111{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004112 struct hda_bus *bus = codec->bus;
4113
Takashi Iwaicb53c622007-08-10 17:21:45 +02004114 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004115 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004116 return;
4117
Takashi Iwaia2f63092009-11-11 09:34:25 +01004118 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004119 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004120 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004121 if (bus->ops.pm_notify)
4122 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004123 hda_call_codec_resume(codec);
4124 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004125 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004127EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004128
4129#define power_save(codec) \
4130 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004131
Takashi Iwaid5191e52009-11-16 14:58:17 +01004132/**
4133 * snd_hda_power_down - Power-down the codec
4134 * @codec: HD-audio codec
4135 *
4136 * Decrement the power-up counter and schedules the power-off work if
4137 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004138 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004139void snd_hda_power_down(struct hda_codec *codec)
4140{
4141 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004142 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004143 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004144 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004145 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004146 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004147 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004148 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004150EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004151
Takashi Iwaid5191e52009-11-16 14:58:17 +01004152/**
4153 * snd_hda_check_amp_list_power - Check the amp list and update the power
4154 * @codec: HD-audio codec
4155 * @check: the object containing an AMP list and the status
4156 * @nid: NID to check / update
4157 *
4158 * Check whether the given NID is in the amp list. If it's in the list,
4159 * check the current AMP status, and update the the power-status according
4160 * to the mute status.
4161 *
4162 * This function is supposed to be set or called from the check_power_status
4163 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004164 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004165int snd_hda_check_amp_list_power(struct hda_codec *codec,
4166 struct hda_loopback_check *check,
4167 hda_nid_t nid)
4168{
Takashi Iwai031024e2011-05-02 11:29:30 +02004169 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004170 int ch, v;
4171
4172 if (!check->amplist)
4173 return 0;
4174 for (p = check->amplist; p->nid; p++) {
4175 if (p->nid == nid)
4176 break;
4177 }
4178 if (!p->nid)
4179 return 0; /* nothing changed */
4180
4181 for (p = check->amplist; p->nid; p++) {
4182 for (ch = 0; ch < 2; ch++) {
4183 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4184 p->idx);
4185 if (!(v & HDA_AMP_MUTE) && v > 0) {
4186 if (!check->power_on) {
4187 check->power_on = 1;
4188 snd_hda_power_up(codec);
4189 }
4190 return 1;
4191 }
4192 }
4193 }
4194 if (check->power_on) {
4195 check->power_on = 0;
4196 snd_hda_power_down(codec);
4197 }
4198 return 0;
4199}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004200EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004203/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004204 * Channel mode helper
4205 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004206
4207/**
4208 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4209 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004210int snd_hda_ch_mode_info(struct hda_codec *codec,
4211 struct snd_ctl_elem_info *uinfo,
4212 const struct hda_channel_mode *chmode,
4213 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004214{
4215 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4216 uinfo->count = 1;
4217 uinfo->value.enumerated.items = num_chmodes;
4218 if (uinfo->value.enumerated.item >= num_chmodes)
4219 uinfo->value.enumerated.item = num_chmodes - 1;
4220 sprintf(uinfo->value.enumerated.name, "%dch",
4221 chmode[uinfo->value.enumerated.item].channels);
4222 return 0;
4223}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004224EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004225
Takashi Iwaid5191e52009-11-16 14:58:17 +01004226/**
4227 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4228 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004229int snd_hda_ch_mode_get(struct hda_codec *codec,
4230 struct snd_ctl_elem_value *ucontrol,
4231 const struct hda_channel_mode *chmode,
4232 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004233 int max_channels)
4234{
4235 int i;
4236
4237 for (i = 0; i < num_chmodes; i++) {
4238 if (max_channels == chmode[i].channels) {
4239 ucontrol->value.enumerated.item[0] = i;
4240 break;
4241 }
4242 }
4243 return 0;
4244}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004245EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004246
Takashi Iwaid5191e52009-11-16 14:58:17 +01004247/**
4248 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4249 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004250int snd_hda_ch_mode_put(struct hda_codec *codec,
4251 struct snd_ctl_elem_value *ucontrol,
4252 const struct hda_channel_mode *chmode,
4253 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004254 int *max_channelsp)
4255{
4256 unsigned int mode;
4257
4258 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004259 if (mode >= num_chmodes)
4260 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004261 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004262 return 0;
4263 /* change the current channel setting */
4264 *max_channelsp = chmode[mode].channels;
4265 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004266 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004267 return 1;
4268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004269EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271/*
4272 * input MUX helper
4273 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004274
4275/**
4276 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4277 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004278int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4279 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
4281 unsigned int index;
4282
4283 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4284 uinfo->count = 1;
4285 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004286 if (!imux->num_items)
4287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 index = uinfo->value.enumerated.item;
4289 if (index >= imux->num_items)
4290 index = imux->num_items - 1;
4291 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4292 return 0;
4293}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004294EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Takashi Iwaid5191e52009-11-16 14:58:17 +01004296/**
4297 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4298 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004299int snd_hda_input_mux_put(struct hda_codec *codec,
4300 const struct hda_input_mux *imux,
4301 struct snd_ctl_elem_value *ucontrol,
4302 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 unsigned int *cur_val)
4304{
4305 unsigned int idx;
4306
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004307 if (!imux->num_items)
4308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 idx = ucontrol->value.enumerated.item[0];
4310 if (idx >= imux->num_items)
4311 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004312 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004314 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4315 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 *cur_val = idx;
4317 return 1;
4318}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004319EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321
4322/*
4323 * Multi-channel / digital-out PCM helper functions
4324 */
4325
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004326/* setup SPDIF output stream */
4327static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4328 unsigned int stream_tag, unsigned int format)
4329{
Stephen Warren7c935972011-06-01 11:14:17 -06004330 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4331
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004332 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004333 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004334 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004335 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004336 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004337 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004338 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004339 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004340 for (d = codec->slave_dig_outs; *d; d++)
4341 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4342 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004343 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004344 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004345 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004346 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004347 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004348}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004349
Takashi Iwai2f728532008-09-25 16:32:41 +02004350static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4351{
4352 snd_hda_codec_cleanup_stream(codec, nid);
4353 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004354 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004355 for (d = codec->slave_dig_outs; *d; d++)
4356 snd_hda_codec_cleanup_stream(codec, *d);
4357 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004358}
4359
Takashi Iwaid5191e52009-11-16 14:58:17 +01004360/**
4361 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4362 * @bus: HD-audio bus
4363 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004364void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4365{
4366 struct hda_codec *codec;
4367
4368 if (!bus)
4369 return;
4370 list_for_each_entry(codec, &bus->codec_list, list) {
4371#ifdef CONFIG_SND_HDA_POWER_SAVE
4372 if (!codec->power_on)
4373 continue;
4374#endif
4375 if (codec->patch_ops.reboot_notify)
4376 codec->patch_ops.reboot_notify(codec);
4377 }
4378}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004379EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004380
Takashi Iwaid5191e52009-11-16 14:58:17 +01004381/**
4382 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004384int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4385 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Ingo Molnar62932df2006-01-16 16:34:20 +01004387 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004388 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4389 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004390 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004392 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004395EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Takashi Iwaid5191e52009-11-16 14:58:17 +01004397/**
4398 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4399 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004400int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4401 struct hda_multi_out *mout,
4402 unsigned int stream_tag,
4403 unsigned int format,
4404 struct snd_pcm_substream *substream)
4405{
4406 mutex_lock(&codec->spdif_mutex);
4407 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4408 mutex_unlock(&codec->spdif_mutex);
4409 return 0;
4410}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004411EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004412
Takashi Iwaid5191e52009-11-16 14:58:17 +01004413/**
4414 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4415 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004416int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4417 struct hda_multi_out *mout)
4418{
4419 mutex_lock(&codec->spdif_mutex);
4420 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4421 mutex_unlock(&codec->spdif_mutex);
4422 return 0;
4423}
4424EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4425
Takashi Iwaid5191e52009-11-16 14:58:17 +01004426/**
4427 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004429int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4430 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
Ingo Molnar62932df2006-01-16 16:34:20 +01004432 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004434 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return 0;
4436}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004437EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Takashi Iwaid5191e52009-11-16 14:58:17 +01004439/**
4440 * snd_hda_multi_out_analog_open - open analog outputs
4441 *
4442 * Open analog outputs and set up the hw-constraints.
4443 * If the digital outputs can be opened as slave, open the digital
4444 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004446int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4447 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004448 struct snd_pcm_substream *substream,
4449 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
Takashi Iwai9a081602008-02-12 18:37:26 +01004451 struct snd_pcm_runtime *runtime = substream->runtime;
4452 runtime->hw.channels_max = mout->max_channels;
4453 if (mout->dig_out_nid) {
4454 if (!mout->analog_rates) {
4455 mout->analog_rates = hinfo->rates;
4456 mout->analog_formats = hinfo->formats;
4457 mout->analog_maxbps = hinfo->maxbps;
4458 } else {
4459 runtime->hw.rates = mout->analog_rates;
4460 runtime->hw.formats = mout->analog_formats;
4461 hinfo->maxbps = mout->analog_maxbps;
4462 }
4463 if (!mout->spdif_rates) {
4464 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4465 &mout->spdif_rates,
4466 &mout->spdif_formats,
4467 &mout->spdif_maxbps);
4468 }
4469 mutex_lock(&codec->spdif_mutex);
4470 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004471 if ((runtime->hw.rates & mout->spdif_rates) &&
4472 (runtime->hw.formats & mout->spdif_formats)) {
4473 runtime->hw.rates &= mout->spdif_rates;
4474 runtime->hw.formats &= mout->spdif_formats;
4475 if (mout->spdif_maxbps < hinfo->maxbps)
4476 hinfo->maxbps = mout->spdif_maxbps;
4477 } else {
4478 mout->share_spdif = 0;
4479 /* FIXME: need notify? */
4480 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004481 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004482 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4485 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Takashi Iwaid5191e52009-11-16 14:58:17 +01004489/**
4490 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4491 *
4492 * Set up the i/o for analog out.
4493 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004495int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4496 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 unsigned int stream_tag,
4498 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004499 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Takashi Iwaidda14412011-05-02 11:29:30 +02004501 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004503 struct hda_spdif_out *spdif =
4504 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 int i;
4506
Ingo Molnar62932df2006-01-16 16:34:20 +01004507 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004508 if (mout->dig_out_nid && mout->share_spdif &&
4509 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004511 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4512 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004513 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004515 setup_dig_out_stream(codec, mout->dig_out_nid,
4516 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 } else {
4518 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004519 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 }
4521 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004522 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004525 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4526 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004527 if (!mout->no_share_stream &&
4528 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004530 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4531 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004532 /* extra outputs copied from front */
4533 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004534 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004535 snd_hda_codec_setup_stream(codec,
4536 mout->extra_out_nid[i],
4537 stream_tag, 0, format);
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 /* surrounds */
4540 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004541 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004542 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4543 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004544 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004545 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4546 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548 return 0;
4549}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004550EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Takashi Iwaid5191e52009-11-16 14:58:17 +01004552/**
4553 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004555int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4556 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Takashi Iwaidda14412011-05-02 11:29:30 +02004558 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 int i;
4560
4561 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004562 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004564 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004565 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4566 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004567 snd_hda_codec_cleanup_stream(codec,
4568 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004569 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004571 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 mout->dig_out_used = 0;
4573 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004574 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return 0;
4576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004577EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004579/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004580 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004581 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004582
Takashi Iwaidda14412011-05-02 11:29:30 +02004583static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004584{
4585 for (; *list; list++)
4586 if (*list == nid)
4587 return 1;
4588 return 0;
4589}
4590
Steve Longerbeam81937d32007-05-08 15:33:03 +02004591
4592/*
4593 * Sort an associated group of pins according to their sequence numbers.
4594 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004595static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004596 int num_pins)
4597{
4598 int i, j;
4599 short seq;
4600 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004601
Steve Longerbeam81937d32007-05-08 15:33:03 +02004602 for (i = 0; i < num_pins; i++) {
4603 for (j = i + 1; j < num_pins; j++) {
4604 if (sequences[i] > sequences[j]) {
4605 seq = sequences[i];
4606 sequences[i] = sequences[j];
4607 sequences[j] = seq;
4608 nid = pins[i];
4609 pins[i] = pins[j];
4610 pins[j] = nid;
4611 }
4612 }
4613 }
4614}
4615
4616
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004617/* add the found input-pin to the cfg->inputs[] table */
4618static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4619 int type)
4620{
4621 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4622 cfg->inputs[cfg->num_inputs].pin = nid;
4623 cfg->inputs[cfg->num_inputs].type = type;
4624 cfg->num_inputs++;
4625 }
4626}
4627
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004628/* sort inputs in the order of AUTO_PIN_* type */
4629static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4630{
4631 int i, j;
4632
4633 for (i = 0; i < cfg->num_inputs; i++) {
4634 for (j = i + 1; j < cfg->num_inputs; j++) {
4635 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4636 struct auto_pin_cfg_item tmp;
4637 tmp = cfg->inputs[i];
4638 cfg->inputs[i] = cfg->inputs[j];
4639 cfg->inputs[j] = tmp;
4640 }
4641 }
4642 }
4643}
4644
Takashi Iwai82bc9552006-03-21 11:24:42 +01004645/*
4646 * Parse all pin widgets and store the useful pin nids to cfg
4647 *
4648 * The number of line-outs or any primary output is stored in line_outs,
4649 * and the corresponding output pins are assigned to line_out_pins[],
4650 * in the order of front, rear, CLFE, side, ...
4651 *
4652 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004653 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004654 * is detected, one of speaker of HP pins is assigned as the primary
4655 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4656 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004657 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004658 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004659 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4660 * respectively.
4661 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004662int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4663 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004664 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004665{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004666 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004667 short seq, assoc_line_out, assoc_speaker;
4668 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4669 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004670 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004671 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004672
4673 memset(cfg, 0, sizeof(*cfg));
4674
Steve Longerbeam81937d32007-05-08 15:33:03 +02004675 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4676 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004677 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004678 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004680 end_nid = codec->start_nid + codec->num_nodes;
4681 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004682 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004683 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004684 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004685 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004686
4687 /* read all default configuration for pin complex */
4688 if (wid_type != AC_WID_PIN)
4689 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004690 /* ignore the given nids (e.g. pc-beep returns error) */
4691 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4692 continue;
4693
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004694 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004695 conn = get_defcfg_connect(def_conf);
4696 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004697 continue;
4698 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004699 dev = get_defcfg_device(def_conf);
4700
4701 /* workaround for buggy BIOS setups */
4702 if (dev == AC_JACK_LINE_OUT) {
4703 if (conn == AC_JACK_PORT_FIXED)
4704 dev = AC_JACK_SPEAKER;
4705 }
4706
4707 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004708 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004709 seq = get_defcfg_sequence(def_conf);
4710 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004711
4712 if (!(wid_caps & AC_WCAP_STEREO))
4713 if (!cfg->mono_out_pin)
4714 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004715 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004716 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004717 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004718 assoc_line_out = assoc;
4719 else if (assoc_line_out != assoc)
4720 continue;
4721 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4722 continue;
4723 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004724 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004725 cfg->line_outs++;
4726 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004727 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004728 seq = get_defcfg_sequence(def_conf);
4729 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004730 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004731 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004732 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004733 assoc_speaker = assoc;
4734 else if (assoc_speaker != assoc)
4735 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004736 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4737 continue;
4738 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004739 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004740 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004741 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004742 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004743 seq = get_defcfg_sequence(def_conf);
4744 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004745 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4746 continue;
4747 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004748 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004749 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004750 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004751 case AC_JACK_MIC_IN:
4752 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004753 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004754 case AC_JACK_LINE_IN:
4755 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004756 break;
4757 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004758 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004759 break;
4760 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004761 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004762 break;
4763 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004764 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004765 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4766 continue;
4767 cfg->dig_out_pins[cfg->dig_outs] = nid;
4768 cfg->dig_out_type[cfg->dig_outs] =
4769 (loc == AC_JACK_LOC_HDMI) ?
4770 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4771 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004772 break;
4773 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004774 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004775 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004776 if (loc == AC_JACK_LOC_HDMI)
4777 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4778 else
4779 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004780 break;
4781 }
4782 }
4783
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004784 /* FIX-UP:
4785 * If no line-out is defined but multiple HPs are found,
4786 * some of them might be the real line-outs.
4787 */
4788 if (!cfg->line_outs && cfg->hp_outs > 1) {
4789 int i = 0;
4790 while (i < cfg->hp_outs) {
4791 /* The real HPs should have the sequence 0x0f */
4792 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4793 i++;
4794 continue;
4795 }
4796 /* Move it to the line-out table */
4797 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4798 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4799 cfg->line_outs++;
4800 cfg->hp_outs--;
4801 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4802 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004803 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004804 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4805 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004806 memset(cfg->hp_pins + cfg->hp_outs, 0,
4807 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004808 if (!cfg->hp_outs)
4809 cfg->line_out_type = AUTO_PIN_HP_OUT;
4810
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004811 }
4812
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004813 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004814 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4815 cfg->line_outs);
4816 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4817 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004818 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4819 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004820
Steve Longerbeam81937d32007-05-08 15:33:03 +02004821 /*
4822 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4823 * as a primary output
4824 */
4825 if (!cfg->line_outs) {
4826 if (cfg->speaker_outs) {
4827 cfg->line_outs = cfg->speaker_outs;
4828 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4829 sizeof(cfg->speaker_pins));
4830 cfg->speaker_outs = 0;
4831 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4832 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4833 } else if (cfg->hp_outs) {
4834 cfg->line_outs = cfg->hp_outs;
4835 memcpy(cfg->line_out_pins, cfg->hp_pins,
4836 sizeof(cfg->hp_pins));
4837 cfg->hp_outs = 0;
4838 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4839 cfg->line_out_type = AUTO_PIN_HP_OUT;
4840 }
4841 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004842
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004843 /* Reorder the surround channels
4844 * ALSA sequence is front/surr/clfe/side
4845 * HDA sequence is:
4846 * 4-ch: front/surr => OK as it is
4847 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004848 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004849 */
4850 switch (cfg->line_outs) {
4851 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004852 case 4:
4853 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004854 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004855 cfg->line_out_pins[2] = nid;
4856 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004857 }
4858
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004859 sort_autocfg_input_pins(cfg);
4860
Takashi Iwai82bc9552006-03-21 11:24:42 +01004861 /*
4862 * debug prints of the parsed results
4863 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004864 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 +01004865 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4866 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004867 cfg->line_out_pins[4],
4868 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4869 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4870 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004871 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4872 cfg->speaker_outs, cfg->speaker_pins[0],
4873 cfg->speaker_pins[1], cfg->speaker_pins[2],
4874 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004875 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4876 cfg->hp_outs, cfg->hp_pins[0],
4877 cfg->hp_pins[1], cfg->hp_pins[2],
4878 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004879 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004880 if (cfg->dig_outs)
4881 snd_printd(" dig-out=0x%x/0x%x\n",
4882 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004883 snd_printd(" inputs:");
4884 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004885 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004886 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004887 cfg->inputs[i].pin);
4888 }
4889 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004890 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004891 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004892
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004893 return 0;
4894}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004895EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004896
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004897int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004898{
4899 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004900 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004901 if (conn == AC_JACK_PORT_NONE)
4902 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004903 /* Windows may claim the internal mic to be BOTH, too */
4904 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004905 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004906 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004907 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004908 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004909 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004910 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004911 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004912 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004913 return INPUT_PIN_ATTR_FRONT;
4914 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004915}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004916EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004917
Takashi Iwai990061c2010-09-09 22:08:44 +02004918/**
4919 * hda_get_input_pin_label - Give a label for the given input pin
4920 *
4921 * When check_location is true, the function checks the pin location
4922 * for mic and line-in pins, and set an appropriate prefix like "Front",
4923 * "Rear", "Internal".
4924 */
4925
Takashi Iwai10a20af2010-09-09 16:28:02 +02004926const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4927 int check_location)
4928{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004929 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004930 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004931 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4932 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004933 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004934
Takashi Iwai10a20af2010-09-09 16:28:02 +02004935 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004936
4937 switch (get_defcfg_device(def_conf)) {
4938 case AC_JACK_MIC_IN:
4939 if (!check_location)
4940 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004941 attr = snd_hda_get_input_pin_attr(def_conf);
4942 if (!attr)
4943 return "None";
4944 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004945 case AC_JACK_LINE_IN:
4946 if (!check_location)
4947 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004948 attr = snd_hda_get_input_pin_attr(def_conf);
4949 if (!attr)
4950 return "None";
4951 if (attr == INPUT_PIN_ATTR_DOCK)
4952 return "Dock Line";
4953 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004954 case AC_JACK_AUX:
4955 return "Aux";
4956 case AC_JACK_CD:
4957 return "CD";
4958 case AC_JACK_SPDIF_IN:
4959 return "SPDIF In";
4960 case AC_JACK_DIG_OTHER_IN:
4961 return "Digital In";
4962 default:
4963 return "Misc";
4964 }
4965}
4966EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4967
Takashi Iwaia1c98512010-09-09 21:36:27 +02004968/* Check whether the location prefix needs to be added to the label.
4969 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4970 * have to put "Front" prefix to each label. In such a case, returns false.
4971 */
4972static int check_mic_location_need(struct hda_codec *codec,
4973 const struct auto_pin_cfg *cfg,
4974 int input)
4975{
4976 unsigned int defc;
4977 int i, attr, attr2;
4978
4979 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004980 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004981 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004982 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004983 return 1;
4984
4985 attr = 0;
4986 for (i = 0; i < cfg->num_inputs; i++) {
4987 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004988 attr2 = snd_hda_get_input_pin_attr(defc);
4989 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004990 if (attr && attr != attr2)
4991 return 1; /* different locations found */
4992 attr = attr2;
4993 }
4994 }
4995 return 0;
4996}
4997
Takashi Iwai990061c2010-09-09 22:08:44 +02004998/**
4999 * hda_get_autocfg_input_label - Get a label for the given input
5000 *
5001 * Get a label for the given input pin defined by the autocfg item.
5002 * Unlike hda_get_input_pin_label(), this function checks all inputs
5003 * defined in autocfg and avoids the redundant mic/line prefix as much as
5004 * possible.
5005 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005006const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5007 const struct auto_pin_cfg *cfg,
5008 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005009{
5010 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005011 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005012
Takashi Iwai10a20af2010-09-09 16:28:02 +02005013 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5014 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5015 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005016 if (has_multiple_pins && type == AUTO_PIN_MIC)
5017 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005018 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5019 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005020}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005021EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5022
Takashi Iwai990061c2010-09-09 22:08:44 +02005023/**
5024 * snd_hda_add_imux_item - Add an item to input_mux
5025 *
5026 * When the same label is used already in the existing items, the number
5027 * suffix is appended to the label. This label index number is stored
5028 * to type_idx when non-NULL pointer is given.
5029 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005030int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5031 int index, int *type_idx)
5032{
5033 int i, label_idx = 0;
5034 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5035 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5036 return -EINVAL;
5037 }
5038 for (i = 0; i < imux->num_items; i++) {
5039 if (!strncmp(label, imux->items[i].label, strlen(label)))
5040 label_idx++;
5041 }
5042 if (type_idx)
5043 *type_idx = label_idx;
5044 if (label_idx > 0)
5045 snprintf(imux->items[imux->num_items].label,
5046 sizeof(imux->items[imux->num_items].label),
5047 "%s %d", label, label_idx);
5048 else
5049 strlcpy(imux->items[imux->num_items].label, label,
5050 sizeof(imux->items[imux->num_items].label));
5051 imux->items[imux->num_items].index = index;
5052 imux->num_items++;
5053 return 0;
5054}
5055EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005056
Takashi Iwai4a471b72005-12-07 13:56:29 +01005057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058#ifdef CONFIG_PM
5059/*
5060 * power management
5061 */
5062
5063/**
5064 * snd_hda_suspend - suspend the codecs
5065 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 *
5067 * Returns 0 if successful.
5068 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005069int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005071 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Takashi Iwai0ba21762007-04-16 11:29:14 +02005073 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02005074#ifdef CONFIG_SND_HDA_POWER_SAVE
5075 if (!codec->power_on)
5076 continue;
5077#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02005078 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 }
5080 return 0;
5081}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005082EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084/**
5085 * snd_hda_resume - resume the codecs
5086 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 *
5088 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005089 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005090 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005091 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 */
5093int snd_hda_resume(struct hda_bus *bus)
5094{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005095 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Takashi Iwai0ba21762007-04-16 11:29:14 +02005097 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005098 if (snd_hda_codec_needs_resume(codec))
5099 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 return 0;
5102}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005103EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005104#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005105
5106/*
5107 * generic arrays
5108 */
5109
Takashi Iwaid5191e52009-11-16 14:58:17 +01005110/**
5111 * snd_array_new - get a new element from the given array
5112 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005113 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005114 * Get a new element from the given array. If it exceeds the
5115 * pre-allocated array size, re-allocate the array.
5116 *
5117 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005118 */
5119void *snd_array_new(struct snd_array *array)
5120{
5121 if (array->used >= array->alloced) {
5122 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005123 void *nlist;
5124 if (snd_BUG_ON(num >= 4096))
5125 return NULL;
5126 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005127 if (!nlist)
5128 return NULL;
5129 if (array->list) {
5130 memcpy(nlist, array->list,
5131 array->elem_size * array->alloced);
5132 kfree(array->list);
5133 }
5134 array->list = nlist;
5135 array->alloced = num;
5136 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005137 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005138}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005139EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005140
Takashi Iwaid5191e52009-11-16 14:58:17 +01005141/**
5142 * snd_array_free - free the given array elements
5143 * @array: the array object
5144 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005145void snd_array_free(struct snd_array *array)
5146{
5147 kfree(array->list);
5148 array->used = 0;
5149 array->alloced = 0;
5150 array->list = NULL;
5151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005152EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005153
Takashi Iwaid5191e52009-11-16 14:58:17 +01005154/**
5155 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5156 * @pcm: PCM caps bits
5157 * @buf: the string buffer to write
5158 * @buflen: the max buffer length
5159 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005160 * used by hda_proc.c and hda_eld.c
5161 */
5162void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5163{
5164 static unsigned int rates[] = {
5165 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5166 96000, 176400, 192000, 384000
5167 };
5168 int i, j;
5169
5170 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5171 if (pcm & (1 << i))
5172 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5173
5174 buf[j] = '\0'; /* necessary when j == 0 */
5175}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005176EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005177
Takashi Iwaid5191e52009-11-16 14:58:17 +01005178/**
5179 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5180 * @pcm: PCM caps bits
5181 * @buf: the string buffer to write
5182 * @buflen: the max buffer length
5183 *
5184 * used by hda_proc.c and hda_eld.c
5185 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005186void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5187{
5188 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5189 int i, j;
5190
5191 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5192 if (pcm & (AC_SUPPCM_BITS_8 << i))
5193 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5194
5195 buf[j] = '\0'; /* necessary when j == 0 */
5196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005197EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005198
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005199#ifdef CONFIG_SND_HDA_INPUT_JACK
5200/*
5201 * Input-jack notification support
5202 */
5203struct hda_jack_item {
5204 hda_nid_t nid;
5205 int type;
5206 struct snd_jack *jack;
5207};
5208
5209static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5210 int type)
5211{
5212 switch (type) {
5213 case SND_JACK_HEADPHONE:
5214 return "Headphone";
5215 case SND_JACK_MICROPHONE:
5216 return "Mic";
5217 case SND_JACK_LINEOUT:
5218 return "Line-out";
5219 case SND_JACK_HEADSET:
5220 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005221 case SND_JACK_VIDEOOUT:
5222 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005223 default:
5224 return "Misc";
5225 }
5226}
5227
5228static void hda_free_jack_priv(struct snd_jack *jack)
5229{
5230 struct hda_jack_item *jacks = jack->private_data;
5231 jacks->nid = 0;
5232 jacks->jack = NULL;
5233}
5234
5235int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5236 const char *name)
5237{
5238 struct hda_jack_item *jack;
5239 int err;
5240
5241 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5242 jack = snd_array_new(&codec->jacks);
5243 if (!jack)
5244 return -ENOMEM;
5245
5246 jack->nid = nid;
5247 jack->type = type;
5248 if (!name)
5249 name = get_jack_default_name(codec, nid, type);
5250 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5251 if (err < 0) {
5252 jack->nid = 0;
5253 return err;
5254 }
5255 jack->jack->private_data = jack;
5256 jack->jack->private_free = hda_free_jack_priv;
5257 return 0;
5258}
5259EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5260
5261void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5262{
5263 struct hda_jack_item *jacks = codec->jacks.list;
5264 int i;
5265
5266 if (!jacks)
5267 return;
5268
5269 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5270 unsigned int pin_ctl;
5271 unsigned int present;
5272 int type;
5273
5274 if (jacks->nid != nid)
5275 continue;
5276 present = snd_hda_jack_detect(codec, nid);
5277 type = jacks->type;
5278 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5279 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5280 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5281 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5282 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5283 }
5284 snd_jack_report(jacks->jack, present ? type : 0);
5285 }
5286}
5287EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5288
5289/* free jack instances manually when clearing/reconfiguring */
5290void snd_hda_input_jack_free(struct hda_codec *codec)
5291{
5292 if (!codec->bus->shutdown && codec->jacks.list) {
5293 struct hda_jack_item *jacks = codec->jacks.list;
5294 int i;
5295 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5296 if (jacks->jack)
5297 snd_device_free(codec->bus->card, jacks->jack);
5298 }
5299 }
5300 snd_array_free(&codec->jacks);
5301}
5302EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5303#endif /* CONFIG_SND_HDA_INPUT_JACK */
5304
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005305MODULE_DESCRIPTION("HDA codec core");
5306MODULE_LICENSE("GPL");