blob: e9136711b2d593de31814ca4abf74ac88fb8a3b9 [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 Iwaiaeb4b882011-11-10 12:28:38 +01002334typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2335
2336/* apply the function to all matching slave ctls in the mixer list */
2337static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2338 map_slave_func_t func, void *data)
2339{
2340 struct hda_nid_item *items;
2341 const char * const *s;
2342 int i, err;
2343
2344 items = codec->mixers.list;
2345 for (i = 0; i < codec->mixers.used; i++) {
2346 struct snd_kcontrol *sctl = items[i].kctl;
2347 if (!sctl || !sctl->id.name ||
2348 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2349 continue;
2350 for (s = slaves; *s; s++) {
2351 if (!strcmp(sctl->id.name, *s)) {
2352 err = func(data, sctl);
2353 if (err)
2354 return err;
2355 break;
2356 }
2357 }
2358 }
2359 return 0;
2360}
2361
2362static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2363{
2364 return 1;
2365}
2366
Takashi Iwaid5191e52009-11-16 14:58:17 +01002367/**
2368 * snd_hda_add_vmaster - create a virtual master control and add slaves
2369 * @codec: HD-audio codec
2370 * @name: vmaster control name
2371 * @tlv: TLV data (optional)
2372 * @slaves: slave control names (optional)
2373 *
2374 * Create a virtual master control with the given name. The TLV data
2375 * must be either NULL or a valid data.
2376 *
2377 * @slaves is a NULL-terminated array of strings, each of which is a
2378 * slave control name. All controls with these names are assigned to
2379 * the new virtual master control.
2380 *
2381 * This function returns zero if successful or a negative error code.
2382 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002383int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002384 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002385{
2386 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002387 int err;
2388
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002389 err = map_slaves(codec, slaves, check_slave_present, NULL);
2390 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002391 snd_printdd("No slave found for %s\n", name);
2392 return 0;
2393 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002394 kctl = snd_ctl_make_virtual_master(name, tlv);
2395 if (!kctl)
2396 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002397 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002398 if (err < 0)
2399 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002400
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002401 err = map_slaves(codec, slaves, (map_slave_func_t)snd_ctl_add_slave,
2402 kctl);
2403 if (err < 0)
2404 return err;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002405 return 0;
2406}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002407EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002408
Takashi Iwaid5191e52009-11-16 14:58:17 +01002409/**
2410 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2411 *
2412 * The control element is supposed to have the private_value field
2413 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2414 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002415int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 int chs = get_amp_channels(kcontrol);
2419
2420 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2421 uinfo->count = chs == 3 ? 2 : 1;
2422 uinfo->value.integer.min = 0;
2423 uinfo->value.integer.max = 1;
2424 return 0;
2425}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002426EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Takashi Iwaid5191e52009-11-16 14:58:17 +01002428/**
2429 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2430 *
2431 * The control element is supposed to have the private_value field
2432 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2433 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002434int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438 hda_nid_t nid = get_amp_nid(kcontrol);
2439 int chs = get_amp_channels(kcontrol);
2440 int dir = get_amp_direction(kcontrol);
2441 int idx = get_amp_index(kcontrol);
2442 long *valp = ucontrol->value.integer.value;
2443
2444 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002445 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002446 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002449 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return 0;
2451}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002452EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Takashi Iwaid5191e52009-11-16 14:58:17 +01002454/**
2455 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2456 *
2457 * The control element is supposed to have the private_value field
2458 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2459 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002460int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2464 hda_nid_t nid = get_amp_nid(kcontrol);
2465 int chs = get_amp_channels(kcontrol);
2466 int dir = get_amp_direction(kcontrol);
2467 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 long *valp = ucontrol->value.integer.value;
2469 int change = 0;
2470
Takashi Iwaicb53c622007-08-10 17:21:45 +02002471 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002472 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002473 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002474 HDA_AMP_MUTE,
2475 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002476 valp++;
2477 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002478 if (chs & 2)
2479 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002480 HDA_AMP_MUTE,
2481 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002482 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002483 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return change;
2485}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002486EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Takashi Iwai67d634c2009-11-16 15:35:59 +01002488#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002489/**
2490 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2491 *
2492 * This function calls snd_hda_enable_beep_device(), which behaves differently
2493 * depending on beep_mode option.
2494 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002495int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
2497{
2498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2499 long *valp = ucontrol->value.integer.value;
2500
2501 snd_hda_enable_beep_device(codec, *valp);
2502 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2503}
2504EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002505#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/*
Takashi Iwai985be542005-11-02 18:26:49 +01002508 * bound volume controls
2509 *
2510 * bind multiple volumes (# indices, from 0)
2511 */
2512
2513#define AMP_VAL_IDX_SHIFT 19
2514#define AMP_VAL_IDX_MASK (0x0f<<19)
2515
Takashi Iwaid5191e52009-11-16 14:58:17 +01002516/**
2517 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2518 *
2519 * The control element is supposed to have the private_value field
2520 * set up via HDA_BIND_MUTE*() macros.
2521 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002522int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002524{
2525 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2526 unsigned long pval;
2527 int err;
2528
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002529 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002530 pval = kcontrol->private_value;
2531 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2532 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2533 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002534 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002535 return err;
2536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002537EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002538
Takashi Iwaid5191e52009-11-16 14:58:17 +01002539/**
2540 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2541 *
2542 * The control element is supposed to have the private_value field
2543 * set up via HDA_BIND_MUTE*() macros.
2544 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002545int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002547{
2548 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2549 unsigned long pval;
2550 int i, indices, err = 0, change = 0;
2551
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002552 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002553 pval = kcontrol->private_value;
2554 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2555 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002556 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2557 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002558 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2559 if (err < 0)
2560 break;
2561 change |= err;
2562 }
2563 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002564 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002565 return err < 0 ? err : change;
2566}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002567EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002568
Takashi Iwaid5191e52009-11-16 14:58:17 +01002569/**
2570 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2571 *
2572 * The control element is supposed to have the private_value field
2573 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002574 */
2575int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
2578 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2579 struct hda_bind_ctls *c;
2580 int err;
2581
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002582 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002583 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002584 kcontrol->private_value = *c->values;
2585 err = c->ops->info(kcontrol, uinfo);
2586 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002587 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002588 return err;
2589}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002590EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002591
Takashi Iwaid5191e52009-11-16 14:58:17 +01002592/**
2593 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2594 *
2595 * The control element is supposed to have the private_value field
2596 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2597 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002598int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
2601 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2602 struct hda_bind_ctls *c;
2603 int err;
2604
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002605 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002606 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002607 kcontrol->private_value = *c->values;
2608 err = c->ops->get(kcontrol, ucontrol);
2609 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002610 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002611 return err;
2612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002613EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002614
Takashi Iwaid5191e52009-11-16 14:58:17 +01002615/**
2616 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2617 *
2618 * The control element is supposed to have the private_value field
2619 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2620 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002621int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2623{
2624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2625 struct hda_bind_ctls *c;
2626 unsigned long *vals;
2627 int err = 0, change = 0;
2628
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002629 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002630 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002631 for (vals = c->values; *vals; vals++) {
2632 kcontrol->private_value = *vals;
2633 err = c->ops->put(kcontrol, ucontrol);
2634 if (err < 0)
2635 break;
2636 change |= err;
2637 }
2638 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002639 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002640 return err < 0 ? err : change;
2641}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002642EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002643
Takashi Iwaid5191e52009-11-16 14:58:17 +01002644/**
2645 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2646 *
2647 * The control element is supposed to have the private_value field
2648 * set up via HDA_BIND_VOL() macro.
2649 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002650int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2651 unsigned int size, unsigned int __user *tlv)
2652{
2653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2654 struct hda_bind_ctls *c;
2655 int err;
2656
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002657 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002658 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002659 kcontrol->private_value = *c->values;
2660 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2661 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002662 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002663 return err;
2664}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002665EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002666
2667struct hda_ctl_ops snd_hda_bind_vol = {
2668 .info = snd_hda_mixer_amp_volume_info,
2669 .get = snd_hda_mixer_amp_volume_get,
2670 .put = snd_hda_mixer_amp_volume_put,
2671 .tlv = snd_hda_mixer_amp_tlv
2672};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002673EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002674
2675struct hda_ctl_ops snd_hda_bind_sw = {
2676 .info = snd_hda_mixer_amp_switch_info,
2677 .get = snd_hda_mixer_amp_switch_get,
2678 .put = snd_hda_mixer_amp_switch_put,
2679 .tlv = snd_hda_mixer_amp_tlv
2680};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002681EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002682
2683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 * SPDIF out controls
2685 */
2686
Takashi Iwai0ba21762007-04-16 11:29:14 +02002687static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2691 uinfo->count = 1;
2692 return 0;
2693}
2694
Takashi Iwai0ba21762007-04-16 11:29:14 +02002695static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2699 IEC958_AES0_NONAUDIO |
2700 IEC958_AES0_CON_EMPHASIS_5015 |
2701 IEC958_AES0_CON_NOT_COPYRIGHT;
2702 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2703 IEC958_AES1_CON_ORIGINAL;
2704 return 0;
2705}
2706
Takashi Iwai0ba21762007-04-16 11:29:14 +02002707static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2711 IEC958_AES0_NONAUDIO |
2712 IEC958_AES0_PRO_EMPHASIS_5015;
2713 return 0;
2714}
2715
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002720 int idx = kcontrol->private_value;
2721 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Stephen Warren7c9359762011-06-01 11:14:17 -06002723 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2724 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2725 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2726 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 return 0;
2729}
2730
2731/* convert from SPDIF status bits to HDA SPDIF bits
2732 * bit 0 (DigEn) is always set zero (to be filled later)
2733 */
2734static unsigned short convert_from_spdif_status(unsigned int sbits)
2735{
2736 unsigned short val = 0;
2737
2738 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002739 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002741 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002743 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2744 IEC958_AES0_PRO_EMPHASIS_5015)
2745 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002747 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2748 IEC958_AES0_CON_EMPHASIS_5015)
2749 val |= AC_DIG1_EMPHASIS;
2750 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2751 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002753 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2755 }
2756 return val;
2757}
2758
2759/* convert to SPDIF status bits from HDA SPDIF bits
2760 */
2761static unsigned int convert_to_spdif_status(unsigned short val)
2762{
2763 unsigned int sbits = 0;
2764
Takashi Iwai0ba21762007-04-16 11:29:14 +02002765 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002767 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 sbits |= IEC958_AES0_PROFESSIONAL;
2769 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002770 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2772 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002773 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002775 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2779 sbits |= val & (0x7f << 8);
2780 }
2781 return sbits;
2782}
2783
Takashi Iwai2f728532008-09-25 16:32:41 +02002784/* set digital convert verbs both for the given NID and its slaves */
2785static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2786 int verb, int val)
2787{
Takashi Iwaidda14412011-05-02 11:29:30 +02002788 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002789
Takashi Iwai9e976972008-11-25 08:17:20 +01002790 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002791 d = codec->slave_dig_outs;
2792 if (!d)
2793 return;
2794 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002795 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002796}
2797
2798static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2799 int dig1, int dig2)
2800{
2801 if (dig1 != -1)
2802 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2803 if (dig2 != -1)
2804 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2805}
2806
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002811 int idx = kcontrol->private_value;
2812 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2813 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 unsigned short val;
2815 int change;
2816
Ingo Molnar62932df2006-01-16 16:34:20 +01002817 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002818 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2820 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2821 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002822 val = convert_from_spdif_status(spdif->status);
2823 val |= spdif->ctls & 1;
2824 change = spdif->ctls != val;
2825 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002826 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002827 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002828 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return change;
2830}
2831
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002832#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Takashi Iwai0ba21762007-04-16 11:29:14 +02002834static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002838 int idx = kcontrol->private_value;
2839 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Stephen Warren7c9359762011-06-01 11:14:17 -06002841 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
2843}
2844
Stephen Warren74b654c2011-06-01 11:14:18 -06002845static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2846 int dig1, int dig2)
2847{
2848 set_dig_out_convert(codec, nid, dig1, dig2);
2849 /* unmute amp switch (if any) */
2850 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2851 (dig1 & AC_DIG1_ENABLE))
2852 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2853 HDA_AMP_MUTE, 0);
2854}
2855
Takashi Iwai0ba21762007-04-16 11:29:14 +02002856static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002860 int idx = kcontrol->private_value;
2861 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2862 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 unsigned short val;
2864 int change;
2865
Ingo Molnar62932df2006-01-16 16:34:20 +01002866 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002867 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002869 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002870 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002871 spdif->ctls = val;
2872 if (change && nid != (u16)-1)
2873 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002874 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return change;
2876}
2877
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002878static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 {
2880 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2881 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002882 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .info = snd_hda_spdif_mask_info,
2884 .get = snd_hda_spdif_cmask_get,
2885 },
2886 {
2887 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2888 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002889 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 .info = snd_hda_spdif_mask_info,
2891 .get = snd_hda_spdif_pmask_get,
2892 },
2893 {
2894 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002895 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .info = snd_hda_spdif_mask_info,
2897 .get = snd_hda_spdif_default_get,
2898 .put = snd_hda_spdif_default_put,
2899 },
2900 {
2901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002902 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 .info = snd_hda_spdif_out_switch_info,
2904 .get = snd_hda_spdif_out_switch_get,
2905 .put = snd_hda_spdif_out_switch_put,
2906 },
2907 { } /* end */
2908};
2909
2910/**
2911 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2912 * @codec: the HDA codec
2913 * @nid: audio out widget NID
2914 *
2915 * Creates controls related with the SPDIF output.
2916 * Called from each patch supporting the SPDIF out.
2917 *
2918 * Returns 0 if successful, or a negative error code.
2919 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002920int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2921 hda_nid_t associated_nid,
2922 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002925 struct snd_kcontrol *kctl;
2926 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002927 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002928 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Takashi Iwai1afe2062010-12-23 10:17:52 +01002930 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2931 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002932 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2933 return -EBUSY;
2934 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002935 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2937 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002938 if (!kctl)
2939 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002940 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002941 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002942 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return err;
2945 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002946 spdif->nid = cvt_nid;
2947 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002948 AC_VERB_GET_DIGI_CONVERT_1, 0);
2949 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return 0;
2951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002952EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Stephen Warren7c9359762011-06-01 11:14:17 -06002954struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2955 hda_nid_t nid)
2956{
2957 int i;
2958 for (i = 0; i < codec->spdif_out.used; i++) {
2959 struct hda_spdif_out *spdif =
2960 snd_array_elem(&codec->spdif_out, i);
2961 if (spdif->nid == nid)
2962 return spdif;
2963 }
2964 return NULL;
2965}
2966EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2967
Stephen Warren74b654c2011-06-01 11:14:18 -06002968void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2969{
2970 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2971
2972 mutex_lock(&codec->spdif_mutex);
2973 spdif->nid = (u16)-1;
2974 mutex_unlock(&codec->spdif_mutex);
2975}
2976EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2977
2978void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2979{
2980 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2981 unsigned short val;
2982
2983 mutex_lock(&codec->spdif_mutex);
2984 if (spdif->nid != nid) {
2985 spdif->nid = nid;
2986 val = spdif->ctls;
2987 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2988 }
2989 mutex_unlock(&codec->spdif_mutex);
2990}
2991EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002994 * SPDIF sharing with analog output
2995 */
2996static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_value *ucontrol)
2998{
2999 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3000 ucontrol->value.integer.value[0] = mout->share_spdif;
3001 return 0;
3002}
3003
3004static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
3006{
3007 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3008 mout->share_spdif = !!ucontrol->value.integer.value[0];
3009 return 0;
3010}
3011
3012static struct snd_kcontrol_new spdif_share_sw = {
3013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3014 .name = "IEC958 Default PCM Playback Switch",
3015 .info = snd_ctl_boolean_mono_info,
3016 .get = spdif_share_sw_get,
3017 .put = spdif_share_sw_put,
3018};
3019
Takashi Iwaid5191e52009-11-16 14:58:17 +01003020/**
3021 * snd_hda_create_spdif_share_sw - create Default PCM switch
3022 * @codec: the HDA codec
3023 * @mout: multi-out instance
3024 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003025int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3026 struct hda_multi_out *mout)
3027{
3028 if (!mout->dig_out_nid)
3029 return 0;
3030 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003031 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3032 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003034EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003035
3036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 * SPDIF input
3038 */
3039
3040#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3041
Takashi Iwai0ba21762007-04-16 11:29:14 +02003042static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3043 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3046
3047 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3048 return 0;
3049}
3050
Takashi Iwai0ba21762007-04-16 11:29:14 +02003051static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3055 hda_nid_t nid = kcontrol->private_value;
3056 unsigned int val = !!ucontrol->value.integer.value[0];
3057 int change;
3058
Ingo Molnar62932df2006-01-16 16:34:20 +01003059 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003061 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003063 snd_hda_codec_write_cache(codec, nid, 0,
3064 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003066 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return change;
3068}
3069
Takashi Iwai0ba21762007-04-16 11:29:14 +02003070static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3074 hda_nid_t nid = kcontrol->private_value;
3075 unsigned short val;
3076 unsigned int sbits;
3077
Andrew Paprocki3982d172007-12-19 12:13:44 +01003078 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 sbits = convert_to_spdif_status(val);
3080 ucontrol->value.iec958.status[0] = sbits;
3081 ucontrol->value.iec958.status[1] = sbits >> 8;
3082 ucontrol->value.iec958.status[2] = sbits >> 16;
3083 ucontrol->value.iec958.status[3] = sbits >> 24;
3084 return 0;
3085}
3086
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003087static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 {
3089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003090 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 .info = snd_hda_spdif_in_switch_info,
3092 .get = snd_hda_spdif_in_switch_get,
3093 .put = snd_hda_spdif_in_switch_put,
3094 },
3095 {
3096 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003098 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 .info = snd_hda_spdif_mask_info,
3100 .get = snd_hda_spdif_in_status_get,
3101 },
3102 { } /* end */
3103};
3104
3105/**
3106 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3107 * @codec: the HDA codec
3108 * @nid: audio in widget NID
3109 *
3110 * Creates controls related with the SPDIF input.
3111 * Called from each patch supporting the SPDIF in.
3112 *
3113 * Returns 0 if successful, or a negative error code.
3114 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003115int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003118 struct snd_kcontrol *kctl;
3119 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003120 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Takashi Iwai1afe2062010-12-23 10:17:52 +01003122 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3123 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003124 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3125 return -EBUSY;
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3128 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003129 if (!kctl)
3130 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003132 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return err;
3135 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003136 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003137 snd_hda_codec_read(codec, nid, 0,
3138 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003139 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return 0;
3141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003142EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Takashi Iwai2a439522011-07-26 09:52:50 +02003144#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003145/*
3146 * command cache
3147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003149/* build a 32bit cache key with the widget id and the command parameter */
3150#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3151#define get_cmd_cache_nid(key) ((key) & 0xff)
3152#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3153
3154/**
3155 * snd_hda_codec_write_cache - send a single command with caching
3156 * @codec: the HDA codec
3157 * @nid: NID to send the command
3158 * @direct: direct flag
3159 * @verb: the verb to send
3160 * @parm: the parameter for the verb
3161 *
3162 * Send a single command without waiting for response.
3163 *
3164 * Returns 0 if successful, or a negative error code.
3165 */
3166int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3167 int direct, unsigned int verb, unsigned int parm)
3168{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003169 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3170 struct hda_cache_head *c;
3171 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003172
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003173 if (err < 0)
3174 return err;
3175 /* parm may contain the verb stuff for get/set amp */
3176 verb = verb | (parm >> 8);
3177 parm &= 0xff;
3178 key = build_cmd_cache_key(nid, verb);
3179 mutex_lock(&codec->bus->cmd_mutex);
3180 c = get_alloc_hash(&codec->cmd_cache, key);
3181 if (c)
3182 c->val = parm;
3183 mutex_unlock(&codec->bus->cmd_mutex);
3184 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003185}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003186EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003187
Takashi Iwaid5191e52009-11-16 14:58:17 +01003188/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003189 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3190 * @codec: the HDA codec
3191 * @nid: NID to send the command
3192 * @direct: direct flag
3193 * @verb: the verb to send
3194 * @parm: the parameter for the verb
3195 *
3196 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3197 * command if the parameter is already identical with the cached value.
3198 * If not, it sends the command and refreshes the cache.
3199 *
3200 * Returns 0 if successful, or a negative error code.
3201 */
3202int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3203 int direct, unsigned int verb, unsigned int parm)
3204{
3205 struct hda_cache_head *c;
3206 u32 key;
3207
3208 /* parm may contain the verb stuff for get/set amp */
3209 verb = verb | (parm >> 8);
3210 parm &= 0xff;
3211 key = build_cmd_cache_key(nid, verb);
3212 mutex_lock(&codec->bus->cmd_mutex);
3213 c = get_hash(&codec->cmd_cache, key);
3214 if (c && c->val == parm) {
3215 mutex_unlock(&codec->bus->cmd_mutex);
3216 return 0;
3217 }
3218 mutex_unlock(&codec->bus->cmd_mutex);
3219 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3220}
3221EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3222
3223/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003224 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3225 * @codec: HD-audio codec
3226 *
3227 * Execute all verbs recorded in the command caches to resume.
3228 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003229void snd_hda_codec_resume_cache(struct hda_codec *codec)
3230{
Takashi Iwai603c4012008-07-30 15:01:44 +02003231 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003232 int i;
3233
Takashi Iwai603c4012008-07-30 15:01:44 +02003234 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003235 u32 key = buffer->key;
3236 if (!key)
3237 continue;
3238 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3239 get_cmd_cache_cmd(key), buffer->val);
3240 }
3241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003242EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003243
3244/**
3245 * snd_hda_sequence_write_cache - sequence writes with caching
3246 * @codec: the HDA codec
3247 * @seq: VERB array to send
3248 *
3249 * Send the commands sequentially from the given array.
3250 * Thte commands are recorded on cache for power-save and resume.
3251 * The array must be terminated with NID=0.
3252 */
3253void snd_hda_sequence_write_cache(struct hda_codec *codec,
3254 const struct hda_verb *seq)
3255{
3256 for (; seq->nid; seq++)
3257 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3258 seq->param);
3259}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003260EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003261#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003262
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003263void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3264 unsigned int power_state,
3265 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003266{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003267 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003268 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003269
Takashi Iwaicb53c622007-08-10 17:21:45 +02003270 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003271 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003272 if (!(wcaps & AC_WCAP_POWER))
3273 continue;
3274 /* don't power down the widget if it controls eapd and
3275 * EAPD_BTLENABLE is set.
3276 */
3277 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3278 get_wcaps_type(wcaps) == AC_WID_PIN &&
3279 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3280 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003281 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003282 if (eapd & 0x02)
3283 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003284 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003285 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3286 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003287 }
3288
Takashi Iwaicb53c622007-08-10 17:21:45 +02003289 if (power_state == AC_PWRST_D0) {
3290 unsigned long end_time;
3291 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003292 /* wait until the codec reachs to D0 */
3293 end_time = jiffies + msecs_to_jiffies(500);
3294 do {
3295 state = snd_hda_codec_read(codec, fg, 0,
3296 AC_VERB_GET_POWER_STATE, 0);
3297 if (state == power_state)
3298 break;
3299 msleep(1);
3300 } while (time_after_eq(end_time, jiffies));
3301 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003302}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003303EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3304
3305/*
3306 * set power state of the codec
3307 */
3308static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3309 unsigned int power_state)
3310{
3311 if (codec->patch_ops.set_power_state) {
3312 codec->patch_ops.set_power_state(codec, fg, power_state);
3313 return;
3314 }
3315
3316 /* this delay seems necessary to avoid click noise at power-down */
3317 if (power_state == AC_PWRST_D3)
3318 msleep(100);
3319 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3320 power_state);
3321 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3322}
Takashi Iwai54d17402005-11-21 16:33:22 +01003323
Takashi Iwai11aeff02008-07-30 15:01:46 +02003324#ifdef CONFIG_SND_HDA_HWDEP
3325/* execute additional init verbs */
3326static void hda_exec_init_verbs(struct hda_codec *codec)
3327{
3328 if (codec->init_verbs.list)
3329 snd_hda_sequence_write(codec, codec->init_verbs.list);
3330}
3331#else
3332static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3333#endif
3334
Takashi Iwai2a439522011-07-26 09:52:50 +02003335#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003336/*
3337 * call suspend and power-down; used both from PM and power-save
3338 */
3339static void hda_call_codec_suspend(struct hda_codec *codec)
3340{
3341 if (codec->patch_ops.suspend)
3342 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003343 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003344 hda_set_power_state(codec,
3345 codec->afg ? codec->afg : codec->mfg,
3346 AC_PWRST_D3);
3347#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003348 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003349 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003350 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003351 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003352 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003353#endif
3354}
3355
3356/*
3357 * kick up codec; used both from PM and power-save
3358 */
3359static void hda_call_codec_resume(struct hda_codec *codec)
3360{
3361 hda_set_power_state(codec,
3362 codec->afg ? codec->afg : codec->mfg,
3363 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003364 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003365 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003366 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003367 if (codec->patch_ops.resume)
3368 codec->patch_ops.resume(codec);
3369 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003370 if (codec->patch_ops.init)
3371 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003372 snd_hda_codec_resume_amp(codec);
3373 snd_hda_codec_resume_cache(codec);
3374 }
3375}
Takashi Iwai2a439522011-07-26 09:52:50 +02003376#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003377
Takashi Iwai54d17402005-11-21 16:33:22 +01003378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379/**
3380 * snd_hda_build_controls - build mixer controls
3381 * @bus: the BUS
3382 *
3383 * Creates mixer controls for each codec included in the bus.
3384 *
3385 * Returns 0 if successful, otherwise a negative error code.
3386 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003387int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003389 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Takashi Iwai0ba21762007-04-16 11:29:14 +02003391 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003392 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003393 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003394 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003395 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003396 err = snd_hda_codec_reset(codec);
3397 if (err < 0) {
3398 printk(KERN_ERR
3399 "hda_codec: cannot revert codec\n");
3400 return err;
3401 }
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003404 return 0;
3405}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003406EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003408int snd_hda_codec_build_controls(struct hda_codec *codec)
3409{
3410 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003411 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003412 /* continue to initialize... */
3413 if (codec->patch_ops.init)
3414 err = codec->patch_ops.init(codec);
3415 if (!err && codec->patch_ops.build_controls)
3416 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003417 if (err < 0)
3418 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return 0;
3420}
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422/*
3423 * stream formats
3424 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003425struct hda_rate_tbl {
3426 unsigned int hz;
3427 unsigned int alsa_bits;
3428 unsigned int hda_fmt;
3429};
3430
Takashi Iwai92f10b32010-08-03 14:21:00 +02003431/* rate = base * mult / div */
3432#define HDA_RATE(base, mult, div) \
3433 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3434 (((div) - 1) << AC_FMT_DIV_SHIFT))
3435
Takashi Iwaibefdf312005-08-22 13:57:55 +02003436static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003438
3439 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003440 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3441 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3442 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3443 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3444 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3445 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3446 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3447 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3448 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3449 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3450 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003451#define AC_PAR_PCM_RATE_BITS 11
3452 /* up to bits 10, 384kHZ isn't supported properly */
3453
3454 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003455 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003456
Takashi Iwaibefdf312005-08-22 13:57:55 +02003457 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458};
3459
3460/**
3461 * snd_hda_calc_stream_format - calculate format bitset
3462 * @rate: the sample rate
3463 * @channels: the number of channels
3464 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3465 * @maxbps: the max. bps
3466 *
3467 * Calculate the format bitset from the given rate, channels and th PCM format.
3468 *
3469 * Return zero if invalid.
3470 */
3471unsigned int snd_hda_calc_stream_format(unsigned int rate,
3472 unsigned int channels,
3473 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003474 unsigned int maxbps,
3475 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
3477 int i;
3478 unsigned int val = 0;
3479
Takashi Iwaibefdf312005-08-22 13:57:55 +02003480 for (i = 0; rate_bits[i].hz; i++)
3481 if (rate_bits[i].hz == rate) {
3482 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 break;
3484 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003485 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 snd_printdd("invalid rate %d\n", rate);
3487 return 0;
3488 }
3489
3490 if (channels == 0 || channels > 8) {
3491 snd_printdd("invalid channels %d\n", channels);
3492 return 0;
3493 }
3494 val |= channels - 1;
3495
3496 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003497 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003498 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003499 break;
3500 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003501 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 case 20:
3504 case 24:
3505 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003506 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003507 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003509 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003511 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 break;
3513 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003514 snd_printdd("invalid format width %d\n",
3515 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return 0;
3517 }
3518
Anssi Hannula32c168c2010-08-03 13:28:57 +03003519 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003520 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return val;
3523}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003524EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003526static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3527{
3528 unsigned int val = 0;
3529 if (nid != codec->afg &&
3530 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3531 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3532 if (!val || val == -1)
3533 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3534 if (!val || val == -1)
3535 return 0;
3536 return val;
3537}
3538
3539static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3540{
3541 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3542 get_pcm_param);
3543}
3544
3545static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3546{
3547 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3548 if (!streams || streams == -1)
3549 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3550 if (!streams || streams == -1)
3551 return 0;
3552 return streams;
3553}
3554
3555static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3556{
3557 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3558 get_stream_param);
3559}
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561/**
3562 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3563 * @codec: the HDA codec
3564 * @nid: NID to query
3565 * @ratesp: the pointer to store the detected rate bitflags
3566 * @formatsp: the pointer to store the detected formats
3567 * @bpsp: the pointer to store the detected format widths
3568 *
3569 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3570 * or @bsps argument is ignored.
3571 *
3572 * Returns 0 if successful, otherwise a negative error code.
3573 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003574int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3576{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003577 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003579 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003580 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 if (ratesp) {
3583 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003584 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003586 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003588 if (rates == 0) {
3589 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3590 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3591 nid, val,
3592 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3593 return -EIO;
3594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 *ratesp = rates;
3596 }
3597
3598 if (formatsp || bpsp) {
3599 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003600 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003602 streams = query_stream_param(codec, nid);
3603 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 bps = 0;
3607 if (streams & AC_SUPFMT_PCM) {
3608 if (val & AC_SUPPCM_BITS_8) {
3609 formats |= SNDRV_PCM_FMTBIT_U8;
3610 bps = 8;
3611 }
3612 if (val & AC_SUPPCM_BITS_16) {
3613 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3614 bps = 16;
3615 }
3616 if (wcaps & AC_WCAP_DIGITAL) {
3617 if (val & AC_SUPPCM_BITS_32)
3618 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3619 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3620 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3621 if (val & AC_SUPPCM_BITS_24)
3622 bps = 24;
3623 else if (val & AC_SUPPCM_BITS_20)
3624 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003625 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3626 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3628 if (val & AC_SUPPCM_BITS_32)
3629 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 else if (val & AC_SUPPCM_BITS_24)
3631 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003632 else if (val & AC_SUPPCM_BITS_20)
3633 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 }
3635 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003636 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003638 if (!bps)
3639 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003640 }
3641 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003642 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* temporary hack: we have still no proper support
3644 * for the direct AC3 stream...
3645 */
3646 formats |= SNDRV_PCM_FMTBIT_U8;
3647 bps = 8;
3648 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003649 if (formats == 0) {
3650 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3651 "(nid=0x%x, val=0x%x, ovrd=%i, "
3652 "streams=0x%x)\n",
3653 nid, val,
3654 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3655 streams);
3656 return -EIO;
3657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (formatsp)
3659 *formatsp = formats;
3660 if (bpsp)
3661 *bpsp = bps;
3662 }
3663
3664 return 0;
3665}
Stephen Warren384a48d2011-06-01 11:14:21 -06003666EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
3668/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003669 * snd_hda_is_supported_format - Check the validity of the format
3670 * @codec: HD-audio codec
3671 * @nid: NID to check
3672 * @format: the HD-audio format value to check
3673 *
3674 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 *
3676 * Returns 1 if supported, 0 if not.
3677 */
3678int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3679 unsigned int format)
3680{
3681 int i;
3682 unsigned int val = 0, rate, stream;
3683
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003684 val = query_pcm_param(codec, nid);
3685 if (!val)
3686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003689 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003690 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (val & (1 << i))
3692 break;
3693 return 0;
3694 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003695 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return 0;
3697
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003698 stream = query_stream_param(codec, nid);
3699 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return 0;
3701
3702 if (stream & AC_SUPFMT_PCM) {
3703 switch (format & 0xf0) {
3704 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003705 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return 0;
3707 break;
3708 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003709 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return 0;
3711 break;
3712 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003713 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return 0;
3715 break;
3716 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003717 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return 0;
3719 break;
3720 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003721 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return 0;
3723 break;
3724 default:
3725 return 0;
3726 }
3727 } else {
3728 /* FIXME: check for float32 and AC3? */
3729 }
3730
3731 return 1;
3732}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003733EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735/*
3736 * PCM stuff
3737 */
3738static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3739 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003740 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 return 0;
3743}
3744
3745static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3746 struct hda_codec *codec,
3747 unsigned int stream_tag,
3748 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003749 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3752 return 0;
3753}
3754
3755static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3756 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003757 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Takashi Iwai888afa12008-03-18 09:57:50 +01003759 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 0;
3761}
3762
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003763static int set_pcm_default_values(struct hda_codec *codec,
3764 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003766 int err;
3767
Takashi Iwai0ba21762007-04-16 11:29:14 +02003768 /* query support PCM information from the given NID */
3769 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003770 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003771 info->rates ? NULL : &info->rates,
3772 info->formats ? NULL : &info->formats,
3773 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003774 if (err < 0)
3775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777 if (info->ops.open == NULL)
3778 info->ops.open = hda_pcm_default_open_close;
3779 if (info->ops.close == NULL)
3780 info->ops.close = hda_pcm_default_open_close;
3781 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003782 if (snd_BUG_ON(!info->nid))
3783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 info->ops.prepare = hda_pcm_default_prepare;
3785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003787 if (snd_BUG_ON(!info->nid))
3788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 info->ops.cleanup = hda_pcm_default_cleanup;
3790 }
3791 return 0;
3792}
3793
Takashi Iwaieb541332010-08-06 13:48:11 +02003794/*
3795 * codec prepare/cleanup entries
3796 */
3797int snd_hda_codec_prepare(struct hda_codec *codec,
3798 struct hda_pcm_stream *hinfo,
3799 unsigned int stream,
3800 unsigned int format,
3801 struct snd_pcm_substream *substream)
3802{
3803 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003804 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003805 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3806 if (ret >= 0)
3807 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003808 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003809 return ret;
3810}
3811EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3812
3813void snd_hda_codec_cleanup(struct hda_codec *codec,
3814 struct hda_pcm_stream *hinfo,
3815 struct snd_pcm_substream *substream)
3816{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003817 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003818 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003819 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003820}
3821EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3822
Takashi Iwaid5191e52009-11-16 14:58:17 +01003823/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003824const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3825 "Audio", "SPDIF", "HDMI", "Modem"
3826};
3827
Takashi Iwai176d5332008-07-30 15:01:44 +02003828/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003829 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003830 *
3831 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003832 */
3833static int get_empty_pcm_device(struct hda_bus *bus, int type)
3834{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003835 /* audio device indices; not linear to keep compatibility */
3836 static int audio_idx[HDA_PCM_NTYPES][5] = {
3837 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3838 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003839 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003840 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003841 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003842 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003843
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003844 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003845 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3846 return -EINVAL;
3847 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003848
3849 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3850 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3851 return audio_idx[type][i];
3852
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003853 snd_printk(KERN_WARNING "Too many %s devices\n",
3854 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003855 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003856}
3857
3858/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003859 * attach a new PCM stream
3860 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003861static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003862{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003863 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003864 struct hda_pcm_stream *info;
3865 int stream, err;
3866
Takashi Iwaib91f0802008-11-04 08:43:08 +01003867 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003868 return -EINVAL;
3869 for (stream = 0; stream < 2; stream++) {
3870 info = &pcm->stream[stream];
3871 if (info->substreams) {
3872 err = set_pcm_default_values(codec, info);
3873 if (err < 0)
3874 return err;
3875 }
3876 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003877 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003878}
3879
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003880/* assign all PCMs of the given codec */
3881int snd_hda_codec_build_pcms(struct hda_codec *codec)
3882{
3883 unsigned int pcm;
3884 int err;
3885
3886 if (!codec->num_pcms) {
3887 if (!codec->patch_ops.build_pcms)
3888 return 0;
3889 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003890 if (err < 0) {
3891 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003892 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003893 err = snd_hda_codec_reset(codec);
3894 if (err < 0) {
3895 printk(KERN_ERR
3896 "hda_codec: cannot revert codec\n");
3897 return err;
3898 }
3899 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003900 }
3901 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3902 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3903 int dev;
3904
3905 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003906 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003907
3908 if (!cpcm->pcm) {
3909 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3910 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003911 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003912 cpcm->device = dev;
3913 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003914 if (err < 0) {
3915 printk(KERN_ERR "hda_codec: cannot attach "
3916 "PCM stream %d for codec #%d\n",
3917 dev, codec->addr);
3918 continue; /* no fatal error */
3919 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003920 }
3921 }
3922 return 0;
3923}
3924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925/**
3926 * snd_hda_build_pcms - build PCM information
3927 * @bus: the BUS
3928 *
3929 * Create PCM information for each codec included in the bus.
3930 *
3931 * The build_pcms codec patch is requested to set up codec->num_pcms and
3932 * codec->pcm_info properly. The array is referred by the top-level driver
3933 * to create its PCM instances.
3934 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3935 * callback.
3936 *
3937 * At least, substreams, channels_min and channels_max must be filled for
3938 * each stream. substreams = 0 indicates that the stream doesn't exist.
3939 * When rates and/or formats are zero, the supported values are queried
3940 * from the given nid. The nid is used also by the default ops.prepare
3941 * and ops.cleanup callbacks.
3942 *
3943 * The driver needs to call ops.open in its open callback. Similarly,
3944 * ops.close is supposed to be called in the close callback.
3945 * ops.prepare should be called in the prepare or hw_params callback
3946 * with the proper parameters for set up.
3947 * ops.cleanup should be called in hw_free for clean up of streams.
3948 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003949 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003951int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003953 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Takashi Iwai0ba21762007-04-16 11:29:14 +02003955 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003956 int err = snd_hda_codec_build_pcms(codec);
3957 if (err < 0)
3958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960 return 0;
3961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964/**
3965 * snd_hda_check_board_config - compare the current codec with the config table
3966 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003967 * @num_configs: number of config enums
3968 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 * @tbl: configuration table, terminated by null entries
3970 *
3971 * Compares the modelname or PCI subsystem id of the current codec with the
3972 * given configuration table. If a matching entry is found, returns its
3973 * config value (supposed to be 0 or positive).
3974 *
3975 * If no entries are matching, the function returns a negative value.
3976 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003977int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003978 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003979 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003981 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003982 int i;
3983 for (i = 0; i < num_configs; i++) {
3984 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003985 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003986 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3987 "selected\n", models[i]);
3988 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
3990 }
3991 }
3992
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003993 if (!codec->bus->pci || !tbl)
3994 return -1;
3995
3996 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3997 if (!tbl)
3998 return -1;
3999 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004000#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004001 char tmp[10];
4002 const char *model = NULL;
4003 if (models)
4004 model = models[tbl->value];
4005 if (!model) {
4006 sprintf(tmp, "#%d", tbl->value);
4007 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004009 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4010 "for config %x:%x (%s)\n",
4011 model, tbl->subvendor, tbl->subdevice,
4012 (tbl->name ? tbl->name : "Unknown device"));
4013#endif
4014 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
4016 return -1;
4017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004018EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004021 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004022 subsystem ID with the
4023 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004024
4025 This is important for Gateway notebooks with SB450 HDA Audio
4026 where the vendor ID of the PCI device is:
4027 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4028 and the vendor/subvendor are found only at the codec.
4029
4030 * @codec: the HDA codec
4031 * @num_configs: number of config enums
4032 * @models: array of model name strings
4033 * @tbl: configuration table, terminated by null entries
4034 *
4035 * Compares the modelname or PCI subsystem id of the current codec with the
4036 * given configuration table. If a matching entry is found, returns its
4037 * config value (supposed to be 0 or positive).
4038 *
4039 * If no entries are matching, the function returns a negative value.
4040 */
4041int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004042 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004043 const struct snd_pci_quirk *tbl)
4044{
4045 const struct snd_pci_quirk *q;
4046
4047 /* Search for codec ID */
4048 for (q = tbl; q->subvendor; q++) {
4049 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
4050
4051 if (vendorid == codec->subsystem_id)
4052 break;
4053 }
4054
4055 if (!q->subvendor)
4056 return -1;
4057
4058 tbl = q;
4059
4060 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004061#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004062 char tmp[10];
4063 const char *model = NULL;
4064 if (models)
4065 model = models[tbl->value];
4066 if (!model) {
4067 sprintf(tmp, "#%d", tbl->value);
4068 model = tmp;
4069 }
4070 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4071 "for config %x:%x (%s)\n",
4072 model, tbl->subvendor, tbl->subdevice,
4073 (tbl->name ? tbl->name : "Unknown device"));
4074#endif
4075 return tbl->value;
4076 }
4077 return -1;
4078}
4079EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4080
4081/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 * snd_hda_add_new_ctls - create controls from the array
4083 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004084 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 *
4086 * This helper function creates and add new controls in the given array.
4087 * The array must be terminated with an empty entry as terminator.
4088 *
4089 * Returns 0 if successful, or a negative error code.
4090 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004091int snd_hda_add_new_ctls(struct hda_codec *codec,
4092 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004097 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004098 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004099 if (knew->iface == -1) /* skip this codec private value */
4100 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004101 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004102 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004103 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004104 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004105 if (addr > 0)
4106 kctl->id.device = addr;
4107 if (idx > 0)
4108 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004109 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004110 if (!err)
4111 break;
4112 /* try first with another device index corresponding to
4113 * the codec addr; if it still fails (or it's the
4114 * primary codec), then try another control index
4115 */
4116 if (!addr && codec->addr)
4117 addr = codec->addr;
4118 else if (!idx && !knew->index) {
4119 idx = find_empty_mixer_ctl_idx(codec,
4120 knew->name);
4121 if (idx <= 0)
4122 return err;
4123 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004124 return err;
4125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
4127 return 0;
4128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004129EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Takashi Iwaicb53c622007-08-10 17:21:45 +02004131#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004132static void hda_power_work(struct work_struct *work)
4133{
4134 struct hda_codec *codec =
4135 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004136 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004137
Maxim Levitsky2e492462007-09-03 15:26:57 +02004138 if (!codec->power_on || codec->power_count) {
4139 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004140 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004141 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004142
Takashi Iwaid66fee52011-08-02 15:39:31 +02004143 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004144 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004145 if (bus->ops.pm_notify)
4146 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004147}
4148
4149static void hda_keep_power_on(struct hda_codec *codec)
4150{
4151 codec->power_count++;
4152 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004153 codec->power_jiffies = jiffies;
4154}
4155
Takashi Iwaid5191e52009-11-16 14:58:17 +01004156/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004157void snd_hda_update_power_acct(struct hda_codec *codec)
4158{
4159 unsigned long delta = jiffies - codec->power_jiffies;
4160 if (codec->power_on)
4161 codec->power_on_acct += delta;
4162 else
4163 codec->power_off_acct += delta;
4164 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004165}
4166
Takashi Iwaid5191e52009-11-16 14:58:17 +01004167/**
4168 * snd_hda_power_up - Power-up the codec
4169 * @codec: HD-audio codec
4170 *
4171 * Increment the power-up counter and power up the hardware really when
4172 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004173 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004174void snd_hda_power_up(struct hda_codec *codec)
4175{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004176 struct hda_bus *bus = codec->bus;
4177
Takashi Iwaicb53c622007-08-10 17:21:45 +02004178 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004179 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004180 return;
4181
Takashi Iwaid66fee52011-08-02 15:39:31 +02004182 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004183 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004184 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004185 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004186 if (bus->ops.pm_notify)
4187 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004188 hda_call_codec_resume(codec);
4189 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004190 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004192EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004193
4194#define power_save(codec) \
4195 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004196
Takashi Iwaid5191e52009-11-16 14:58:17 +01004197/**
4198 * snd_hda_power_down - Power-down the codec
4199 * @codec: HD-audio codec
4200 *
4201 * Decrement the power-up counter and schedules the power-off work if
4202 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004203 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004204void snd_hda_power_down(struct hda_codec *codec)
4205{
4206 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004207 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004208 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004209 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004210 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004211 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004212 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004213 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004215EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004216
Takashi Iwaid5191e52009-11-16 14:58:17 +01004217/**
4218 * snd_hda_check_amp_list_power - Check the amp list and update the power
4219 * @codec: HD-audio codec
4220 * @check: the object containing an AMP list and the status
4221 * @nid: NID to check / update
4222 *
4223 * Check whether the given NID is in the amp list. If it's in the list,
4224 * check the current AMP status, and update the the power-status according
4225 * to the mute status.
4226 *
4227 * This function is supposed to be set or called from the check_power_status
4228 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004229 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004230int snd_hda_check_amp_list_power(struct hda_codec *codec,
4231 struct hda_loopback_check *check,
4232 hda_nid_t nid)
4233{
Takashi Iwai031024e2011-05-02 11:29:30 +02004234 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004235 int ch, v;
4236
4237 if (!check->amplist)
4238 return 0;
4239 for (p = check->amplist; p->nid; p++) {
4240 if (p->nid == nid)
4241 break;
4242 }
4243 if (!p->nid)
4244 return 0; /* nothing changed */
4245
4246 for (p = check->amplist; p->nid; p++) {
4247 for (ch = 0; ch < 2; ch++) {
4248 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4249 p->idx);
4250 if (!(v & HDA_AMP_MUTE) && v > 0) {
4251 if (!check->power_on) {
4252 check->power_on = 1;
4253 snd_hda_power_up(codec);
4254 }
4255 return 1;
4256 }
4257 }
4258 }
4259 if (check->power_on) {
4260 check->power_on = 0;
4261 snd_hda_power_down(codec);
4262 }
4263 return 0;
4264}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004265EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004268/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004269 * Channel mode helper
4270 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004271
4272/**
4273 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4274 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004275int snd_hda_ch_mode_info(struct hda_codec *codec,
4276 struct snd_ctl_elem_info *uinfo,
4277 const struct hda_channel_mode *chmode,
4278 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004279{
4280 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4281 uinfo->count = 1;
4282 uinfo->value.enumerated.items = num_chmodes;
4283 if (uinfo->value.enumerated.item >= num_chmodes)
4284 uinfo->value.enumerated.item = num_chmodes - 1;
4285 sprintf(uinfo->value.enumerated.name, "%dch",
4286 chmode[uinfo->value.enumerated.item].channels);
4287 return 0;
4288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004289EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004290
Takashi Iwaid5191e52009-11-16 14:58:17 +01004291/**
4292 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004294int snd_hda_ch_mode_get(struct hda_codec *codec,
4295 struct snd_ctl_elem_value *ucontrol,
4296 const struct hda_channel_mode *chmode,
4297 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004298 int max_channels)
4299{
4300 int i;
4301
4302 for (i = 0; i < num_chmodes; i++) {
4303 if (max_channels == chmode[i].channels) {
4304 ucontrol->value.enumerated.item[0] = i;
4305 break;
4306 }
4307 }
4308 return 0;
4309}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004310EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004311
Takashi Iwaid5191e52009-11-16 14:58:17 +01004312/**
4313 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4314 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004315int snd_hda_ch_mode_put(struct hda_codec *codec,
4316 struct snd_ctl_elem_value *ucontrol,
4317 const struct hda_channel_mode *chmode,
4318 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004319 int *max_channelsp)
4320{
4321 unsigned int mode;
4322
4323 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004324 if (mode >= num_chmodes)
4325 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004326 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004327 return 0;
4328 /* change the current channel setting */
4329 *max_channelsp = chmode[mode].channels;
4330 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004331 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004332 return 1;
4333}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004334EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336/*
4337 * input MUX helper
4338 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004339
4340/**
4341 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4342 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004343int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4344 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 unsigned int index;
4347
4348 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4349 uinfo->count = 1;
4350 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004351 if (!imux->num_items)
4352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 index = uinfo->value.enumerated.item;
4354 if (index >= imux->num_items)
4355 index = imux->num_items - 1;
4356 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4357 return 0;
4358}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004359EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Takashi Iwaid5191e52009-11-16 14:58:17 +01004361/**
4362 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4363 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004364int snd_hda_input_mux_put(struct hda_codec *codec,
4365 const struct hda_input_mux *imux,
4366 struct snd_ctl_elem_value *ucontrol,
4367 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 unsigned int *cur_val)
4369{
4370 unsigned int idx;
4371
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004372 if (!imux->num_items)
4373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 idx = ucontrol->value.enumerated.item[0];
4375 if (idx >= imux->num_items)
4376 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004377 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004379 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4380 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 *cur_val = idx;
4382 return 1;
4383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004384EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386
4387/*
4388 * Multi-channel / digital-out PCM helper functions
4389 */
4390
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004391/* setup SPDIF output stream */
4392static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4393 unsigned int stream_tag, unsigned int format)
4394{
Stephen Warren7c9359762011-06-01 11:14:17 -06004395 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4396
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004397 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004398 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004399 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004400 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004401 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004402 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004403 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004404 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004405 for (d = codec->slave_dig_outs; *d; d++)
4406 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4407 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004408 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004409 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004410 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004411 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004412 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004413}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004414
Takashi Iwai2f728532008-09-25 16:32:41 +02004415static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4416{
4417 snd_hda_codec_cleanup_stream(codec, nid);
4418 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004419 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004420 for (d = codec->slave_dig_outs; *d; d++)
4421 snd_hda_codec_cleanup_stream(codec, *d);
4422 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004423}
4424
Takashi Iwaid5191e52009-11-16 14:58:17 +01004425/**
4426 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4427 * @bus: HD-audio bus
4428 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004429void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4430{
4431 struct hda_codec *codec;
4432
4433 if (!bus)
4434 return;
4435 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004436 if (hda_codec_is_power_on(codec) &&
4437 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004438 codec->patch_ops.reboot_notify(codec);
4439 }
4440}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004441EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004442
Takashi Iwaid5191e52009-11-16 14:58:17 +01004443/**
4444 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004446int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4447 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Ingo Molnar62932df2006-01-16 16:34:20 +01004449 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004450 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4451 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004452 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004454 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return 0;
4456}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004457EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Takashi Iwaid5191e52009-11-16 14:58:17 +01004459/**
4460 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4461 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004462int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4463 struct hda_multi_out *mout,
4464 unsigned int stream_tag,
4465 unsigned int format,
4466 struct snd_pcm_substream *substream)
4467{
4468 mutex_lock(&codec->spdif_mutex);
4469 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4470 mutex_unlock(&codec->spdif_mutex);
4471 return 0;
4472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004473EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004474
Takashi Iwaid5191e52009-11-16 14:58:17 +01004475/**
4476 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4477 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004478int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4479 struct hda_multi_out *mout)
4480{
4481 mutex_lock(&codec->spdif_mutex);
4482 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4483 mutex_unlock(&codec->spdif_mutex);
4484 return 0;
4485}
4486EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4487
Takashi Iwaid5191e52009-11-16 14:58:17 +01004488/**
4489 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004491int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4492 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Ingo Molnar62932df2006-01-16 16:34:20 +01004494 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004496 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return 0;
4498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004499EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Takashi Iwaid5191e52009-11-16 14:58:17 +01004501/**
4502 * snd_hda_multi_out_analog_open - open analog outputs
4503 *
4504 * Open analog outputs and set up the hw-constraints.
4505 * If the digital outputs can be opened as slave, open the digital
4506 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004508int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4509 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004510 struct snd_pcm_substream *substream,
4511 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Takashi Iwai9a081602008-02-12 18:37:26 +01004513 struct snd_pcm_runtime *runtime = substream->runtime;
4514 runtime->hw.channels_max = mout->max_channels;
4515 if (mout->dig_out_nid) {
4516 if (!mout->analog_rates) {
4517 mout->analog_rates = hinfo->rates;
4518 mout->analog_formats = hinfo->formats;
4519 mout->analog_maxbps = hinfo->maxbps;
4520 } else {
4521 runtime->hw.rates = mout->analog_rates;
4522 runtime->hw.formats = mout->analog_formats;
4523 hinfo->maxbps = mout->analog_maxbps;
4524 }
4525 if (!mout->spdif_rates) {
4526 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4527 &mout->spdif_rates,
4528 &mout->spdif_formats,
4529 &mout->spdif_maxbps);
4530 }
4531 mutex_lock(&codec->spdif_mutex);
4532 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004533 if ((runtime->hw.rates & mout->spdif_rates) &&
4534 (runtime->hw.formats & mout->spdif_formats)) {
4535 runtime->hw.rates &= mout->spdif_rates;
4536 runtime->hw.formats &= mout->spdif_formats;
4537 if (mout->spdif_maxbps < hinfo->maxbps)
4538 hinfo->maxbps = mout->spdif_maxbps;
4539 } else {
4540 mout->share_spdif = 0;
4541 /* FIXME: need notify? */
4542 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004543 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004544 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4547 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4548}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004549EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Takashi Iwaid5191e52009-11-16 14:58:17 +01004551/**
4552 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4553 *
4554 * Set up the i/o for analog out.
4555 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004557int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4558 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 unsigned int stream_tag,
4560 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004561 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Takashi Iwaidda14412011-05-02 11:29:30 +02004563 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004565 struct hda_spdif_out *spdif =
4566 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 int i;
4568
Ingo Molnar62932df2006-01-16 16:34:20 +01004569 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004570 if (mout->dig_out_nid && mout->share_spdif &&
4571 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004573 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4574 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004575 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004577 setup_dig_out_stream(codec, mout->dig_out_nid,
4578 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 } else {
4580 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004581 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
4583 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004584 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004587 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4588 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004589 if (!mout->no_share_stream &&
4590 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004592 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4593 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004594 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004595 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4596 if (!mout->no_share_stream && mout->hp_out_nid[i])
4597 snd_hda_codec_setup_stream(codec,
4598 mout->hp_out_nid[i],
4599 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004600 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004601 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004602 snd_hda_codec_setup_stream(codec,
4603 mout->extra_out_nid[i],
4604 stream_tag, 0, format);
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /* surrounds */
4607 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004608 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004609 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4610 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004611 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004612 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4613 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615 return 0;
4616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004617EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Takashi Iwaid5191e52009-11-16 14:58:17 +01004619/**
4620 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004622int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4623 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Takashi Iwaidda14412011-05-02 11:29:30 +02004625 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 int i;
4627
4628 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004629 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004631 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004632 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4633 if (mout->hp_out_nid[i])
4634 snd_hda_codec_cleanup_stream(codec,
4635 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004636 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4637 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004638 snd_hda_codec_cleanup_stream(codec,
4639 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004640 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004642 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 mout->dig_out_used = 0;
4644 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004645 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return 0;
4647}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004648EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004650/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004651 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004652 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004653
Takashi Iwaidda14412011-05-02 11:29:30 +02004654static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004655{
4656 for (; *list; list++)
4657 if (*list == nid)
4658 return 1;
4659 return 0;
4660}
4661
Steve Longerbeam81937d32007-05-08 15:33:03 +02004662
4663/*
4664 * Sort an associated group of pins according to their sequence numbers.
4665 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004666static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004667 int num_pins)
4668{
4669 int i, j;
4670 short seq;
4671 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004672
Steve Longerbeam81937d32007-05-08 15:33:03 +02004673 for (i = 0; i < num_pins; i++) {
4674 for (j = i + 1; j < num_pins; j++) {
4675 if (sequences[i] > sequences[j]) {
4676 seq = sequences[i];
4677 sequences[i] = sequences[j];
4678 sequences[j] = seq;
4679 nid = pins[i];
4680 pins[i] = pins[j];
4681 pins[j] = nid;
4682 }
4683 }
4684 }
4685}
4686
4687
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004688/* add the found input-pin to the cfg->inputs[] table */
4689static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4690 int type)
4691{
4692 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4693 cfg->inputs[cfg->num_inputs].pin = nid;
4694 cfg->inputs[cfg->num_inputs].type = type;
4695 cfg->num_inputs++;
4696 }
4697}
4698
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004699/* sort inputs in the order of AUTO_PIN_* type */
4700static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4701{
4702 int i, j;
4703
4704 for (i = 0; i < cfg->num_inputs; i++) {
4705 for (j = i + 1; j < cfg->num_inputs; j++) {
4706 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4707 struct auto_pin_cfg_item tmp;
4708 tmp = cfg->inputs[i];
4709 cfg->inputs[i] = cfg->inputs[j];
4710 cfg->inputs[j] = tmp;
4711 }
4712 }
4713 }
4714}
4715
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004716/* Reorder the surround channels
4717 * ALSA sequence is front/surr/clfe/side
4718 * HDA sequence is:
4719 * 4-ch: front/surr => OK as it is
4720 * 6-ch: front/clfe/surr
4721 * 8-ch: front/clfe/rear/side|fc
4722 */
4723static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4724{
4725 hda_nid_t nid;
4726
4727 switch (nums) {
4728 case 3:
4729 case 4:
4730 nid = pins[1];
4731 pins[1] = pins[2];
4732 pins[2] = nid;
4733 break;
4734 }
4735}
4736
Takashi Iwai82bc9552006-03-21 11:24:42 +01004737/*
4738 * Parse all pin widgets and store the useful pin nids to cfg
4739 *
4740 * The number of line-outs or any primary output is stored in line_outs,
4741 * and the corresponding output pins are assigned to line_out_pins[],
4742 * in the order of front, rear, CLFE, side, ...
4743 *
4744 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004745 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004746 * is detected, one of speaker of HP pins is assigned as the primary
4747 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4748 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004749 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004750 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004751 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4752 * respectively.
4753 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004754int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4755 struct auto_pin_cfg *cfg,
4756 const hda_nid_t *ignore_nids,
4757 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004758{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004759 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004760 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004761 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4762 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004763 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004764 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004765
4766 memset(cfg, 0, sizeof(*cfg));
4767
Steve Longerbeam81937d32007-05-08 15:33:03 +02004768 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4769 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004770 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004771 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004772
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004773 end_nid = codec->start_nid + codec->num_nodes;
4774 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004775 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004776 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004777 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004778 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004779
4780 /* read all default configuration for pin complex */
4781 if (wid_type != AC_WID_PIN)
4782 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004783 /* ignore the given nids (e.g. pc-beep returns error) */
4784 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4785 continue;
4786
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004787 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004788 conn = get_defcfg_connect(def_conf);
4789 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004790 continue;
4791 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004792 dev = get_defcfg_device(def_conf);
4793
4794 /* workaround for buggy BIOS setups */
4795 if (dev == AC_JACK_LINE_OUT) {
4796 if (conn == AC_JACK_PORT_FIXED)
4797 dev = AC_JACK_SPEAKER;
4798 }
4799
4800 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004801 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004802 seq = get_defcfg_sequence(def_conf);
4803 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004804
4805 if (!(wid_caps & AC_WCAP_STEREO))
4806 if (!cfg->mono_out_pin)
4807 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004808 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004809 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004810 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004811 assoc_line_out = assoc;
4812 else if (assoc_line_out != assoc)
4813 continue;
4814 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4815 continue;
4816 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004817 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004818 cfg->line_outs++;
4819 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004820 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004821 seq = get_defcfg_sequence(def_conf);
4822 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004823 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4824 continue;
4825 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004826 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004827 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004828 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004829 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004830 seq = get_defcfg_sequence(def_conf);
4831 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004832 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4833 continue;
4834 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004835 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004836 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004837 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004838 case AC_JACK_MIC_IN:
4839 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004840 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004841 case AC_JACK_LINE_IN:
4842 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004843 break;
4844 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004845 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004846 break;
4847 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004848 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004849 break;
4850 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004851 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004852 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4853 continue;
4854 cfg->dig_out_pins[cfg->dig_outs] = nid;
4855 cfg->dig_out_type[cfg->dig_outs] =
4856 (loc == AC_JACK_LOC_HDMI) ?
4857 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4858 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004859 break;
4860 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004861 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004862 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004863 if (loc == AC_JACK_LOC_HDMI)
4864 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4865 else
4866 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004867 break;
4868 }
4869 }
4870
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004871 /* FIX-UP:
4872 * If no line-out is defined but multiple HPs are found,
4873 * some of them might be the real line-outs.
4874 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004875 if (!cfg->line_outs && cfg->hp_outs > 1 &&
4876 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004877 int i = 0;
4878 while (i < cfg->hp_outs) {
4879 /* The real HPs should have the sequence 0x0f */
4880 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4881 i++;
4882 continue;
4883 }
4884 /* Move it to the line-out table */
4885 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4886 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4887 cfg->line_outs++;
4888 cfg->hp_outs--;
4889 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4890 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004891 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004892 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4893 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004894 memset(cfg->hp_pins + cfg->hp_outs, 0,
4895 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004896 if (!cfg->hp_outs)
4897 cfg->line_out_type = AUTO_PIN_HP_OUT;
4898
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004899 }
4900
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004901 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004902 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4903 cfg->line_outs);
4904 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4905 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004906 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4907 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004908
Steve Longerbeam81937d32007-05-08 15:33:03 +02004909 /*
4910 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4911 * as a primary output
4912 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004913 if (!cfg->line_outs &&
4914 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02004915 if (cfg->speaker_outs) {
4916 cfg->line_outs = cfg->speaker_outs;
4917 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4918 sizeof(cfg->speaker_pins));
4919 cfg->speaker_outs = 0;
4920 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4921 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4922 } else if (cfg->hp_outs) {
4923 cfg->line_outs = cfg->hp_outs;
4924 memcpy(cfg->line_out_pins, cfg->hp_pins,
4925 sizeof(cfg->hp_pins));
4926 cfg->hp_outs = 0;
4927 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4928 cfg->line_out_type = AUTO_PIN_HP_OUT;
4929 }
4930 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004931
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004932 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
4933 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
4934 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004935
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004936 sort_autocfg_input_pins(cfg);
4937
Takashi Iwai82bc9552006-03-21 11:24:42 +01004938 /*
4939 * debug prints of the parsed results
4940 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004941 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 +01004942 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4943 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004944 cfg->line_out_pins[4],
4945 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4946 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4947 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004948 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4949 cfg->speaker_outs, cfg->speaker_pins[0],
4950 cfg->speaker_pins[1], cfg->speaker_pins[2],
4951 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004952 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4953 cfg->hp_outs, cfg->hp_pins[0],
4954 cfg->hp_pins[1], cfg->hp_pins[2],
4955 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004956 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004957 if (cfg->dig_outs)
4958 snd_printd(" dig-out=0x%x/0x%x\n",
4959 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004960 snd_printd(" inputs:");
4961 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004962 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004963 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004964 cfg->inputs[i].pin);
4965 }
4966 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004967 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004968 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004969
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004970 return 0;
4971}
Takashi Iwaid025feb2011-08-23 15:24:39 +02004972EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004973
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004974int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004975{
4976 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004977 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004978 if (conn == AC_JACK_PORT_NONE)
4979 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004980 /* Windows may claim the internal mic to be BOTH, too */
4981 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004982 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004983 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004984 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004985 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004986 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004987 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004988 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004989 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004990 return INPUT_PIN_ATTR_FRONT;
4991 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004992}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004993EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004994
Takashi Iwai990061c2010-09-09 22:08:44 +02004995/**
4996 * hda_get_input_pin_label - Give a label for the given input pin
4997 *
4998 * When check_location is true, the function checks the pin location
4999 * for mic and line-in pins, and set an appropriate prefix like "Front",
5000 * "Rear", "Internal".
5001 */
5002
Takashi Iwai10a20af2010-09-09 16:28:02 +02005003const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
5004 int check_location)
5005{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005006 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005007 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005008 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5009 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005010 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005011
Takashi Iwai10a20af2010-09-09 16:28:02 +02005012 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005013
5014 switch (get_defcfg_device(def_conf)) {
5015 case AC_JACK_MIC_IN:
5016 if (!check_location)
5017 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005018 attr = snd_hda_get_input_pin_attr(def_conf);
5019 if (!attr)
5020 return "None";
5021 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005022 case AC_JACK_LINE_IN:
5023 if (!check_location)
5024 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005025 attr = snd_hda_get_input_pin_attr(def_conf);
5026 if (!attr)
5027 return "None";
5028 if (attr == INPUT_PIN_ATTR_DOCK)
5029 return "Dock Line";
5030 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005031 case AC_JACK_AUX:
5032 return "Aux";
5033 case AC_JACK_CD:
5034 return "CD";
5035 case AC_JACK_SPDIF_IN:
5036 return "SPDIF In";
5037 case AC_JACK_DIG_OTHER_IN:
5038 return "Digital In";
5039 default:
5040 return "Misc";
5041 }
5042}
5043EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
5044
Takashi Iwaia1c98512010-09-09 21:36:27 +02005045/* Check whether the location prefix needs to be added to the label.
5046 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5047 * have to put "Front" prefix to each label. In such a case, returns false.
5048 */
5049static int check_mic_location_need(struct hda_codec *codec,
5050 const struct auto_pin_cfg *cfg,
5051 int input)
5052{
5053 unsigned int defc;
5054 int i, attr, attr2;
5055
5056 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005057 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005058 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005059 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005060 return 1;
5061
5062 attr = 0;
5063 for (i = 0; i < cfg->num_inputs; i++) {
5064 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005065 attr2 = snd_hda_get_input_pin_attr(defc);
5066 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005067 if (attr && attr != attr2)
5068 return 1; /* different locations found */
5069 attr = attr2;
5070 }
5071 }
5072 return 0;
5073}
5074
Takashi Iwai990061c2010-09-09 22:08:44 +02005075/**
5076 * hda_get_autocfg_input_label - Get a label for the given input
5077 *
5078 * Get a label for the given input pin defined by the autocfg item.
5079 * Unlike hda_get_input_pin_label(), this function checks all inputs
5080 * defined in autocfg and avoids the redundant mic/line prefix as much as
5081 * possible.
5082 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005083const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5084 const struct auto_pin_cfg *cfg,
5085 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005086{
5087 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005088 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005089
Takashi Iwai10a20af2010-09-09 16:28:02 +02005090 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5091 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5092 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005093 if (has_multiple_pins && type == AUTO_PIN_MIC)
5094 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005095 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5096 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005097}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005098EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5099
Takashi Iwai990061c2010-09-09 22:08:44 +02005100/**
5101 * snd_hda_add_imux_item - Add an item to input_mux
5102 *
5103 * When the same label is used already in the existing items, the number
5104 * suffix is appended to the label. This label index number is stored
5105 * to type_idx when non-NULL pointer is given.
5106 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005107int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5108 int index, int *type_idx)
5109{
5110 int i, label_idx = 0;
5111 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5112 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5113 return -EINVAL;
5114 }
5115 for (i = 0; i < imux->num_items; i++) {
5116 if (!strncmp(label, imux->items[i].label, strlen(label)))
5117 label_idx++;
5118 }
5119 if (type_idx)
5120 *type_idx = label_idx;
5121 if (label_idx > 0)
5122 snprintf(imux->items[imux->num_items].label,
5123 sizeof(imux->items[imux->num_items].label),
5124 "%s %d", label, label_idx);
5125 else
5126 strlcpy(imux->items[imux->num_items].label, label,
5127 sizeof(imux->items[imux->num_items].label));
5128 imux->items[imux->num_items].index = index;
5129 imux->num_items++;
5130 return 0;
5131}
5132EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005133
Takashi Iwai4a471b72005-12-07 13:56:29 +01005134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135#ifdef CONFIG_PM
5136/*
5137 * power management
5138 */
5139
5140/**
5141 * snd_hda_suspend - suspend the codecs
5142 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 *
5144 * Returns 0 if successful.
5145 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005146int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005148 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Takashi Iwai0ba21762007-04-16 11:29:14 +02005150 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005151 if (hda_codec_is_power_on(codec))
5152 hda_call_codec_suspend(codec);
5153 if (codec->patch_ops.post_suspend)
5154 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 }
5156 return 0;
5157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005158EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
5160/**
5161 * snd_hda_resume - resume the codecs
5162 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 *
5164 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005165 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005166 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005167 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 */
5169int snd_hda_resume(struct hda_bus *bus)
5170{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005171 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Takashi Iwai0ba21762007-04-16 11:29:14 +02005173 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005174 if (codec->patch_ops.pre_resume)
5175 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005176 if (snd_hda_codec_needs_resume(codec))
5177 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return 0;
5180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005181EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005182#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005183
5184/*
5185 * generic arrays
5186 */
5187
Takashi Iwaid5191e52009-11-16 14:58:17 +01005188/**
5189 * snd_array_new - get a new element from the given array
5190 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005191 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005192 * Get a new element from the given array. If it exceeds the
5193 * pre-allocated array size, re-allocate the array.
5194 *
5195 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005196 */
5197void *snd_array_new(struct snd_array *array)
5198{
5199 if (array->used >= array->alloced) {
5200 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005201 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005202 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005203 void *nlist;
5204 if (snd_BUG_ON(num >= 4096))
5205 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005206 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005207 if (!nlist)
5208 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005209 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005210 array->list = nlist;
5211 array->alloced = num;
5212 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005213 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005215EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005216
Takashi Iwaid5191e52009-11-16 14:58:17 +01005217/**
5218 * snd_array_free - free the given array elements
5219 * @array: the array object
5220 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005221void snd_array_free(struct snd_array *array)
5222{
5223 kfree(array->list);
5224 array->used = 0;
5225 array->alloced = 0;
5226 array->list = NULL;
5227}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005228EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005229
Takashi Iwaid5191e52009-11-16 14:58:17 +01005230/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005231 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5232 * @pcm: PCM caps bits
5233 * @buf: the string buffer to write
5234 * @buflen: the max buffer length
5235 *
5236 * used by hda_proc.c and hda_eld.c
5237 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005238void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5239{
5240 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5241 int i, j;
5242
5243 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5244 if (pcm & (AC_SUPPCM_BITS_8 << i))
5245 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5246
5247 buf[j] = '\0'; /* necessary when j == 0 */
5248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005249EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005250
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005251#ifdef CONFIG_SND_HDA_INPUT_JACK
5252/*
5253 * Input-jack notification support
5254 */
5255struct hda_jack_item {
5256 hda_nid_t nid;
5257 int type;
5258 struct snd_jack *jack;
5259};
5260
5261static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5262 int type)
5263{
5264 switch (type) {
5265 case SND_JACK_HEADPHONE:
5266 return "Headphone";
5267 case SND_JACK_MICROPHONE:
5268 return "Mic";
5269 case SND_JACK_LINEOUT:
5270 return "Line-out";
David Henningsson7c2f8e42011-10-05 15:53:25 +02005271 case SND_JACK_LINEIN:
5272 return "Line-in";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005273 case SND_JACK_HEADSET:
5274 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005275 case SND_JACK_VIDEOOUT:
5276 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005277 default:
5278 return "Misc";
5279 }
5280}
5281
5282static void hda_free_jack_priv(struct snd_jack *jack)
5283{
5284 struct hda_jack_item *jacks = jack->private_data;
5285 jacks->nid = 0;
5286 jacks->jack = NULL;
5287}
5288
5289int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5290 const char *name)
5291{
5292 struct hda_jack_item *jack;
5293 int err;
5294
5295 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5296 jack = snd_array_new(&codec->jacks);
5297 if (!jack)
5298 return -ENOMEM;
5299
5300 jack->nid = nid;
5301 jack->type = type;
5302 if (!name)
5303 name = get_jack_default_name(codec, nid, type);
5304 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5305 if (err < 0) {
5306 jack->nid = 0;
5307 return err;
5308 }
5309 jack->jack->private_data = jack;
5310 jack->jack->private_free = hda_free_jack_priv;
5311 return 0;
5312}
5313EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5314
5315void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5316{
5317 struct hda_jack_item *jacks = codec->jacks.list;
5318 int i;
5319
5320 if (!jacks)
5321 return;
5322
5323 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5324 unsigned int pin_ctl;
5325 unsigned int present;
5326 int type;
5327
5328 if (jacks->nid != nid)
5329 continue;
5330 present = snd_hda_jack_detect(codec, nid);
5331 type = jacks->type;
5332 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5333 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5334 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5335 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5336 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5337 }
5338 snd_jack_report(jacks->jack, present ? type : 0);
5339 }
5340}
5341EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5342
5343/* free jack instances manually when clearing/reconfiguring */
5344void snd_hda_input_jack_free(struct hda_codec *codec)
5345{
5346 if (!codec->bus->shutdown && codec->jacks.list) {
5347 struct hda_jack_item *jacks = codec->jacks.list;
5348 int i;
5349 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5350 if (jacks->jack)
5351 snd_device_free(codec->bus->card, jacks->jack);
5352 }
5353 }
5354 snd_array_free(&codec->jacks);
5355}
5356EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5357#endif /* CONFIG_SND_HDA_INPUT_JACK */
5358
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005359MODULE_DESCRIPTION("HDA codec core");
5360MODULE_LICENSE("GPL");