blob: 2c79e96d0324db2042fe973fab81471146cfab77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
246 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return res;
248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100249EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/**
252 * snd_hda_codec_write - send a single command without waiting for response
253 * @codec: the HDA codec
254 * @nid: NID to send the command
255 * @direct: direct flag
256 * @verb: the verb to send
257 * @parm: the parameter for the verb
258 *
259 * Send a single command without waiting for response.
260 *
261 * Returns 0 if successful, or a negative error code.
262 */
263int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
264 unsigned int verb, unsigned int parm)
265{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200266 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100267 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200268 return codec_exec_verb(codec, cmd,
269 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100271EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/**
274 * snd_hda_sequence_write - sequence writes
275 * @codec: the HDA codec
276 * @seq: VERB array to send
277 *
278 * Send the commands sequentially from the given array.
279 * The array must be terminated with NID=0.
280 */
281void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282{
283 for (; seq->nid; seq++)
284 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100286EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
289 * snd_hda_get_sub_nodes - get the range of sub nodes
290 * @codec: the HDA codec
291 * @nid: NID to parse
292 * @start_id: the pointer to store the start NID
293 *
294 * Parse the NID and store the start NID of its sub-nodes.
295 * Returns the number of sub-nodes.
296 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200297int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
298 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned int parm;
301
302 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200303 if (parm == -1)
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *start_id = (parm >> 16) & 0x7fff;
306 return (int)(parm & 0x7fff);
307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100308EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310/**
311 * snd_hda_get_connections - get connection list
312 * @codec: the HDA codec
313 * @nid: NID to parse
314 * @conn_list: connection list array
315 * @max_conns: max. number of connections to store
316 *
317 * Parses the connection list of the given widget and stores the list
318 * of NIDs.
319 *
320 * Returns the number of connections, or a negative error code.
321 */
322int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
323 hda_nid_t *conn_list, int max_conns)
324{
325 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100326 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200328 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100329 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwaida3cec32008-08-08 17:12:14 +0200331 if (snd_BUG_ON(!conn_list || max_conns <= 0))
332 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200334 wcaps = get_wcaps(codec, nid);
335 if (!(wcaps & AC_WCAP_CONN_LIST) &&
336 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200337 snd_printk(KERN_WARNING "hda_codec: "
338 "connection list not available for 0x%x\n", nid);
339 return -EINVAL;
340 }
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
343 if (parm & AC_CLIST_LONG) {
344 /* long form */
345 shift = 16;
346 num_elems = 2;
347 } else {
348 /* short form */
349 shift = 8;
350 num_elems = 4;
351 }
352 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 mask = (1 << (shift-1)) - 1;
354
Takashi Iwai0ba21762007-04-16 11:29:14 +0200355 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0; /* no connection */
357
358 if (conn_len == 1) {
359 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200360 parm = snd_hda_codec_read(codec, nid, 0,
361 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200362 if (parm == -1 && codec->bus->rirb_error)
363 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 conn_list[0] = parm & mask;
365 return 1;
366 }
367
368 /* multi connection */
369 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100370 prev_nid = 0;
371 for (i = 0; i < conn_len; i++) {
372 int range_val;
373 hda_nid_t val, n;
374
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200375 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100376 parm = snd_hda_codec_read(codec, nid, 0,
377 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200378 if (parm == -1 && codec->bus->rirb_error)
379 return -EIO;
380 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200381 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100382 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200383 if (val == 0) {
384 snd_printk(KERN_WARNING "hda_codec: "
385 "invalid CONNECT_LIST verb %x[%i]:%x\n",
386 nid, i, parm);
387 return 0;
388 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100389 parm >>= shift;
390 if (range_val) {
391 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200392 if (!prev_nid || prev_nid >= val) {
393 snd_printk(KERN_WARNING "hda_codec: "
394 "invalid dep_range_val %x:%x\n",
395 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100396 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100398 for (n = prev_nid + 1; n <= val; n++) {
399 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200400 snd_printk(KERN_ERR "hda_codec: "
401 "Too many connections %d for NID 0x%x\n",
402 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100403 return -EINVAL;
404 }
405 conn_list[conns++] = n;
406 }
407 } else {
408 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200409 snd_printk(KERN_ERR "hda_codec: "
410 "Too many connections %d for NID 0x%x\n",
411 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100412 return -EINVAL;
413 }
414 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100416 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 return conns;
419}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100420EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422
423/**
424 * snd_hda_queue_unsol_event - add an unsolicited event to queue
425 * @bus: the BUS
426 * @res: unsolicited event (lower 32bit of RIRB entry)
427 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
428 *
429 * Adds the given event to the queue. The events are processed in
430 * the workqueue asynchronously. Call this function in the interrupt
431 * hanlder when RIRB receives an unsolicited event.
432 *
433 * Returns 0 if successful, or a negative error code.
434 */
435int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
436{
437 struct hda_bus_unsolicited *unsol;
438 unsigned int wp;
439
Takashi Iwai0ba21762007-04-16 11:29:14 +0200440 unsol = bus->unsol;
441 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443
444 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
445 unsol->wp = wp;
446
447 wp <<= 1;
448 unsol->queue[wp] = res;
449 unsol->queue[wp + 1] = res_ex;
450
Takashi Iwai6acaed32009-01-12 10:09:24 +0100451 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return 0;
454}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100455EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800458 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
David Howellsc4028952006-11-22 14:57:56 +0000460static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
David Howellsc4028952006-11-22 14:57:56 +0000462 struct hda_bus_unsolicited *unsol =
463 container_of(work, struct hda_bus_unsolicited, work);
464 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct hda_codec *codec;
466 unsigned int rp, caddr, res;
467
468 while (unsol->rp != unsol->wp) {
469 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
470 unsol->rp = rp;
471 rp <<= 1;
472 res = unsol->queue[rp];
473 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200474 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 continue;
476 codec = bus->caddr_tbl[caddr & 0x0f];
477 if (codec && codec->patch_ops.unsol_event)
478 codec->patch_ops.unsol_event(codec, res);
479 }
480}
481
482/*
483 * initialize unsolicited queue
484 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200485static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct hda_bus_unsolicited *unsol;
488
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100489 if (bus->unsol) /* already initialized */
490 return 0;
491
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200492 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200493 if (!unsol) {
494 snd_printk(KERN_ERR "hda_codec: "
495 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -ENOMEM;
497 }
David Howellsc4028952006-11-22 14:57:56 +0000498 INIT_WORK(&unsol->work, process_unsol_events);
499 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 bus->unsol = unsol;
501 return 0;
502}
503
504/*
505 * destructor
506 */
507static void snd_hda_codec_free(struct hda_codec *codec);
508
509static int snd_hda_bus_free(struct hda_bus *bus)
510{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200511 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Takashi Iwai0ba21762007-04-16 11:29:14 +0200513 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100515 if (bus->workq)
516 flush_workqueue(bus->workq);
517 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200519 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 snd_hda_codec_free(codec);
521 }
522 if (bus->ops.private_free)
523 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100524 if (bus->workq)
525 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 kfree(bus);
527 return 0;
528}
529
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100530static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100533 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return snd_hda_bus_free(bus);
535}
536
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200537#ifdef CONFIG_SND_HDA_HWDEP
538static int snd_hda_bus_dev_register(struct snd_device *device)
539{
540 struct hda_bus *bus = device->device_data;
541 struct hda_codec *codec;
542 list_for_each_entry(codec, &bus->codec_list, list) {
543 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100544 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200545 }
546 return 0;
547}
548#else
549#define snd_hda_bus_dev_register NULL
550#endif
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/**
553 * snd_hda_bus_new - create a HDA bus
554 * @card: the card entry
555 * @temp: the template for hda_bus information
556 * @busp: the pointer to store the created bus instance
557 *
558 * Returns 0 if successful, or a negative error code.
559 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100560int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200561 const struct hda_bus_template *temp,
562 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct hda_bus *bus;
565 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100566 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200567 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 .dev_free = snd_hda_bus_dev_free,
569 };
570
Takashi Iwaida3cec32008-08-08 17:12:14 +0200571 if (snd_BUG_ON(!temp))
572 return -EINVAL;
573 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (busp)
577 *busp = NULL;
578
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200579 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (bus == NULL) {
581 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
582 return -ENOMEM;
583 }
584
585 bus->card = card;
586 bus->private_data = temp->private_data;
587 bus->pci = temp->pci;
588 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100589 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 bus->ops = temp->ops;
591
Ingo Molnar62932df2006-01-16 16:34:20 +0100592 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200593 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 INIT_LIST_HEAD(&bus->codec_list);
595
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100596 snprintf(bus->workq_name, sizeof(bus->workq_name),
597 "hd-audio%d", card->number);
598 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100599 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100600 snd_printk(KERN_ERR "cannot create workqueue %s\n",
601 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100602 kfree(bus);
603 return -ENOMEM;
604 }
605
Takashi Iwai0ba21762007-04-16 11:29:14 +0200606 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
607 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 snd_hda_bus_free(bus);
609 return err;
610 }
611 if (busp)
612 *busp = bus;
613 return 0;
614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100615EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Takashi Iwai82467612007-07-27 19:15:54 +0200617#ifdef CONFIG_SND_HDA_GENERIC
618#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200619 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200620#else
621#define is_generic_config(codec) 0
622#endif
623
Takashi Iwai645f10c2008-11-28 15:07:37 +0100624#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100625#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
626#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100627#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100628#endif
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * find a matching codec preset
632 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200633static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200634find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100636 struct hda_codec_preset_list *tbl;
637 const struct hda_codec_preset *preset;
638 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Takashi Iwai82467612007-07-27 19:15:54 +0200640 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100641 return NULL; /* use the generic parser */
642
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100643 again:
644 mutex_lock(&preset_mutex);
645 list_for_each_entry(tbl, &hda_preset_tables, list) {
646 if (!try_module_get(tbl->owner)) {
647 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
648 continue;
649 }
650 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100652 if (preset->afg && preset->afg != codec->afg)
653 continue;
654 if (preset->mfg && preset->mfg != codec->mfg)
655 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200656 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200658 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200659 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100660 preset->rev == codec->revision_id)) {
661 mutex_unlock(&preset_mutex);
662 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100666 module_put(tbl->owner);
667 }
668 mutex_unlock(&preset_mutex);
669
670 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
671 char name[32];
672 if (!mod_requested)
673 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
674 codec->vendor_id);
675 else
676 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
677 (codec->vendor_id >> 16) & 0xffff);
678 request_module(name);
679 mod_requested++;
680 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 return NULL;
683}
684
685/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200686 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200688static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 const struct hda_vendor_id *c;
691 const char *vendor = NULL;
692 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200693 char tmp[16];
694
695 if (codec->vendor_name)
696 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 for (c = hda_vendor_ids; c->id; c++) {
699 if (c->id == vendor_id) {
700 vendor = c->name;
701 break;
702 }
703 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200704 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sprintf(tmp, "Generic %04x", vendor_id);
706 vendor = tmp;
707 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200708 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
709 if (!codec->vendor_name)
710 return -ENOMEM;
711
712 get_chip_name:
713 if (codec->chip_name)
714 return 0;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200717 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
718 else {
719 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
720 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
721 }
722 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200723 return -ENOMEM;
724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200728 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100730static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200732 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 hda_nid_t nid;
734
735 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
736 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200738 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200739 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200740 case AC_GRP_AUDIO_FUNCTION:
741 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200742 codec->afg_function_id = function_id & 0xff;
743 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200744 break;
745 case AC_GRP_MODEM_FUNCTION:
746 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200747 codec->mfg_function_id = function_id & 0xff;
748 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200749 break;
750 default:
751 break;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100757 * read widget caps for each widget and store in cache
758 */
759static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
760{
761 int i;
762 hda_nid_t nid;
763
764 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
765 &codec->start_nid);
766 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200767 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100768 return -ENOMEM;
769 nid = codec->start_nid;
770 for (i = 0; i < codec->num_nodes; i++, nid++)
771 codec->wcaps[i] = snd_hda_param_read(codec, nid,
772 AC_PAR_AUDIO_WIDGET_CAP);
773 return 0;
774}
775
Takashi Iwai3be14142009-02-20 14:11:16 +0100776/* read all pin default configurations and save codec->init_pins */
777static int read_pin_defaults(struct hda_codec *codec)
778{
779 int i;
780 hda_nid_t nid = codec->start_nid;
781
782 for (i = 0; i < codec->num_nodes; i++, nid++) {
783 struct hda_pincfg *pin;
784 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200785 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100786 if (wid_type != AC_WID_PIN)
787 continue;
788 pin = snd_array_new(&codec->init_pins);
789 if (!pin)
790 return -ENOMEM;
791 pin->nid = nid;
792 pin->cfg = snd_hda_codec_read(codec, nid, 0,
793 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200794 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
795 AC_VERB_GET_PIN_WIDGET_CONTROL,
796 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100797 }
798 return 0;
799}
800
801/* look up the given pin config list and return the item matching with NID */
802static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
803 struct snd_array *array,
804 hda_nid_t nid)
805{
806 int i;
807 for (i = 0; i < array->used; i++) {
808 struct hda_pincfg *pin = snd_array_elem(array, i);
809 if (pin->nid == nid)
810 return pin;
811 }
812 return NULL;
813}
814
815/* write a config value for the given NID */
816static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
817 unsigned int cfg)
818{
819 int i;
820 for (i = 0; i < 4; i++) {
821 snd_hda_codec_write(codec, nid, 0,
822 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
823 cfg & 0xff);
824 cfg >>= 8;
825 }
826}
827
828/* set the current pin config value for the given NID.
829 * the value is cached, and read via snd_hda_codec_get_pincfg()
830 */
831int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
832 hda_nid_t nid, unsigned int cfg)
833{
834 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100835 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100836
Takashi Iwaib82855a2009-12-27 11:24:56 +0100837 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
838 return -EINVAL;
839
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100840 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100841 pin = look_up_pincfg(codec, list, nid);
842 if (!pin) {
843 pin = snd_array_new(list);
844 if (!pin)
845 return -ENOMEM;
846 pin->nid = nid;
847 }
848 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100849
850 /* change only when needed; e.g. if the pincfg is already present
851 * in user_pins[], don't write it
852 */
853 cfg = snd_hda_codec_get_pincfg(codec, nid);
854 if (oldcfg != cfg)
855 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100856 return 0;
857}
858
Takashi Iwaid5191e52009-11-16 14:58:17 +0100859/**
860 * snd_hda_codec_set_pincfg - Override a pin default configuration
861 * @codec: the HDA codec
862 * @nid: NID to set the pin config
863 * @cfg: the pin default config value
864 *
865 * Override a pin default configuration value in the cache.
866 * This value can be read by snd_hda_codec_get_pincfg() in a higher
867 * priority than the real hardware value.
868 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100869int snd_hda_codec_set_pincfg(struct hda_codec *codec,
870 hda_nid_t nid, unsigned int cfg)
871{
Takashi Iwai346ff702009-02-23 09:42:57 +0100872 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100873}
874EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
875
Takashi Iwaid5191e52009-11-16 14:58:17 +0100876/**
877 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
878 * @codec: the HDA codec
879 * @nid: NID to get the pin config
880 *
881 * Get the current pin config value of the given pin NID.
882 * If the pincfg value is cached or overridden via sysfs or driver,
883 * returns the cached value.
884 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100885unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
886{
887 struct hda_pincfg *pin;
888
Takashi Iwai3be14142009-02-20 14:11:16 +0100889#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100890 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100891 if (pin)
892 return pin->cfg;
893#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100894 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
895 if (pin)
896 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100897 pin = look_up_pincfg(codec, &codec->init_pins, nid);
898 if (pin)
899 return pin->cfg;
900 return 0;
901}
902EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
903
904/* restore all current pin configs */
905static void restore_pincfgs(struct hda_codec *codec)
906{
907 int i;
908 for (i = 0; i < codec->init_pins.used; i++) {
909 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
910 set_pincfg(codec, pin->nid,
911 snd_hda_codec_get_pincfg(codec, pin->nid));
912 }
913}
Takashi Iwai54d17402005-11-21 16:33:22 +0100914
Takashi Iwai92ee6162009-12-27 11:18:59 +0100915/**
916 * snd_hda_shutup_pins - Shut up all pins
917 * @codec: the HDA codec
918 *
919 * Clear all pin controls to shup up before suspend for avoiding click noise.
920 * The controls aren't cached so that they can be resumed properly.
921 */
922void snd_hda_shutup_pins(struct hda_codec *codec)
923{
924 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200925 /* don't shut up pins when unloading the driver; otherwise it breaks
926 * the default pin setup at the next load of the driver
927 */
928 if (codec->bus->shutdown)
929 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100930 for (i = 0; i < codec->init_pins.used; i++) {
931 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
932 /* use read here for syncing after issuing each verb */
933 snd_hda_codec_read(codec, pin->nid, 0,
934 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
935 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200936 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100937}
938EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
939
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200940/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
941static void restore_shutup_pins(struct hda_codec *codec)
942{
943 int i;
944 if (!codec->pins_shutup)
945 return;
946 if (codec->bus->shutdown)
947 return;
948 for (i = 0; i < codec->init_pins.used; i++) {
949 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
950 snd_hda_codec_write(codec, pin->nid, 0,
951 AC_VERB_SET_PIN_WIDGET_CONTROL,
952 pin->ctrl);
953 }
954 codec->pins_shutup = 0;
955}
956
Takashi Iwai01751f52007-08-10 16:59:39 +0200957static void init_hda_cache(struct hda_cache_rec *cache,
958 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200959static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200960
Takashi Iwai3be14142009-02-20 14:11:16 +0100961/* restore the initial pin cfgs and release all pincfg lists */
962static void restore_init_pincfgs(struct hda_codec *codec)
963{
Takashi Iwai346ff702009-02-23 09:42:57 +0100964 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100965 * so that only the values in init_pins are restored
966 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100967 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100968#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100969 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100970#endif
971 restore_pincfgs(codec);
972 snd_array_free(&codec->init_pins);
973}
974
Takashi Iwai54d17402005-11-21 16:33:22 +0100975/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200976 * audio-converter setup caches
977 */
978struct hda_cvt_setup {
979 hda_nid_t nid;
980 u8 stream_tag;
981 u8 channel_id;
982 u16 format_id;
983 unsigned char active; /* cvt is currently used */
984 unsigned char dirty; /* setups should be cleared */
985};
986
987/* get or create a cache entry for the given audio converter NID */
988static struct hda_cvt_setup *
989get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
990{
991 struct hda_cvt_setup *p;
992 int i;
993
994 for (i = 0; i < codec->cvt_setups.used; i++) {
995 p = snd_array_elem(&codec->cvt_setups, i);
996 if (p->nid == nid)
997 return p;
998 }
999 p = snd_array_new(&codec->cvt_setups);
1000 if (p)
1001 p->nid = nid;
1002 return p;
1003}
1004
1005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * codec destructor
1007 */
1008static void snd_hda_codec_free(struct hda_codec *codec)
1009{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001010 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001012 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001013#ifdef CONFIG_SND_HDA_POWER_SAVE
1014 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001015 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001016#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001018 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001019 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 codec->bus->caddr_tbl[codec->addr] = NULL;
1021 if (codec->patch_ops.free)
1022 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001023 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001024 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001025 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001026 kfree(codec->vendor_name);
1027 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001028 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001029 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree(codec);
1031}
1032
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001033static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1034 unsigned int power_state);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/**
1037 * snd_hda_codec_new - create a HDA codec
1038 * @bus: the bus to assign
1039 * @codec_addr: the codec address
1040 * @codecp: the pointer to store the generated codec
1041 *
1042 * Returns 0 if successful, or a negative error code.
1043 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001044int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1045 unsigned int codec_addr,
1046 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001049 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int err;
1051
Takashi Iwaida3cec32008-08-08 17:12:14 +02001052 if (snd_BUG_ON(!bus))
1053 return -EINVAL;
1054 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1055 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001058 snd_printk(KERN_ERR "hda_codec: "
1059 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EBUSY;
1061 }
1062
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001063 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (codec == NULL) {
1065 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1066 return -ENOMEM;
1067 }
1068
1069 codec->bus = bus;
1070 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001071 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001072 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001073 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001074 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001075 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1076 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001077 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001078 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001079 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001080 if (codec->bus->modelname) {
1081 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1082 if (!codec->modelname) {
1083 snd_hda_codec_free(codec);
1084 return -ENODEV;
1085 }
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Takashi Iwaicb53c622007-08-10 17:21:45 +02001088#ifdef CONFIG_SND_HDA_POWER_SAVE
1089 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1090 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1091 * the caller has to power down appropriatley after initialization
1092 * phase.
1093 */
1094 hda_keep_power_on(codec);
1095#endif
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 list_add_tail(&codec->list, &bus->codec_list);
1098 bus->caddr_tbl[codec_addr] = codec;
1099
Takashi Iwai0ba21762007-04-16 11:29:14 +02001100 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1101 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001102 if (codec->vendor_id == -1)
1103 /* read again, hopefully the access method was corrected
1104 * in the last read...
1105 */
1106 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1107 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001108 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1109 AC_PAR_SUBSYSTEM_ID);
1110 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1111 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001113 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001114 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001115 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001116 err = -ENODEV;
1117 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
Takashi Iwai3be14142009-02-20 14:11:16 +01001120 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1121 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001122 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001123 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001124 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001125 err = read_pin_defaults(codec);
1126 if (err < 0)
1127 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001128
Takashi Iwai0ba21762007-04-16 11:29:14 +02001129 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001130 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001131 codec->subsystem_id =
1132 snd_hda_codec_read(codec, nid, 0,
1133 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001134 }
1135
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001136 /* power-up all before initialization */
1137 hda_set_power_state(codec,
1138 codec->afg ? codec->afg : codec->mfg,
1139 AC_PWRST_D0);
1140
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001141 snd_hda_codec_proc_new(codec);
1142
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001143 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001144
1145 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1146 codec->subsystem_id, codec->revision_id);
1147 snd_component_add(codec->bus->card, component);
1148
1149 if (codecp)
1150 *codecp = codec;
1151 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001152
1153 error:
1154 snd_hda_codec_free(codec);
1155 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001157EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001158
Takashi Iwaid5191e52009-11-16 14:58:17 +01001159/**
1160 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1161 * @codec: the HDA codec
1162 *
1163 * Start parsing of the given codec tree and (re-)initialize the whole
1164 * patch instance.
1165 *
1166 * Returns 0 if successful or a negative error code.
1167 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001168int snd_hda_codec_configure(struct hda_codec *codec)
1169{
1170 int err;
1171
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001172 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001173 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001174 err = get_codec_name(codec);
1175 if (err < 0)
1176 return err;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Takashi Iwai82467612007-07-27 19:15:54 +02001179 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001181 goto patched;
1182 }
Takashi Iwai82467612007-07-27 19:15:54 +02001183 if (codec->preset && codec->preset->patch) {
1184 err = codec->preset->patch(codec);
1185 goto patched;
1186 }
1187
1188 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001189 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001190 if (err < 0)
1191 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001192
1193 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001194 if (!err && codec->patch_ops.unsol_event)
1195 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001196 /* audio codec should override the mixer name */
1197 if (!err && (codec->afg || !*codec->bus->card->mixername))
1198 snprintf(codec->bus->card->mixername,
1199 sizeof(codec->bus->card->mixername),
1200 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001203EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205/**
1206 * snd_hda_codec_setup_stream - set up the codec for streaming
1207 * @codec: the CODEC to set up
1208 * @nid: the NID to set up
1209 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1210 * @channel_id: channel id to pass, zero based.
1211 * @format: stream format.
1212 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001213void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1214 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int channel_id, int format)
1216{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001217 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001218 struct hda_cvt_setup *p;
1219 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001220 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001221 int i;
1222
Takashi Iwai0ba21762007-04-16 11:29:14 +02001223 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001224 return;
1225
Takashi Iwai0ba21762007-04-16 11:29:14 +02001226 snd_printdd("hda_codec_setup_stream: "
1227 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001229 p = get_hda_cvt_setup(codec, nid);
1230 if (!p)
1231 return;
1232 /* update the stream-id if changed */
1233 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1234 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1235 newval = (stream_tag << 4) | channel_id;
1236 if (oldval != newval)
1237 snd_hda_codec_write(codec, nid, 0,
1238 AC_VERB_SET_CHANNEL_STREAMID,
1239 newval);
1240 p->stream_tag = stream_tag;
1241 p->channel_id = channel_id;
1242 }
1243 /* update the format-id if changed */
1244 if (p->format_id != format) {
1245 oldval = snd_hda_codec_read(codec, nid, 0,
1246 AC_VERB_GET_STREAM_FORMAT, 0);
1247 if (oldval != format) {
1248 msleep(1);
1249 snd_hda_codec_write(codec, nid, 0,
1250 AC_VERB_SET_STREAM_FORMAT,
1251 format);
1252 }
1253 p->format_id = format;
1254 }
1255 p->active = 1;
1256 p->dirty = 0;
1257
1258 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001259 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001260 list_for_each_entry(c, &codec->bus->codec_list, list) {
1261 for (i = 0; i < c->cvt_setups.used; i++) {
1262 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001263 if (!p->active && p->stream_tag == stream_tag &&
1264 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001265 p->dirty = 1;
1266 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001269EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Takashi Iwaif0cea792010-08-13 11:56:53 +02001271static void really_cleanup_stream(struct hda_codec *codec,
1272 struct hda_cvt_setup *q);
1273
Takashi Iwaid5191e52009-11-16 14:58:17 +01001274/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001275 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001276 * @codec: the CODEC to clean up
1277 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001278 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001279 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001280void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1281 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001282{
Takashi Iwaieb541332010-08-06 13:48:11 +02001283 struct hda_cvt_setup *p;
1284
Takashi Iwai888afa12008-03-18 09:57:50 +01001285 if (!nid)
1286 return;
1287
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001288 if (codec->no_sticky_stream)
1289 do_now = 1;
1290
Takashi Iwai888afa12008-03-18 09:57:50 +01001291 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001292 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001293 if (p) {
1294 /* here we just clear the active flag when do_now isn't set;
1295 * actual clean-ups will be done later in
1296 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1297 */
1298 if (do_now)
1299 really_cleanup_stream(codec, p);
1300 else
1301 p->active = 0;
1302 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001303}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001304EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001305
Takashi Iwaieb541332010-08-06 13:48:11 +02001306static void really_cleanup_stream(struct hda_codec *codec,
1307 struct hda_cvt_setup *q)
1308{
1309 hda_nid_t nid = q->nid;
1310 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1311 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1312 memset(q, 0, sizeof(*q));
1313 q->nid = nid;
1314}
1315
1316/* clean up the all conflicting obsolete streams */
1317static void purify_inactive_streams(struct hda_codec *codec)
1318{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001319 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001320 int i;
1321
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001322 list_for_each_entry(c, &codec->bus->codec_list, list) {
1323 for (i = 0; i < c->cvt_setups.used; i++) {
1324 struct hda_cvt_setup *p;
1325 p = snd_array_elem(&c->cvt_setups, i);
1326 if (p->dirty)
1327 really_cleanup_stream(c, p);
1328 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001329 }
1330}
1331
1332/* clean up all streams; called from suspend */
1333static void hda_cleanup_all_streams(struct hda_codec *codec)
1334{
1335 int i;
1336
1337 for (i = 0; i < codec->cvt_setups.used; i++) {
1338 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1339 if (p->stream_tag)
1340 really_cleanup_stream(codec, p);
1341 }
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
1345 * amp access functions
1346 */
1347
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001348/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001349#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001350#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001351#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1352#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001354#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001357static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001358 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Takashi Iwai01751f52007-08-10 16:59:39 +02001360 memset(cache, 0, sizeof(*cache));
1361 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001362 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001363}
1364
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001365static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001366{
Takashi Iwai603c4012008-07-30 15:01:44 +02001367 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001371static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Takashi Iwai01751f52007-08-10 16:59:39 +02001373 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1374 u16 cur = cache->hash[idx];
1375 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001378 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (info->key == key)
1380 return info;
1381 cur = info->next;
1382 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001383 return NULL;
1384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001386/* query the hash. allocate an entry if not found. */
1387static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1388 u32 key)
1389{
1390 struct hda_cache_head *info = get_hash(cache, key);
1391 if (!info) {
1392 u16 idx, cur;
1393 /* add a new hash entry */
1394 info = snd_array_new(&cache->buf);
1395 if (!info)
1396 return NULL;
1397 cur = snd_array_index(&cache->buf, info);
1398 info->key = key;
1399 info->val = 0;
1400 idx = key % (u16)ARRAY_SIZE(cache->hash);
1401 info->next = cache->hash[idx];
1402 cache->hash[idx] = cur;
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return info;
1405}
1406
Takashi Iwai01751f52007-08-10 16:59:39 +02001407/* query and allocate an amp hash entry */
1408static inline struct hda_amp_info *
1409get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1410{
1411 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1412}
1413
Takashi Iwaid5191e52009-11-16 14:58:17 +01001414/**
1415 * query_amp_caps - query AMP capabilities
1416 * @codec: the HD-auio codec
1417 * @nid: the NID to query
1418 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1419 *
1420 * Query AMP capabilities for the given widget and direction.
1421 * Returns the obtained capability bits.
1422 *
1423 * When cap bits have been already read, this doesn't read again but
1424 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001426u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001428 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Takashi Iwai0ba21762007-04-16 11:29:14 +02001430 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1431 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001433 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001434 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001436 info->amp_caps = snd_hda_param_read(codec, nid,
1437 direction == HDA_OUTPUT ?
1438 AC_PAR_AMP_OUT_CAP :
1439 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001440 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001441 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443 return info->amp_caps;
1444}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001445EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Takashi Iwaid5191e52009-11-16 14:58:17 +01001447/**
1448 * snd_hda_override_amp_caps - Override the AMP capabilities
1449 * @codec: the CODEC to clean up
1450 * @nid: the NID to clean up
1451 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1452 * @caps: the capability bits to set
1453 *
1454 * Override the cached AMP caps bits value by the given one.
1455 * This function is useful if the driver needs to adjust the AMP ranges,
1456 * e.g. limit to 0dB, etc.
1457 *
1458 * Returns zero if successful or a negative error code.
1459 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001460int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1461 unsigned int caps)
1462{
1463 struct hda_amp_info *info;
1464
1465 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1466 if (!info)
1467 return -EINVAL;
1468 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001469 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001470 return 0;
1471}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001472EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001473
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001474static unsigned int
1475query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1476 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001477{
1478 struct hda_amp_info *info;
1479
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001480 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001481 if (!info)
1482 return 0;
1483 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001484 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001485 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001486 }
1487 return info->amp_caps;
1488}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001489
1490static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1491{
1492 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1493}
1494
Takashi Iwaid5191e52009-11-16 14:58:17 +01001495/**
1496 * snd_hda_query_pin_caps - Query PIN capabilities
1497 * @codec: the HD-auio codec
1498 * @nid: the NID to query
1499 *
1500 * Query PIN capabilities for the given widget.
1501 * Returns the obtained capability bits.
1502 *
1503 * When cap bits have been already read, this doesn't read again but
1504 * returns the cached value.
1505 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001506u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1507{
1508 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1509 read_pin_cap);
1510}
Takashi Iwai1327a322009-03-23 13:07:47 +01001511EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1512
Wu Fengguang864f92b2009-11-18 12:38:02 +08001513/**
1514 * snd_hda_pin_sense - execute pin sense measurement
1515 * @codec: the CODEC to sense
1516 * @nid: the pin NID to sense
1517 *
1518 * Execute necessary pin sense measurement and return its Presence Detect,
1519 * Impedance, ELD Valid etc. status bits.
1520 */
1521u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1522{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001523 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001524
Takashi Iwai729d55b2009-12-25 22:49:01 +01001525 if (!codec->no_trigger_sense) {
1526 pincap = snd_hda_query_pin_caps(codec, nid);
1527 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001528 snd_hda_codec_read(codec, nid, 0,
1529 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001530 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001531 return snd_hda_codec_read(codec, nid, 0,
1532 AC_VERB_GET_PIN_SENSE, 0);
1533}
1534EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1535
1536/**
1537 * snd_hda_jack_detect - query pin Presence Detect status
1538 * @codec: the CODEC to sense
1539 * @nid: the pin NID to sense
1540 *
1541 * Query and return the pin's Presence Detect status.
1542 */
1543int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1544{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001545 u32 sense = snd_hda_pin_sense(codec, nid);
1546 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001547}
1548EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001552 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001554static unsigned int get_vol_mute(struct hda_codec *codec,
1555 struct hda_amp_info *info, hda_nid_t nid,
1556 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 u32 val, parm;
1559
Takashi Iwai01751f52007-08-10 16:59:39 +02001560 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001561 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1564 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1565 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001566 val = snd_hda_codec_read(codec, nid, 0,
1567 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001569 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001570 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001574 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001576static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001577 hda_nid_t nid, int ch, int direction, int index,
1578 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 u32 parm;
1581
1582 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1583 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1584 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1585 parm |= val;
1586 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001587 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Takashi Iwaid5191e52009-11-16 14:58:17 +01001590/**
1591 * snd_hda_codec_amp_read - Read AMP value
1592 * @codec: HD-audio codec
1593 * @nid: NID to read the AMP value
1594 * @ch: channel (left=0 or right=1)
1595 * @direction: #HDA_INPUT or #HDA_OUTPUT
1596 * @index: the index value (only for input direction)
1597 *
1598 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
Takashi Iwai834be882006-03-01 14:16:17 +01001600int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1601 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001603 struct hda_amp_info *info;
1604 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1605 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001607 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001609EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwaid5191e52009-11-16 14:58:17 +01001611/**
1612 * snd_hda_codec_amp_update - update the AMP value
1613 * @codec: HD-audio codec
1614 * @nid: NID to read the AMP value
1615 * @ch: channel (left=0 or right=1)
1616 * @direction: #HDA_INPUT or #HDA_OUTPUT
1617 * @idx: the index value (only for input direction)
1618 * @mask: bit mask to set
1619 * @val: the bits value to set
1620 *
1621 * Update the AMP value with a bit mask.
1622 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001623 */
Takashi Iwai834be882006-03-01 14:16:17 +01001624int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1625 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001627 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001628
Takashi Iwai0ba21762007-04-16 11:29:14 +02001629 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1630 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001632 if (snd_BUG_ON(mask & ~0xff))
1633 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001634 val &= mask;
1635 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001636 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001638 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return 1;
1640}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001641EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Takashi Iwaid5191e52009-11-16 14:58:17 +01001643/**
1644 * snd_hda_codec_amp_stereo - update the AMP stereo values
1645 * @codec: HD-audio codec
1646 * @nid: NID to read the AMP value
1647 * @direction: #HDA_INPUT or #HDA_OUTPUT
1648 * @idx: the index value (only for input direction)
1649 * @mask: bit mask to set
1650 * @val: the bits value to set
1651 *
1652 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1653 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001654 */
1655int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1656 int direction, int idx, int mask, int val)
1657{
1658 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001659
1660 if (snd_BUG_ON(mask & ~0xff))
1661 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001662 for (ch = 0; ch < 2; ch++)
1663 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1664 idx, mask, val);
1665 return ret;
1666}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001667EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001668
Takashi Iwaicb53c622007-08-10 17:21:45 +02001669#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001670/**
1671 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1672 * @codec: HD-audio codec
1673 *
1674 * Resume the all amp commands from the cache.
1675 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001676void snd_hda_codec_resume_amp(struct hda_codec *codec)
1677{
Takashi Iwai603c4012008-07-30 15:01:44 +02001678 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001679 int i;
1680
Takashi Iwai603c4012008-07-30 15:01:44 +02001681 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001682 u32 key = buffer->head.key;
1683 hda_nid_t nid;
1684 unsigned int idx, dir, ch;
1685 if (!key)
1686 continue;
1687 nid = key & 0xff;
1688 idx = (key >> 16) & 0xff;
1689 dir = (key >> 24) & 0xff;
1690 for (ch = 0; ch < 2; ch++) {
1691 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1692 continue;
1693 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1694 buffer->vol[ch]);
1695 }
1696 }
1697}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001698EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001699#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001701static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1702 unsigned int ofs)
1703{
1704 u32 caps = query_amp_caps(codec, nid, dir);
1705 /* get num steps */
1706 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1707 if (ofs < caps)
1708 caps -= ofs;
1709 return caps;
1710}
1711
Takashi Iwaid5191e52009-11-16 14:58:17 +01001712/**
1713 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1714 *
1715 * The control element is supposed to have the private_value field
1716 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1717 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001718int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1722 u16 nid = get_amp_nid(kcontrol);
1723 u8 chs = get_amp_channels(kcontrol);
1724 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001725 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001727 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1728 uinfo->count = chs == 3 ? 2 : 1;
1729 uinfo->value.integer.min = 0;
1730 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1731 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001732 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001733 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1734 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return -EINVAL;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return 0;
1738}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001739EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001741
1742static inline unsigned int
1743read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1744 int ch, int dir, int idx, unsigned int ofs)
1745{
1746 unsigned int val;
1747 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1748 val &= HDA_AMP_VOLMASK;
1749 if (val >= ofs)
1750 val -= ofs;
1751 else
1752 val = 0;
1753 return val;
1754}
1755
1756static inline int
1757update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1758 int ch, int dir, int idx, unsigned int ofs,
1759 unsigned int val)
1760{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001761 unsigned int maxval;
1762
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001763 if (val > 0)
1764 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001765 /* ofs = 0: raw max value */
1766 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001767 if (val > maxval)
1768 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001769 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1770 HDA_AMP_VOLMASK, val);
1771}
1772
Takashi Iwaid5191e52009-11-16 14:58:17 +01001773/**
1774 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1775 *
1776 * The control element is supposed to have the private_value field
1777 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1778 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001779int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1783 hda_nid_t nid = get_amp_nid(kcontrol);
1784 int chs = get_amp_channels(kcontrol);
1785 int dir = get_amp_direction(kcontrol);
1786 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001787 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 long *valp = ucontrol->value.integer.value;
1789
1790 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001791 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001793 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
1795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001796EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Takashi Iwaid5191e52009-11-16 14:58:17 +01001798/**
1799 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1800 *
1801 * The control element is supposed to have the private_value field
1802 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1803 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 hda_nid_t nid = get_amp_nid(kcontrol);
1809 int chs = get_amp_channels(kcontrol);
1810 int dir = get_amp_direction(kcontrol);
1811 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001812 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 long *valp = ucontrol->value.integer.value;
1814 int change = 0;
1815
Takashi Iwaicb53c622007-08-10 17:21:45 +02001816 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001817 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001818 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001819 valp++;
1820 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001821 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001822 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001823 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return change;
1825}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001826EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Takashi Iwaid5191e52009-11-16 14:58:17 +01001828/**
1829 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1830 *
1831 * The control element is supposed to have the private_value field
1832 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1833 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001834int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1835 unsigned int size, unsigned int __user *_tlv)
1836{
1837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1838 hda_nid_t nid = get_amp_nid(kcontrol);
1839 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001840 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001841 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001842 u32 caps, val1, val2;
1843
1844 if (size < 4 * sizeof(unsigned int))
1845 return -ENOMEM;
1846 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001847 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1848 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001849 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001850 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001851 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001852 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001853 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001854 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1855 return -EFAULT;
1856 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1857 return -EFAULT;
1858 if (put_user(val1, _tlv + 2))
1859 return -EFAULT;
1860 if (put_user(val2, _tlv + 3))
1861 return -EFAULT;
1862 return 0;
1863}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001864EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001865
Takashi Iwaid5191e52009-11-16 14:58:17 +01001866/**
1867 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1868 * @codec: HD-audio codec
1869 * @nid: NID of a reference widget
1870 * @dir: #HDA_INPUT or #HDA_OUTPUT
1871 * @tlv: TLV data to be stored, at least 4 elements
1872 *
1873 * Set (static) TLV data for a virtual master volume using the AMP caps
1874 * obtained from the reference NID.
1875 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001876 */
1877void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1878 unsigned int *tlv)
1879{
1880 u32 caps;
1881 int nums, step;
1882
1883 caps = query_amp_caps(codec, nid, dir);
1884 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1885 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1886 step = (step + 1) * 25;
1887 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1888 tlv[1] = 2 * sizeof(unsigned int);
1889 tlv[2] = -nums * step;
1890 tlv[3] = step;
1891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001892EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001893
1894/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001895static struct snd_kcontrol *
1896_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1897 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001898{
1899 struct snd_ctl_elem_id id;
1900 memset(&id, 0, sizeof(id));
1901 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001902 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001903 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1904 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001905 strcpy(id.name, name);
1906 return snd_ctl_find_id(codec->bus->card, &id);
1907}
1908
Takashi Iwaid5191e52009-11-16 14:58:17 +01001909/**
1910 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1911 * @codec: HD-audio codec
1912 * @name: ctl id name string
1913 *
1914 * Get the control element with the given id string and IFACE_MIXER.
1915 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001916struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1917 const char *name)
1918{
1919 return _snd_hda_find_mixer_ctl(codec, name, 0);
1920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001921EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001922
Takashi Iwai1afe2062010-12-23 10:17:52 +01001923static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1924{
1925 int idx;
1926 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1927 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1928 return idx;
1929 }
1930 return -EBUSY;
1931}
1932
Takashi Iwaid5191e52009-11-16 14:58:17 +01001933/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001934 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001935 * @codec: HD-audio codec
1936 * @nid: corresponding NID (optional)
1937 * @kctl: the control element to assign
1938 *
1939 * Add the given control element to an array inside the codec instance.
1940 * All control elements belonging to a codec are supposed to be added
1941 * by this function so that a proper clean-up works at the free or
1942 * reconfiguration time.
1943 *
1944 * If non-zero @nid is passed, the NID is assigned to the control element.
1945 * The assignment is shown in the codec proc file.
1946 *
1947 * snd_hda_ctl_add() checks the control subdev id field whether
1948 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001949 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1950 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001951 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001952int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1953 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001954{
1955 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001956 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001957 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001958
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001959 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001960 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001961 if (nid == 0)
1962 nid = get_amp_nid_(kctl->private_value);
1963 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001964 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1965 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001966 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001967 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001968 err = snd_ctl_add(codec->bus->card, kctl);
1969 if (err < 0)
1970 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001971 item = snd_array_new(&codec->mixers);
1972 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001973 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001974 item->kctl = kctl;
1975 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001976 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001977 return 0;
1978}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001979EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001982 * snd_hda_add_nid - Assign a NID to a control element
1983 * @codec: HD-audio codec
1984 * @nid: corresponding NID (optional)
1985 * @kctl: the control element to assign
1986 * @index: index to kctl
1987 *
1988 * Add the given control element to an array inside the codec instance.
1989 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1990 * NID:KCTL mapping - for example "Capture Source" selector.
1991 */
1992int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1993 unsigned int index, hda_nid_t nid)
1994{
1995 struct hda_nid_item *item;
1996
1997 if (nid > 0) {
1998 item = snd_array_new(&codec->nids);
1999 if (!item)
2000 return -ENOMEM;
2001 item->kctl = kctl;
2002 item->index = index;
2003 item->nid = nid;
2004 return 0;
2005 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002006 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2007 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002008 return -EINVAL;
2009}
2010EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2011
2012/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002013 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2014 * @codec: HD-audio codec
2015 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002016void snd_hda_ctls_clear(struct hda_codec *codec)
2017{
2018 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002019 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002020 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002021 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002022 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002023 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002024}
2025
Takashi Iwaia65d6292009-02-23 16:57:04 +01002026/* pseudo device locking
2027 * toggle card->shutdown to allow/disallow the device access (as a hack)
2028 */
2029static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002030{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002031 spin_lock(&card->files_lock);
2032 if (card->shutdown) {
2033 spin_unlock(&card->files_lock);
2034 return -EINVAL;
2035 }
2036 card->shutdown = 1;
2037 spin_unlock(&card->files_lock);
2038 return 0;
2039}
2040
2041static void hda_unlock_devices(struct snd_card *card)
2042{
2043 spin_lock(&card->files_lock);
2044 card->shutdown = 0;
2045 spin_unlock(&card->files_lock);
2046}
2047
Takashi Iwaid5191e52009-11-16 14:58:17 +01002048/**
2049 * snd_hda_codec_reset - Clear all objects assigned to the codec
2050 * @codec: HD-audio codec
2051 *
2052 * This frees the all PCM and control elements assigned to the codec, and
2053 * clears the caches and restores the pin default configurations.
2054 *
2055 * When a device is being used, it returns -EBSY. If successfully freed,
2056 * returns zero.
2057 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002058int snd_hda_codec_reset(struct hda_codec *codec)
2059{
2060 struct snd_card *card = codec->bus->card;
2061 int i, pcm;
2062
2063 if (hda_lock_devices(card) < 0)
2064 return -EBUSY;
2065 /* check whether the codec isn't used by any mixer or PCM streams */
2066 if (!list_empty(&card->ctl_files)) {
2067 hda_unlock_devices(card);
2068 return -EBUSY;
2069 }
2070 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2071 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2072 if (!cpcm->pcm)
2073 continue;
2074 if (cpcm->pcm->streams[0].substream_opened ||
2075 cpcm->pcm->streams[1].substream_opened) {
2076 hda_unlock_devices(card);
2077 return -EBUSY;
2078 }
2079 }
2080
2081 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002082
2083#ifdef CONFIG_SND_HDA_POWER_SAVE
2084 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002085 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002086#endif
2087 snd_hda_ctls_clear(codec);
2088 /* relase PCMs */
2089 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002090 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002091 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002092 clear_bit(codec->pcm_info[i].device,
2093 codec->bus->pcm_dev_bits);
2094 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002095 }
2096 if (codec->patch_ops.free)
2097 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002098 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002099 codec->spec = NULL;
2100 free_hda_cache(&codec->amp_cache);
2101 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002102 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2103 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002104 /* free only driver_pins so that init_pins + user_pins are restored */
2105 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002106 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002107 codec->num_pcms = 0;
2108 codec->pcm_info = NULL;
2109 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002110 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2111 codec->slave_dig_outs = NULL;
2112 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002113 module_put(codec->owner);
2114 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002115
2116 /* allow device access again */
2117 hda_unlock_devices(card);
2118 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002119}
2120
Takashi Iwaid5191e52009-11-16 14:58:17 +01002121/**
2122 * snd_hda_add_vmaster - create a virtual master control and add slaves
2123 * @codec: HD-audio codec
2124 * @name: vmaster control name
2125 * @tlv: TLV data (optional)
2126 * @slaves: slave control names (optional)
2127 *
2128 * Create a virtual master control with the given name. The TLV data
2129 * must be either NULL or a valid data.
2130 *
2131 * @slaves is a NULL-terminated array of strings, each of which is a
2132 * slave control name. All controls with these names are assigned to
2133 * the new virtual master control.
2134 *
2135 * This function returns zero if successful or a negative error code.
2136 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002137int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002138 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002139{
2140 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002141 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002142 int err;
2143
Takashi Iwai2f085542008-02-22 18:43:50 +01002144 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2145 ;
2146 if (!*s) {
2147 snd_printdd("No slave found for %s\n", name);
2148 return 0;
2149 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002150 kctl = snd_ctl_make_virtual_master(name, tlv);
2151 if (!kctl)
2152 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002153 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002154 if (err < 0)
2155 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002156
Takashi Iwai2134ea42008-01-10 16:53:55 +01002157 for (s = slaves; *s; s++) {
2158 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002159 int i = 0;
2160 for (;;) {
2161 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2162 if (!sctl) {
2163 if (!i)
2164 snd_printdd("Cannot find slave %s, "
2165 "skipped\n", *s);
2166 break;
2167 }
2168 err = snd_ctl_add_slave(kctl, sctl);
2169 if (err < 0)
2170 return err;
2171 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002172 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002173 }
2174 return 0;
2175}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002176EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002177
Takashi Iwaid5191e52009-11-16 14:58:17 +01002178/**
2179 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2180 *
2181 * The control element is supposed to have the private_value field
2182 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2183 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002184int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 int chs = get_amp_channels(kcontrol);
2188
2189 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2190 uinfo->count = chs == 3 ? 2 : 1;
2191 uinfo->value.integer.min = 0;
2192 uinfo->value.integer.max = 1;
2193 return 0;
2194}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002195EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197/**
2198 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2199 *
2200 * The control element is supposed to have the private_value field
2201 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2202 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002203int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2207 hda_nid_t nid = get_amp_nid(kcontrol);
2208 int chs = get_amp_channels(kcontrol);
2209 int dir = get_amp_direction(kcontrol);
2210 int idx = get_amp_index(kcontrol);
2211 long *valp = ucontrol->value.integer.value;
2212
2213 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002214 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002215 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002217 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002218 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return 0;
2220}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002221EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Takashi Iwaid5191e52009-11-16 14:58:17 +01002223/**
2224 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2225 *
2226 * The control element is supposed to have the private_value field
2227 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2228 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002229int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2230 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2233 hda_nid_t nid = get_amp_nid(kcontrol);
2234 int chs = get_amp_channels(kcontrol);
2235 int dir = get_amp_direction(kcontrol);
2236 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 long *valp = ucontrol->value.integer.value;
2238 int change = 0;
2239
Takashi Iwaicb53c622007-08-10 17:21:45 +02002240 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002241 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002242 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002243 HDA_AMP_MUTE,
2244 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002245 valp++;
2246 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002247 if (chs & 2)
2248 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002249 HDA_AMP_MUTE,
2250 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002251 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002252 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return change;
2254}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002255EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Takashi Iwai67d634c2009-11-16 15:35:59 +01002257#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002258/**
2259 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2260 *
2261 * This function calls snd_hda_enable_beep_device(), which behaves differently
2262 * depending on beep_mode option.
2263 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002264int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2266{
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 long *valp = ucontrol->value.integer.value;
2269
2270 snd_hda_enable_beep_device(codec, *valp);
2271 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2272}
2273EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002274#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276/*
Takashi Iwai985be542005-11-02 18:26:49 +01002277 * bound volume controls
2278 *
2279 * bind multiple volumes (# indices, from 0)
2280 */
2281
2282#define AMP_VAL_IDX_SHIFT 19
2283#define AMP_VAL_IDX_MASK (0x0f<<19)
2284
Takashi Iwaid5191e52009-11-16 14:58:17 +01002285/**
2286 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2287 *
2288 * The control element is supposed to have the private_value field
2289 * set up via HDA_BIND_MUTE*() macros.
2290 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002291int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002293{
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295 unsigned long pval;
2296 int err;
2297
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002298 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002299 pval = kcontrol->private_value;
2300 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2301 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2302 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002303 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002304 return err;
2305}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002306EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002307
Takashi Iwaid5191e52009-11-16 14:58:17 +01002308/**
2309 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2310 *
2311 * The control element is supposed to have the private_value field
2312 * set up via HDA_BIND_MUTE*() macros.
2313 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002316{
2317 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2318 unsigned long pval;
2319 int i, indices, err = 0, change = 0;
2320
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002321 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002322 pval = kcontrol->private_value;
2323 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2324 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002325 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2326 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002327 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2328 if (err < 0)
2329 break;
2330 change |= err;
2331 }
2332 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002333 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002334 return err < 0 ? err : change;
2335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002336EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002337
Takashi Iwaid5191e52009-11-16 14:58:17 +01002338/**
2339 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2340 *
2341 * The control element is supposed to have the private_value field
2342 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002343 */
2344int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_info *uinfo)
2346{
2347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2348 struct hda_bind_ctls *c;
2349 int err;
2350
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002351 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002352 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002353 kcontrol->private_value = *c->values;
2354 err = c->ops->info(kcontrol, uinfo);
2355 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002356 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002357 return err;
2358}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002359EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002360
Takashi Iwaid5191e52009-11-16 14:58:17 +01002361/**
2362 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2363 *
2364 * The control element is supposed to have the private_value field
2365 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2366 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002367int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
2369{
2370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2371 struct hda_bind_ctls *c;
2372 int err;
2373
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002374 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002375 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002376 kcontrol->private_value = *c->values;
2377 err = c->ops->get(kcontrol, ucontrol);
2378 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002379 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002380 return err;
2381}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002382EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002383
Takashi Iwaid5191e52009-11-16 14:58:17 +01002384/**
2385 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2386 *
2387 * The control element is supposed to have the private_value field
2388 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2389 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002390int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 struct hda_bind_ctls *c;
2395 unsigned long *vals;
2396 int err = 0, change = 0;
2397
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002398 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002399 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002400 for (vals = c->values; *vals; vals++) {
2401 kcontrol->private_value = *vals;
2402 err = c->ops->put(kcontrol, ucontrol);
2403 if (err < 0)
2404 break;
2405 change |= err;
2406 }
2407 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002408 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002409 return err < 0 ? err : change;
2410}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002411EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002412
Takashi Iwaid5191e52009-11-16 14:58:17 +01002413/**
2414 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2415 *
2416 * The control element is supposed to have the private_value field
2417 * set up via HDA_BIND_VOL() macro.
2418 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002419int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2420 unsigned int size, unsigned int __user *tlv)
2421{
2422 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2423 struct hda_bind_ctls *c;
2424 int err;
2425
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002426 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002427 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002428 kcontrol->private_value = *c->values;
2429 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2430 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002431 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002432 return err;
2433}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002434EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002435
2436struct hda_ctl_ops snd_hda_bind_vol = {
2437 .info = snd_hda_mixer_amp_volume_info,
2438 .get = snd_hda_mixer_amp_volume_get,
2439 .put = snd_hda_mixer_amp_volume_put,
2440 .tlv = snd_hda_mixer_amp_tlv
2441};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002442EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002443
2444struct hda_ctl_ops snd_hda_bind_sw = {
2445 .info = snd_hda_mixer_amp_switch_info,
2446 .get = snd_hda_mixer_amp_switch_get,
2447 .put = snd_hda_mixer_amp_switch_put,
2448 .tlv = snd_hda_mixer_amp_tlv
2449};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002450EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002451
2452/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * SPDIF out controls
2454 */
2455
Takashi Iwai0ba21762007-04-16 11:29:14 +02002456static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2460 uinfo->count = 1;
2461 return 0;
2462}
2463
Takashi Iwai0ba21762007-04-16 11:29:14 +02002464static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2468 IEC958_AES0_NONAUDIO |
2469 IEC958_AES0_CON_EMPHASIS_5015 |
2470 IEC958_AES0_CON_NOT_COPYRIGHT;
2471 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2472 IEC958_AES1_CON_ORIGINAL;
2473 return 0;
2474}
2475
Takashi Iwai0ba21762007-04-16 11:29:14 +02002476static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2480 IEC958_AES0_NONAUDIO |
2481 IEC958_AES0_PRO_EMPHASIS_5015;
2482 return 0;
2483}
2484
Takashi Iwai0ba21762007-04-16 11:29:14 +02002485static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2489
2490 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2491 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2492 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2493 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2494
2495 return 0;
2496}
2497
2498/* convert from SPDIF status bits to HDA SPDIF bits
2499 * bit 0 (DigEn) is always set zero (to be filled later)
2500 */
2501static unsigned short convert_from_spdif_status(unsigned int sbits)
2502{
2503 unsigned short val = 0;
2504
2505 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002508 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002510 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2511 IEC958_AES0_PRO_EMPHASIS_5015)
2512 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002514 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2515 IEC958_AES0_CON_EMPHASIS_5015)
2516 val |= AC_DIG1_EMPHASIS;
2517 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2518 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002520 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2522 }
2523 return val;
2524}
2525
2526/* convert to SPDIF status bits from HDA SPDIF bits
2527 */
2528static unsigned int convert_to_spdif_status(unsigned short val)
2529{
2530 unsigned int sbits = 0;
2531
Takashi Iwai0ba21762007-04-16 11:29:14 +02002532 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002534 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 sbits |= IEC958_AES0_PROFESSIONAL;
2536 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002537 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2539 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002540 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002542 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002544 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2546 sbits |= val & (0x7f << 8);
2547 }
2548 return sbits;
2549}
2550
Takashi Iwai2f728532008-09-25 16:32:41 +02002551/* set digital convert verbs both for the given NID and its slaves */
2552static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2553 int verb, int val)
2554{
2555 hda_nid_t *d;
2556
Takashi Iwai9e976972008-11-25 08:17:20 +01002557 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002558 d = codec->slave_dig_outs;
2559 if (!d)
2560 return;
2561 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002562 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002563}
2564
2565static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2566 int dig1, int dig2)
2567{
2568 if (dig1 != -1)
2569 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2570 if (dig2 != -1)
2571 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2572}
2573
Takashi Iwai0ba21762007-04-16 11:29:14 +02002574static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2575 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2578 hda_nid_t nid = kcontrol->private_value;
2579 unsigned short val;
2580 int change;
2581
Ingo Molnar62932df2006-01-16 16:34:20 +01002582 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 codec->spdif_status = ucontrol->value.iec958.status[0] |
2584 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2585 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2586 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2587 val = convert_from_spdif_status(codec->spdif_status);
2588 val |= codec->spdif_ctls & 1;
2589 change = codec->spdif_ctls != val;
2590 codec->spdif_ctls = val;
2591
Takashi Iwai2f728532008-09-25 16:32:41 +02002592 if (change)
2593 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Ingo Molnar62932df2006-01-16 16:34:20 +01002595 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return change;
2597}
2598
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002599#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608}
2609
Takashi Iwai0ba21762007-04-16 11:29:14 +02002610static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2614 hda_nid_t nid = kcontrol->private_value;
2615 unsigned short val;
2616 int change;
2617
Ingo Molnar62932df2006-01-16 16:34:20 +01002618 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002619 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002621 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002623 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002625 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002626 /* unmute amp switch (if any) */
2627 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002628 (val & AC_DIG1_ENABLE))
2629 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2630 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002632 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return change;
2634}
2635
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002636static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 {
2638 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002640 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 .info = snd_hda_spdif_mask_info,
2642 .get = snd_hda_spdif_cmask_get,
2643 },
2644 {
2645 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002647 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 .info = snd_hda_spdif_mask_info,
2649 .get = snd_hda_spdif_pmask_get,
2650 },
2651 {
2652 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002653 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 .info = snd_hda_spdif_mask_info,
2655 .get = snd_hda_spdif_default_get,
2656 .put = snd_hda_spdif_default_put,
2657 },
2658 {
2659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002660 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 .info = snd_hda_spdif_out_switch_info,
2662 .get = snd_hda_spdif_out_switch_get,
2663 .put = snd_hda_spdif_out_switch_put,
2664 },
2665 { } /* end */
2666};
2667
2668/**
2669 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2670 * @codec: the HDA codec
2671 * @nid: audio out widget NID
2672 *
2673 * Creates controls related with the SPDIF output.
2674 * Called from each patch supporting the SPDIF out.
2675 *
2676 * Returns 0 if successful, or a negative error code.
2677 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002678int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002681 struct snd_kcontrol *kctl;
2682 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002683 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Takashi Iwai1afe2062010-12-23 10:17:52 +01002685 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2686 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002687 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2688 return -EBUSY;
2689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2691 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002692 if (!kctl)
2693 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002694 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002696 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return err;
2699 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002700 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002701 snd_hda_codec_read(codec, nid, 0,
2702 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2704 return 0;
2705}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002706EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002709 * SPDIF sharing with analog output
2710 */
2711static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2713{
2714 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2715 ucontrol->value.integer.value[0] = mout->share_spdif;
2716 return 0;
2717}
2718
2719static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
2721{
2722 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2723 mout->share_spdif = !!ucontrol->value.integer.value[0];
2724 return 0;
2725}
2726
2727static struct snd_kcontrol_new spdif_share_sw = {
2728 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2729 .name = "IEC958 Default PCM Playback Switch",
2730 .info = snd_ctl_boolean_mono_info,
2731 .get = spdif_share_sw_get,
2732 .put = spdif_share_sw_put,
2733};
2734
Takashi Iwaid5191e52009-11-16 14:58:17 +01002735/**
2736 * snd_hda_create_spdif_share_sw - create Default PCM switch
2737 * @codec: the HDA codec
2738 * @mout: multi-out instance
2739 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002740int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2741 struct hda_multi_out *mout)
2742{
2743 if (!mout->dig_out_nid)
2744 return 0;
2745 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002746 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2747 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002748}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002749EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002750
2751/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 * SPDIF input
2753 */
2754
2755#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2756
Takashi Iwai0ba21762007-04-16 11:29:14 +02002757static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761
2762 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2763 return 0;
2764}
2765
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 hda_nid_t nid = kcontrol->private_value;
2771 unsigned int val = !!ucontrol->value.integer.value[0];
2772 int change;
2773
Ingo Molnar62932df2006-01-16 16:34:20 +01002774 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002776 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002778 snd_hda_codec_write_cache(codec, nid, 0,
2779 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002781 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return change;
2783}
2784
Takashi Iwai0ba21762007-04-16 11:29:14 +02002785static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2789 hda_nid_t nid = kcontrol->private_value;
2790 unsigned short val;
2791 unsigned int sbits;
2792
Andrew Paprocki3982d172007-12-19 12:13:44 +01002793 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 sbits = convert_to_spdif_status(val);
2795 ucontrol->value.iec958.status[0] = sbits;
2796 ucontrol->value.iec958.status[1] = sbits >> 8;
2797 ucontrol->value.iec958.status[2] = sbits >> 16;
2798 ucontrol->value.iec958.status[3] = sbits >> 24;
2799 return 0;
2800}
2801
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002802static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 {
2804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002805 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .info = snd_hda_spdif_in_switch_info,
2807 .get = snd_hda_spdif_in_switch_get,
2808 .put = snd_hda_spdif_in_switch_put,
2809 },
2810 {
2811 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002813 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 .info = snd_hda_spdif_mask_info,
2815 .get = snd_hda_spdif_in_status_get,
2816 },
2817 { } /* end */
2818};
2819
2820/**
2821 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2822 * @codec: the HDA codec
2823 * @nid: audio in widget NID
2824 *
2825 * Creates controls related with the SPDIF input.
2826 * Called from each patch supporting the SPDIF in.
2827 *
2828 * Returns 0 if successful, or a negative error code.
2829 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002830int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002833 struct snd_kcontrol *kctl;
2834 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002835 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Takashi Iwai1afe2062010-12-23 10:17:52 +01002837 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2838 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002839 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2840 return -EBUSY;
2841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2843 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002844 if (!kctl)
2845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002847 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002848 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return err;
2850 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002852 snd_hda_codec_read(codec, nid, 0,
2853 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return 0;
2856}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002857EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Takashi Iwaicb53c622007-08-10 17:21:45 +02002859#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002860/*
2861 * command cache
2862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002864/* build a 32bit cache key with the widget id and the command parameter */
2865#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2866#define get_cmd_cache_nid(key) ((key) & 0xff)
2867#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2868
2869/**
2870 * snd_hda_codec_write_cache - send a single command with caching
2871 * @codec: the HDA codec
2872 * @nid: NID to send the command
2873 * @direct: direct flag
2874 * @verb: the verb to send
2875 * @parm: the parameter for the verb
2876 *
2877 * Send a single command without waiting for response.
2878 *
2879 * Returns 0 if successful, or a negative error code.
2880 */
2881int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2882 int direct, unsigned int verb, unsigned int parm)
2883{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002884 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2885 struct hda_cache_head *c;
2886 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002887
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002888 if (err < 0)
2889 return err;
2890 /* parm may contain the verb stuff for get/set amp */
2891 verb = verb | (parm >> 8);
2892 parm &= 0xff;
2893 key = build_cmd_cache_key(nid, verb);
2894 mutex_lock(&codec->bus->cmd_mutex);
2895 c = get_alloc_hash(&codec->cmd_cache, key);
2896 if (c)
2897 c->val = parm;
2898 mutex_unlock(&codec->bus->cmd_mutex);
2899 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002900}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002901EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002902
Takashi Iwaid5191e52009-11-16 14:58:17 +01002903/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002904 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2905 * @codec: the HDA codec
2906 * @nid: NID to send the command
2907 * @direct: direct flag
2908 * @verb: the verb to send
2909 * @parm: the parameter for the verb
2910 *
2911 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2912 * command if the parameter is already identical with the cached value.
2913 * If not, it sends the command and refreshes the cache.
2914 *
2915 * Returns 0 if successful, or a negative error code.
2916 */
2917int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2918 int direct, unsigned int verb, unsigned int parm)
2919{
2920 struct hda_cache_head *c;
2921 u32 key;
2922
2923 /* parm may contain the verb stuff for get/set amp */
2924 verb = verb | (parm >> 8);
2925 parm &= 0xff;
2926 key = build_cmd_cache_key(nid, verb);
2927 mutex_lock(&codec->bus->cmd_mutex);
2928 c = get_hash(&codec->cmd_cache, key);
2929 if (c && c->val == parm) {
2930 mutex_unlock(&codec->bus->cmd_mutex);
2931 return 0;
2932 }
2933 mutex_unlock(&codec->bus->cmd_mutex);
2934 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2935}
2936EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2937
2938/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002939 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2940 * @codec: HD-audio codec
2941 *
2942 * Execute all verbs recorded in the command caches to resume.
2943 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002944void snd_hda_codec_resume_cache(struct hda_codec *codec)
2945{
Takashi Iwai603c4012008-07-30 15:01:44 +02002946 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002947 int i;
2948
Takashi Iwai603c4012008-07-30 15:01:44 +02002949 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002950 u32 key = buffer->key;
2951 if (!key)
2952 continue;
2953 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2954 get_cmd_cache_cmd(key), buffer->val);
2955 }
2956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002957EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002958
2959/**
2960 * snd_hda_sequence_write_cache - sequence writes with caching
2961 * @codec: the HDA codec
2962 * @seq: VERB array to send
2963 *
2964 * Send the commands sequentially from the given array.
2965 * Thte commands are recorded on cache for power-save and resume.
2966 * The array must be terminated with NID=0.
2967 */
2968void snd_hda_sequence_write_cache(struct hda_codec *codec,
2969 const struct hda_verb *seq)
2970{
2971 for (; seq->nid; seq++)
2972 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2973 seq->param);
2974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002975EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002976#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002977
Takashi Iwai54d17402005-11-21 16:33:22 +01002978/*
2979 * set power state of the codec
2980 */
2981static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2982 unsigned int power_state)
2983{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002984 hda_nid_t nid;
2985 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002986
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002987 /* this delay seems necessary to avoid click noise at power-down */
2988 if (power_state == AC_PWRST_D3)
2989 msleep(100);
2990 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002991 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002992 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002993 if (power_state == AC_PWRST_D0 &&
2994 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002995 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002996
Takashi Iwaicb53c622007-08-10 17:21:45 +02002997 nid = codec->start_nid;
2998 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002999 unsigned int wcaps = get_wcaps(codec, nid);
3000 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003001 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003002 if (power_state == AC_PWRST_D3 &&
3003 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003004 unsigned int pincap;
3005 /*
3006 * don't power down the widget if it controls
3007 * eapd and EAPD_BTLENABLE is set.
3008 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003009 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003010 if (pincap & AC_PINCAP_EAPD) {
3011 int eapd = snd_hda_codec_read(codec,
3012 nid, 0,
3013 AC_VERB_GET_EAPD_BTLENABLE, 0);
3014 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003015 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003016 continue;
3017 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003018 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003019 snd_hda_codec_write(codec, nid, 0,
3020 AC_VERB_SET_POWER_STATE,
3021 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003022 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003023 }
3024
Takashi Iwaicb53c622007-08-10 17:21:45 +02003025 if (power_state == AC_PWRST_D0) {
3026 unsigned long end_time;
3027 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003028 /* wait until the codec reachs to D0 */
3029 end_time = jiffies + msecs_to_jiffies(500);
3030 do {
3031 state = snd_hda_codec_read(codec, fg, 0,
3032 AC_VERB_GET_POWER_STATE, 0);
3033 if (state == power_state)
3034 break;
3035 msleep(1);
3036 } while (time_after_eq(end_time, jiffies));
3037 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003038}
3039
Takashi Iwai11aeff02008-07-30 15:01:46 +02003040#ifdef CONFIG_SND_HDA_HWDEP
3041/* execute additional init verbs */
3042static void hda_exec_init_verbs(struct hda_codec *codec)
3043{
3044 if (codec->init_verbs.list)
3045 snd_hda_sequence_write(codec, codec->init_verbs.list);
3046}
3047#else
3048static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3049#endif
3050
Takashi Iwaicb53c622007-08-10 17:21:45 +02003051#ifdef SND_HDA_NEEDS_RESUME
3052/*
3053 * call suspend and power-down; used both from PM and power-save
3054 */
3055static void hda_call_codec_suspend(struct hda_codec *codec)
3056{
3057 if (codec->patch_ops.suspend)
3058 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003059 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003060 hda_set_power_state(codec,
3061 codec->afg ? codec->afg : codec->mfg,
3062 AC_PWRST_D3);
3063#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003064 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003065 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003066 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003067 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003068 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003069#endif
3070}
3071
3072/*
3073 * kick up codec; used both from PM and power-save
3074 */
3075static void hda_call_codec_resume(struct hda_codec *codec)
3076{
3077 hda_set_power_state(codec,
3078 codec->afg ? codec->afg : codec->mfg,
3079 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003080 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003081 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003082 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003083 if (codec->patch_ops.resume)
3084 codec->patch_ops.resume(codec);
3085 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003086 if (codec->patch_ops.init)
3087 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003088 snd_hda_codec_resume_amp(codec);
3089 snd_hda_codec_resume_cache(codec);
3090 }
3091}
3092#endif /* SND_HDA_NEEDS_RESUME */
3093
Takashi Iwai54d17402005-11-21 16:33:22 +01003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095/**
3096 * snd_hda_build_controls - build mixer controls
3097 * @bus: the BUS
3098 *
3099 * Creates mixer controls for each codec included in the bus.
3100 *
3101 * Returns 0 if successful, otherwise a negative error code.
3102 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003103int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003108 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003109 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003110 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003111 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003112 err = snd_hda_codec_reset(codec);
3113 if (err < 0) {
3114 printk(KERN_ERR
3115 "hda_codec: cannot revert codec\n");
3116 return err;
3117 }
3118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003120 return 0;
3121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003122EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003124int snd_hda_codec_build_controls(struct hda_codec *codec)
3125{
3126 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003127 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003128 /* continue to initialize... */
3129 if (codec->patch_ops.init)
3130 err = codec->patch_ops.init(codec);
3131 if (!err && codec->patch_ops.build_controls)
3132 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003133 if (err < 0)
3134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return 0;
3136}
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138/*
3139 * stream formats
3140 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003141struct hda_rate_tbl {
3142 unsigned int hz;
3143 unsigned int alsa_bits;
3144 unsigned int hda_fmt;
3145};
3146
Takashi Iwai92f10b32010-08-03 14:21:00 +02003147/* rate = base * mult / div */
3148#define HDA_RATE(base, mult, div) \
3149 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3150 (((div) - 1) << AC_FMT_DIV_SHIFT))
3151
Takashi Iwaibefdf312005-08-22 13:57:55 +02003152static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003154
3155 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003156 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3157 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3158 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3159 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3160 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3161 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3162 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3163 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3164 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3165 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3166 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003167#define AC_PAR_PCM_RATE_BITS 11
3168 /* up to bits 10, 384kHZ isn't supported properly */
3169
3170 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003171 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003172
Takashi Iwaibefdf312005-08-22 13:57:55 +02003173 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174};
3175
3176/**
3177 * snd_hda_calc_stream_format - calculate format bitset
3178 * @rate: the sample rate
3179 * @channels: the number of channels
3180 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3181 * @maxbps: the max. bps
3182 *
3183 * Calculate the format bitset from the given rate, channels and th PCM format.
3184 *
3185 * Return zero if invalid.
3186 */
3187unsigned int snd_hda_calc_stream_format(unsigned int rate,
3188 unsigned int channels,
3189 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003190 unsigned int maxbps,
3191 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 int i;
3194 unsigned int val = 0;
3195
Takashi Iwaibefdf312005-08-22 13:57:55 +02003196 for (i = 0; rate_bits[i].hz; i++)
3197 if (rate_bits[i].hz == rate) {
3198 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 break;
3200 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003201 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 snd_printdd("invalid rate %d\n", rate);
3203 return 0;
3204 }
3205
3206 if (channels == 0 || channels > 8) {
3207 snd_printdd("invalid channels %d\n", channels);
3208 return 0;
3209 }
3210 val |= channels - 1;
3211
3212 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003213 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003214 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003215 break;
3216 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003217 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 case 20:
3220 case 24:
3221 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003222 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003223 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003225 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003227 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 break;
3229 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003230 snd_printdd("invalid format width %d\n",
3231 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233 }
3234
Anssi Hannula32c168c2010-08-03 13:28:57 +03003235 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003236 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return val;
3239}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003240EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003242static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3243{
3244 unsigned int val = 0;
3245 if (nid != codec->afg &&
3246 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3247 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3248 if (!val || val == -1)
3249 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3250 if (!val || val == -1)
3251 return 0;
3252 return val;
3253}
3254
3255static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3256{
3257 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3258 get_pcm_param);
3259}
3260
3261static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3262{
3263 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3264 if (!streams || streams == -1)
3265 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3266 if (!streams || streams == -1)
3267 return 0;
3268 return streams;
3269}
3270
3271static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3272{
3273 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3274 get_stream_param);
3275}
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277/**
3278 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3279 * @codec: the HDA codec
3280 * @nid: NID to query
3281 * @ratesp: the pointer to store the detected rate bitflags
3282 * @formatsp: the pointer to store the detected formats
3283 * @bpsp: the pointer to store the detected format widths
3284 *
3285 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3286 * or @bsps argument is ignored.
3287 *
3288 * Returns 0 if successful, otherwise a negative error code.
3289 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003290static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3292{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003293 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003295 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003296 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 if (ratesp) {
3299 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003300 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003302 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003304 if (rates == 0) {
3305 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3306 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3307 nid, val,
3308 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3309 return -EIO;
3310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 *ratesp = rates;
3312 }
3313
3314 if (formatsp || bpsp) {
3315 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003316 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003318 streams = query_stream_param(codec, nid);
3319 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 bps = 0;
3323 if (streams & AC_SUPFMT_PCM) {
3324 if (val & AC_SUPPCM_BITS_8) {
3325 formats |= SNDRV_PCM_FMTBIT_U8;
3326 bps = 8;
3327 }
3328 if (val & AC_SUPPCM_BITS_16) {
3329 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3330 bps = 16;
3331 }
3332 if (wcaps & AC_WCAP_DIGITAL) {
3333 if (val & AC_SUPPCM_BITS_32)
3334 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3335 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3336 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3337 if (val & AC_SUPPCM_BITS_24)
3338 bps = 24;
3339 else if (val & AC_SUPPCM_BITS_20)
3340 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003341 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3342 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3344 if (val & AC_SUPPCM_BITS_32)
3345 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 else if (val & AC_SUPPCM_BITS_24)
3347 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003348 else if (val & AC_SUPPCM_BITS_20)
3349 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
3351 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003352 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003354 if (!bps)
3355 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003356 }
3357 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003358 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 /* temporary hack: we have still no proper support
3360 * for the direct AC3 stream...
3361 */
3362 formats |= SNDRV_PCM_FMTBIT_U8;
3363 bps = 8;
3364 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003365 if (formats == 0) {
3366 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3367 "(nid=0x%x, val=0x%x, ovrd=%i, "
3368 "streams=0x%x)\n",
3369 nid, val,
3370 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3371 streams);
3372 return -EIO;
3373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (formatsp)
3375 *formatsp = formats;
3376 if (bpsp)
3377 *bpsp = bps;
3378 }
3379
3380 return 0;
3381}
3382
3383/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003384 * snd_hda_is_supported_format - Check the validity of the format
3385 * @codec: HD-audio codec
3386 * @nid: NID to check
3387 * @format: the HD-audio format value to check
3388 *
3389 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 *
3391 * Returns 1 if supported, 0 if not.
3392 */
3393int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3394 unsigned int format)
3395{
3396 int i;
3397 unsigned int val = 0, rate, stream;
3398
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003399 val = query_pcm_param(codec, nid);
3400 if (!val)
3401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003404 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003405 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (val & (1 << i))
3407 break;
3408 return 0;
3409 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003410 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003413 stream = query_stream_param(codec, nid);
3414 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
3416
3417 if (stream & AC_SUPFMT_PCM) {
3418 switch (format & 0xf0) {
3419 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003420 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422 break;
3423 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003424 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return 0;
3426 break;
3427 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003428 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430 break;
3431 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003432 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return 0;
3434 break;
3435 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003436 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return 0;
3438 break;
3439 default:
3440 return 0;
3441 }
3442 } else {
3443 /* FIXME: check for float32 and AC3? */
3444 }
3445
3446 return 1;
3447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003448EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450/*
3451 * PCM stuff
3452 */
3453static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3454 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003455 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 return 0;
3458}
3459
3460static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3461 struct hda_codec *codec,
3462 unsigned int stream_tag,
3463 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003464 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
3466 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3467 return 0;
3468}
3469
3470static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3471 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003472 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Takashi Iwai888afa12008-03-18 09:57:50 +01003474 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 return 0;
3476}
3477
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003478static int set_pcm_default_values(struct hda_codec *codec,
3479 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003481 int err;
3482
Takashi Iwai0ba21762007-04-16 11:29:14 +02003483 /* query support PCM information from the given NID */
3484 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003485 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003486 info->rates ? NULL : &info->rates,
3487 info->formats ? NULL : &info->formats,
3488 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003489 if (err < 0)
3490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
3492 if (info->ops.open == NULL)
3493 info->ops.open = hda_pcm_default_open_close;
3494 if (info->ops.close == NULL)
3495 info->ops.close = hda_pcm_default_open_close;
3496 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003497 if (snd_BUG_ON(!info->nid))
3498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 info->ops.prepare = hda_pcm_default_prepare;
3500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003502 if (snd_BUG_ON(!info->nid))
3503 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 info->ops.cleanup = hda_pcm_default_cleanup;
3505 }
3506 return 0;
3507}
3508
Takashi Iwaieb541332010-08-06 13:48:11 +02003509/*
3510 * codec prepare/cleanup entries
3511 */
3512int snd_hda_codec_prepare(struct hda_codec *codec,
3513 struct hda_pcm_stream *hinfo,
3514 unsigned int stream,
3515 unsigned int format,
3516 struct snd_pcm_substream *substream)
3517{
3518 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003519 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003520 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3521 if (ret >= 0)
3522 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003523 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003524 return ret;
3525}
3526EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3527
3528void snd_hda_codec_cleanup(struct hda_codec *codec,
3529 struct hda_pcm_stream *hinfo,
3530 struct snd_pcm_substream *substream)
3531{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003532 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003533 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003534 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003535}
3536EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3537
Takashi Iwaid5191e52009-11-16 14:58:17 +01003538/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003539const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3540 "Audio", "SPDIF", "HDMI", "Modem"
3541};
3542
Takashi Iwai176d5332008-07-30 15:01:44 +02003543/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003544 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003545 *
3546 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003547 */
3548static int get_empty_pcm_device(struct hda_bus *bus, int type)
3549{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003550 /* audio device indices; not linear to keep compatibility */
3551 static int audio_idx[HDA_PCM_NTYPES][5] = {
3552 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3553 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003554 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003555 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003556 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003557 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003558
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003559 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003560 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3561 return -EINVAL;
3562 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003563
3564 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3565 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3566 return audio_idx[type][i];
3567
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003568 snd_printk(KERN_WARNING "Too many %s devices\n",
3569 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003570 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003571}
3572
3573/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003574 * attach a new PCM stream
3575 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003576static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003577{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003578 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003579 struct hda_pcm_stream *info;
3580 int stream, err;
3581
Takashi Iwaib91f0802008-11-04 08:43:08 +01003582 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003583 return -EINVAL;
3584 for (stream = 0; stream < 2; stream++) {
3585 info = &pcm->stream[stream];
3586 if (info->substreams) {
3587 err = set_pcm_default_values(codec, info);
3588 if (err < 0)
3589 return err;
3590 }
3591 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003592 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003593}
3594
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003595/* assign all PCMs of the given codec */
3596int snd_hda_codec_build_pcms(struct hda_codec *codec)
3597{
3598 unsigned int pcm;
3599 int err;
3600
3601 if (!codec->num_pcms) {
3602 if (!codec->patch_ops.build_pcms)
3603 return 0;
3604 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003605 if (err < 0) {
3606 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003607 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003608 err = snd_hda_codec_reset(codec);
3609 if (err < 0) {
3610 printk(KERN_ERR
3611 "hda_codec: cannot revert codec\n");
3612 return err;
3613 }
3614 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003615 }
3616 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3617 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3618 int dev;
3619
3620 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003621 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003622
3623 if (!cpcm->pcm) {
3624 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3625 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003626 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003627 cpcm->device = dev;
3628 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003629 if (err < 0) {
3630 printk(KERN_ERR "hda_codec: cannot attach "
3631 "PCM stream %d for codec #%d\n",
3632 dev, codec->addr);
3633 continue; /* no fatal error */
3634 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003635 }
3636 }
3637 return 0;
3638}
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640/**
3641 * snd_hda_build_pcms - build PCM information
3642 * @bus: the BUS
3643 *
3644 * Create PCM information for each codec included in the bus.
3645 *
3646 * The build_pcms codec patch is requested to set up codec->num_pcms and
3647 * codec->pcm_info properly. The array is referred by the top-level driver
3648 * to create its PCM instances.
3649 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3650 * callback.
3651 *
3652 * At least, substreams, channels_min and channels_max must be filled for
3653 * each stream. substreams = 0 indicates that the stream doesn't exist.
3654 * When rates and/or formats are zero, the supported values are queried
3655 * from the given nid. The nid is used also by the default ops.prepare
3656 * and ops.cleanup callbacks.
3657 *
3658 * The driver needs to call ops.open in its open callback. Similarly,
3659 * ops.close is supposed to be called in the close callback.
3660 * ops.prepare should be called in the prepare or hw_params callback
3661 * with the proper parameters for set up.
3662 * ops.cleanup should be called in hw_free for clean up of streams.
3663 *
3664 * This function returns 0 if successfull, or a negative error code.
3665 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003666int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003668 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Takashi Iwai0ba21762007-04-16 11:29:14 +02003670 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003671 int err = snd_hda_codec_build_pcms(codec);
3672 if (err < 0)
3673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
3675 return 0;
3676}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003677EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679/**
3680 * snd_hda_check_board_config - compare the current codec with the config table
3681 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003682 * @num_configs: number of config enums
3683 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * @tbl: configuration table, terminated by null entries
3685 *
3686 * Compares the modelname or PCI subsystem id of the current codec with the
3687 * given configuration table. If a matching entry is found, returns its
3688 * config value (supposed to be 0 or positive).
3689 *
3690 * If no entries are matching, the function returns a negative value.
3691 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003692int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003693 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003694 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003696 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003697 int i;
3698 for (i = 0; i < num_configs; i++) {
3699 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003700 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003701 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3702 "selected\n", models[i]);
3703 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 }
3705 }
3706 }
3707
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003708 if (!codec->bus->pci || !tbl)
3709 return -1;
3710
3711 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3712 if (!tbl)
3713 return -1;
3714 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003715#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003716 char tmp[10];
3717 const char *model = NULL;
3718 if (models)
3719 model = models[tbl->value];
3720 if (!model) {
3721 sprintf(tmp, "#%d", tbl->value);
3722 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003724 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3725 "for config %x:%x (%s)\n",
3726 model, tbl->subvendor, tbl->subdevice,
3727 (tbl->name ? tbl->name : "Unknown device"));
3728#endif
3729 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
3731 return -1;
3732}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003733EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003736 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003737 subsystem ID with the
3738 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003739
3740 This is important for Gateway notebooks with SB450 HDA Audio
3741 where the vendor ID of the PCI device is:
3742 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3743 and the vendor/subvendor are found only at the codec.
3744
3745 * @codec: the HDA codec
3746 * @num_configs: number of config enums
3747 * @models: array of model name strings
3748 * @tbl: configuration table, terminated by null entries
3749 *
3750 * Compares the modelname or PCI subsystem id of the current codec with the
3751 * given configuration table. If a matching entry is found, returns its
3752 * config value (supposed to be 0 or positive).
3753 *
3754 * If no entries are matching, the function returns a negative value.
3755 */
3756int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003757 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003758 const struct snd_pci_quirk *tbl)
3759{
3760 const struct snd_pci_quirk *q;
3761
3762 /* Search for codec ID */
3763 for (q = tbl; q->subvendor; q++) {
3764 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3765
3766 if (vendorid == codec->subsystem_id)
3767 break;
3768 }
3769
3770 if (!q->subvendor)
3771 return -1;
3772
3773 tbl = q;
3774
3775 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003776#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003777 char tmp[10];
3778 const char *model = NULL;
3779 if (models)
3780 model = models[tbl->value];
3781 if (!model) {
3782 sprintf(tmp, "#%d", tbl->value);
3783 model = tmp;
3784 }
3785 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3786 "for config %x:%x (%s)\n",
3787 model, tbl->subvendor, tbl->subdevice,
3788 (tbl->name ? tbl->name : "Unknown device"));
3789#endif
3790 return tbl->value;
3791 }
3792 return -1;
3793}
3794EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3795
3796/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 * snd_hda_add_new_ctls - create controls from the array
3798 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003799 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 *
3801 * This helper function creates and add new controls in the given array.
3802 * The array must be terminated with an empty entry as terminator.
3803 *
3804 * Returns 0 if successful, or a negative error code.
3805 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003806int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003811 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003812 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003813 if (knew->iface == -1) /* skip this codec private value */
3814 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003815 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003816 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003817 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003818 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003819 if (addr > 0)
3820 kctl->id.device = addr;
3821 if (idx > 0)
3822 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003823 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003824 if (!err)
3825 break;
3826 /* try first with another device index corresponding to
3827 * the codec addr; if it still fails (or it's the
3828 * primary codec), then try another control index
3829 */
3830 if (!addr && codec->addr)
3831 addr = codec->addr;
3832 else if (!idx && !knew->index) {
3833 idx = find_empty_mixer_ctl_idx(codec,
3834 knew->name);
3835 if (idx <= 0)
3836 return err;
3837 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003838 return err;
3839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
3841 return 0;
3842}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003843EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Takashi Iwaicb53c622007-08-10 17:21:45 +02003845#ifdef CONFIG_SND_HDA_POWER_SAVE
3846static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3847 unsigned int power_state);
3848
3849static void hda_power_work(struct work_struct *work)
3850{
3851 struct hda_codec *codec =
3852 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003853 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003854
Maxim Levitsky2e492462007-09-03 15:26:57 +02003855 if (!codec->power_on || codec->power_count) {
3856 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003857 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003858 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003859
3860 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003861 if (bus->ops.pm_notify)
3862 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003863}
3864
3865static void hda_keep_power_on(struct hda_codec *codec)
3866{
3867 codec->power_count++;
3868 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003869 codec->power_jiffies = jiffies;
3870}
3871
Takashi Iwaid5191e52009-11-16 14:58:17 +01003872/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003873void snd_hda_update_power_acct(struct hda_codec *codec)
3874{
3875 unsigned long delta = jiffies - codec->power_jiffies;
3876 if (codec->power_on)
3877 codec->power_on_acct += delta;
3878 else
3879 codec->power_off_acct += delta;
3880 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003881}
3882
Takashi Iwaid5191e52009-11-16 14:58:17 +01003883/**
3884 * snd_hda_power_up - Power-up the codec
3885 * @codec: HD-audio codec
3886 *
3887 * Increment the power-up counter and power up the hardware really when
3888 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003889 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003890void snd_hda_power_up(struct hda_codec *codec)
3891{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003892 struct hda_bus *bus = codec->bus;
3893
Takashi Iwaicb53c622007-08-10 17:21:45 +02003894 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003895 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003896 return;
3897
Takashi Iwaia2f63092009-11-11 09:34:25 +01003898 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003899 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003900 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003901 if (bus->ops.pm_notify)
3902 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003903 hda_call_codec_resume(codec);
3904 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003905 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003907EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003908
3909#define power_save(codec) \
3910 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003911
Takashi Iwaid5191e52009-11-16 14:58:17 +01003912/**
3913 * snd_hda_power_down - Power-down the codec
3914 * @codec: HD-audio codec
3915 *
3916 * Decrement the power-up counter and schedules the power-off work if
3917 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003918 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003919void snd_hda_power_down(struct hda_codec *codec)
3920{
3921 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003922 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003923 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003924 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003925 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003926 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003927 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003928 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003929}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003930EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003931
Takashi Iwaid5191e52009-11-16 14:58:17 +01003932/**
3933 * snd_hda_check_amp_list_power - Check the amp list and update the power
3934 * @codec: HD-audio codec
3935 * @check: the object containing an AMP list and the status
3936 * @nid: NID to check / update
3937 *
3938 * Check whether the given NID is in the amp list. If it's in the list,
3939 * check the current AMP status, and update the the power-status according
3940 * to the mute status.
3941 *
3942 * This function is supposed to be set or called from the check_power_status
3943 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003944 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003945int snd_hda_check_amp_list_power(struct hda_codec *codec,
3946 struct hda_loopback_check *check,
3947 hda_nid_t nid)
3948{
3949 struct hda_amp_list *p;
3950 int ch, v;
3951
3952 if (!check->amplist)
3953 return 0;
3954 for (p = check->amplist; p->nid; p++) {
3955 if (p->nid == nid)
3956 break;
3957 }
3958 if (!p->nid)
3959 return 0; /* nothing changed */
3960
3961 for (p = check->amplist; p->nid; p++) {
3962 for (ch = 0; ch < 2; ch++) {
3963 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3964 p->idx);
3965 if (!(v & HDA_AMP_MUTE) && v > 0) {
3966 if (!check->power_on) {
3967 check->power_on = 1;
3968 snd_hda_power_up(codec);
3969 }
3970 return 1;
3971 }
3972 }
3973 }
3974 if (check->power_on) {
3975 check->power_on = 0;
3976 snd_hda_power_down(codec);
3977 }
3978 return 0;
3979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003980EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003983/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003984 * Channel mode helper
3985 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003986
3987/**
3988 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3989 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003990int snd_hda_ch_mode_info(struct hda_codec *codec,
3991 struct snd_ctl_elem_info *uinfo,
3992 const struct hda_channel_mode *chmode,
3993 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003994{
3995 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3996 uinfo->count = 1;
3997 uinfo->value.enumerated.items = num_chmodes;
3998 if (uinfo->value.enumerated.item >= num_chmodes)
3999 uinfo->value.enumerated.item = num_chmodes - 1;
4000 sprintf(uinfo->value.enumerated.name, "%dch",
4001 chmode[uinfo->value.enumerated.item].channels);
4002 return 0;
4003}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004004EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004005
Takashi Iwaid5191e52009-11-16 14:58:17 +01004006/**
4007 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4008 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004009int snd_hda_ch_mode_get(struct hda_codec *codec,
4010 struct snd_ctl_elem_value *ucontrol,
4011 const struct hda_channel_mode *chmode,
4012 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004013 int max_channels)
4014{
4015 int i;
4016
4017 for (i = 0; i < num_chmodes; i++) {
4018 if (max_channels == chmode[i].channels) {
4019 ucontrol->value.enumerated.item[0] = i;
4020 break;
4021 }
4022 }
4023 return 0;
4024}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004025EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004026
Takashi Iwaid5191e52009-11-16 14:58:17 +01004027/**
4028 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4029 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004030int snd_hda_ch_mode_put(struct hda_codec *codec,
4031 struct snd_ctl_elem_value *ucontrol,
4032 const struct hda_channel_mode *chmode,
4033 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004034 int *max_channelsp)
4035{
4036 unsigned int mode;
4037
4038 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004039 if (mode >= num_chmodes)
4040 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004041 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004042 return 0;
4043 /* change the current channel setting */
4044 *max_channelsp = chmode[mode].channels;
4045 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004046 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004047 return 1;
4048}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004049EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051/*
4052 * input MUX helper
4053 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004054
4055/**
4056 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4057 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004058int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4059 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
4061 unsigned int index;
4062
4063 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4064 uinfo->count = 1;
4065 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004066 if (!imux->num_items)
4067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 index = uinfo->value.enumerated.item;
4069 if (index >= imux->num_items)
4070 index = imux->num_items - 1;
4071 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4072 return 0;
4073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004074EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Takashi Iwaid5191e52009-11-16 14:58:17 +01004076/**
4077 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4078 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004079int snd_hda_input_mux_put(struct hda_codec *codec,
4080 const struct hda_input_mux *imux,
4081 struct snd_ctl_elem_value *ucontrol,
4082 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 unsigned int *cur_val)
4084{
4085 unsigned int idx;
4086
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004087 if (!imux->num_items)
4088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 idx = ucontrol->value.enumerated.item[0];
4090 if (idx >= imux->num_items)
4091 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004092 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004094 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4095 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 *cur_val = idx;
4097 return 1;
4098}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004099EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
4101
4102/*
4103 * Multi-channel / digital-out PCM helper functions
4104 */
4105
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004106/* setup SPDIF output stream */
4107static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4108 unsigned int stream_tag, unsigned int format)
4109{
4110 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004111 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004112 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004113 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4114 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004115 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004116 if (codec->slave_dig_outs) {
4117 hda_nid_t *d;
4118 for (d = codec->slave_dig_outs; *d; d++)
4119 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4120 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004121 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004122 /* turn on again (if needed) */
4123 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4124 set_dig_out_convert(codec, nid,
4125 codec->spdif_ctls & 0xff, -1);
4126}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004127
Takashi Iwai2f728532008-09-25 16:32:41 +02004128static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4129{
4130 snd_hda_codec_cleanup_stream(codec, nid);
4131 if (codec->slave_dig_outs) {
4132 hda_nid_t *d;
4133 for (d = codec->slave_dig_outs; *d; d++)
4134 snd_hda_codec_cleanup_stream(codec, *d);
4135 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004136}
4137
Takashi Iwaid5191e52009-11-16 14:58:17 +01004138/**
4139 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4140 * @bus: HD-audio bus
4141 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004142void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4143{
4144 struct hda_codec *codec;
4145
4146 if (!bus)
4147 return;
4148 list_for_each_entry(codec, &bus->codec_list, list) {
4149#ifdef CONFIG_SND_HDA_POWER_SAVE
4150 if (!codec->power_on)
4151 continue;
4152#endif
4153 if (codec->patch_ops.reboot_notify)
4154 codec->patch_ops.reboot_notify(codec);
4155 }
4156}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004157EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004158
Takashi Iwaid5191e52009-11-16 14:58:17 +01004159/**
4160 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004162int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4163 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Ingo Molnar62932df2006-01-16 16:34:20 +01004165 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004166 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4167 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004168 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004170 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return 0;
4172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004173EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Takashi Iwaid5191e52009-11-16 14:58:17 +01004175/**
4176 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4177 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004178int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4179 struct hda_multi_out *mout,
4180 unsigned int stream_tag,
4181 unsigned int format,
4182 struct snd_pcm_substream *substream)
4183{
4184 mutex_lock(&codec->spdif_mutex);
4185 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4186 mutex_unlock(&codec->spdif_mutex);
4187 return 0;
4188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004189EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004190
Takashi Iwaid5191e52009-11-16 14:58:17 +01004191/**
4192 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4193 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004194int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4195 struct hda_multi_out *mout)
4196{
4197 mutex_lock(&codec->spdif_mutex);
4198 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4199 mutex_unlock(&codec->spdif_mutex);
4200 return 0;
4201}
4202EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4203
Takashi Iwaid5191e52009-11-16 14:58:17 +01004204/**
4205 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4208 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Ingo Molnar62932df2006-01-16 16:34:20 +01004210 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004212 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return 0;
4214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004215EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Takashi Iwaid5191e52009-11-16 14:58:17 +01004217/**
4218 * snd_hda_multi_out_analog_open - open analog outputs
4219 *
4220 * Open analog outputs and set up the hw-constraints.
4221 * If the digital outputs can be opened as slave, open the digital
4222 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004224int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4225 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004226 struct snd_pcm_substream *substream,
4227 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
Takashi Iwai9a081602008-02-12 18:37:26 +01004229 struct snd_pcm_runtime *runtime = substream->runtime;
4230 runtime->hw.channels_max = mout->max_channels;
4231 if (mout->dig_out_nid) {
4232 if (!mout->analog_rates) {
4233 mout->analog_rates = hinfo->rates;
4234 mout->analog_formats = hinfo->formats;
4235 mout->analog_maxbps = hinfo->maxbps;
4236 } else {
4237 runtime->hw.rates = mout->analog_rates;
4238 runtime->hw.formats = mout->analog_formats;
4239 hinfo->maxbps = mout->analog_maxbps;
4240 }
4241 if (!mout->spdif_rates) {
4242 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4243 &mout->spdif_rates,
4244 &mout->spdif_formats,
4245 &mout->spdif_maxbps);
4246 }
4247 mutex_lock(&codec->spdif_mutex);
4248 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004249 if ((runtime->hw.rates & mout->spdif_rates) &&
4250 (runtime->hw.formats & mout->spdif_formats)) {
4251 runtime->hw.rates &= mout->spdif_rates;
4252 runtime->hw.formats &= mout->spdif_formats;
4253 if (mout->spdif_maxbps < hinfo->maxbps)
4254 hinfo->maxbps = mout->spdif_maxbps;
4255 } else {
4256 mout->share_spdif = 0;
4257 /* FIXME: need notify? */
4258 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004259 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004260 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4263 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4264}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004265EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Takashi Iwaid5191e52009-11-16 14:58:17 +01004267/**
4268 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4269 *
4270 * Set up the i/o for analog out.
4271 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004273int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4274 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 unsigned int stream_tag,
4276 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004277 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
4279 hda_nid_t *nids = mout->dac_nids;
4280 int chs = substream->runtime->channels;
4281 int i;
4282
Ingo Molnar62932df2006-01-16 16:34:20 +01004283 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004284 if (mout->dig_out_nid && mout->share_spdif &&
4285 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004287 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4288 format) &&
4289 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004291 setup_dig_out_stream(codec, mout->dig_out_nid,
4292 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 } else {
4294 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004295 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 }
4297 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004298 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004301 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4302 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004303 if (!mout->no_share_stream &&
4304 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004306 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4307 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004308 /* extra outputs copied from front */
4309 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004310 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004311 snd_hda_codec_setup_stream(codec,
4312 mout->extra_out_nid[i],
4313 stream_tag, 0, format);
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 /* surrounds */
4316 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004317 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004318 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4319 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004320 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004321 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4322 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 }
4324 return 0;
4325}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004326EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Takashi Iwaid5191e52009-11-16 14:58:17 +01004328/**
4329 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004331int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4332 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
4334 hda_nid_t *nids = mout->dac_nids;
4335 int i;
4336
4337 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004338 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004340 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004341 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4342 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004343 snd_hda_codec_cleanup_stream(codec,
4344 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004345 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004347 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 mout->dig_out_used = 0;
4349 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004350 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return 0;
4352}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004353EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004355/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004356 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004357 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004358
Takashi Iwai12f288b2007-08-02 15:51:59 +02004359static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004360{
4361 for (; *list; list++)
4362 if (*list == nid)
4363 return 1;
4364 return 0;
4365}
4366
Steve Longerbeam81937d32007-05-08 15:33:03 +02004367
4368/*
4369 * Sort an associated group of pins according to their sequence numbers.
4370 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004371static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004372 int num_pins)
4373{
4374 int i, j;
4375 short seq;
4376 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004377
Steve Longerbeam81937d32007-05-08 15:33:03 +02004378 for (i = 0; i < num_pins; i++) {
4379 for (j = i + 1; j < num_pins; j++) {
4380 if (sequences[i] > sequences[j]) {
4381 seq = sequences[i];
4382 sequences[i] = sequences[j];
4383 sequences[j] = seq;
4384 nid = pins[i];
4385 pins[i] = pins[j];
4386 pins[j] = nid;
4387 }
4388 }
4389 }
4390}
4391
4392
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004393/* add the found input-pin to the cfg->inputs[] table */
4394static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4395 int type)
4396{
4397 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4398 cfg->inputs[cfg->num_inputs].pin = nid;
4399 cfg->inputs[cfg->num_inputs].type = type;
4400 cfg->num_inputs++;
4401 }
4402}
4403
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004404/* sort inputs in the order of AUTO_PIN_* type */
4405static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4406{
4407 int i, j;
4408
4409 for (i = 0; i < cfg->num_inputs; i++) {
4410 for (j = i + 1; j < cfg->num_inputs; j++) {
4411 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4412 struct auto_pin_cfg_item tmp;
4413 tmp = cfg->inputs[i];
4414 cfg->inputs[i] = cfg->inputs[j];
4415 cfg->inputs[j] = tmp;
4416 }
4417 }
4418 }
4419}
4420
Takashi Iwai82bc9552006-03-21 11:24:42 +01004421/*
4422 * Parse all pin widgets and store the useful pin nids to cfg
4423 *
4424 * The number of line-outs or any primary output is stored in line_outs,
4425 * and the corresponding output pins are assigned to line_out_pins[],
4426 * in the order of front, rear, CLFE, side, ...
4427 *
4428 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004429 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004430 * is detected, one of speaker of HP pins is assigned as the primary
4431 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4432 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004433 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004434 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004435 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4436 * respectively.
4437 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004438int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4439 struct auto_pin_cfg *cfg,
4440 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004441{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004442 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004443 short seq, assoc_line_out, assoc_speaker;
4444 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4445 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004446 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004447 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004448
4449 memset(cfg, 0, sizeof(*cfg));
4450
Steve Longerbeam81937d32007-05-08 15:33:03 +02004451 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4452 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004453 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004454 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004455
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004456 end_nid = codec->start_nid + codec->num_nodes;
4457 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004458 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004459 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004460 unsigned int def_conf;
4461 short assoc, loc;
4462
4463 /* read all default configuration for pin complex */
4464 if (wid_type != AC_WID_PIN)
4465 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004466 /* ignore the given nids (e.g. pc-beep returns error) */
4467 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4468 continue;
4469
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004470 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004471 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4472 continue;
4473 loc = get_defcfg_location(def_conf);
4474 switch (get_defcfg_device(def_conf)) {
4475 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004476 seq = get_defcfg_sequence(def_conf);
4477 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004478
4479 if (!(wid_caps & AC_WCAP_STEREO))
4480 if (!cfg->mono_out_pin)
4481 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004482 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004483 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004484 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004485 assoc_line_out = assoc;
4486 else if (assoc_line_out != assoc)
4487 continue;
4488 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4489 continue;
4490 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004491 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004492 cfg->line_outs++;
4493 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004494 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004495 seq = get_defcfg_sequence(def_conf);
4496 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004497 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004498 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004499 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004500 assoc_speaker = assoc;
4501 else if (assoc_speaker != assoc)
4502 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004503 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4504 continue;
4505 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004506 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004507 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004508 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004509 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004510 seq = get_defcfg_sequence(def_conf);
4511 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004512 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4513 continue;
4514 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004515 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004516 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004517 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004518 case AC_JACK_MIC_IN:
4519 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004520 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004521 case AC_JACK_LINE_IN:
4522 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004523 break;
4524 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004525 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004526 break;
4527 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004528 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004529 break;
4530 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004531 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004532 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4533 continue;
4534 cfg->dig_out_pins[cfg->dig_outs] = nid;
4535 cfg->dig_out_type[cfg->dig_outs] =
4536 (loc == AC_JACK_LOC_HDMI) ?
4537 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4538 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004539 break;
4540 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004541 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004542 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004543 if (loc == AC_JACK_LOC_HDMI)
4544 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4545 else
4546 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004547 break;
4548 }
4549 }
4550
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004551 /* FIX-UP:
4552 * If no line-out is defined but multiple HPs are found,
4553 * some of them might be the real line-outs.
4554 */
4555 if (!cfg->line_outs && cfg->hp_outs > 1) {
4556 int i = 0;
4557 while (i < cfg->hp_outs) {
4558 /* The real HPs should have the sequence 0x0f */
4559 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4560 i++;
4561 continue;
4562 }
4563 /* Move it to the line-out table */
4564 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4565 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4566 cfg->line_outs++;
4567 cfg->hp_outs--;
4568 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4569 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004570 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004571 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4572 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004573 memset(cfg->hp_pins + cfg->hp_outs, 0,
4574 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004575 if (!cfg->hp_outs)
4576 cfg->line_out_type = AUTO_PIN_HP_OUT;
4577
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004578 }
4579
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004580 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004581 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4582 cfg->line_outs);
4583 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4584 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004585 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4586 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004587
Steve Longerbeam81937d32007-05-08 15:33:03 +02004588 /*
4589 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4590 * as a primary output
4591 */
4592 if (!cfg->line_outs) {
4593 if (cfg->speaker_outs) {
4594 cfg->line_outs = cfg->speaker_outs;
4595 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4596 sizeof(cfg->speaker_pins));
4597 cfg->speaker_outs = 0;
4598 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4599 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4600 } else if (cfg->hp_outs) {
4601 cfg->line_outs = cfg->hp_outs;
4602 memcpy(cfg->line_out_pins, cfg->hp_pins,
4603 sizeof(cfg->hp_pins));
4604 cfg->hp_outs = 0;
4605 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4606 cfg->line_out_type = AUTO_PIN_HP_OUT;
4607 }
4608 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004609
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004610 /* Reorder the surround channels
4611 * ALSA sequence is front/surr/clfe/side
4612 * HDA sequence is:
4613 * 4-ch: front/surr => OK as it is
4614 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004615 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004616 */
4617 switch (cfg->line_outs) {
4618 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004619 case 4:
4620 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004621 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004622 cfg->line_out_pins[2] = nid;
4623 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004624 }
4625
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004626 sort_autocfg_input_pins(cfg);
4627
Takashi Iwai82bc9552006-03-21 11:24:42 +01004628 /*
4629 * debug prints of the parsed results
4630 */
4631 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4632 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4633 cfg->line_out_pins[2], cfg->line_out_pins[3],
4634 cfg->line_out_pins[4]);
4635 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4636 cfg->speaker_outs, cfg->speaker_pins[0],
4637 cfg->speaker_pins[1], cfg->speaker_pins[2],
4638 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004639 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4640 cfg->hp_outs, cfg->hp_pins[0],
4641 cfg->hp_pins[1], cfg->hp_pins[2],
4642 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004643 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004644 if (cfg->dig_outs)
4645 snd_printd(" dig-out=0x%x/0x%x\n",
4646 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004647 snd_printd(" inputs:");
4648 for (i = 0; i < cfg->num_inputs; i++) {
4649 snd_printdd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004650 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004651 cfg->inputs[i].pin);
4652 }
4653 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004654 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004655 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004656
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004657 return 0;
4658}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004659EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004660
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004661int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004662{
4663 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004664 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004665 if (conn == AC_JACK_PORT_NONE)
4666 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004667 /* Windows may claim the internal mic to be BOTH, too */
4668 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004669 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004670 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004671 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004672 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004673 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004674 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004675 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004676 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004677 return INPUT_PIN_ATTR_FRONT;
4678 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004679}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004680EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004681
Takashi Iwai990061c2010-09-09 22:08:44 +02004682/**
4683 * hda_get_input_pin_label - Give a label for the given input pin
4684 *
4685 * When check_location is true, the function checks the pin location
4686 * for mic and line-in pins, and set an appropriate prefix like "Front",
4687 * "Rear", "Internal".
4688 */
4689
Takashi Iwai10a20af2010-09-09 16:28:02 +02004690const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4691 int check_location)
4692{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004693 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004694 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004695 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4696 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004697 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004698
Takashi Iwai10a20af2010-09-09 16:28:02 +02004699 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004700
4701 switch (get_defcfg_device(def_conf)) {
4702 case AC_JACK_MIC_IN:
4703 if (!check_location)
4704 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004705 attr = snd_hda_get_input_pin_attr(def_conf);
4706 if (!attr)
4707 return "None";
4708 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004709 case AC_JACK_LINE_IN:
4710 if (!check_location)
4711 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004712 attr = snd_hda_get_input_pin_attr(def_conf);
4713 if (!attr)
4714 return "None";
4715 if (attr == INPUT_PIN_ATTR_DOCK)
4716 return "Dock Line";
4717 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004718 case AC_JACK_AUX:
4719 return "Aux";
4720 case AC_JACK_CD:
4721 return "CD";
4722 case AC_JACK_SPDIF_IN:
4723 return "SPDIF In";
4724 case AC_JACK_DIG_OTHER_IN:
4725 return "Digital In";
4726 default:
4727 return "Misc";
4728 }
4729}
4730EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4731
Takashi Iwaia1c98512010-09-09 21:36:27 +02004732/* Check whether the location prefix needs to be added to the label.
4733 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4734 * have to put "Front" prefix to each label. In such a case, returns false.
4735 */
4736static int check_mic_location_need(struct hda_codec *codec,
4737 const struct auto_pin_cfg *cfg,
4738 int input)
4739{
4740 unsigned int defc;
4741 int i, attr, attr2;
4742
4743 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004744 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004745 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004746 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004747 return 1;
4748
4749 attr = 0;
4750 for (i = 0; i < cfg->num_inputs; i++) {
4751 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004752 attr2 = snd_hda_get_input_pin_attr(defc);
4753 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004754 if (attr && attr != attr2)
4755 return 1; /* different locations found */
4756 attr = attr2;
4757 }
4758 }
4759 return 0;
4760}
4761
Takashi Iwai990061c2010-09-09 22:08:44 +02004762/**
4763 * hda_get_autocfg_input_label - Get a label for the given input
4764 *
4765 * Get a label for the given input pin defined by the autocfg item.
4766 * Unlike hda_get_input_pin_label(), this function checks all inputs
4767 * defined in autocfg and avoids the redundant mic/line prefix as much as
4768 * possible.
4769 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004770const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4771 const struct auto_pin_cfg *cfg,
4772 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004773{
4774 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004775 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004776
Takashi Iwai10a20af2010-09-09 16:28:02 +02004777 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4778 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4779 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004780 if (has_multiple_pins && type == AUTO_PIN_MIC)
4781 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004782 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4783 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004784}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004785EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4786
Takashi Iwai990061c2010-09-09 22:08:44 +02004787/**
4788 * snd_hda_add_imux_item - Add an item to input_mux
4789 *
4790 * When the same label is used already in the existing items, the number
4791 * suffix is appended to the label. This label index number is stored
4792 * to type_idx when non-NULL pointer is given.
4793 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004794int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4795 int index, int *type_idx)
4796{
4797 int i, label_idx = 0;
4798 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4799 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4800 return -EINVAL;
4801 }
4802 for (i = 0; i < imux->num_items; i++) {
4803 if (!strncmp(label, imux->items[i].label, strlen(label)))
4804 label_idx++;
4805 }
4806 if (type_idx)
4807 *type_idx = label_idx;
4808 if (label_idx > 0)
4809 snprintf(imux->items[imux->num_items].label,
4810 sizeof(imux->items[imux->num_items].label),
4811 "%s %d", label, label_idx);
4812 else
4813 strlcpy(imux->items[imux->num_items].label, label,
4814 sizeof(imux->items[imux->num_items].label));
4815 imux->items[imux->num_items].index = index;
4816 imux->num_items++;
4817 return 0;
4818}
4819EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004820
Takashi Iwai4a471b72005-12-07 13:56:29 +01004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822#ifdef CONFIG_PM
4823/*
4824 * power management
4825 */
4826
4827/**
4828 * snd_hda_suspend - suspend the codecs
4829 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 *
4831 * Returns 0 if successful.
4832 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004833int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004835 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Takashi Iwai0ba21762007-04-16 11:29:14 +02004837 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004838#ifdef CONFIG_SND_HDA_POWER_SAVE
4839 if (!codec->power_on)
4840 continue;
4841#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004842 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
4844 return 0;
4845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004846EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848/**
4849 * snd_hda_resume - resume the codecs
4850 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 *
4852 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004853 *
4854 * This fucntion is defined only when POWER_SAVE isn't set.
4855 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 */
4857int snd_hda_resume(struct hda_bus *bus)
4858{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004859 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Takashi Iwai0ba21762007-04-16 11:29:14 +02004861 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004862 if (snd_hda_codec_needs_resume(codec))
4863 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return 0;
4866}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004867EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004868#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004869
4870/*
4871 * generic arrays
4872 */
4873
Takashi Iwaid5191e52009-11-16 14:58:17 +01004874/**
4875 * snd_array_new - get a new element from the given array
4876 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004877 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004878 * Get a new element from the given array. If it exceeds the
4879 * pre-allocated array size, re-allocate the array.
4880 *
4881 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004882 */
4883void *snd_array_new(struct snd_array *array)
4884{
4885 if (array->used >= array->alloced) {
4886 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004887 void *nlist;
4888 if (snd_BUG_ON(num >= 4096))
4889 return NULL;
4890 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004891 if (!nlist)
4892 return NULL;
4893 if (array->list) {
4894 memcpy(nlist, array->list,
4895 array->elem_size * array->alloced);
4896 kfree(array->list);
4897 }
4898 array->list = nlist;
4899 array->alloced = num;
4900 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004901 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004903EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004904
Takashi Iwaid5191e52009-11-16 14:58:17 +01004905/**
4906 * snd_array_free - free the given array elements
4907 * @array: the array object
4908 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004909void snd_array_free(struct snd_array *array)
4910{
4911 kfree(array->list);
4912 array->used = 0;
4913 array->alloced = 0;
4914 array->list = NULL;
4915}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004916EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004917
Takashi Iwaid5191e52009-11-16 14:58:17 +01004918/**
4919 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4920 * @pcm: PCM caps bits
4921 * @buf: the string buffer to write
4922 * @buflen: the max buffer length
4923 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004924 * used by hda_proc.c and hda_eld.c
4925 */
4926void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4927{
4928 static unsigned int rates[] = {
4929 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4930 96000, 176400, 192000, 384000
4931 };
4932 int i, j;
4933
4934 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4935 if (pcm & (1 << i))
4936 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4937
4938 buf[j] = '\0'; /* necessary when j == 0 */
4939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004940EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004941
Takashi Iwaid5191e52009-11-16 14:58:17 +01004942/**
4943 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4944 * @pcm: PCM caps bits
4945 * @buf: the string buffer to write
4946 * @buflen: the max buffer length
4947 *
4948 * used by hda_proc.c and hda_eld.c
4949 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004950void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4951{
4952 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4953 int i, j;
4954
4955 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4956 if (pcm & (AC_SUPPCM_BITS_8 << i))
4957 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4958
4959 buf[j] = '\0'; /* necessary when j == 0 */
4960}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004961EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004962
Takashi Iwaicd372fb2011-03-03 14:40:14 +01004963#ifdef CONFIG_SND_HDA_INPUT_JACK
4964/*
4965 * Input-jack notification support
4966 */
4967struct hda_jack_item {
4968 hda_nid_t nid;
4969 int type;
4970 struct snd_jack *jack;
4971};
4972
4973static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
4974 int type)
4975{
4976 switch (type) {
4977 case SND_JACK_HEADPHONE:
4978 return "Headphone";
4979 case SND_JACK_MICROPHONE:
4980 return "Mic";
4981 case SND_JACK_LINEOUT:
4982 return "Line-out";
4983 case SND_JACK_HEADSET:
4984 return "Headset";
4985 default:
4986 return "Misc";
4987 }
4988}
4989
4990static void hda_free_jack_priv(struct snd_jack *jack)
4991{
4992 struct hda_jack_item *jacks = jack->private_data;
4993 jacks->nid = 0;
4994 jacks->jack = NULL;
4995}
4996
4997int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
4998 const char *name)
4999{
5000 struct hda_jack_item *jack;
5001 int err;
5002
5003 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5004 jack = snd_array_new(&codec->jacks);
5005 if (!jack)
5006 return -ENOMEM;
5007
5008 jack->nid = nid;
5009 jack->type = type;
5010 if (!name)
5011 name = get_jack_default_name(codec, nid, type);
5012 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5013 if (err < 0) {
5014 jack->nid = 0;
5015 return err;
5016 }
5017 jack->jack->private_data = jack;
5018 jack->jack->private_free = hda_free_jack_priv;
5019 return 0;
5020}
5021EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5022
5023void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5024{
5025 struct hda_jack_item *jacks = codec->jacks.list;
5026 int i;
5027
5028 if (!jacks)
5029 return;
5030
5031 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5032 unsigned int pin_ctl;
5033 unsigned int present;
5034 int type;
5035
5036 if (jacks->nid != nid)
5037 continue;
5038 present = snd_hda_jack_detect(codec, nid);
5039 type = jacks->type;
5040 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5041 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5042 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5043 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5044 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5045 }
5046 snd_jack_report(jacks->jack, present ? type : 0);
5047 }
5048}
5049EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5050
5051/* free jack instances manually when clearing/reconfiguring */
5052void snd_hda_input_jack_free(struct hda_codec *codec)
5053{
5054 if (!codec->bus->shutdown && codec->jacks.list) {
5055 struct hda_jack_item *jacks = codec->jacks.list;
5056 int i;
5057 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5058 if (jacks->jack)
5059 snd_device_free(codec->bus->card, jacks->jack);
5060 }
5061 }
5062 snd_array_free(&codec->jacks);
5063}
5064EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5065#endif /* CONFIG_SND_HDA_INPUT_JACK */
5066
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005067MODULE_DESCRIPTION("HDA codec core");
5068MODULE_LICENSE("GPL");