blob: 916a1863af73b6a3df0e6c79ae97a4ce0c5db2f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020031#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010033#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020035#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020036#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwaid66fee52011-08-02 15:39:31 +020038#define CREATE_TRACE_POINTS
39#include "hda_trace.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * vendor / preset table
43 */
44
45struct hda_vendor_id {
46 unsigned int id;
47 const char *name;
48};
49
50/* codec vendor labels */
51static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020053 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020054 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010055 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010056 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020058 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010059 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020060 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020062 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020064 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010065 { 0x17e8, "Chrontel" },
66 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000067 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010069 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020070 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 {} /* terminator */
72};
73
Takashi Iwai1289e9e2008-11-27 15:47:11 +010074static DEFINE_MUTEX(preset_mutex);
75static LIST_HEAD(hda_preset_tables);
76
77int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
78{
79 mutex_lock(&preset_mutex);
80 list_add_tail(&preset->list, &hda_preset_tables);
81 mutex_unlock(&preset_mutex);
82 return 0;
83}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010084EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010085
86int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
87{
88 mutex_lock(&preset_mutex);
89 list_del(&preset->list);
90 mutex_unlock(&preset_mutex);
91 return 0;
92}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010093EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Takashi Iwaicb53c622007-08-10 17:21:45 +020095#ifdef CONFIG_SND_HDA_POWER_SAVE
96static void hda_power_work(struct work_struct *work);
97static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020098#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099#else
100static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200102#endif
103
Takashi Iwaid5191e52009-11-16 14:58:17 +0100104/**
105 * snd_hda_get_jack_location - Give a location string of the jack
106 * @cfg: pin default config value
107 *
108 * Parse the pin default config value and returns the string of the
109 * jack location, e.g. "Rear", "Front", etc.
110 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400111const char *snd_hda_get_jack_location(u32 cfg)
112{
113 static char *bases[7] = {
114 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
115 };
116 static unsigned char specials_idx[] = {
117 0x07, 0x08,
118 0x17, 0x18, 0x19,
119 0x37, 0x38
120 };
121 static char *specials[] = {
122 "Rear Panel", "Drive Bar",
123 "Riser", "HDMI", "ATAPI",
124 "Mobile-In", "Mobile-Out"
125 };
126 int i;
127 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
128 if ((cfg & 0x0f) < 7)
129 return bases[cfg & 0x0f];
130 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
131 if (cfg == specials_idx[i])
132 return specials[i];
133 }
134 return "UNKNOWN";
135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100136EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400137
Takashi Iwaid5191e52009-11-16 14:58:17 +0100138/**
139 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
140 * @cfg: pin default config value
141 *
142 * Parse the pin default config value and returns the string of the
143 * jack connectivity, i.e. external or internal connection.
144 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145const char *snd_hda_get_jack_connectivity(u32 cfg)
146{
147 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
148
149 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100151EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400152
Takashi Iwaid5191e52009-11-16 14:58:17 +0100153/**
154 * snd_hda_get_jack_type - Give a type string of the jack
155 * @cfg: pin default config value
156 *
157 * Parse the pin default config value and returns the string of the
158 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
159 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400160const char *snd_hda_get_jack_type(u32 cfg)
161{
162 static char *jack_types[16] = {
163 "Line Out", "Speaker", "HP Out", "CD",
164 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
165 "Line In", "Aux", "Mic", "Telephony",
166 "SPDIF In", "Digitial In", "Reserved", "Other"
167 };
168
169 return jack_types[(cfg & AC_DEFCFG_DEVICE)
170 >> AC_DEFCFG_DEVICE_SHIFT];
171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100172EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400173
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100174/*
175 * Compose a 32bit command word to be sent to the HD-audio controller
176 */
177static inline unsigned int
178make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
179 unsigned int verb, unsigned int parm)
180{
181 u32 val;
182
Takashi Iwai82e1b802009-07-17 12:47:34 +0200183 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
184 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800185 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
186 codec->addr, direct, nid, verb, parm);
187 return ~0;
188 }
189
190 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100191 val |= (u32)direct << 27;
192 val |= (u32)nid << 20;
193 val |= verb << 8;
194 val |= parm;
195 return val;
196}
197
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200198/*
199 * Send and receive a verb
200 */
201static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
202 unsigned int *res)
203{
204 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206
Wu Fengguang6430aee2009-07-17 16:49:19 +0800207 if (cmd == ~0)
208 return -1;
209
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200210 if (res)
211 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200212 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200213 snd_hda_power_up(codec);
214 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200215 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200217 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800218 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200219 trace_hda_get_response(codec, *res);
220 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221 mutex_unlock(&bus->cmd_mutex);
222 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200223 if (res && *res == -1 && bus->rirb_error) {
224 if (bus->response_reset) {
225 snd_printd("hda_codec: resetting BUS due to "
226 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200228 bus->ops.bus_reset(bus);
229 }
230 goto again;
231 }
232 /* clear reset-flag when the communication gets recovered */
233 if (!err)
234 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200235 return err;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
239 * snd_hda_codec_read - send a command and get the response
240 * @codec: the HDA codec
241 * @nid: NID to send the command
242 * @direct: direct flag
243 * @verb: the verb to send
244 * @parm: the parameter for the verb
245 *
246 * Send a single command and read the corresponding response.
247 *
248 * Returns the obtained response value, or -1 for an error.
249 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200250unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
251 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned int verb, unsigned int parm)
253{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200254 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
255 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700256 if (codec_exec_verb(codec, cmd, &res))
257 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return res;
259}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100260EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/**
263 * snd_hda_codec_write - send a single command without waiting for response
264 * @codec: the HDA codec
265 * @nid: NID to send the command
266 * @direct: direct flag
267 * @verb: the verb to send
268 * @parm: the parameter for the verb
269 *
270 * Send a single command without waiting for response.
271 *
272 * Returns 0 if successful, or a negative error code.
273 */
274int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
275 unsigned int verb, unsigned int parm)
276{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200277 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100278 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200279 return codec_exec_verb(codec, cmd,
280 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100282EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/**
285 * snd_hda_sequence_write - sequence writes
286 * @codec: the HDA codec
287 * @seq: VERB array to send
288 *
289 * Send the commands sequentially from the given array.
290 * The array must be terminated with NID=0.
291 */
292void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
293{
294 for (; seq->nid; seq++)
295 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
296}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100297EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/**
300 * snd_hda_get_sub_nodes - get the range of sub nodes
301 * @codec: the HDA codec
302 * @nid: NID to parse
303 * @start_id: the pointer to store the start NID
304 *
305 * Parse the NID and store the start NID of its sub-nodes.
306 * Returns the number of sub-nodes.
307 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200308int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
309 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned int parm;
312
313 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200314 if (parm == -1)
315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *start_id = (parm >> 16) & 0x7fff;
317 return (int)(parm & 0x7fff);
318}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100319EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200321/* look up the cached results */
322static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
323{
324 int i, len;
325 for (i = 0; i < array->used; ) {
326 hda_nid_t *p = snd_array_elem(array, i);
327 if (nid == *p)
328 return p;
329 len = p[1];
330 i += len + 2;
331 }
332 return NULL;
333}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200336 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * @codec: the HDA codec
338 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200339 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
341 * Parses the connection list of the given widget and stores the list
342 * of NIDs.
343 *
344 * Returns the number of connections, or a negative error code.
345 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200346int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
347 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348{
349 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200350 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200351 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200352 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200354
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 again:
356 /* if the connection-list is already cached, read it */
357 p = lookup_conn_list(array, nid);
358 if (p) {
359 if (listp)
360 *listp = p + 2;
361 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200362 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 if (snd_BUG_ON(added))
364 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200365
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200366 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200367 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200368 if (len < 0)
369 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200370 err = snd_hda_override_conn_list(codec, nid, len, list);
371 if (err < 0)
372 return err;
373 added = true;
374 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200375}
Takashi Iwaidce20792011-06-24 14:10:28 +0200376EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
377
378/**
379 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * @codec: the HDA codec
381 * @nid: NID to parse
382 * @conn_list: connection list array
383 * @max_conns: max. number of connections to store
384 *
385 * Parses the connection list of the given widget and stores the list
386 * of NIDs.
387 *
388 * Returns the number of connections, or a negative error code.
389 */
390int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200391 hda_nid_t *conn_list, int max_conns)
392{
Takashi Iwaidce20792011-06-24 14:10:28 +0200393 const hda_nid_t *list;
394 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200395
Takashi Iwaidce20792011-06-24 14:10:28 +0200396 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 if (len > max_conns) {
399 snd_printk(KERN_ERR "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
401 len, nid);
402 return -EINVAL;
403 }
404 memcpy(conn_list, list, len * sizeof(hda_nid_t));
405 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200406}
407EXPORT_SYMBOL_HDA(snd_hda_get_connections);
408
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200409/**
410 * snd_hda_get_raw_connections - copy connection list without cache
411 * @codec: the HDA codec
412 * @nid: NID to parse
413 * @conn_list: connection list array
414 * @max_conns: max. number of connections to store
415 *
416 * Like snd_hda_get_connections(), copy the connection list but without
417 * checking through the connection-list cache.
418 * Currently called only from hda_proc.c, so not exported.
419 */
420int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
421 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100424 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200426 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100427 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwaida3cec32008-08-08 17:12:14 +0200429 if (snd_BUG_ON(!conn_list || max_conns <= 0))
430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200432 wcaps = get_wcaps(codec, nid);
433 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200434 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
435 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
438 if (parm & AC_CLIST_LONG) {
439 /* long form */
440 shift = 16;
441 num_elems = 2;
442 } else {
443 /* short form */
444 shift = 8;
445 num_elems = 4;
446 }
447 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 mask = (1 << (shift-1)) - 1;
449
Takashi Iwai0ba21762007-04-16 11:29:14 +0200450 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0; /* no connection */
452
453 if (conn_len == 1) {
454 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200455 parm = snd_hda_codec_read(codec, nid, 0,
456 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200457 if (parm == -1 && codec->bus->rirb_error)
458 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 conn_list[0] = parm & mask;
460 return 1;
461 }
462
463 /* multi connection */
464 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100465 prev_nid = 0;
466 for (i = 0; i < conn_len; i++) {
467 int range_val;
468 hda_nid_t val, n;
469
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 parm = snd_hda_codec_read(codec, nid, 0,
472 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200473 if (parm == -1 && codec->bus->rirb_error)
474 return -EIO;
475 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200476 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200478 if (val == 0) {
479 snd_printk(KERN_WARNING "hda_codec: "
480 "invalid CONNECT_LIST verb %x[%i]:%x\n",
481 nid, i, parm);
482 return 0;
483 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 parm >>= shift;
485 if (range_val) {
486 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!prev_nid || prev_nid >= val) {
488 snd_printk(KERN_WARNING "hda_codec: "
489 "invalid dep_range_val %x:%x\n",
490 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100491 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 for (n = prev_nid + 1; n <= val; n++) {
494 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200495 snd_printk(KERN_ERR "hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
497 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100498 return -EINVAL;
499 }
500 conn_list[conns++] = n;
501 }
502 } else {
503 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200504 snd_printk(KERN_ERR "hda_codec: "
505 "Too many connections %d for NID 0x%x\n",
506 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100507 return -EINVAL;
508 }
509 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100511 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return conns;
514}
515
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200516static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
517{
518 hda_nid_t *p = snd_array_new(array);
519 if (!p)
520 return false;
521 *p = nid;
522 return true;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200526 * snd_hda_override_conn_list - add/modify the connection-list to cache
527 * @codec: the HDA codec
528 * @nid: NID to parse
529 * @len: number of connection list entries
530 * @list: the list of connection entries
531 *
532 * Add or modify the given connection-list to the cache. If the corresponding
533 * cache already exists, invalidate it and append a new one.
534 *
535 * Returns zero or a negative error code.
536 */
537int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
538 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200540 struct snd_array *array = &codec->conn_lists;
541 hda_nid_t *p;
542 int i, old_used;
543
544 p = lookup_conn_list(array, nid);
545 if (p)
546 *p = -1; /* invalidate the old entry */
547
548 old_used = array->used;
549 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
550 goto error_add;
551 for (i = 0; i < len; i++)
552 if (!add_conn_list(array, list[i]))
553 goto error_add;
554 return 0;
555
556 error_add:
557 array->used = old_used;
558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
561
562/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
568 *
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
572 */
573int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
574 hda_nid_t nid, int recursive)
575{
576 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
577 int i, nums;
578
579 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
580 for (i = 0; i < nums; i++)
581 if (conn[i] == nid)
582 return i;
583 if (!recursive)
584 return -1;
585 if (recursive > 5) {
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
587 return -1;
588 }
589 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 for (i = 0; i < nums; i++) {
591 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
592 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
593 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200594 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
595 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200596 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 return -1;
598}
599EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/**
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
603 * @bus: the BUS
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
606 *
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
610 *
611 * Returns 0 if successful, or a negative error code.
612 */
613int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
614{
615 struct hda_bus_unsolicited *unsol;
616 unsigned int wp;
617
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200618 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200619 unsol = bus->unsol;
620 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622
623 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
624 unsol->wp = wp;
625
626 wp <<= 1;
627 unsol->queue[wp] = res;
628 unsol->queue[wp + 1] = res_ex;
629
Takashi Iwai6acaed32009-01-12 10:09:24 +0100630 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return 0;
633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100634EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800637 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
David Howellsc4028952006-11-22 14:57:56 +0000639static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David Howellsc4028952006-11-22 14:57:56 +0000641 struct hda_bus_unsolicited *unsol =
642 container_of(work, struct hda_bus_unsolicited, work);
643 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct hda_codec *codec;
645 unsigned int rp, caddr, res;
646
647 while (unsol->rp != unsol->wp) {
648 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
649 unsol->rp = rp;
650 rp <<= 1;
651 res = unsol->queue[rp];
652 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200653 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 codec = bus->caddr_tbl[caddr & 0x0f];
656 if (codec && codec->patch_ops.unsol_event)
657 codec->patch_ops.unsol_event(codec, res);
658 }
659}
660
661/*
662 * initialize unsolicited queue
663 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200664static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct hda_bus_unsolicited *unsol;
667
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100668 if (bus->unsol) /* already initialized */
669 return 0;
670
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200671 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200672 if (!unsol) {
673 snd_printk(KERN_ERR "hda_codec: "
674 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -ENOMEM;
676 }
David Howellsc4028952006-11-22 14:57:56 +0000677 INIT_WORK(&unsol->work, process_unsol_events);
678 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 bus->unsol = unsol;
680 return 0;
681}
682
683/*
684 * destructor
685 */
686static void snd_hda_codec_free(struct hda_codec *codec);
687
688static int snd_hda_bus_free(struct hda_bus *bus)
689{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100694 if (bus->workq)
695 flush_workqueue(bus->workq);
696 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200698 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snd_hda_codec_free(codec);
700 }
701 if (bus->ops.private_free)
702 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100703 if (bus->workq)
704 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(bus);
706 return 0;
707}
708
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100709static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100712 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return snd_hda_bus_free(bus);
714}
715
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200716#ifdef CONFIG_SND_HDA_HWDEP
717static int snd_hda_bus_dev_register(struct snd_device *device)
718{
719 struct hda_bus *bus = device->device_data;
720 struct hda_codec *codec;
721 list_for_each_entry(codec, &bus->codec_list, list) {
722 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100723 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200724 }
725 return 0;
726}
727#else
728#define snd_hda_bus_dev_register NULL
729#endif
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100739int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200740 const struct hda_bus_template *temp,
741 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct hda_bus *bus;
744 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200746 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .dev_free = snd_hda_bus_dev_free,
748 };
749
Takashi Iwaida3cec32008-08-08 17:12:14 +0200750 if (snd_BUG_ON(!temp))
751 return -EINVAL;
752 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (busp)
756 *busp = NULL;
757
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200758 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (bus == NULL) {
760 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
761 return -ENOMEM;
762 }
763
764 bus->card = card;
765 bus->private_data = temp->private_data;
766 bus->pci = temp->pci;
767 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100768 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 bus->ops = temp->ops;
770
Ingo Molnar62932df2006-01-16 16:34:20 +0100771 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200772 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&bus->codec_list);
774
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100775 snprintf(bus->workq_name, sizeof(bus->workq_name),
776 "hd-audio%d", card->number);
777 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100778 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100779 snd_printk(KERN_ERR "cannot create workqueue %s\n",
780 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 kfree(bus);
782 return -ENOMEM;
783 }
784
Takashi Iwai0ba21762007-04-16 11:29:14 +0200785 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
786 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_hda_bus_free(bus);
788 return err;
789 }
790 if (busp)
791 *busp = bus;
792 return 0;
793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100794EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai82467612007-07-27 19:15:54 +0200796#ifdef CONFIG_SND_HDA_GENERIC
797#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200798 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200799#else
800#define is_generic_config(codec) 0
801#endif
802
Takashi Iwai645f10c2008-11-28 15:07:37 +0100803#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100804#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
805#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#endif
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
810 * find a matching codec preset
811 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200812static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200813find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100815 struct hda_codec_preset_list *tbl;
816 const struct hda_codec_preset *preset;
817 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai82467612007-07-27 19:15:54 +0200819 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100820 return NULL; /* use the generic parser */
821
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100822 again:
823 mutex_lock(&preset_mutex);
824 list_for_each_entry(tbl, &hda_preset_tables, list) {
825 if (!try_module_get(tbl->owner)) {
826 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
827 continue;
828 }
829 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100831 if (preset->afg && preset->afg != codec->afg)
832 continue;
833 if (preset->mfg && preset->mfg != codec->mfg)
834 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200837 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 preset->rev == codec->revision_id)) {
840 mutex_unlock(&preset_mutex);
841 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 module_put(tbl->owner);
846 }
847 mutex_unlock(&preset_mutex);
848
849 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
850 char name[32];
851 if (!mod_requested)
852 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
853 codec->vendor_id);
854 else
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
856 (codec->vendor_id >> 16) & 0xffff);
857 request_module(name);
858 mod_requested++;
859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 return NULL;
862}
863
864/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200865 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 const struct hda_vendor_id *c;
870 const char *vendor = NULL;
871 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200872 char tmp[16];
873
874 if (codec->vendor_name)
875 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 for (c = hda_vendor_ids; c->id; c++) {
878 if (c->id == vendor_id) {
879 vendor = c->name;
880 break;
881 }
882 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200883 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sprintf(tmp, "Generic %04x", vendor_id);
885 vendor = tmp;
886 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200887 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
888 if (!codec->vendor_name)
889 return -ENOMEM;
890
891 get_chip_name:
892 if (codec->chip_name)
893 return 0;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200896 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
897 else {
898 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
899 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
900 }
901 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200902 return -ENOMEM;
903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200907 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200911 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 hda_nid_t nid;
913
914 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
915 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200916 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200917 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200918 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200919 case AC_GRP_AUDIO_FUNCTION:
920 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200921 codec->afg_function_id = function_id & 0xff;
922 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200923 break;
924 case AC_GRP_MODEM_FUNCTION:
925 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 codec->mfg_function_id = function_id & 0xff;
927 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 break;
929 default:
930 break;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100936 * read widget caps for each widget and store in cache
937 */
938static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
939{
940 int i;
941 hda_nid_t nid;
942
943 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
944 &codec->start_nid);
945 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200946 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100947 return -ENOMEM;
948 nid = codec->start_nid;
949 for (i = 0; i < codec->num_nodes; i++, nid++)
950 codec->wcaps[i] = snd_hda_param_read(codec, nid,
951 AC_PAR_AUDIO_WIDGET_CAP);
952 return 0;
953}
954
Takashi Iwai3be14142009-02-20 14:11:16 +0100955/* read all pin default configurations and save codec->init_pins */
956static int read_pin_defaults(struct hda_codec *codec)
957{
958 int i;
959 hda_nid_t nid = codec->start_nid;
960
961 for (i = 0; i < codec->num_nodes; i++, nid++) {
962 struct hda_pincfg *pin;
963 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200964 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100965 if (wid_type != AC_WID_PIN)
966 continue;
967 pin = snd_array_new(&codec->init_pins);
968 if (!pin)
969 return -ENOMEM;
970 pin->nid = nid;
971 pin->cfg = snd_hda_codec_read(codec, nid, 0,
972 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200973 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL,
975 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976 }
977 return 0;
978}
979
980/* look up the given pin config list and return the item matching with NID */
981static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
982 struct snd_array *array,
983 hda_nid_t nid)
984{
985 int i;
986 for (i = 0; i < array->used; i++) {
987 struct hda_pincfg *pin = snd_array_elem(array, i);
988 if (pin->nid == nid)
989 return pin;
990 }
991 return NULL;
992}
993
994/* write a config value for the given NID */
995static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
996 unsigned int cfg)
997{
998 int i;
999 for (i = 0; i < 4; i++) {
1000 snd_hda_codec_write(codec, nid, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1002 cfg & 0xff);
1003 cfg >>= 8;
1004 }
1005}
1006
1007/* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1009 */
1010int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1011 hda_nid_t nid, unsigned int cfg)
1012{
1013 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001014 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015
Takashi Iwaib82855a2009-12-27 11:24:56 +01001016 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1017 return -EINVAL;
1018
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001019 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001020 pin = look_up_pincfg(codec, list, nid);
1021 if (!pin) {
1022 pin = snd_array_new(list);
1023 if (!pin)
1024 return -ENOMEM;
1025 pin->nid = nid;
1026 }
1027 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001028
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1031 */
1032 cfg = snd_hda_codec_get_pincfg(codec, nid);
1033 if (oldcfg != cfg)
1034 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035 return 0;
1036}
1037
Takashi Iwaid5191e52009-11-16 14:58:17 +01001038/**
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1043 *
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1047 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001048int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1049 hda_nid_t nid, unsigned int cfg)
1050{
Takashi Iwai346ff702009-02-23 09:42:57 +01001051 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001052}
1053EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1054
Takashi Iwaid5191e52009-11-16 14:58:17 +01001055/**
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1059 *
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1063 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001064unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwai3be14142009-02-20 14:11:16 +01001068#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001069 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 if (pin)
1071 return pin->cfg;
1072#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001073 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1074 if (pin)
1075 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1077 if (pin)
1078 return pin->cfg;
1079 return 0;
1080}
1081EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1082
1083/* restore all current pin configs */
1084static void restore_pincfgs(struct hda_codec *codec)
1085{
1086 int i;
1087 for (i = 0; i < codec->init_pins.used; i++) {
1088 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1089 set_pincfg(codec, pin->nid,
1090 snd_hda_codec_get_pincfg(codec, pin->nid));
1091 }
1092}
Takashi Iwai54d17402005-11-21 16:33:22 +01001093
Takashi Iwai92ee6162009-12-27 11:18:59 +01001094/**
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1097 *
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1100 */
1101void snd_hda_shutup_pins(struct hda_codec *codec)
1102{
1103 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1106 */
1107 if (codec->bus->shutdown)
1108 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001109 for (i = 0; i < codec->init_pins.used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec, pin->nid, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1114 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001115 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001116}
1117EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1118
Takashi Iwai2a439522011-07-26 09:52:50 +02001119#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001120/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121static void restore_shutup_pins(struct hda_codec *codec)
1122{
1123 int i;
1124 if (!codec->pins_shutup)
1125 return;
1126 if (codec->bus->shutdown)
1127 return;
1128 for (i = 0; i < codec->init_pins.used; i++) {
1129 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1130 snd_hda_codec_write(codec, pin->nid, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL,
1132 pin->ctrl);
1133 }
1134 codec->pins_shutup = 0;
1135}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001136#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001137
Takashi Iwai01751f52007-08-10 16:59:39 +02001138static void init_hda_cache(struct hda_cache_rec *cache,
1139 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001140static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001141
Takashi Iwai3be14142009-02-20 14:11:16 +01001142/* restore the initial pin cfgs and release all pincfg lists */
1143static void restore_init_pincfgs(struct hda_codec *codec)
1144{
Takashi Iwai346ff702009-02-23 09:42:57 +01001145 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001146 * so that only the values in init_pins are restored
1147 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001148 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001149#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#endif
1152 restore_pincfgs(codec);
1153 snd_array_free(&codec->init_pins);
1154}
1155
Takashi Iwai54d17402005-11-21 16:33:22 +01001156/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001157 * audio-converter setup caches
1158 */
1159struct hda_cvt_setup {
1160 hda_nid_t nid;
1161 u8 stream_tag;
1162 u8 channel_id;
1163 u16 format_id;
1164 unsigned char active; /* cvt is currently used */
1165 unsigned char dirty; /* setups should be cleared */
1166};
1167
1168/* get or create a cache entry for the given audio converter NID */
1169static struct hda_cvt_setup *
1170get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1171{
1172 struct hda_cvt_setup *p;
1173 int i;
1174
1175 for (i = 0; i < codec->cvt_setups.used; i++) {
1176 p = snd_array_elem(&codec->cvt_setups, i);
1177 if (p->nid == nid)
1178 return p;
1179 }
1180 p = snd_array_new(&codec->cvt_setups);
1181 if (p)
1182 p->nid = nid;
1183 return p;
1184}
1185
1186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * codec destructor
1188 */
1189static void snd_hda_codec_free(struct hda_codec *codec)
1190{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001193 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001194#ifdef CONFIG_SND_HDA_POWER_SAVE
1195 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001196 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001199 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001200 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001201 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001202 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 codec->bus->caddr_tbl[codec->addr] = NULL;
1204 if (codec->patch_ops.free)
1205 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001206 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001207 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001208 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001209 kfree(codec->vendor_name);
1210 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001211 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001212 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 kfree(codec);
1214}
1215
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001216static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1217 unsigned int power_state);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/**
1220 * snd_hda_codec_new - create a HDA codec
1221 * @bus: the bus to assign
1222 * @codec_addr: the codec address
1223 * @codecp: the pointer to store the generated codec
1224 *
1225 * Returns 0 if successful, or a negative error code.
1226 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001227int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1228 unsigned int codec_addr,
1229 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001232 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err;
1234
Takashi Iwaida3cec32008-08-08 17:12:14 +02001235 if (snd_BUG_ON(!bus))
1236 return -EINVAL;
1237 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1238 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001241 snd_printk(KERN_ERR "hda_codec: "
1242 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EBUSY;
1244 }
1245
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001246 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (codec == NULL) {
1248 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1249 return -ENOMEM;
1250 }
1251
1252 codec->bus = bus;
1253 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001254 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001255 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001256 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001257 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001258 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1259 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001260 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001261 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001262 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001263 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001264 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001265 if (codec->bus->modelname) {
1266 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1267 if (!codec->modelname) {
1268 snd_hda_codec_free(codec);
1269 return -ENODEV;
1270 }
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Takashi Iwaicb53c622007-08-10 17:21:45 +02001273#ifdef CONFIG_SND_HDA_POWER_SAVE
1274 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1275 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1276 * the caller has to power down appropriatley after initialization
1277 * phase.
1278 */
1279 hda_keep_power_on(codec);
1280#endif
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 list_add_tail(&codec->list, &bus->codec_list);
1283 bus->caddr_tbl[codec_addr] = codec;
1284
Takashi Iwai0ba21762007-04-16 11:29:14 +02001285 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1286 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001287 if (codec->vendor_id == -1)
1288 /* read again, hopefully the access method was corrected
1289 * in the last read...
1290 */
1291 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1292 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001293 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1294 AC_PAR_SUBSYSTEM_ID);
1295 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001298 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001299 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001300 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001301 err = -ENODEV;
1302 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1306 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001307 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001309 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 err = read_pin_defaults(codec);
1311 if (err < 0)
1312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001315 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001316 codec->subsystem_id =
1317 snd_hda_codec_read(codec, nid, 0,
1318 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001319 }
1320
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001321 /* power-up all before initialization */
1322 hda_set_power_state(codec,
1323 codec->afg ? codec->afg : codec->mfg,
1324 AC_PWRST_D0);
1325
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001326 snd_hda_codec_proc_new(codec);
1327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001329
1330 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1331 codec->subsystem_id, codec->revision_id);
1332 snd_component_add(codec->bus->card, component);
1333
1334 if (codecp)
1335 *codecp = codec;
1336 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001337
1338 error:
1339 snd_hda_codec_free(codec);
1340 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001342EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001343
Takashi Iwaid5191e52009-11-16 14:58:17 +01001344/**
1345 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1346 * @codec: the HDA codec
1347 *
1348 * Start parsing of the given codec tree and (re-)initialize the whole
1349 * patch instance.
1350 *
1351 * Returns 0 if successful or a negative error code.
1352 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353int snd_hda_codec_configure(struct hda_codec *codec)
1354{
1355 int err;
1356
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001357 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001358 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001359 err = get_codec_name(codec);
1360 if (err < 0)
1361 return err;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Takashi Iwai82467612007-07-27 19:15:54 +02001364 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001366 goto patched;
1367 }
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (codec->preset && codec->preset->patch) {
1369 err = codec->preset->patch(codec);
1370 goto patched;
1371 }
1372
1373 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001374 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001375 if (err < 0)
1376 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001377
1378 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001379 if (!err && codec->patch_ops.unsol_event)
1380 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001381 /* audio codec should override the mixer name */
1382 if (!err && (codec->afg || !*codec->bus->card->mixername))
1383 snprintf(codec->bus->card->mixername,
1384 sizeof(codec->bus->card->mixername),
1385 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001388EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/**
1391 * snd_hda_codec_setup_stream - set up the codec for streaming
1392 * @codec: the CODEC to set up
1393 * @nid: the NID to set up
1394 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1395 * @channel_id: channel id to pass, zero based.
1396 * @format: stream format.
1397 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1399 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int channel_id, int format)
1401{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001402 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001403 struct hda_cvt_setup *p;
1404 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001405 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001406 int i;
1407
Takashi Iwai0ba21762007-04-16 11:29:14 +02001408 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001409 return;
1410
Takashi Iwai0ba21762007-04-16 11:29:14 +02001411 snd_printdd("hda_codec_setup_stream: "
1412 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001414 p = get_hda_cvt_setup(codec, nid);
1415 if (!p)
1416 return;
1417 /* update the stream-id if changed */
1418 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1419 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1420 newval = (stream_tag << 4) | channel_id;
1421 if (oldval != newval)
1422 snd_hda_codec_write(codec, nid, 0,
1423 AC_VERB_SET_CHANNEL_STREAMID,
1424 newval);
1425 p->stream_tag = stream_tag;
1426 p->channel_id = channel_id;
1427 }
1428 /* update the format-id if changed */
1429 if (p->format_id != format) {
1430 oldval = snd_hda_codec_read(codec, nid, 0,
1431 AC_VERB_GET_STREAM_FORMAT, 0);
1432 if (oldval != format) {
1433 msleep(1);
1434 snd_hda_codec_write(codec, nid, 0,
1435 AC_VERB_SET_STREAM_FORMAT,
1436 format);
1437 }
1438 p->format_id = format;
1439 }
1440 p->active = 1;
1441 p->dirty = 0;
1442
1443 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001444 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001445 list_for_each_entry(c, &codec->bus->codec_list, list) {
1446 for (i = 0; i < c->cvt_setups.used; i++) {
1447 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001448 if (!p->active && p->stream_tag == stream_tag &&
1449 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001450 p->dirty = 1;
1451 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001454EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Takashi Iwaif0cea792010-08-13 11:56:53 +02001456static void really_cleanup_stream(struct hda_codec *codec,
1457 struct hda_cvt_setup *q);
1458
Takashi Iwaid5191e52009-11-16 14:58:17 +01001459/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001461 * @codec: the CODEC to clean up
1462 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001463 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001464 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001465void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1466 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001467{
Takashi Iwaieb541332010-08-06 13:48:11 +02001468 struct hda_cvt_setup *p;
1469
Takashi Iwai888afa12008-03-18 09:57:50 +01001470 if (!nid)
1471 return;
1472
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001473 if (codec->no_sticky_stream)
1474 do_now = 1;
1475
Takashi Iwai888afa12008-03-18 09:57:50 +01001476 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001477 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001478 if (p) {
1479 /* here we just clear the active flag when do_now isn't set;
1480 * actual clean-ups will be done later in
1481 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1482 */
1483 if (do_now)
1484 really_cleanup_stream(codec, p);
1485 else
1486 p->active = 0;
1487 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001488}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001489EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001490
Takashi Iwaieb541332010-08-06 13:48:11 +02001491static void really_cleanup_stream(struct hda_codec *codec,
1492 struct hda_cvt_setup *q)
1493{
1494 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001495 if (q->stream_tag || q->channel_id)
1496 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1497 if (q->format_id)
1498 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1499);
Takashi Iwaieb541332010-08-06 13:48:11 +02001500 memset(q, 0, sizeof(*q));
1501 q->nid = nid;
1502}
1503
1504/* clean up the all conflicting obsolete streams */
1505static void purify_inactive_streams(struct hda_codec *codec)
1506{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001507 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001508 int i;
1509
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001510 list_for_each_entry(c, &codec->bus->codec_list, list) {
1511 for (i = 0; i < c->cvt_setups.used; i++) {
1512 struct hda_cvt_setup *p;
1513 p = snd_array_elem(&c->cvt_setups, i);
1514 if (p->dirty)
1515 really_cleanup_stream(c, p);
1516 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001517 }
1518}
1519
Takashi Iwai2a439522011-07-26 09:52:50 +02001520#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001521/* clean up all streams; called from suspend */
1522static void hda_cleanup_all_streams(struct hda_codec *codec)
1523{
1524 int i;
1525
1526 for (i = 0; i < codec->cvt_setups.used; i++) {
1527 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1528 if (p->stream_tag)
1529 really_cleanup_stream(codec, p);
1530 }
1531}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001532#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/*
1535 * amp access functions
1536 */
1537
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001538/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001539#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001540#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001541#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1542#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001544#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001547static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001548 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Takashi Iwai01751f52007-08-10 16:59:39 +02001550 memset(cache, 0, sizeof(*cache));
1551 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001552 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001553}
1554
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001555static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001556{
Takashi Iwai603c4012008-07-30 15:01:44 +02001557 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001561static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwai01751f52007-08-10 16:59:39 +02001563 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1564 u16 cur = cache->hash[idx];
1565 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001568 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (info->key == key)
1570 return info;
1571 cur = info->next;
1572 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001573 return NULL;
1574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001576/* query the hash. allocate an entry if not found. */
1577static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1578 u32 key)
1579{
1580 struct hda_cache_head *info = get_hash(cache, key);
1581 if (!info) {
1582 u16 idx, cur;
1583 /* add a new hash entry */
1584 info = snd_array_new(&cache->buf);
1585 if (!info)
1586 return NULL;
1587 cur = snd_array_index(&cache->buf, info);
1588 info->key = key;
1589 info->val = 0;
1590 idx = key % (u16)ARRAY_SIZE(cache->hash);
1591 info->next = cache->hash[idx];
1592 cache->hash[idx] = cur;
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return info;
1595}
1596
Takashi Iwai01751f52007-08-10 16:59:39 +02001597/* query and allocate an amp hash entry */
1598static inline struct hda_amp_info *
1599get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1600{
1601 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1602}
1603
Takashi Iwaid5191e52009-11-16 14:58:17 +01001604/**
1605 * query_amp_caps - query AMP capabilities
1606 * @codec: the HD-auio codec
1607 * @nid: the NID to query
1608 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1609 *
1610 * Query AMP capabilities for the given widget and direction.
1611 * Returns the obtained capability bits.
1612 *
1613 * When cap bits have been already read, this doesn't read again but
1614 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001616u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001618 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Takashi Iwai0ba21762007-04-16 11:29:14 +02001620 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1621 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001623 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626 info->amp_caps = snd_hda_param_read(codec, nid,
1627 direction == HDA_OUTPUT ?
1628 AC_PAR_AMP_OUT_CAP :
1629 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001630 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001631 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 return info->amp_caps;
1634}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001635EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Takashi Iwaid5191e52009-11-16 14:58:17 +01001637/**
1638 * snd_hda_override_amp_caps - Override the AMP capabilities
1639 * @codec: the CODEC to clean up
1640 * @nid: the NID to clean up
1641 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1642 * @caps: the capability bits to set
1643 *
1644 * Override the cached AMP caps bits value by the given one.
1645 * This function is useful if the driver needs to adjust the AMP ranges,
1646 * e.g. limit to 0dB, etc.
1647 *
1648 * Returns zero if successful or a negative error code.
1649 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001650int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1651 unsigned int caps)
1652{
1653 struct hda_amp_info *info;
1654
1655 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1656 if (!info)
1657 return -EINVAL;
1658 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001659 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001660 return 0;
1661}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001662EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001663
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001664static unsigned int
1665query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1666 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001667{
1668 struct hda_amp_info *info;
1669
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001670 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001671 if (!info)
1672 return 0;
1673 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001674 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001675 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001676 }
1677 return info->amp_caps;
1678}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001679
1680static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1681{
1682 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1683}
1684
Takashi Iwaid5191e52009-11-16 14:58:17 +01001685/**
1686 * snd_hda_query_pin_caps - Query PIN capabilities
1687 * @codec: the HD-auio codec
1688 * @nid: the NID to query
1689 *
1690 * Query PIN capabilities for the given widget.
1691 * Returns the obtained capability bits.
1692 *
1693 * When cap bits have been already read, this doesn't read again but
1694 * returns the cached value.
1695 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001696u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1697{
1698 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1699 read_pin_cap);
1700}
Takashi Iwai1327a322009-03-23 13:07:47 +01001701EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1702
Wu Fengguang864f92b2009-11-18 12:38:02 +08001703/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001704 * snd_hda_override_pin_caps - Override the pin capabilities
1705 * @codec: the CODEC
1706 * @nid: the NID to override
1707 * @caps: the capability bits to set
1708 *
1709 * Override the cached PIN capabilitiy bits value by the given one.
1710 *
1711 * Returns zero if successful or a negative error code.
1712 */
1713int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1714 unsigned int caps)
1715{
1716 struct hda_amp_info *info;
1717 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1718 if (!info)
1719 return -ENOMEM;
1720 info->amp_caps = caps;
1721 info->head.val |= INFO_AMP_CAPS;
1722 return 0;
1723}
1724EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1725
1726/**
Wu Fengguang864f92b2009-11-18 12:38:02 +08001727 * snd_hda_pin_sense - execute pin sense measurement
1728 * @codec: the CODEC to sense
1729 * @nid: the pin NID to sense
1730 *
1731 * Execute necessary pin sense measurement and return its Presence Detect,
1732 * Impedance, ELD Valid etc. status bits.
1733 */
1734u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1735{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001736 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001737
Takashi Iwai729d55b2009-12-25 22:49:01 +01001738 if (!codec->no_trigger_sense) {
1739 pincap = snd_hda_query_pin_caps(codec, nid);
1740 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001741 snd_hda_codec_read(codec, nid, 0,
1742 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001743 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001744 return snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_PIN_SENSE, 0);
1746}
1747EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1748
1749/**
1750 * snd_hda_jack_detect - query pin Presence Detect status
1751 * @codec: the CODEC to sense
1752 * @nid: the pin NID to sense
1753 *
1754 * Query and return the pin's Presence Detect status.
1755 */
1756int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1757{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001758 u32 sense = snd_hda_pin_sense(codec, nid);
1759 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001760}
1761EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/*
1764 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001765 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001767static unsigned int get_vol_mute(struct hda_codec *codec,
1768 struct hda_amp_info *info, hda_nid_t nid,
1769 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 u32 val, parm;
1772
Takashi Iwai01751f52007-08-10 16:59:39 +02001773 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001774 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1777 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1778 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001779 val = snd_hda_codec_read(codec, nid, 0,
1780 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001782 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001783 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001787 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001789static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001790 hda_nid_t nid, int ch, int direction, int index,
1791 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 u32 parm;
1794
1795 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1796 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1797 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1798 parm |= val;
1799 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001800 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Takashi Iwaid5191e52009-11-16 14:58:17 +01001803/**
1804 * snd_hda_codec_amp_read - Read AMP value
1805 * @codec: HD-audio codec
1806 * @nid: NID to read the AMP value
1807 * @ch: channel (left=0 or right=1)
1808 * @direction: #HDA_INPUT or #HDA_OUTPUT
1809 * @index: the index value (only for input direction)
1810 *
1811 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Takashi Iwai834be882006-03-01 14:16:17 +01001813int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1814 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001816 struct hda_amp_info *info;
1817 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1818 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001820 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001822EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Takashi Iwaid5191e52009-11-16 14:58:17 +01001824/**
1825 * snd_hda_codec_amp_update - update the AMP value
1826 * @codec: HD-audio codec
1827 * @nid: NID to read the AMP value
1828 * @ch: channel (left=0 or right=1)
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1833 *
1834 * Update the AMP value with a bit mask.
1835 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001836 */
Takashi Iwai834be882006-03-01 14:16:17 +01001837int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1838 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001840 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001841
Takashi Iwai0ba21762007-04-16 11:29:14 +02001842 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1843 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001845 if (snd_BUG_ON(mask & ~0xff))
1846 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001847 val &= mask;
1848 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001849 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001851 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 1;
1853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Takashi Iwaid5191e52009-11-16 14:58:17 +01001856/**
1857 * snd_hda_codec_amp_stereo - update the AMP stereo values
1858 * @codec: HD-audio codec
1859 * @nid: NID to read the AMP value
1860 * @direction: #HDA_INPUT or #HDA_OUTPUT
1861 * @idx: the index value (only for input direction)
1862 * @mask: bit mask to set
1863 * @val: the bits value to set
1864 *
1865 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1866 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001867 */
1868int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1869 int direction, int idx, int mask, int val)
1870{
1871 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001872
1873 if (snd_BUG_ON(mask & ~0xff))
1874 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001875 for (ch = 0; ch < 2; ch++)
1876 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1877 idx, mask, val);
1878 return ret;
1879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001880EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001881
Takashi Iwai2a439522011-07-26 09:52:50 +02001882#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001883/**
1884 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1885 * @codec: HD-audio codec
1886 *
1887 * Resume the all amp commands from the cache.
1888 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001889void snd_hda_codec_resume_amp(struct hda_codec *codec)
1890{
Takashi Iwai603c4012008-07-30 15:01:44 +02001891 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001892 int i;
1893
Takashi Iwai603c4012008-07-30 15:01:44 +02001894 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001895 u32 key = buffer->head.key;
1896 hda_nid_t nid;
1897 unsigned int idx, dir, ch;
1898 if (!key)
1899 continue;
1900 nid = key & 0xff;
1901 idx = (key >> 16) & 0xff;
1902 dir = (key >> 24) & 0xff;
1903 for (ch = 0; ch < 2; ch++) {
1904 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1905 continue;
1906 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1907 buffer->vol[ch]);
1908 }
1909 }
1910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001911EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001912#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001914static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1915 unsigned int ofs)
1916{
1917 u32 caps = query_amp_caps(codec, nid, dir);
1918 /* get num steps */
1919 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1920 if (ofs < caps)
1921 caps -= ofs;
1922 return caps;
1923}
1924
Takashi Iwaid5191e52009-11-16 14:58:17 +01001925/**
1926 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1927 *
1928 * The control element is supposed to have the private_value field
1929 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1930 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001931int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1935 u16 nid = get_amp_nid(kcontrol);
1936 u8 chs = get_amp_channels(kcontrol);
1937 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001938 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001940 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1941 uinfo->count = chs == 3 ? 2 : 1;
1942 uinfo->value.integer.min = 0;
1943 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1944 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001945 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001946 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1947 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -EINVAL;
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
1951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001952EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001954
1955static inline unsigned int
1956read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1957 int ch, int dir, int idx, unsigned int ofs)
1958{
1959 unsigned int val;
1960 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1961 val &= HDA_AMP_VOLMASK;
1962 if (val >= ofs)
1963 val -= ofs;
1964 else
1965 val = 0;
1966 return val;
1967}
1968
1969static inline int
1970update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1971 int ch, int dir, int idx, unsigned int ofs,
1972 unsigned int val)
1973{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001974 unsigned int maxval;
1975
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001976 if (val > 0)
1977 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001978 /* ofs = 0: raw max value */
1979 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001980 if (val > maxval)
1981 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001982 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1983 HDA_AMP_VOLMASK, val);
1984}
1985
Takashi Iwaid5191e52009-11-16 14:58:17 +01001986/**
1987 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1988 *
1989 * The control element is supposed to have the private_value field
1990 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1991 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001992int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1996 hda_nid_t nid = get_amp_nid(kcontrol);
1997 int chs = get_amp_channels(kcontrol);
1998 int dir = get_amp_direction(kcontrol);
1999 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 long *valp = ucontrol->value.integer.value;
2002
2003 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002004 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 0;
2008}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002009EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Takashi Iwaid5191e52009-11-16 14:58:17 +01002011/**
2012 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2013 *
2014 * The control element is supposed to have the private_value field
2015 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2016 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002017int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2021 hda_nid_t nid = get_amp_nid(kcontrol);
2022 int chs = get_amp_channels(kcontrol);
2023 int dir = get_amp_direction(kcontrol);
2024 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002025 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 long *valp = ucontrol->value.integer.value;
2027 int change = 0;
2028
Takashi Iwaicb53c622007-08-10 17:21:45 +02002029 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002030 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002031 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002032 valp++;
2033 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002034 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002035 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002036 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return change;
2038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002039EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Takashi Iwaid5191e52009-11-16 14:58:17 +01002041/**
2042 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2043 *
2044 * The control element is supposed to have the private_value field
2045 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2046 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002047int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2048 unsigned int size, unsigned int __user *_tlv)
2049{
2050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2051 hda_nid_t nid = get_amp_nid(kcontrol);
2052 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002053 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002054 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002055 u32 caps, val1, val2;
2056
2057 if (size < 4 * sizeof(unsigned int))
2058 return -ENOMEM;
2059 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002060 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2061 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002062 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002063 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002064 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002065 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002066 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002067 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2068 return -EFAULT;
2069 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2070 return -EFAULT;
2071 if (put_user(val1, _tlv + 2))
2072 return -EFAULT;
2073 if (put_user(val2, _tlv + 3))
2074 return -EFAULT;
2075 return 0;
2076}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002077EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2081 * @codec: HD-audio codec
2082 * @nid: NID of a reference widget
2083 * @dir: #HDA_INPUT or #HDA_OUTPUT
2084 * @tlv: TLV data to be stored, at least 4 elements
2085 *
2086 * Set (static) TLV data for a virtual master volume using the AMP caps
2087 * obtained from the reference NID.
2088 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002089 */
2090void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2091 unsigned int *tlv)
2092{
2093 u32 caps;
2094 int nums, step;
2095
2096 caps = query_amp_caps(codec, nid, dir);
2097 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2098 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2099 step = (step + 1) * 25;
2100 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2101 tlv[1] = 2 * sizeof(unsigned int);
2102 tlv[2] = -nums * step;
2103 tlv[3] = step;
2104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002105EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002106
2107/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002108static struct snd_kcontrol *
2109_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2110 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002111{
2112 struct snd_ctl_elem_id id;
2113 memset(&id, 0, sizeof(id));
2114 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002115 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002116 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2117 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002118 strcpy(id.name, name);
2119 return snd_ctl_find_id(codec->bus->card, &id);
2120}
2121
Takashi Iwaid5191e52009-11-16 14:58:17 +01002122/**
2123 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2124 * @codec: HD-audio codec
2125 * @name: ctl id name string
2126 *
2127 * Get the control element with the given id string and IFACE_MIXER.
2128 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002129struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2130 const char *name)
2131{
2132 return _snd_hda_find_mixer_ctl(codec, name, 0);
2133}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002134EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002135
Takashi Iwai1afe2062010-12-23 10:17:52 +01002136static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2137{
2138 int idx;
2139 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2140 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2141 return idx;
2142 }
2143 return -EBUSY;
2144}
2145
Takashi Iwaid5191e52009-11-16 14:58:17 +01002146/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002147 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002148 * @codec: HD-audio codec
2149 * @nid: corresponding NID (optional)
2150 * @kctl: the control element to assign
2151 *
2152 * Add the given control element to an array inside the codec instance.
2153 * All control elements belonging to a codec are supposed to be added
2154 * by this function so that a proper clean-up works at the free or
2155 * reconfiguration time.
2156 *
2157 * If non-zero @nid is passed, the NID is assigned to the control element.
2158 * The assignment is shown in the codec proc file.
2159 *
2160 * snd_hda_ctl_add() checks the control subdev id field whether
2161 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002162 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2163 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002165int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2166 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002167{
2168 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002169 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002170 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002171
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002172 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002173 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002174 if (nid == 0)
2175 nid = get_amp_nid_(kctl->private_value);
2176 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002177 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2178 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002179 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002180 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002181 err = snd_ctl_add(codec->bus->card, kctl);
2182 if (err < 0)
2183 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002184 item = snd_array_new(&codec->mixers);
2185 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002186 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002187 item->kctl = kctl;
2188 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002189 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002190 return 0;
2191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002192EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002193
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002195 * snd_hda_add_nid - Assign a NID to a control element
2196 * @codec: HD-audio codec
2197 * @nid: corresponding NID (optional)
2198 * @kctl: the control element to assign
2199 * @index: index to kctl
2200 *
2201 * Add the given control element to an array inside the codec instance.
2202 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2203 * NID:KCTL mapping - for example "Capture Source" selector.
2204 */
2205int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2206 unsigned int index, hda_nid_t nid)
2207{
2208 struct hda_nid_item *item;
2209
2210 if (nid > 0) {
2211 item = snd_array_new(&codec->nids);
2212 if (!item)
2213 return -ENOMEM;
2214 item->kctl = kctl;
2215 item->index = index;
2216 item->nid = nid;
2217 return 0;
2218 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002219 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2220 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002221 return -EINVAL;
2222}
2223EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2224
2225/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002226 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2227 * @codec: HD-audio codec
2228 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002229void snd_hda_ctls_clear(struct hda_codec *codec)
2230{
2231 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002232 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002233 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002234 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002235 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002236 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002237}
2238
Takashi Iwaia65d6292009-02-23 16:57:04 +01002239/* pseudo device locking
2240 * toggle card->shutdown to allow/disallow the device access (as a hack)
2241 */
2242static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002243{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002244 spin_lock(&card->files_lock);
2245 if (card->shutdown) {
2246 spin_unlock(&card->files_lock);
2247 return -EINVAL;
2248 }
2249 card->shutdown = 1;
2250 spin_unlock(&card->files_lock);
2251 return 0;
2252}
2253
2254static void hda_unlock_devices(struct snd_card *card)
2255{
2256 spin_lock(&card->files_lock);
2257 card->shutdown = 0;
2258 spin_unlock(&card->files_lock);
2259}
2260
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261/**
2262 * snd_hda_codec_reset - Clear all objects assigned to the codec
2263 * @codec: HD-audio codec
2264 *
2265 * This frees the all PCM and control elements assigned to the codec, and
2266 * clears the caches and restores the pin default configurations.
2267 *
2268 * When a device is being used, it returns -EBSY. If successfully freed,
2269 * returns zero.
2270 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002271int snd_hda_codec_reset(struct hda_codec *codec)
2272{
2273 struct snd_card *card = codec->bus->card;
2274 int i, pcm;
2275
2276 if (hda_lock_devices(card) < 0)
2277 return -EBUSY;
2278 /* check whether the codec isn't used by any mixer or PCM streams */
2279 if (!list_empty(&card->ctl_files)) {
2280 hda_unlock_devices(card);
2281 return -EBUSY;
2282 }
2283 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2284 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2285 if (!cpcm->pcm)
2286 continue;
2287 if (cpcm->pcm->streams[0].substream_opened ||
2288 cpcm->pcm->streams[1].substream_opened) {
2289 hda_unlock_devices(card);
2290 return -EBUSY;
2291 }
2292 }
2293
2294 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002295
2296#ifdef CONFIG_SND_HDA_POWER_SAVE
2297 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002298 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002299#endif
2300 snd_hda_ctls_clear(codec);
2301 /* relase PCMs */
2302 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002303 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002304 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002305 clear_bit(codec->pcm_info[i].device,
2306 codec->bus->pcm_dev_bits);
2307 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002308 }
2309 if (codec->patch_ops.free)
2310 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002311 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002312 codec->spec = NULL;
2313 free_hda_cache(&codec->amp_cache);
2314 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002315 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2316 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002317 /* free only driver_pins so that init_pins + user_pins are restored */
2318 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002319 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002320 codec->num_pcms = 0;
2321 codec->pcm_info = NULL;
2322 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002323 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2324 codec->slave_dig_outs = NULL;
2325 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002326 module_put(codec->owner);
2327 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002328
2329 /* allow device access again */
2330 hda_unlock_devices(card);
2331 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002332}
2333
Takashi Iwaid5191e52009-11-16 14:58:17 +01002334/**
2335 * snd_hda_add_vmaster - create a virtual master control and add slaves
2336 * @codec: HD-audio codec
2337 * @name: vmaster control name
2338 * @tlv: TLV data (optional)
2339 * @slaves: slave control names (optional)
2340 *
2341 * Create a virtual master control with the given name. The TLV data
2342 * must be either NULL or a valid data.
2343 *
2344 * @slaves is a NULL-terminated array of strings, each of which is a
2345 * slave control name. All controls with these names are assigned to
2346 * the new virtual master control.
2347 *
2348 * This function returns zero if successful or a negative error code.
2349 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002350int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002351 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002352{
2353 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002354 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002355 int err;
2356
Takashi Iwai2f085542008-02-22 18:43:50 +01002357 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2358 ;
2359 if (!*s) {
2360 snd_printdd("No slave found for %s\n", name);
2361 return 0;
2362 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002363 kctl = snd_ctl_make_virtual_master(name, tlv);
2364 if (!kctl)
2365 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002366 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002367 if (err < 0)
2368 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002369
Takashi Iwai2134ea42008-01-10 16:53:55 +01002370 for (s = slaves; *s; s++) {
2371 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002372 int i = 0;
2373 for (;;) {
2374 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2375 if (!sctl) {
2376 if (!i)
2377 snd_printdd("Cannot find slave %s, "
2378 "skipped\n", *s);
2379 break;
2380 }
2381 err = snd_ctl_add_slave(kctl, sctl);
2382 if (err < 0)
2383 return err;
2384 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002385 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002386 }
2387 return 0;
2388}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002389EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002390
Takashi Iwaid5191e52009-11-16 14:58:17 +01002391/**
2392 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2393 *
2394 * The control element is supposed to have the private_value field
2395 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002397int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2398 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
2400 int chs = get_amp_channels(kcontrol);
2401
2402 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2403 uinfo->count = chs == 3 ? 2 : 1;
2404 uinfo->value.integer.min = 0;
2405 uinfo->value.integer.max = 1;
2406 return 0;
2407}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002408EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Takashi Iwaid5191e52009-11-16 14:58:17 +01002410/**
2411 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2412 *
2413 * The control element is supposed to have the private_value field
2414 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2415 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002416int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 hda_nid_t nid = get_amp_nid(kcontrol);
2421 int chs = get_amp_channels(kcontrol);
2422 int dir = get_amp_direction(kcontrol);
2423 int idx = get_amp_index(kcontrol);
2424 long *valp = ucontrol->value.integer.value;
2425
2426 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002427 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002428 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002430 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002431 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return 0;
2433}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002434EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Takashi Iwaid5191e52009-11-16 14:58:17 +01002436/**
2437 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2438 *
2439 * The control element is supposed to have the private_value field
2440 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2441 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002442int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446 hda_nid_t nid = get_amp_nid(kcontrol);
2447 int chs = get_amp_channels(kcontrol);
2448 int dir = get_amp_direction(kcontrol);
2449 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 long *valp = ucontrol->value.integer.value;
2451 int change = 0;
2452
Takashi Iwaicb53c622007-08-10 17:21:45 +02002453 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002454 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002455 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002456 HDA_AMP_MUTE,
2457 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002458 valp++;
2459 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002460 if (chs & 2)
2461 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002462 HDA_AMP_MUTE,
2463 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002464 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002465 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return change;
2467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002468EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Takashi Iwai67d634c2009-11-16 15:35:59 +01002470#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002471/**
2472 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2473 *
2474 * This function calls snd_hda_enable_beep_device(), which behaves differently
2475 * depending on beep_mode option.
2476 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002477int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_value *ucontrol)
2479{
2480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2481 long *valp = ucontrol->value.integer.value;
2482
2483 snd_hda_enable_beep_device(codec, *valp);
2484 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2485}
2486EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002487#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/*
Takashi Iwai985be542005-11-02 18:26:49 +01002490 * bound volume controls
2491 *
2492 * bind multiple volumes (# indices, from 0)
2493 */
2494
2495#define AMP_VAL_IDX_SHIFT 19
2496#define AMP_VAL_IDX_MASK (0x0f<<19)
2497
Takashi Iwaid5191e52009-11-16 14:58:17 +01002498/**
2499 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2500 *
2501 * The control element is supposed to have the private_value field
2502 * set up via HDA_BIND_MUTE*() macros.
2503 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002504int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002506{
2507 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2508 unsigned long pval;
2509 int err;
2510
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002511 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002512 pval = kcontrol->private_value;
2513 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2514 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2515 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002516 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002517 return err;
2518}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002519EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002520
Takashi Iwaid5191e52009-11-16 14:58:17 +01002521/**
2522 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2523 *
2524 * The control element is supposed to have the private_value field
2525 * set up via HDA_BIND_MUTE*() macros.
2526 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002527int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002529{
2530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2531 unsigned long pval;
2532 int i, indices, err = 0, change = 0;
2533
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002534 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002535 pval = kcontrol->private_value;
2536 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2537 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002538 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2539 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002540 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2541 if (err < 0)
2542 break;
2543 change |= err;
2544 }
2545 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002546 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002547 return err < 0 ? err : change;
2548}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002549EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002550
Takashi Iwaid5191e52009-11-16 14:58:17 +01002551/**
2552 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2553 *
2554 * The control element is supposed to have the private_value field
2555 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002556 */
2557int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_info *uinfo)
2559{
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2561 struct hda_bind_ctls *c;
2562 int err;
2563
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002564 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002565 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002566 kcontrol->private_value = *c->values;
2567 err = c->ops->info(kcontrol, uinfo);
2568 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002569 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002570 return err;
2571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002572EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002573
Takashi Iwaid5191e52009-11-16 14:58:17 +01002574/**
2575 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2576 *
2577 * The control element is supposed to have the private_value field
2578 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2579 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002580int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 struct hda_bind_ctls *c;
2585 int err;
2586
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002587 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002588 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002589 kcontrol->private_value = *c->values;
2590 err = c->ops->get(kcontrol, ucontrol);
2591 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002592 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002593 return err;
2594}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002595EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002596
Takashi Iwaid5191e52009-11-16 14:58:17 +01002597/**
2598 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2599 *
2600 * The control element is supposed to have the private_value field
2601 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2602 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002603int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2604 struct snd_ctl_elem_value *ucontrol)
2605{
2606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2607 struct hda_bind_ctls *c;
2608 unsigned long *vals;
2609 int err = 0, change = 0;
2610
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002611 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002612 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002613 for (vals = c->values; *vals; vals++) {
2614 kcontrol->private_value = *vals;
2615 err = c->ops->put(kcontrol, ucontrol);
2616 if (err < 0)
2617 break;
2618 change |= err;
2619 }
2620 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002621 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002622 return err < 0 ? err : change;
2623}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002624EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002625
Takashi Iwaid5191e52009-11-16 14:58:17 +01002626/**
2627 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2628 *
2629 * The control element is supposed to have the private_value field
2630 * set up via HDA_BIND_VOL() macro.
2631 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002632int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2633 unsigned int size, unsigned int __user *tlv)
2634{
2635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2636 struct hda_bind_ctls *c;
2637 int err;
2638
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002639 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002640 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002641 kcontrol->private_value = *c->values;
2642 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2643 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002644 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002645 return err;
2646}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002647EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002648
2649struct hda_ctl_ops snd_hda_bind_vol = {
2650 .info = snd_hda_mixer_amp_volume_info,
2651 .get = snd_hda_mixer_amp_volume_get,
2652 .put = snd_hda_mixer_amp_volume_put,
2653 .tlv = snd_hda_mixer_amp_tlv
2654};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002655EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002656
2657struct hda_ctl_ops snd_hda_bind_sw = {
2658 .info = snd_hda_mixer_amp_switch_info,
2659 .get = snd_hda_mixer_amp_switch_get,
2660 .put = snd_hda_mixer_amp_switch_put,
2661 .tlv = snd_hda_mixer_amp_tlv
2662};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002663EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002664
2665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 * SPDIF out controls
2667 */
2668
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2673 uinfo->count = 1;
2674 return 0;
2675}
2676
Takashi Iwai0ba21762007-04-16 11:29:14 +02002677static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2681 IEC958_AES0_NONAUDIO |
2682 IEC958_AES0_CON_EMPHASIS_5015 |
2683 IEC958_AES0_CON_NOT_COPYRIGHT;
2684 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2685 IEC958_AES1_CON_ORIGINAL;
2686 return 0;
2687}
2688
Takashi Iwai0ba21762007-04-16 11:29:14 +02002689static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2693 IEC958_AES0_NONAUDIO |
2694 IEC958_AES0_PRO_EMPHASIS_5015;
2695 return 0;
2696}
2697
Takashi Iwai0ba21762007-04-16 11:29:14 +02002698static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002702 int idx = kcontrol->private_value;
2703 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Stephen Warren7c9359762011-06-01 11:14:17 -06002705 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2706 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2707 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2708 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 return 0;
2711}
2712
2713/* convert from SPDIF status bits to HDA SPDIF bits
2714 * bit 0 (DigEn) is always set zero (to be filled later)
2715 */
2716static unsigned short convert_from_spdif_status(unsigned int sbits)
2717{
2718 unsigned short val = 0;
2719
2720 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002725 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2726 IEC958_AES0_PRO_EMPHASIS_5015)
2727 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002729 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2730 IEC958_AES0_CON_EMPHASIS_5015)
2731 val |= AC_DIG1_EMPHASIS;
2732 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2733 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002735 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2737 }
2738 return val;
2739}
2740
2741/* convert to SPDIF status bits from HDA SPDIF bits
2742 */
2743static unsigned int convert_to_spdif_status(unsigned short val)
2744{
2745 unsigned int sbits = 0;
2746
Takashi Iwai0ba21762007-04-16 11:29:14 +02002747 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 sbits |= IEC958_AES0_PROFESSIONAL;
2751 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002752 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2754 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002755 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002757 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002759 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2761 sbits |= val & (0x7f << 8);
2762 }
2763 return sbits;
2764}
2765
Takashi Iwai2f728532008-09-25 16:32:41 +02002766/* set digital convert verbs both for the given NID and its slaves */
2767static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2768 int verb, int val)
2769{
Takashi Iwaidda14412011-05-02 11:29:30 +02002770 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002771
Takashi Iwai9e976972008-11-25 08:17:20 +01002772 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002773 d = codec->slave_dig_outs;
2774 if (!d)
2775 return;
2776 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002777 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002778}
2779
2780static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2781 int dig1, int dig2)
2782{
2783 if (dig1 != -1)
2784 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2785 if (dig2 != -1)
2786 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2787}
2788
Takashi Iwai0ba21762007-04-16 11:29:14 +02002789static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002793 int idx = kcontrol->private_value;
2794 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2795 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 unsigned short val;
2797 int change;
2798
Ingo Molnar62932df2006-01-16 16:34:20 +01002799 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002800 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2802 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2803 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002804 val = convert_from_spdif_status(spdif->status);
2805 val |= spdif->ctls & 1;
2806 change = spdif->ctls != val;
2807 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002808 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002809 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002810 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return change;
2812}
2813
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002814#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Takashi Iwai0ba21762007-04-16 11:29:14 +02002816static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002820 int idx = kcontrol->private_value;
2821 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Stephen Warren7c9359762011-06-01 11:14:17 -06002823 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return 0;
2825}
2826
Stephen Warren74b654c2011-06-01 11:14:18 -06002827static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2828 int dig1, int dig2)
2829{
2830 set_dig_out_convert(codec, nid, dig1, dig2);
2831 /* unmute amp switch (if any) */
2832 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2833 (dig1 & AC_DIG1_ENABLE))
2834 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2835 HDA_AMP_MUTE, 0);
2836}
2837
Takashi Iwai0ba21762007-04-16 11:29:14 +02002838static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002842 int idx = kcontrol->private_value;
2843 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2844 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 unsigned short val;
2846 int change;
2847
Ingo Molnar62932df2006-01-16 16:34:20 +01002848 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002849 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002852 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002853 spdif->ctls = val;
2854 if (change && nid != (u16)-1)
2855 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002856 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return change;
2858}
2859
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002860static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 {
2862 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002864 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 .info = snd_hda_spdif_mask_info,
2866 .get = snd_hda_spdif_cmask_get,
2867 },
2868 {
2869 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002871 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 .info = snd_hda_spdif_mask_info,
2873 .get = snd_hda_spdif_pmask_get,
2874 },
2875 {
2876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002877 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .info = snd_hda_spdif_mask_info,
2879 .get = snd_hda_spdif_default_get,
2880 .put = snd_hda_spdif_default_put,
2881 },
2882 {
2883 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002884 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .info = snd_hda_spdif_out_switch_info,
2886 .get = snd_hda_spdif_out_switch_get,
2887 .put = snd_hda_spdif_out_switch_put,
2888 },
2889 { } /* end */
2890};
2891
2892/**
2893 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2894 * @codec: the HDA codec
2895 * @nid: audio out widget NID
2896 *
2897 * Creates controls related with the SPDIF output.
2898 * Called from each patch supporting the SPDIF out.
2899 *
2900 * Returns 0 if successful, or a negative error code.
2901 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002902int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2903 hda_nid_t associated_nid,
2904 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002907 struct snd_kcontrol *kctl;
2908 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002909 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002910 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Takashi Iwai1afe2062010-12-23 10:17:52 +01002912 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2913 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002914 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2915 return -EBUSY;
2916 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002917 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2919 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002920 if (!kctl)
2921 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002922 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002923 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002924 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002925 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return err;
2927 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002928 spdif->nid = cvt_nid;
2929 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002930 AC_VERB_GET_DIGI_CONVERT_1, 0);
2931 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return 0;
2933}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002934EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Stephen Warren7c9359762011-06-01 11:14:17 -06002936struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2937 hda_nid_t nid)
2938{
2939 int i;
2940 for (i = 0; i < codec->spdif_out.used; i++) {
2941 struct hda_spdif_out *spdif =
2942 snd_array_elem(&codec->spdif_out, i);
2943 if (spdif->nid == nid)
2944 return spdif;
2945 }
2946 return NULL;
2947}
2948EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2949
Stephen Warren74b654c2011-06-01 11:14:18 -06002950void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2951{
2952 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2953
2954 mutex_lock(&codec->spdif_mutex);
2955 spdif->nid = (u16)-1;
2956 mutex_unlock(&codec->spdif_mutex);
2957}
2958EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2959
2960void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2961{
2962 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2963 unsigned short val;
2964
2965 mutex_lock(&codec->spdif_mutex);
2966 if (spdif->nid != nid) {
2967 spdif->nid = nid;
2968 val = spdif->ctls;
2969 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2970 }
2971 mutex_unlock(&codec->spdif_mutex);
2972}
2973EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002976 * SPDIF sharing with analog output
2977 */
2978static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2979 struct snd_ctl_elem_value *ucontrol)
2980{
2981 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2982 ucontrol->value.integer.value[0] = mout->share_spdif;
2983 return 0;
2984}
2985
2986static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2987 struct snd_ctl_elem_value *ucontrol)
2988{
2989 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2990 mout->share_spdif = !!ucontrol->value.integer.value[0];
2991 return 0;
2992}
2993
2994static struct snd_kcontrol_new spdif_share_sw = {
2995 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2996 .name = "IEC958 Default PCM Playback Switch",
2997 .info = snd_ctl_boolean_mono_info,
2998 .get = spdif_share_sw_get,
2999 .put = spdif_share_sw_put,
3000};
3001
Takashi Iwaid5191e52009-11-16 14:58:17 +01003002/**
3003 * snd_hda_create_spdif_share_sw - create Default PCM switch
3004 * @codec: the HDA codec
3005 * @mout: multi-out instance
3006 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003007int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3008 struct hda_multi_out *mout)
3009{
3010 if (!mout->dig_out_nid)
3011 return 0;
3012 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003013 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3014 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003015}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003016EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003017
3018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 * SPDIF input
3020 */
3021
3022#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3023
Takashi Iwai0ba21762007-04-16 11:29:14 +02003024static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3025 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3028
3029 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3030 return 0;
3031}
3032
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3034 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3037 hda_nid_t nid = kcontrol->private_value;
3038 unsigned int val = !!ucontrol->value.integer.value[0];
3039 int change;
3040
Ingo Molnar62932df2006-01-16 16:34:20 +01003041 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003043 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003045 snd_hda_codec_write_cache(codec, nid, 0,
3046 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003048 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return change;
3050}
3051
Takashi Iwai0ba21762007-04-16 11:29:14 +02003052static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3056 hda_nid_t nid = kcontrol->private_value;
3057 unsigned short val;
3058 unsigned int sbits;
3059
Andrew Paprocki3982d172007-12-19 12:13:44 +01003060 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 sbits = convert_to_spdif_status(val);
3062 ucontrol->value.iec958.status[0] = sbits;
3063 ucontrol->value.iec958.status[1] = sbits >> 8;
3064 ucontrol->value.iec958.status[2] = sbits >> 16;
3065 ucontrol->value.iec958.status[3] = sbits >> 24;
3066 return 0;
3067}
3068
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003069static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 {
3071 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003072 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 .info = snd_hda_spdif_in_switch_info,
3074 .get = snd_hda_spdif_in_switch_get,
3075 .put = snd_hda_spdif_in_switch_put,
3076 },
3077 {
3078 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003080 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .info = snd_hda_spdif_mask_info,
3082 .get = snd_hda_spdif_in_status_get,
3083 },
3084 { } /* end */
3085};
3086
3087/**
3088 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3089 * @codec: the HDA codec
3090 * @nid: audio in widget NID
3091 *
3092 * Creates controls related with the SPDIF input.
3093 * Called from each patch supporting the SPDIF in.
3094 *
3095 * Returns 0 if successful, or a negative error code.
3096 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003097int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003100 struct snd_kcontrol *kctl;
3101 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003102 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Takashi Iwai1afe2062010-12-23 10:17:52 +01003104 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3105 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003106 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3107 return -EBUSY;
3108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3110 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003111 if (!kctl)
3112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003114 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return err;
3117 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003119 snd_hda_codec_read(codec, nid, 0,
3120 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003121 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return 0;
3123}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003124EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Takashi Iwai2a439522011-07-26 09:52:50 +02003126#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003127/*
3128 * command cache
3129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003131/* build a 32bit cache key with the widget id and the command parameter */
3132#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3133#define get_cmd_cache_nid(key) ((key) & 0xff)
3134#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3135
3136/**
3137 * snd_hda_codec_write_cache - send a single command with caching
3138 * @codec: the HDA codec
3139 * @nid: NID to send the command
3140 * @direct: direct flag
3141 * @verb: the verb to send
3142 * @parm: the parameter for the verb
3143 *
3144 * Send a single command without waiting for response.
3145 *
3146 * Returns 0 if successful, or a negative error code.
3147 */
3148int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3149 int direct, unsigned int verb, unsigned int parm)
3150{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003151 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3152 struct hda_cache_head *c;
3153 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003154
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003155 if (err < 0)
3156 return err;
3157 /* parm may contain the verb stuff for get/set amp */
3158 verb = verb | (parm >> 8);
3159 parm &= 0xff;
3160 key = build_cmd_cache_key(nid, verb);
3161 mutex_lock(&codec->bus->cmd_mutex);
3162 c = get_alloc_hash(&codec->cmd_cache, key);
3163 if (c)
3164 c->val = parm;
3165 mutex_unlock(&codec->bus->cmd_mutex);
3166 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003167}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003168EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003169
Takashi Iwaid5191e52009-11-16 14:58:17 +01003170/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003171 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3172 * @codec: the HDA codec
3173 * @nid: NID to send the command
3174 * @direct: direct flag
3175 * @verb: the verb to send
3176 * @parm: the parameter for the verb
3177 *
3178 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3179 * command if the parameter is already identical with the cached value.
3180 * If not, it sends the command and refreshes the cache.
3181 *
3182 * Returns 0 if successful, or a negative error code.
3183 */
3184int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3185 int direct, unsigned int verb, unsigned int parm)
3186{
3187 struct hda_cache_head *c;
3188 u32 key;
3189
3190 /* parm may contain the verb stuff for get/set amp */
3191 verb = verb | (parm >> 8);
3192 parm &= 0xff;
3193 key = build_cmd_cache_key(nid, verb);
3194 mutex_lock(&codec->bus->cmd_mutex);
3195 c = get_hash(&codec->cmd_cache, key);
3196 if (c && c->val == parm) {
3197 mutex_unlock(&codec->bus->cmd_mutex);
3198 return 0;
3199 }
3200 mutex_unlock(&codec->bus->cmd_mutex);
3201 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3202}
3203EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3204
3205/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003206 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3207 * @codec: HD-audio codec
3208 *
3209 * Execute all verbs recorded in the command caches to resume.
3210 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003211void snd_hda_codec_resume_cache(struct hda_codec *codec)
3212{
Takashi Iwai603c4012008-07-30 15:01:44 +02003213 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003214 int i;
3215
Takashi Iwai603c4012008-07-30 15:01:44 +02003216 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003217 u32 key = buffer->key;
3218 if (!key)
3219 continue;
3220 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3221 get_cmd_cache_cmd(key), buffer->val);
3222 }
3223}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003224EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003225
3226/**
3227 * snd_hda_sequence_write_cache - sequence writes with caching
3228 * @codec: the HDA codec
3229 * @seq: VERB array to send
3230 *
3231 * Send the commands sequentially from the given array.
3232 * Thte commands are recorded on cache for power-save and resume.
3233 * The array must be terminated with NID=0.
3234 */
3235void snd_hda_sequence_write_cache(struct hda_codec *codec,
3236 const struct hda_verb *seq)
3237{
3238 for (; seq->nid; seq++)
3239 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3240 seq->param);
3241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003242EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003243#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003244
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003245void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3246 unsigned int power_state,
3247 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003248{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003249 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003250 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003251
Takashi Iwaicb53c622007-08-10 17:21:45 +02003252 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003253 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003254 if (!(wcaps & AC_WCAP_POWER))
3255 continue;
3256 /* don't power down the widget if it controls eapd and
3257 * EAPD_BTLENABLE is set.
3258 */
3259 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3260 get_wcaps_type(wcaps) == AC_WID_PIN &&
3261 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3262 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003263 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003264 if (eapd & 0x02)
3265 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003266 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003267 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3268 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003269 }
3270
Takashi Iwaicb53c622007-08-10 17:21:45 +02003271 if (power_state == AC_PWRST_D0) {
3272 unsigned long end_time;
3273 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003274 /* wait until the codec reachs to D0 */
3275 end_time = jiffies + msecs_to_jiffies(500);
3276 do {
3277 state = snd_hda_codec_read(codec, fg, 0,
3278 AC_VERB_GET_POWER_STATE, 0);
3279 if (state == power_state)
3280 break;
3281 msleep(1);
3282 } while (time_after_eq(end_time, jiffies));
3283 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003284}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003285EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3286
3287/*
3288 * set power state of the codec
3289 */
3290static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3291 unsigned int power_state)
3292{
3293 if (codec->patch_ops.set_power_state) {
3294 codec->patch_ops.set_power_state(codec, fg, power_state);
3295 return;
3296 }
3297
3298 /* this delay seems necessary to avoid click noise at power-down */
3299 if (power_state == AC_PWRST_D3)
3300 msleep(100);
3301 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3302 power_state);
3303 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3304}
Takashi Iwai54d17402005-11-21 16:33:22 +01003305
Takashi Iwai11aeff02008-07-30 15:01:46 +02003306#ifdef CONFIG_SND_HDA_HWDEP
3307/* execute additional init verbs */
3308static void hda_exec_init_verbs(struct hda_codec *codec)
3309{
3310 if (codec->init_verbs.list)
3311 snd_hda_sequence_write(codec, codec->init_verbs.list);
3312}
3313#else
3314static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3315#endif
3316
Takashi Iwai2a439522011-07-26 09:52:50 +02003317#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003318/*
3319 * call suspend and power-down; used both from PM and power-save
3320 */
3321static void hda_call_codec_suspend(struct hda_codec *codec)
3322{
3323 if (codec->patch_ops.suspend)
3324 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003325 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003326 hda_set_power_state(codec,
3327 codec->afg ? codec->afg : codec->mfg,
3328 AC_PWRST_D3);
3329#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003330 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003331 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003332 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003333 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003334 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003335#endif
3336}
3337
3338/*
3339 * kick up codec; used both from PM and power-save
3340 */
3341static void hda_call_codec_resume(struct hda_codec *codec)
3342{
3343 hda_set_power_state(codec,
3344 codec->afg ? codec->afg : codec->mfg,
3345 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003346 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003347 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003348 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003349 if (codec->patch_ops.resume)
3350 codec->patch_ops.resume(codec);
3351 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003352 if (codec->patch_ops.init)
3353 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003354 snd_hda_codec_resume_amp(codec);
3355 snd_hda_codec_resume_cache(codec);
3356 }
3357}
Takashi Iwai2a439522011-07-26 09:52:50 +02003358#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003359
Takashi Iwai54d17402005-11-21 16:33:22 +01003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361/**
3362 * snd_hda_build_controls - build mixer controls
3363 * @bus: the BUS
3364 *
3365 * Creates mixer controls for each codec included in the bus.
3366 *
3367 * Returns 0 if successful, otherwise a negative error code.
3368 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003369int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003371 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Takashi Iwai0ba21762007-04-16 11:29:14 +02003373 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003374 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003375 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003376 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003377 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003378 err = snd_hda_codec_reset(codec);
3379 if (err < 0) {
3380 printk(KERN_ERR
3381 "hda_codec: cannot revert codec\n");
3382 return err;
3383 }
3384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003386 return 0;
3387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003388EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003390int snd_hda_codec_build_controls(struct hda_codec *codec)
3391{
3392 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003393 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003394 /* continue to initialize... */
3395 if (codec->patch_ops.init)
3396 err = codec->patch_ops.init(codec);
3397 if (!err && codec->patch_ops.build_controls)
3398 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003399 if (err < 0)
3400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return 0;
3402}
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404/*
3405 * stream formats
3406 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003407struct hda_rate_tbl {
3408 unsigned int hz;
3409 unsigned int alsa_bits;
3410 unsigned int hda_fmt;
3411};
3412
Takashi Iwai92f10b32010-08-03 14:21:00 +02003413/* rate = base * mult / div */
3414#define HDA_RATE(base, mult, div) \
3415 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3416 (((div) - 1) << AC_FMT_DIV_SHIFT))
3417
Takashi Iwaibefdf312005-08-22 13:57:55 +02003418static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003420
3421 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003422 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3423 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3424 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3425 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3426 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3427 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3428 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3429 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3430 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3431 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3432 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003433#define AC_PAR_PCM_RATE_BITS 11
3434 /* up to bits 10, 384kHZ isn't supported properly */
3435
3436 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003437 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003438
Takashi Iwaibefdf312005-08-22 13:57:55 +02003439 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440};
3441
3442/**
3443 * snd_hda_calc_stream_format - calculate format bitset
3444 * @rate: the sample rate
3445 * @channels: the number of channels
3446 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3447 * @maxbps: the max. bps
3448 *
3449 * Calculate the format bitset from the given rate, channels and th PCM format.
3450 *
3451 * Return zero if invalid.
3452 */
3453unsigned int snd_hda_calc_stream_format(unsigned int rate,
3454 unsigned int channels,
3455 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003456 unsigned int maxbps,
3457 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 int i;
3460 unsigned int val = 0;
3461
Takashi Iwaibefdf312005-08-22 13:57:55 +02003462 for (i = 0; rate_bits[i].hz; i++)
3463 if (rate_bits[i].hz == rate) {
3464 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 break;
3466 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003467 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 snd_printdd("invalid rate %d\n", rate);
3469 return 0;
3470 }
3471
3472 if (channels == 0 || channels > 8) {
3473 snd_printdd("invalid channels %d\n", channels);
3474 return 0;
3475 }
3476 val |= channels - 1;
3477
3478 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003479 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003480 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003481 break;
3482 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003483 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 case 20:
3486 case 24:
3487 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003488 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003489 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003491 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003493 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 break;
3495 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 snd_printdd("invalid format width %d\n",
3497 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 0;
3499 }
3500
Anssi Hannula32c168c2010-08-03 13:28:57 +03003501 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003502 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return val;
3505}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003506EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003508static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3509{
3510 unsigned int val = 0;
3511 if (nid != codec->afg &&
3512 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3513 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3514 if (!val || val == -1)
3515 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3516 if (!val || val == -1)
3517 return 0;
3518 return val;
3519}
3520
3521static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3522{
3523 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3524 get_pcm_param);
3525}
3526
3527static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3528{
3529 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3530 if (!streams || streams == -1)
3531 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3532 if (!streams || streams == -1)
3533 return 0;
3534 return streams;
3535}
3536
3537static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3538{
3539 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3540 get_stream_param);
3541}
3542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543/**
3544 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3545 * @codec: the HDA codec
3546 * @nid: NID to query
3547 * @ratesp: the pointer to store the detected rate bitflags
3548 * @formatsp: the pointer to store the detected formats
3549 * @bpsp: the pointer to store the detected format widths
3550 *
3551 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3552 * or @bsps argument is ignored.
3553 *
3554 * Returns 0 if successful, otherwise a negative error code.
3555 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003556int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3558{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003559 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003561 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003562 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 if (ratesp) {
3565 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003566 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003568 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003570 if (rates == 0) {
3571 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3572 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3573 nid, val,
3574 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3575 return -EIO;
3576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 *ratesp = rates;
3578 }
3579
3580 if (formatsp || bpsp) {
3581 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003582 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003584 streams = query_stream_param(codec, nid);
3585 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 bps = 0;
3589 if (streams & AC_SUPFMT_PCM) {
3590 if (val & AC_SUPPCM_BITS_8) {
3591 formats |= SNDRV_PCM_FMTBIT_U8;
3592 bps = 8;
3593 }
3594 if (val & AC_SUPPCM_BITS_16) {
3595 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3596 bps = 16;
3597 }
3598 if (wcaps & AC_WCAP_DIGITAL) {
3599 if (val & AC_SUPPCM_BITS_32)
3600 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3601 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3602 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3603 if (val & AC_SUPPCM_BITS_24)
3604 bps = 24;
3605 else if (val & AC_SUPPCM_BITS_20)
3606 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003607 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3608 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3610 if (val & AC_SUPPCM_BITS_32)
3611 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 else if (val & AC_SUPPCM_BITS_24)
3613 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003614 else if (val & AC_SUPPCM_BITS_20)
3615 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003618 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003620 if (!bps)
3621 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003622 }
3623 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003624 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 /* temporary hack: we have still no proper support
3626 * for the direct AC3 stream...
3627 */
3628 formats |= SNDRV_PCM_FMTBIT_U8;
3629 bps = 8;
3630 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003631 if (formats == 0) {
3632 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3633 "(nid=0x%x, val=0x%x, ovrd=%i, "
3634 "streams=0x%x)\n",
3635 nid, val,
3636 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3637 streams);
3638 return -EIO;
3639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 if (formatsp)
3641 *formatsp = formats;
3642 if (bpsp)
3643 *bpsp = bps;
3644 }
3645
3646 return 0;
3647}
Stephen Warren384a48d2011-06-01 11:14:21 -06003648EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003651 * snd_hda_is_supported_format - Check the validity of the format
3652 * @codec: HD-audio codec
3653 * @nid: NID to check
3654 * @format: the HD-audio format value to check
3655 *
3656 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 *
3658 * Returns 1 if supported, 0 if not.
3659 */
3660int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3661 unsigned int format)
3662{
3663 int i;
3664 unsigned int val = 0, rate, stream;
3665
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003666 val = query_pcm_param(codec, nid);
3667 if (!val)
3668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003671 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003672 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (val & (1 << i))
3674 break;
3675 return 0;
3676 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003677 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
3679
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003680 stream = query_stream_param(codec, nid);
3681 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return 0;
3683
3684 if (stream & AC_SUPFMT_PCM) {
3685 switch (format & 0xf0) {
3686 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003687 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return 0;
3689 break;
3690 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003691 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return 0;
3693 break;
3694 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003695 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return 0;
3697 break;
3698 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003699 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return 0;
3701 break;
3702 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003703 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705 break;
3706 default:
3707 return 0;
3708 }
3709 } else {
3710 /* FIXME: check for float32 and AC3? */
3711 }
3712
3713 return 1;
3714}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003715EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717/*
3718 * PCM stuff
3719 */
3720static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3721 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003722 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 return 0;
3725}
3726
3727static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3728 struct hda_codec *codec,
3729 unsigned int stream_tag,
3730 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003731 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3734 return 0;
3735}
3736
3737static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3738 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003739 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
Takashi Iwai888afa12008-03-18 09:57:50 +01003741 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return 0;
3743}
3744
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003745static int set_pcm_default_values(struct hda_codec *codec,
3746 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003748 int err;
3749
Takashi Iwai0ba21762007-04-16 11:29:14 +02003750 /* query support PCM information from the given NID */
3751 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003752 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003753 info->rates ? NULL : &info->rates,
3754 info->formats ? NULL : &info->formats,
3755 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003756 if (err < 0)
3757 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759 if (info->ops.open == NULL)
3760 info->ops.open = hda_pcm_default_open_close;
3761 if (info->ops.close == NULL)
3762 info->ops.close = hda_pcm_default_open_close;
3763 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003764 if (snd_BUG_ON(!info->nid))
3765 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 info->ops.prepare = hda_pcm_default_prepare;
3767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003769 if (snd_BUG_ON(!info->nid))
3770 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 info->ops.cleanup = hda_pcm_default_cleanup;
3772 }
3773 return 0;
3774}
3775
Takashi Iwaieb541332010-08-06 13:48:11 +02003776/*
3777 * codec prepare/cleanup entries
3778 */
3779int snd_hda_codec_prepare(struct hda_codec *codec,
3780 struct hda_pcm_stream *hinfo,
3781 unsigned int stream,
3782 unsigned int format,
3783 struct snd_pcm_substream *substream)
3784{
3785 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003786 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003787 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3788 if (ret >= 0)
3789 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003790 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003791 return ret;
3792}
3793EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3794
3795void snd_hda_codec_cleanup(struct hda_codec *codec,
3796 struct hda_pcm_stream *hinfo,
3797 struct snd_pcm_substream *substream)
3798{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003799 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003800 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003801 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003802}
3803EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3804
Takashi Iwaid5191e52009-11-16 14:58:17 +01003805/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003806const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3807 "Audio", "SPDIF", "HDMI", "Modem"
3808};
3809
Takashi Iwai176d5332008-07-30 15:01:44 +02003810/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003811 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003812 *
3813 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003814 */
3815static int get_empty_pcm_device(struct hda_bus *bus, int type)
3816{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003817 /* audio device indices; not linear to keep compatibility */
3818 static int audio_idx[HDA_PCM_NTYPES][5] = {
3819 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3820 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003821 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003822 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003823 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003824 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003825
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003826 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003827 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3828 return -EINVAL;
3829 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003830
3831 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3832 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3833 return audio_idx[type][i];
3834
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003835 snd_printk(KERN_WARNING "Too many %s devices\n",
3836 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003837 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003838}
3839
3840/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003841 * attach a new PCM stream
3842 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003843static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003844{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003845 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003846 struct hda_pcm_stream *info;
3847 int stream, err;
3848
Takashi Iwaib91f0802008-11-04 08:43:08 +01003849 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003850 return -EINVAL;
3851 for (stream = 0; stream < 2; stream++) {
3852 info = &pcm->stream[stream];
3853 if (info->substreams) {
3854 err = set_pcm_default_values(codec, info);
3855 if (err < 0)
3856 return err;
3857 }
3858 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003859 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003860}
3861
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003862/* assign all PCMs of the given codec */
3863int snd_hda_codec_build_pcms(struct hda_codec *codec)
3864{
3865 unsigned int pcm;
3866 int err;
3867
3868 if (!codec->num_pcms) {
3869 if (!codec->patch_ops.build_pcms)
3870 return 0;
3871 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003872 if (err < 0) {
3873 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003874 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003875 err = snd_hda_codec_reset(codec);
3876 if (err < 0) {
3877 printk(KERN_ERR
3878 "hda_codec: cannot revert codec\n");
3879 return err;
3880 }
3881 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003882 }
3883 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3884 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3885 int dev;
3886
3887 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003888 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003889
3890 if (!cpcm->pcm) {
3891 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3892 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003893 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003894 cpcm->device = dev;
3895 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003896 if (err < 0) {
3897 printk(KERN_ERR "hda_codec: cannot attach "
3898 "PCM stream %d for codec #%d\n",
3899 dev, codec->addr);
3900 continue; /* no fatal error */
3901 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003902 }
3903 }
3904 return 0;
3905}
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907/**
3908 * snd_hda_build_pcms - build PCM information
3909 * @bus: the BUS
3910 *
3911 * Create PCM information for each codec included in the bus.
3912 *
3913 * The build_pcms codec patch is requested to set up codec->num_pcms and
3914 * codec->pcm_info properly. The array is referred by the top-level driver
3915 * to create its PCM instances.
3916 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3917 * callback.
3918 *
3919 * At least, substreams, channels_min and channels_max must be filled for
3920 * each stream. substreams = 0 indicates that the stream doesn't exist.
3921 * When rates and/or formats are zero, the supported values are queried
3922 * from the given nid. The nid is used also by the default ops.prepare
3923 * and ops.cleanup callbacks.
3924 *
3925 * The driver needs to call ops.open in its open callback. Similarly,
3926 * ops.close is supposed to be called in the close callback.
3927 * ops.prepare should be called in the prepare or hw_params callback
3928 * with the proper parameters for set up.
3929 * ops.cleanup should be called in hw_free for clean up of streams.
3930 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003931 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003933int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003935 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Takashi Iwai0ba21762007-04-16 11:29:14 +02003937 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003938 int err = snd_hda_codec_build_pcms(codec);
3939 if (err < 0)
3940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
3942 return 0;
3943}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003944EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946/**
3947 * snd_hda_check_board_config - compare the current codec with the config table
3948 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003949 * @num_configs: number of config enums
3950 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 * @tbl: configuration table, terminated by null entries
3952 *
3953 * Compares the modelname or PCI subsystem id of the current codec with the
3954 * given configuration table. If a matching entry is found, returns its
3955 * config value (supposed to be 0 or positive).
3956 *
3957 * If no entries are matching, the function returns a negative value.
3958 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003959int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003960 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003961 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003963 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003964 int i;
3965 for (i = 0; i < num_configs; i++) {
3966 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003967 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003968 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3969 "selected\n", models[i]);
3970 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
3972 }
3973 }
3974
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003975 if (!codec->bus->pci || !tbl)
3976 return -1;
3977
3978 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3979 if (!tbl)
3980 return -1;
3981 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003982#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003983 char tmp[10];
3984 const char *model = NULL;
3985 if (models)
3986 model = models[tbl->value];
3987 if (!model) {
3988 sprintf(tmp, "#%d", tbl->value);
3989 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003991 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3992 "for config %x:%x (%s)\n",
3993 model, tbl->subvendor, tbl->subdevice,
3994 (tbl->name ? tbl->name : "Unknown device"));
3995#endif
3996 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998 return -1;
3999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004000EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
4002/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004003 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004004 subsystem ID with the
4005 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004006
4007 This is important for Gateway notebooks with SB450 HDA Audio
4008 where the vendor ID of the PCI device is:
4009 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4010 and the vendor/subvendor are found only at the codec.
4011
4012 * @codec: the HDA codec
4013 * @num_configs: number of config enums
4014 * @models: array of model name strings
4015 * @tbl: configuration table, terminated by null entries
4016 *
4017 * Compares the modelname or PCI subsystem id of the current codec with the
4018 * given configuration table. If a matching entry is found, returns its
4019 * config value (supposed to be 0 or positive).
4020 *
4021 * If no entries are matching, the function returns a negative value.
4022 */
4023int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004024 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004025 const struct snd_pci_quirk *tbl)
4026{
4027 const struct snd_pci_quirk *q;
4028
4029 /* Search for codec ID */
4030 for (q = tbl; q->subvendor; q++) {
4031 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
4032
4033 if (vendorid == codec->subsystem_id)
4034 break;
4035 }
4036
4037 if (!q->subvendor)
4038 return -1;
4039
4040 tbl = q;
4041
4042 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004043#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004044 char tmp[10];
4045 const char *model = NULL;
4046 if (models)
4047 model = models[tbl->value];
4048 if (!model) {
4049 sprintf(tmp, "#%d", tbl->value);
4050 model = tmp;
4051 }
4052 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4053 "for config %x:%x (%s)\n",
4054 model, tbl->subvendor, tbl->subdevice,
4055 (tbl->name ? tbl->name : "Unknown device"));
4056#endif
4057 return tbl->value;
4058 }
4059 return -1;
4060}
4061EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4062
4063/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 * snd_hda_add_new_ctls - create controls from the array
4065 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004066 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 *
4068 * This helper function creates and add new controls in the given array.
4069 * The array must be terminated with an empty entry as terminator.
4070 *
4071 * Returns 0 if successful, or a negative error code.
4072 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004073int snd_hda_add_new_ctls(struct hda_codec *codec,
4074 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004079 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004080 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004081 if (knew->iface == -1) /* skip this codec private value */
4082 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004083 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004084 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004085 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004086 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004087 if (addr > 0)
4088 kctl->id.device = addr;
4089 if (idx > 0)
4090 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004091 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004092 if (!err)
4093 break;
4094 /* try first with another device index corresponding to
4095 * the codec addr; if it still fails (or it's the
4096 * primary codec), then try another control index
4097 */
4098 if (!addr && codec->addr)
4099 addr = codec->addr;
4100 else if (!idx && !knew->index) {
4101 idx = find_empty_mixer_ctl_idx(codec,
4102 knew->name);
4103 if (idx <= 0)
4104 return err;
4105 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004106 return err;
4107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
4109 return 0;
4110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004111EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Takashi Iwaicb53c622007-08-10 17:21:45 +02004113#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004114static void hda_power_work(struct work_struct *work)
4115{
4116 struct hda_codec *codec =
4117 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004118 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004119
Maxim Levitsky2e492462007-09-03 15:26:57 +02004120 if (!codec->power_on || codec->power_count) {
4121 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004122 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004123 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004124
Takashi Iwaid66fee52011-08-02 15:39:31 +02004125 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004126 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004127 if (bus->ops.pm_notify)
4128 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004129}
4130
4131static void hda_keep_power_on(struct hda_codec *codec)
4132{
4133 codec->power_count++;
4134 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004135 codec->power_jiffies = jiffies;
4136}
4137
Takashi Iwaid5191e52009-11-16 14:58:17 +01004138/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004139void snd_hda_update_power_acct(struct hda_codec *codec)
4140{
4141 unsigned long delta = jiffies - codec->power_jiffies;
4142 if (codec->power_on)
4143 codec->power_on_acct += delta;
4144 else
4145 codec->power_off_acct += delta;
4146 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004147}
4148
Takashi Iwaid5191e52009-11-16 14:58:17 +01004149/**
4150 * snd_hda_power_up - Power-up the codec
4151 * @codec: HD-audio codec
4152 *
4153 * Increment the power-up counter and power up the hardware really when
4154 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004155 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004156void snd_hda_power_up(struct hda_codec *codec)
4157{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004158 struct hda_bus *bus = codec->bus;
4159
Takashi Iwaicb53c622007-08-10 17:21:45 +02004160 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004161 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004162 return;
4163
Takashi Iwaid66fee52011-08-02 15:39:31 +02004164 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004165 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004166 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004167 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004168 if (bus->ops.pm_notify)
4169 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004170 hda_call_codec_resume(codec);
4171 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004172 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004174EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004175
4176#define power_save(codec) \
4177 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004178
Takashi Iwaid5191e52009-11-16 14:58:17 +01004179/**
4180 * snd_hda_power_down - Power-down the codec
4181 * @codec: HD-audio codec
4182 *
4183 * Decrement the power-up counter and schedules the power-off work if
4184 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004185 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004186void snd_hda_power_down(struct hda_codec *codec)
4187{
4188 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004189 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004190 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004191 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004192 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004193 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004194 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004195 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004197EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004198
Takashi Iwaid5191e52009-11-16 14:58:17 +01004199/**
4200 * snd_hda_check_amp_list_power - Check the amp list and update the power
4201 * @codec: HD-audio codec
4202 * @check: the object containing an AMP list and the status
4203 * @nid: NID to check / update
4204 *
4205 * Check whether the given NID is in the amp list. If it's in the list,
4206 * check the current AMP status, and update the the power-status according
4207 * to the mute status.
4208 *
4209 * This function is supposed to be set or called from the check_power_status
4210 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004211 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004212int snd_hda_check_amp_list_power(struct hda_codec *codec,
4213 struct hda_loopback_check *check,
4214 hda_nid_t nid)
4215{
Takashi Iwai031024e2011-05-02 11:29:30 +02004216 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004217 int ch, v;
4218
4219 if (!check->amplist)
4220 return 0;
4221 for (p = check->amplist; p->nid; p++) {
4222 if (p->nid == nid)
4223 break;
4224 }
4225 if (!p->nid)
4226 return 0; /* nothing changed */
4227
4228 for (p = check->amplist; p->nid; p++) {
4229 for (ch = 0; ch < 2; ch++) {
4230 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4231 p->idx);
4232 if (!(v & HDA_AMP_MUTE) && v > 0) {
4233 if (!check->power_on) {
4234 check->power_on = 1;
4235 snd_hda_power_up(codec);
4236 }
4237 return 1;
4238 }
4239 }
4240 }
4241 if (check->power_on) {
4242 check->power_on = 0;
4243 snd_hda_power_down(codec);
4244 }
4245 return 0;
4246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004247EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004250/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004251 * Channel mode helper
4252 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004253
4254/**
4255 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4256 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004257int snd_hda_ch_mode_info(struct hda_codec *codec,
4258 struct snd_ctl_elem_info *uinfo,
4259 const struct hda_channel_mode *chmode,
4260 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004261{
4262 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4263 uinfo->count = 1;
4264 uinfo->value.enumerated.items = num_chmodes;
4265 if (uinfo->value.enumerated.item >= num_chmodes)
4266 uinfo->value.enumerated.item = num_chmodes - 1;
4267 sprintf(uinfo->value.enumerated.name, "%dch",
4268 chmode[uinfo->value.enumerated.item].channels);
4269 return 0;
4270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004271EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004272
Takashi Iwaid5191e52009-11-16 14:58:17 +01004273/**
4274 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4275 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004276int snd_hda_ch_mode_get(struct hda_codec *codec,
4277 struct snd_ctl_elem_value *ucontrol,
4278 const struct hda_channel_mode *chmode,
4279 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004280 int max_channels)
4281{
4282 int i;
4283
4284 for (i = 0; i < num_chmodes; i++) {
4285 if (max_channels == chmode[i].channels) {
4286 ucontrol->value.enumerated.item[0] = i;
4287 break;
4288 }
4289 }
4290 return 0;
4291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004292EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004293
Takashi Iwaid5191e52009-11-16 14:58:17 +01004294/**
4295 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4296 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004297int snd_hda_ch_mode_put(struct hda_codec *codec,
4298 struct snd_ctl_elem_value *ucontrol,
4299 const struct hda_channel_mode *chmode,
4300 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004301 int *max_channelsp)
4302{
4303 unsigned int mode;
4304
4305 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004306 if (mode >= num_chmodes)
4307 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004308 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004309 return 0;
4310 /* change the current channel setting */
4311 *max_channelsp = chmode[mode].channels;
4312 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004313 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004314 return 1;
4315}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004316EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318/*
4319 * input MUX helper
4320 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004321
4322/**
4323 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4324 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004325int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4326 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
4328 unsigned int index;
4329
4330 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4331 uinfo->count = 1;
4332 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004333 if (!imux->num_items)
4334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 index = uinfo->value.enumerated.item;
4336 if (index >= imux->num_items)
4337 index = imux->num_items - 1;
4338 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4339 return 0;
4340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004341EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Takashi Iwaid5191e52009-11-16 14:58:17 +01004343/**
4344 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4345 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004346int snd_hda_input_mux_put(struct hda_codec *codec,
4347 const struct hda_input_mux *imux,
4348 struct snd_ctl_elem_value *ucontrol,
4349 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 unsigned int *cur_val)
4351{
4352 unsigned int idx;
4353
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004354 if (!imux->num_items)
4355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 idx = ucontrol->value.enumerated.item[0];
4357 if (idx >= imux->num_items)
4358 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004359 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004361 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4362 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 *cur_val = idx;
4364 return 1;
4365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004366EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368
4369/*
4370 * Multi-channel / digital-out PCM helper functions
4371 */
4372
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004373/* setup SPDIF output stream */
4374static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4375 unsigned int stream_tag, unsigned int format)
4376{
Stephen Warren7c9359762011-06-01 11:14:17 -06004377 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4378
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004379 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004380 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004381 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004382 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004383 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004384 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004385 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004386 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004387 for (d = codec->slave_dig_outs; *d; d++)
4388 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4389 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004390 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004391 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004392 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004393 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004394 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004395}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004396
Takashi Iwai2f728532008-09-25 16:32:41 +02004397static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4398{
4399 snd_hda_codec_cleanup_stream(codec, nid);
4400 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004401 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004402 for (d = codec->slave_dig_outs; *d; d++)
4403 snd_hda_codec_cleanup_stream(codec, *d);
4404 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004405}
4406
Takashi Iwaid5191e52009-11-16 14:58:17 +01004407/**
4408 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4409 * @bus: HD-audio bus
4410 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004411void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4412{
4413 struct hda_codec *codec;
4414
4415 if (!bus)
4416 return;
4417 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004418 if (hda_codec_is_power_on(codec) &&
4419 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004420 codec->patch_ops.reboot_notify(codec);
4421 }
4422}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004423EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004424
Takashi Iwaid5191e52009-11-16 14:58:17 +01004425/**
4426 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004428int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4429 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Ingo Molnar62932df2006-01-16 16:34:20 +01004431 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004432 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4433 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004434 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004436 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 return 0;
4438}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004439EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Takashi Iwaid5191e52009-11-16 14:58:17 +01004441/**
4442 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4443 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004444int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4445 struct hda_multi_out *mout,
4446 unsigned int stream_tag,
4447 unsigned int format,
4448 struct snd_pcm_substream *substream)
4449{
4450 mutex_lock(&codec->spdif_mutex);
4451 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4452 mutex_unlock(&codec->spdif_mutex);
4453 return 0;
4454}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004455EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004456
Takashi Iwaid5191e52009-11-16 14:58:17 +01004457/**
4458 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4459 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004460int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4461 struct hda_multi_out *mout)
4462{
4463 mutex_lock(&codec->spdif_mutex);
4464 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4465 mutex_unlock(&codec->spdif_mutex);
4466 return 0;
4467}
4468EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4469
Takashi Iwaid5191e52009-11-16 14:58:17 +01004470/**
4471 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004473int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4474 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
Ingo Molnar62932df2006-01-16 16:34:20 +01004476 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004478 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return 0;
4480}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004481EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Takashi Iwaid5191e52009-11-16 14:58:17 +01004483/**
4484 * snd_hda_multi_out_analog_open - open analog outputs
4485 *
4486 * Open analog outputs and set up the hw-constraints.
4487 * If the digital outputs can be opened as slave, open the digital
4488 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004490int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4491 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004492 struct snd_pcm_substream *substream,
4493 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Takashi Iwai9a081602008-02-12 18:37:26 +01004495 struct snd_pcm_runtime *runtime = substream->runtime;
4496 runtime->hw.channels_max = mout->max_channels;
4497 if (mout->dig_out_nid) {
4498 if (!mout->analog_rates) {
4499 mout->analog_rates = hinfo->rates;
4500 mout->analog_formats = hinfo->formats;
4501 mout->analog_maxbps = hinfo->maxbps;
4502 } else {
4503 runtime->hw.rates = mout->analog_rates;
4504 runtime->hw.formats = mout->analog_formats;
4505 hinfo->maxbps = mout->analog_maxbps;
4506 }
4507 if (!mout->spdif_rates) {
4508 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4509 &mout->spdif_rates,
4510 &mout->spdif_formats,
4511 &mout->spdif_maxbps);
4512 }
4513 mutex_lock(&codec->spdif_mutex);
4514 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004515 if ((runtime->hw.rates & mout->spdif_rates) &&
4516 (runtime->hw.formats & mout->spdif_formats)) {
4517 runtime->hw.rates &= mout->spdif_rates;
4518 runtime->hw.formats &= mout->spdif_formats;
4519 if (mout->spdif_maxbps < hinfo->maxbps)
4520 hinfo->maxbps = mout->spdif_maxbps;
4521 } else {
4522 mout->share_spdif = 0;
4523 /* FIXME: need notify? */
4524 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004525 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004526 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4529 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4530}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004531EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Takashi Iwaid5191e52009-11-16 14:58:17 +01004533/**
4534 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4535 *
4536 * Set up the i/o for analog out.
4537 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004539int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4540 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 unsigned int stream_tag,
4542 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004543 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Takashi Iwaidda14412011-05-02 11:29:30 +02004545 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004547 struct hda_spdif_out *spdif =
4548 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 int i;
4550
Ingo Molnar62932df2006-01-16 16:34:20 +01004551 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004552 if (mout->dig_out_nid && mout->share_spdif &&
4553 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004555 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4556 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004557 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004559 setup_dig_out_stream(codec, mout->dig_out_nid,
4560 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 } else {
4562 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004563 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
4565 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004566 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004569 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4570 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004571 if (!mout->no_share_stream &&
4572 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004574 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4575 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004576 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004577 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4578 if (!mout->no_share_stream && mout->hp_out_nid[i])
4579 snd_hda_codec_setup_stream(codec,
4580 mout->hp_out_nid[i],
4581 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004582 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004583 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004584 snd_hda_codec_setup_stream(codec,
4585 mout->extra_out_nid[i],
4586 stream_tag, 0, format);
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 /* surrounds */
4589 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004590 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004591 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4592 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004593 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004594 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4595 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
4597 return 0;
4598}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004599EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Takashi Iwaid5191e52009-11-16 14:58:17 +01004601/**
4602 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004604int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4605 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Takashi Iwaidda14412011-05-02 11:29:30 +02004607 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 int i;
4609
4610 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004611 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004613 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004614 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4615 if (mout->hp_out_nid[i])
4616 snd_hda_codec_cleanup_stream(codec,
4617 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004618 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4619 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004620 snd_hda_codec_cleanup_stream(codec,
4621 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004622 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004624 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 mout->dig_out_used = 0;
4626 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004627 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 return 0;
4629}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004630EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004632/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004633 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004634 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004635
Takashi Iwaidda14412011-05-02 11:29:30 +02004636static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004637{
4638 for (; *list; list++)
4639 if (*list == nid)
4640 return 1;
4641 return 0;
4642}
4643
Steve Longerbeam81937d32007-05-08 15:33:03 +02004644
4645/*
4646 * Sort an associated group of pins according to their sequence numbers.
4647 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004648static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004649 int num_pins)
4650{
4651 int i, j;
4652 short seq;
4653 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004654
Steve Longerbeam81937d32007-05-08 15:33:03 +02004655 for (i = 0; i < num_pins; i++) {
4656 for (j = i + 1; j < num_pins; j++) {
4657 if (sequences[i] > sequences[j]) {
4658 seq = sequences[i];
4659 sequences[i] = sequences[j];
4660 sequences[j] = seq;
4661 nid = pins[i];
4662 pins[i] = pins[j];
4663 pins[j] = nid;
4664 }
4665 }
4666 }
4667}
4668
4669
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004670/* add the found input-pin to the cfg->inputs[] table */
4671static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4672 int type)
4673{
4674 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4675 cfg->inputs[cfg->num_inputs].pin = nid;
4676 cfg->inputs[cfg->num_inputs].type = type;
4677 cfg->num_inputs++;
4678 }
4679}
4680
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004681/* sort inputs in the order of AUTO_PIN_* type */
4682static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4683{
4684 int i, j;
4685
4686 for (i = 0; i < cfg->num_inputs; i++) {
4687 for (j = i + 1; j < cfg->num_inputs; j++) {
4688 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4689 struct auto_pin_cfg_item tmp;
4690 tmp = cfg->inputs[i];
4691 cfg->inputs[i] = cfg->inputs[j];
4692 cfg->inputs[j] = tmp;
4693 }
4694 }
4695 }
4696}
4697
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004698/* Reorder the surround channels
4699 * ALSA sequence is front/surr/clfe/side
4700 * HDA sequence is:
4701 * 4-ch: front/surr => OK as it is
4702 * 6-ch: front/clfe/surr
4703 * 8-ch: front/clfe/rear/side|fc
4704 */
4705static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4706{
4707 hda_nid_t nid;
4708
4709 switch (nums) {
4710 case 3:
4711 case 4:
4712 nid = pins[1];
4713 pins[1] = pins[2];
4714 pins[2] = nid;
4715 break;
4716 }
4717}
4718
Takashi Iwai82bc9552006-03-21 11:24:42 +01004719/*
4720 * Parse all pin widgets and store the useful pin nids to cfg
4721 *
4722 * The number of line-outs or any primary output is stored in line_outs,
4723 * and the corresponding output pins are assigned to line_out_pins[],
4724 * in the order of front, rear, CLFE, side, ...
4725 *
4726 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004727 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004728 * is detected, one of speaker of HP pins is assigned as the primary
4729 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4730 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004731 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004732 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004733 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4734 * respectively.
4735 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004736int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4737 struct auto_pin_cfg *cfg,
4738 const hda_nid_t *ignore_nids,
4739 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004740{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004741 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004742 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004743 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4744 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004745 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004746 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004747
4748 memset(cfg, 0, sizeof(*cfg));
4749
Steve Longerbeam81937d32007-05-08 15:33:03 +02004750 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4751 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004752 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004753 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004754
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004755 end_nid = codec->start_nid + codec->num_nodes;
4756 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004757 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004758 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004759 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004760 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004761
4762 /* read all default configuration for pin complex */
4763 if (wid_type != AC_WID_PIN)
4764 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004765 /* ignore the given nids (e.g. pc-beep returns error) */
4766 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4767 continue;
4768
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004769 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004770 conn = get_defcfg_connect(def_conf);
4771 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004772 continue;
4773 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004774 dev = get_defcfg_device(def_conf);
4775
4776 /* workaround for buggy BIOS setups */
4777 if (dev == AC_JACK_LINE_OUT) {
4778 if (conn == AC_JACK_PORT_FIXED)
4779 dev = AC_JACK_SPEAKER;
4780 }
4781
4782 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004783 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004784 seq = get_defcfg_sequence(def_conf);
4785 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004786
4787 if (!(wid_caps & AC_WCAP_STEREO))
4788 if (!cfg->mono_out_pin)
4789 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004790 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004791 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004792 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004793 assoc_line_out = assoc;
4794 else if (assoc_line_out != assoc)
4795 continue;
4796 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4797 continue;
4798 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004799 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004800 cfg->line_outs++;
4801 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004802 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004803 seq = get_defcfg_sequence(def_conf);
4804 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004805 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4806 continue;
4807 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004808 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004809 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004810 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004811 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004812 seq = get_defcfg_sequence(def_conf);
4813 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004814 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4815 continue;
4816 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004817 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004818 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004819 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004820 case AC_JACK_MIC_IN:
4821 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004822 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004823 case AC_JACK_LINE_IN:
4824 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004825 break;
4826 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004827 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004828 break;
4829 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004830 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004831 break;
4832 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004833 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004834 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4835 continue;
4836 cfg->dig_out_pins[cfg->dig_outs] = nid;
4837 cfg->dig_out_type[cfg->dig_outs] =
4838 (loc == AC_JACK_LOC_HDMI) ?
4839 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4840 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004841 break;
4842 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004843 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004844 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004845 if (loc == AC_JACK_LOC_HDMI)
4846 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4847 else
4848 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004849 break;
4850 }
4851 }
4852
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004853 /* FIX-UP:
4854 * If no line-out is defined but multiple HPs are found,
4855 * some of them might be the real line-outs.
4856 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004857 if (!cfg->line_outs && cfg->hp_outs > 1 &&
4858 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004859 int i = 0;
4860 while (i < cfg->hp_outs) {
4861 /* The real HPs should have the sequence 0x0f */
4862 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4863 i++;
4864 continue;
4865 }
4866 /* Move it to the line-out table */
4867 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4868 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4869 cfg->line_outs++;
4870 cfg->hp_outs--;
4871 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4872 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004873 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004874 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4875 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004876 memset(cfg->hp_pins + cfg->hp_outs, 0,
4877 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004878 if (!cfg->hp_outs)
4879 cfg->line_out_type = AUTO_PIN_HP_OUT;
4880
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004881 }
4882
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004883 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004884 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4885 cfg->line_outs);
4886 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4887 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004888 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4889 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004890
Steve Longerbeam81937d32007-05-08 15:33:03 +02004891 /*
4892 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4893 * as a primary output
4894 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004895 if (!cfg->line_outs &&
4896 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02004897 if (cfg->speaker_outs) {
4898 cfg->line_outs = cfg->speaker_outs;
4899 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4900 sizeof(cfg->speaker_pins));
4901 cfg->speaker_outs = 0;
4902 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4903 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4904 } else if (cfg->hp_outs) {
4905 cfg->line_outs = cfg->hp_outs;
4906 memcpy(cfg->line_out_pins, cfg->hp_pins,
4907 sizeof(cfg->hp_pins));
4908 cfg->hp_outs = 0;
4909 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4910 cfg->line_out_type = AUTO_PIN_HP_OUT;
4911 }
4912 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004913
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004914 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
4915 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
4916 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004917
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004918 sort_autocfg_input_pins(cfg);
4919
Takashi Iwai82bc9552006-03-21 11:24:42 +01004920 /*
4921 * debug prints of the parsed results
4922 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004923 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai82bc9552006-03-21 11:24:42 +01004924 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4925 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004926 cfg->line_out_pins[4],
4927 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4928 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4929 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004930 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4931 cfg->speaker_outs, cfg->speaker_pins[0],
4932 cfg->speaker_pins[1], cfg->speaker_pins[2],
4933 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004934 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4935 cfg->hp_outs, cfg->hp_pins[0],
4936 cfg->hp_pins[1], cfg->hp_pins[2],
4937 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004938 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004939 if (cfg->dig_outs)
4940 snd_printd(" dig-out=0x%x/0x%x\n",
4941 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004942 snd_printd(" inputs:");
4943 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004944 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004945 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004946 cfg->inputs[i].pin);
4947 }
4948 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004949 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004950 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004951
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004952 return 0;
4953}
Takashi Iwaid025feb2011-08-23 15:24:39 +02004954EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004955
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004956int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004957{
4958 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004959 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004960 if (conn == AC_JACK_PORT_NONE)
4961 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004962 /* Windows may claim the internal mic to be BOTH, too */
4963 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004964 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004965 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004966 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004967 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004968 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004969 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004970 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004971 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004972 return INPUT_PIN_ATTR_FRONT;
4973 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004974}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004975EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004976
Takashi Iwai990061c2010-09-09 22:08:44 +02004977/**
4978 * hda_get_input_pin_label - Give a label for the given input pin
4979 *
4980 * When check_location is true, the function checks the pin location
4981 * for mic and line-in pins, and set an appropriate prefix like "Front",
4982 * "Rear", "Internal".
4983 */
4984
Takashi Iwai10a20af2010-09-09 16:28:02 +02004985const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4986 int check_location)
4987{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004988 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004989 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004990 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4991 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004992 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004993
Takashi Iwai10a20af2010-09-09 16:28:02 +02004994 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004995
4996 switch (get_defcfg_device(def_conf)) {
4997 case AC_JACK_MIC_IN:
4998 if (!check_location)
4999 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005000 attr = snd_hda_get_input_pin_attr(def_conf);
5001 if (!attr)
5002 return "None";
5003 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005004 case AC_JACK_LINE_IN:
5005 if (!check_location)
5006 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005007 attr = snd_hda_get_input_pin_attr(def_conf);
5008 if (!attr)
5009 return "None";
5010 if (attr == INPUT_PIN_ATTR_DOCK)
5011 return "Dock Line";
5012 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005013 case AC_JACK_AUX:
5014 return "Aux";
5015 case AC_JACK_CD:
5016 return "CD";
5017 case AC_JACK_SPDIF_IN:
5018 return "SPDIF In";
5019 case AC_JACK_DIG_OTHER_IN:
5020 return "Digital In";
5021 default:
5022 return "Misc";
5023 }
5024}
5025EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
5026
Takashi Iwaia1c98512010-09-09 21:36:27 +02005027/* Check whether the location prefix needs to be added to the label.
5028 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5029 * have to put "Front" prefix to each label. In such a case, returns false.
5030 */
5031static int check_mic_location_need(struct hda_codec *codec,
5032 const struct auto_pin_cfg *cfg,
5033 int input)
5034{
5035 unsigned int defc;
5036 int i, attr, attr2;
5037
5038 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005039 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005040 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005041 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005042 return 1;
5043
5044 attr = 0;
5045 for (i = 0; i < cfg->num_inputs; i++) {
5046 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005047 attr2 = snd_hda_get_input_pin_attr(defc);
5048 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005049 if (attr && attr != attr2)
5050 return 1; /* different locations found */
5051 attr = attr2;
5052 }
5053 }
5054 return 0;
5055}
5056
Takashi Iwai990061c2010-09-09 22:08:44 +02005057/**
5058 * hda_get_autocfg_input_label - Get a label for the given input
5059 *
5060 * Get a label for the given input pin defined by the autocfg item.
5061 * Unlike hda_get_input_pin_label(), this function checks all inputs
5062 * defined in autocfg and avoids the redundant mic/line prefix as much as
5063 * possible.
5064 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005065const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5066 const struct auto_pin_cfg *cfg,
5067 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005068{
5069 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005070 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005071
Takashi Iwai10a20af2010-09-09 16:28:02 +02005072 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5073 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5074 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005075 if (has_multiple_pins && type == AUTO_PIN_MIC)
5076 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005077 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5078 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005079}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005080EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5081
Takashi Iwai990061c2010-09-09 22:08:44 +02005082/**
5083 * snd_hda_add_imux_item - Add an item to input_mux
5084 *
5085 * When the same label is used already in the existing items, the number
5086 * suffix is appended to the label. This label index number is stored
5087 * to type_idx when non-NULL pointer is given.
5088 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005089int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5090 int index, int *type_idx)
5091{
5092 int i, label_idx = 0;
5093 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5094 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5095 return -EINVAL;
5096 }
5097 for (i = 0; i < imux->num_items; i++) {
5098 if (!strncmp(label, imux->items[i].label, strlen(label)))
5099 label_idx++;
5100 }
5101 if (type_idx)
5102 *type_idx = label_idx;
5103 if (label_idx > 0)
5104 snprintf(imux->items[imux->num_items].label,
5105 sizeof(imux->items[imux->num_items].label),
5106 "%s %d", label, label_idx);
5107 else
5108 strlcpy(imux->items[imux->num_items].label, label,
5109 sizeof(imux->items[imux->num_items].label));
5110 imux->items[imux->num_items].index = index;
5111 imux->num_items++;
5112 return 0;
5113}
5114EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005115
Takashi Iwai4a471b72005-12-07 13:56:29 +01005116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117#ifdef CONFIG_PM
5118/*
5119 * power management
5120 */
5121
5122/**
5123 * snd_hda_suspend - suspend the codecs
5124 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 *
5126 * Returns 0 if successful.
5127 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005128int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005130 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
Takashi Iwai0ba21762007-04-16 11:29:14 +02005132 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005133 if (hda_codec_is_power_on(codec))
5134 hda_call_codec_suspend(codec);
5135 if (codec->patch_ops.post_suspend)
5136 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
5138 return 0;
5139}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005140EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142/**
5143 * snd_hda_resume - resume the codecs
5144 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 *
5146 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005147 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005148 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005149 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 */
5151int snd_hda_resume(struct hda_bus *bus)
5152{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005153 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
Takashi Iwai0ba21762007-04-16 11:29:14 +02005155 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005156 if (codec->patch_ops.pre_resume)
5157 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005158 if (snd_hda_codec_needs_resume(codec))
5159 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return 0;
5162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005163EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005164#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005165
5166/*
5167 * generic arrays
5168 */
5169
Takashi Iwaid5191e52009-11-16 14:58:17 +01005170/**
5171 * snd_array_new - get a new element from the given array
5172 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005173 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005174 * Get a new element from the given array. If it exceeds the
5175 * pre-allocated array size, re-allocate the array.
5176 *
5177 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005178 */
5179void *snd_array_new(struct snd_array *array)
5180{
5181 if (array->used >= array->alloced) {
5182 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005183 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005184 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005185 void *nlist;
5186 if (snd_BUG_ON(num >= 4096))
5187 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005188 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005189 if (!nlist)
5190 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005191 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005192 array->list = nlist;
5193 array->alloced = num;
5194 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005195 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005197EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005198
Takashi Iwaid5191e52009-11-16 14:58:17 +01005199/**
5200 * snd_array_free - free the given array elements
5201 * @array: the array object
5202 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005203void snd_array_free(struct snd_array *array)
5204{
5205 kfree(array->list);
5206 array->used = 0;
5207 array->alloced = 0;
5208 array->list = NULL;
5209}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005210EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005211
Takashi Iwaid5191e52009-11-16 14:58:17 +01005212/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005213 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5214 * @pcm: PCM caps bits
5215 * @buf: the string buffer to write
5216 * @buflen: the max buffer length
5217 *
5218 * used by hda_proc.c and hda_eld.c
5219 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005220void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5221{
5222 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5223 int i, j;
5224
5225 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5226 if (pcm & (AC_SUPPCM_BITS_8 << i))
5227 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5228
5229 buf[j] = '\0'; /* necessary when j == 0 */
5230}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005231EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005232
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005233#ifdef CONFIG_SND_HDA_INPUT_JACK
5234/*
5235 * Input-jack notification support
5236 */
5237struct hda_jack_item {
5238 hda_nid_t nid;
5239 int type;
5240 struct snd_jack *jack;
5241};
5242
5243static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5244 int type)
5245{
5246 switch (type) {
5247 case SND_JACK_HEADPHONE:
5248 return "Headphone";
5249 case SND_JACK_MICROPHONE:
5250 return "Mic";
5251 case SND_JACK_LINEOUT:
5252 return "Line-out";
David Henningsson7c2f8e42011-10-05 15:53:25 +02005253 case SND_JACK_LINEIN:
5254 return "Line-in";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005255 case SND_JACK_HEADSET:
5256 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005257 case SND_JACK_VIDEOOUT:
5258 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005259 default:
5260 return "Misc";
5261 }
5262}
5263
5264static void hda_free_jack_priv(struct snd_jack *jack)
5265{
5266 struct hda_jack_item *jacks = jack->private_data;
5267 jacks->nid = 0;
5268 jacks->jack = NULL;
5269}
5270
5271int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5272 const char *name)
5273{
5274 struct hda_jack_item *jack;
5275 int err;
5276
5277 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5278 jack = snd_array_new(&codec->jacks);
5279 if (!jack)
5280 return -ENOMEM;
5281
5282 jack->nid = nid;
5283 jack->type = type;
5284 if (!name)
5285 name = get_jack_default_name(codec, nid, type);
5286 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5287 if (err < 0) {
5288 jack->nid = 0;
5289 return err;
5290 }
5291 jack->jack->private_data = jack;
5292 jack->jack->private_free = hda_free_jack_priv;
5293 return 0;
5294}
5295EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5296
5297void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5298{
5299 struct hda_jack_item *jacks = codec->jacks.list;
5300 int i;
5301
5302 if (!jacks)
5303 return;
5304
5305 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5306 unsigned int pin_ctl;
5307 unsigned int present;
5308 int type;
5309
5310 if (jacks->nid != nid)
5311 continue;
5312 present = snd_hda_jack_detect(codec, nid);
5313 type = jacks->type;
5314 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5315 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5316 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5317 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5318 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5319 }
5320 snd_jack_report(jacks->jack, present ? type : 0);
5321 }
5322}
5323EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5324
5325/* free jack instances manually when clearing/reconfiguring */
5326void snd_hda_input_jack_free(struct hda_codec *codec)
5327{
5328 if (!codec->bus->shutdown && codec->jacks.list) {
5329 struct hda_jack_item *jacks = codec->jacks.list;
5330 int i;
5331 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5332 if (jacks->jack)
5333 snd_device_free(codec->bus->card, jacks->jack);
5334 }
5335 }
5336 snd_array_free(&codec->jacks);
5337}
5338EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5339#endif /* CONFIG_SND_HDA_INPUT_JACK */
5340
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005341MODULE_DESCRIPTION("HDA codec core");
5342MODULE_LICENSE("GPL");