blob: c74a044284b07a27a0727d6fdc140c5e9ae51ae1 [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
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwaicb53c622007-08-10 17:21:45 +020097#ifdef CONFIG_SND_HDA_POWER_SAVE
98static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#else
102static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200103#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200104#endif
105
Takashi Iwaid5191e52009-11-16 14:58:17 +0100106/**
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
109 *
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
112 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400113const char *snd_hda_get_jack_location(u32 cfg)
114{
115 static char *bases[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 };
118 static unsigned char specials_idx[] = {
119 0x07, 0x08,
120 0x17, 0x18, 0x19,
121 0x37, 0x38
122 };
123 static char *specials[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
127 };
128 int i;
129 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 if ((cfg & 0x0f) < 7)
131 return bases[cfg & 0x0f];
132 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 if (cfg == specials_idx[i])
134 return specials[i];
135 }
136 return "UNKNOWN";
137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139
Takashi Iwaid5191e52009-11-16 14:58:17 +0100140/**
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
143 *
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
146 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147const char *snd_hda_get_jack_connectivity(u32 cfg)
148{
149 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150
151 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100153EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154
Takashi Iwaid5191e52009-11-16 14:58:17 +0100155/**
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
158 *
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162const char *snd_hda_get_jack_type(u32 cfg)
163{
164 static char *jack_types[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
David Henningsson54144192013-04-04 11:47:13 +0200168 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400169 };
170
171 return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 >> AC_DEFCFG_DEVICE_SHIFT];
173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100174EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400175
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176/*
177 * Compose a 32bit command word to be sent to the HD-audio controller
178 */
179static inline unsigned int
180make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 unsigned int verb, unsigned int parm)
182{
183 u32 val;
184
Takashi Iwai82e1b802009-07-17 12:47:34 +0200185 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800187 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec->addr, direct, nid, verb, parm);
189 return ~0;
190 }
191
192 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193 val |= (u32)direct << 27;
194 val |= (u32)nid << 20;
195 val |= verb << 8;
196 val |= parm;
197 return val;
198}
199
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200/*
201 * Send and receive a verb
202 */
203static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
204 unsigned int *res)
205{
206 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200207 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208
Wu Fengguang6430aee2009-07-17 16:49:19 +0800209 if (cmd == ~0)
210 return -1;
211
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 if (res)
213 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 snd_hda_power_up(codec);
216 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200217 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200219 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800220 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200221 trace_hda_get_response(codec, *res);
222 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 mutex_unlock(&bus->cmd_mutex);
224 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200225 if (res && *res == -1 && bus->rirb_error) {
226 if (bus->response_reset) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200230 bus->ops.bus_reset(bus);
231 }
232 goto again;
233 }
234 /* clear reset-flag when the communication gets recovered */
235 if (!err)
236 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 return err;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
247 *
248 * Send a single command and read the corresponding response.
249 *
250 * Returns the obtained response value, or -1 for an error.
251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200252unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int verb, unsigned int parm)
255{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700258 if (codec_exec_verb(codec, cmd, &res))
259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return res;
261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100262EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
271 *
272 * Send a single command without waiting for response.
273 *
274 * Returns 0 if successful, or a negative error code.
275 */
276int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 unsigned int verb, unsigned int parm)
278{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200279 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100280 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200281 return codec_exec_verb(codec, cmd,
282 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100284EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/**
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
290 *
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
293 */
294void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295{
296 for (; seq->nid; seq++)
297 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100299EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
304 * @nid: NID to parse
305 * @start_id: the pointer to store the start NID
306 *
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
309 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200310int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned int parm;
314
315 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200316 if (parm == -1)
317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100321EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200323/* look up the cached results */
324static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
325{
326 int i, len;
327 for (i = 0; i < array->used; ) {
328 hda_nid_t *p = snd_array_elem(array, i);
329 if (nid == *p)
330 return p;
331 len = p[1];
332 i += len + 2;
333 }
334 return NULL;
335}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200338 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @codec: the HDA codec
340 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200341 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * Parses the connection list of the given widget and stores the list
344 * of NIDs.
345 *
346 * Returns the number of connections, or a negative error code.
347 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200348int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350{
351 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200354 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200357 again:
358 /* if the connection-list is already cached, read it */
359 p = lookup_conn_list(array, nid);
360 if (p) {
361 if (listp)
362 *listp = p + 2;
363 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 if (snd_BUG_ON(added))
366 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200368 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200369 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200370 if (len < 0)
371 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 err = snd_hda_override_conn_list(codec, nid, len, list);
373 if (err < 0)
374 return err;
375 added = true;
376 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377}
Takashi Iwaidce20792011-06-24 14:10:28 +0200378EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
379
380/**
381 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * @codec: the HDA codec
383 * @nid: NID to parse
384 * @conn_list: connection list array
385 * @max_conns: max. number of connections to store
386 *
387 * Parses the connection list of the given widget and stores the list
388 * of NIDs.
389 *
390 * Returns the number of connections, or a negative error code.
391 */
392int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200393 hda_nid_t *conn_list, int max_conns)
394{
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 const hda_nid_t *list;
396 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200399 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200400 if (len > max_conns) {
401 snd_printk(KERN_ERR "hda_codec: "
402 "Too many connections %d for NID 0x%x\n",
403 len, nid);
404 return -EINVAL;
405 }
406 memcpy(conn_list, list, len * sizeof(hda_nid_t));
407 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200408}
409EXPORT_SYMBOL_HDA(snd_hda_get_connections);
410
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200411/**
412 * snd_hda_get_raw_connections - copy connection list without cache
413 * @codec: the HDA codec
414 * @nid: NID to parse
415 * @conn_list: connection list array
416 * @max_conns: max. number of connections to store
417 *
418 * Like snd_hda_get_connections(), copy the connection list but without
419 * checking through the connection-list cache.
420 * Currently called only from hda_proc.c, so not exported.
421 */
422int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200428 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100429 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwaida3cec32008-08-08 17:12:14 +0200431 if (snd_BUG_ON(!conn_list || max_conns <= 0))
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200434 wcaps = get_wcaps(codec, nid);
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm & AC_CLIST_LONG) {
441 /* long form */
442 shift = 16;
443 num_elems = 2;
444 } else {
445 /* short form */
446 shift = 8;
447 num_elems = 4;
448 }
449 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 mask = (1 << (shift-1)) - 1;
451
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0; /* no connection */
454
455 if (conn_len == 1) {
456 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200457 parm = snd_hda_codec_read(codec, nid, 0,
458 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200459 if (parm == -1 && codec->bus->rirb_error)
460 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 conn_list[0] = parm & mask;
462 return 1;
463 }
464
465 /* multi connection */
466 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 prev_nid = 0;
468 for (i = 0; i < conn_len; i++) {
469 int range_val;
470 hda_nid_t val, n;
471
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 parm = snd_hda_codec_read(codec, nid, 0,
474 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200475 if (parm == -1 && codec->bus->rirb_error)
476 return -EIO;
477 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100479 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200480 if (val == 0) {
481 snd_printk(KERN_WARNING "hda_codec: "
482 "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 nid, i, parm);
484 return 0;
485 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100486 parm >>= shift;
487 if (range_val) {
488 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!prev_nid || prev_nid >= val) {
490 snd_printk(KERN_WARNING "hda_codec: "
491 "invalid dep_range_val %x:%x\n",
492 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 for (n = prev_nid + 1; n <= val; n++) {
496 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200497 snd_printk(KERN_ERR "hda_codec: "
498 "Too many connections %d for NID 0x%x\n",
499 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 return -EINVAL;
501 }
502 conn_list[conns++] = n;
503 }
504 } else {
505 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200506 snd_printk(KERN_ERR "hda_codec: "
507 "Too many connections %d for NID 0x%x\n",
508 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100509 return -EINVAL;
510 }
511 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return conns;
516}
517
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200518static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519{
520 hda_nid_t *p = snd_array_new(array);
521 if (!p)
522 return false;
523 *p = nid;
524 return true;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200528 * snd_hda_override_conn_list - add/modify the connection-list to cache
529 * @codec: the HDA codec
530 * @nid: NID to parse
531 * @len: number of connection list entries
532 * @list: the list of connection entries
533 *
534 * Add or modify the given connection-list to the cache. If the corresponding
535 * cache already exists, invalidate it and append a new one.
536 *
537 * Returns zero or a negative error code.
538 */
539int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 struct snd_array *array = &codec->conn_lists;
543 hda_nid_t *p;
544 int i, old_used;
545
546 p = lookup_conn_list(array, nid);
547 if (p)
548 *p = -1; /* invalidate the old entry */
549
550 old_used = array->used;
551 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 goto error_add;
553 for (i = 0; i < len; i++)
554 if (!add_conn_list(array, list[i]))
555 goto error_add;
556 return 0;
557
558 error_add:
559 array->used = old_used;
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563
564/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
570 *
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
574 */
575int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
577{
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 int i, nums;
580
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
583 if (conn[i] == nid)
584 return i;
585 if (!recursive)
586 return -1;
587 if (recursive > 5) {
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 return -1;
590 }
591 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 return -1;
600}
601EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @bus: the BUS
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 *
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616{
617 struct hda_bus_unsolicited *unsol;
618 unsigned int wp;
619
Wang YanQinga85550212013-05-07 11:27:33 +0800620 if (!bus || !bus->workq)
621 return 0;
622
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200623 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200624 unsol = bus->unsol;
625 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627
628 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
629 unsol->wp = wp;
630
631 wp <<= 1;
632 unsol->queue[wp] = res;
633 unsol->queue[wp + 1] = res_ex;
634
Takashi Iwai6acaed32009-01-12 10:09:24 +0100635 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 return 0;
638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100639EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800642 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
David Howellsc4028952006-11-22 14:57:56 +0000644static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
David Howellsc4028952006-11-22 14:57:56 +0000646 struct hda_bus_unsolicited *unsol =
647 container_of(work, struct hda_bus_unsolicited, work);
648 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct hda_codec *codec;
650 unsigned int rp, caddr, res;
651
652 while (unsol->rp != unsol->wp) {
653 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
654 unsol->rp = rp;
655 rp <<= 1;
656 res = unsol->queue[rp];
657 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200658 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 continue;
660 codec = bus->caddr_tbl[caddr & 0x0f];
661 if (codec && codec->patch_ops.unsol_event)
662 codec->patch_ops.unsol_event(codec, res);
663 }
664}
665
666/*
667 * initialize unsolicited queue
668 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200669static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct hda_bus_unsolicited *unsol;
672
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100673 if (bus->unsol) /* already initialized */
674 return 0;
675
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200676 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200677 if (!unsol) {
678 snd_printk(KERN_ERR "hda_codec: "
679 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -ENOMEM;
681 }
David Howellsc4028952006-11-22 14:57:56 +0000682 INIT_WORK(&unsol->work, process_unsol_events);
683 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 bus->unsol = unsol;
685 return 0;
686}
687
688/*
689 * destructor
690 */
691static void snd_hda_codec_free(struct hda_codec *codec);
692
693static int snd_hda_bus_free(struct hda_bus *bus)
694{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200695 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Takashi Iwai0ba21762007-04-16 11:29:14 +0200697 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100699 if (bus->workq)
700 flush_workqueue(bus->workq);
701 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200703 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 snd_hda_codec_free(codec);
705 }
706 if (bus->ops.private_free)
707 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100708 if (bus->workq)
709 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 kfree(bus);
711 return 0;
712}
713
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100714static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d3532008-11-21 09:08:06 +0100717 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return snd_hda_bus_free(bus);
719}
720
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200721#ifdef CONFIG_SND_HDA_HWDEP
722static int snd_hda_bus_dev_register(struct snd_device *device)
723{
724 struct hda_bus *bus = device->device_data;
725 struct hda_codec *codec;
726 list_for_each_entry(codec, &bus->codec_list, list) {
727 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100728 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200729 }
730 return 0;
731}
732#else
733#define snd_hda_bus_dev_register NULL
734#endif
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/**
737 * snd_hda_bus_new - create a HDA bus
738 * @card: the card entry
739 * @temp: the template for hda_bus information
740 * @busp: the pointer to store the created bus instance
741 *
742 * Returns 0 if successful, or a negative error code.
743 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100744int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200745 const struct hda_bus_template *temp,
746 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct hda_bus *bus;
749 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100750 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200751 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 .dev_free = snd_hda_bus_dev_free,
753 };
754
Takashi Iwaida3cec32008-08-08 17:12:14 +0200755 if (snd_BUG_ON(!temp))
756 return -EINVAL;
757 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (busp)
761 *busp = NULL;
762
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200763 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (bus == NULL) {
765 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
766 return -ENOMEM;
767 }
768
769 bus->card = card;
770 bus->private_data = temp->private_data;
771 bus->pci = temp->pci;
772 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100773 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 bus->ops = temp->ops;
775
Ingo Molnar62932df2006-01-16 16:34:20 +0100776 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200777 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 INIT_LIST_HEAD(&bus->codec_list);
779
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100780 snprintf(bus->workq_name, sizeof(bus->workq_name),
781 "hd-audio%d", card->number);
782 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100784 snd_printk(KERN_ERR "cannot create workqueue %s\n",
785 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100786 kfree(bus);
787 return -ENOMEM;
788 }
789
Takashi Iwai0ba21762007-04-16 11:29:14 +0200790 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
791 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 snd_hda_bus_free(bus);
793 return err;
794 }
795 if (busp)
796 *busp = bus;
797 return 0;
798}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100799EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Takashi Iwai82467612007-07-27 19:15:54 +0200801#ifdef CONFIG_SND_HDA_GENERIC
802#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200803 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200804#else
805#define is_generic_config(codec) 0
806#endif
807
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
810#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100811#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100812#endif
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
815 * find a matching codec preset
816 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200817static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200818find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100820 struct hda_codec_preset_list *tbl;
821 const struct hda_codec_preset *preset;
822 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Takashi Iwai82467612007-07-27 19:15:54 +0200824 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100825 return NULL; /* use the generic parser */
826
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100827 again:
828 mutex_lock(&preset_mutex);
829 list_for_each_entry(tbl, &hda_preset_tables, list) {
830 if (!try_module_get(tbl->owner)) {
831 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
832 continue;
833 }
834 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100836 if (preset->afg && preset->afg != codec->afg)
837 continue;
838 if (preset->mfg && preset->mfg != codec->mfg)
839 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200842 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200843 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100844 preset->rev == codec->revision_id)) {
845 mutex_unlock(&preset_mutex);
846 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100850 module_put(tbl->owner);
851 }
852 mutex_unlock(&preset_mutex);
853
854 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
855 char name[32];
856 if (!mod_requested)
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
858 codec->vendor_id);
859 else
860 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
861 (codec->vendor_id >> 16) & 0xffff);
862 request_module(name);
863 mod_requested++;
864 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 return NULL;
867}
868
869/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200870 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200872static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 const struct hda_vendor_id *c;
875 const char *vendor = NULL;
876 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200877 char tmp[16];
878
879 if (codec->vendor_name)
880 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 for (c = hda_vendor_ids; c->id; c++) {
883 if (c->id == vendor_id) {
884 vendor = c->name;
885 break;
886 }
887 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200888 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sprintf(tmp, "Generic %04x", vendor_id);
890 vendor = tmp;
891 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200892 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
893 if (!codec->vendor_name)
894 return -ENOMEM;
895
896 get_chip_name:
897 if (codec->chip_name)
898 return 0;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200901 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
902 else {
903 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
904 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
905 }
906 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200907 return -ENOMEM;
908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200912 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100914static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200916 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 hda_nid_t nid;
918
919 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
920 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200921 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200922 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200923 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200924 case AC_GRP_AUDIO_FUNCTION:
925 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 codec->afg_function_id = function_id & 0xff;
927 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 break;
929 case AC_GRP_MODEM_FUNCTION:
930 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200931 codec->mfg_function_id = function_id & 0xff;
932 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200933 break;
934 default:
935 break;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100941 * read widget caps for each widget and store in cache
942 */
943static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
944{
945 int i;
946 hda_nid_t nid;
947
948 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
949 &codec->start_nid);
950 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200951 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100952 return -ENOMEM;
953 nid = codec->start_nid;
954 for (i = 0; i < codec->num_nodes; i++, nid++)
955 codec->wcaps[i] = snd_hda_param_read(codec, nid,
956 AC_PAR_AUDIO_WIDGET_CAP);
957 return 0;
958}
959
Takashi Iwai3be14142009-02-20 14:11:16 +0100960/* read all pin default configurations and save codec->init_pins */
961static int read_pin_defaults(struct hda_codec *codec)
962{
963 int i;
964 hda_nid_t nid = codec->start_nid;
965
966 for (i = 0; i < codec->num_nodes; i++, nid++) {
967 struct hda_pincfg *pin;
968 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200969 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100970 if (wid_type != AC_WID_PIN)
971 continue;
972 pin = snd_array_new(&codec->init_pins);
973 if (!pin)
974 return -ENOMEM;
975 pin->nid = nid;
976 pin->cfg = snd_hda_codec_read(codec, nid, 0,
977 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200978 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
979 AC_VERB_GET_PIN_WIDGET_CONTROL,
980 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100981 }
982 return 0;
983}
984
985/* look up the given pin config list and return the item matching with NID */
986static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
987 struct snd_array *array,
988 hda_nid_t nid)
989{
990 int i;
991 for (i = 0; i < array->used; i++) {
992 struct hda_pincfg *pin = snd_array_elem(array, i);
993 if (pin->nid == nid)
994 return pin;
995 }
996 return NULL;
997}
998
999/* write a config value for the given NID */
1000static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
1001 unsigned int cfg)
1002{
1003 int i;
1004 for (i = 0; i < 4; i++) {
1005 snd_hda_codec_write(codec, nid, 0,
1006 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1007 cfg & 0xff);
1008 cfg >>= 8;
1009 }
1010}
1011
1012/* set the current pin config value for the given NID.
1013 * the value is cached, and read via snd_hda_codec_get_pincfg()
1014 */
1015int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1016 hda_nid_t nid, unsigned int cfg)
1017{
1018 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001019 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001020
Takashi Iwaib82855a2009-12-27 11:24:56 +01001021 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1022 return -EINVAL;
1023
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001024 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001025 pin = look_up_pincfg(codec, list, nid);
1026 if (!pin) {
1027 pin = snd_array_new(list);
1028 if (!pin)
1029 return -ENOMEM;
1030 pin->nid = nid;
1031 }
1032 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001033
1034 /* change only when needed; e.g. if the pincfg is already present
1035 * in user_pins[], don't write it
1036 */
1037 cfg = snd_hda_codec_get_pincfg(codec, nid);
1038 if (oldcfg != cfg)
1039 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001040 return 0;
1041}
1042
Takashi Iwaid5191e52009-11-16 14:58:17 +01001043/**
1044 * snd_hda_codec_set_pincfg - Override a pin default configuration
1045 * @codec: the HDA codec
1046 * @nid: NID to set the pin config
1047 * @cfg: the pin default config value
1048 *
1049 * Override a pin default configuration value in the cache.
1050 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1051 * priority than the real hardware value.
1052 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001053int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1054 hda_nid_t nid, unsigned int cfg)
1055{
Takashi Iwai346ff702009-02-23 09:42:57 +01001056 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001057}
1058EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1059
Takashi Iwaid5191e52009-11-16 14:58:17 +01001060/**
1061 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1062 * @codec: the HDA codec
1063 * @nid: NID to get the pin config
1064 *
1065 * Get the current pin config value of the given pin NID.
1066 * If the pincfg value is cached or overridden via sysfs or driver,
1067 * returns the cached value.
1068 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001069unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1070{
1071 struct hda_pincfg *pin;
1072
Takashi Iwai3be14142009-02-20 14:11:16 +01001073#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001074 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 if (pin)
1076 return pin->cfg;
1077#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001078 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1079 if (pin)
1080 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001081 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1082 if (pin)
1083 return pin->cfg;
1084 return 0;
1085}
1086EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1087
1088/* restore all current pin configs */
1089static void restore_pincfgs(struct hda_codec *codec)
1090{
1091 int i;
1092 for (i = 0; i < codec->init_pins.used; i++) {
1093 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1094 set_pincfg(codec, pin->nid,
1095 snd_hda_codec_get_pincfg(codec, pin->nid));
1096 }
1097}
Takashi Iwai54d17402005-11-21 16:33:22 +01001098
Takashi Iwai92ee6162009-12-27 11:18:59 +01001099/**
1100 * snd_hda_shutup_pins - Shut up all pins
1101 * @codec: the HDA codec
1102 *
1103 * Clear all pin controls to shup up before suspend for avoiding click noise.
1104 * The controls aren't cached so that they can be resumed properly.
1105 */
1106void snd_hda_shutup_pins(struct hda_codec *codec)
1107{
1108 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001109 /* don't shut up pins when unloading the driver; otherwise it breaks
1110 * the default pin setup at the next load of the driver
1111 */
1112 if (codec->bus->shutdown)
1113 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001114 for (i = 0; i < codec->init_pins.used; i++) {
1115 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1116 /* use read here for syncing after issuing each verb */
1117 snd_hda_codec_read(codec, pin->nid, 0,
1118 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1119 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001120 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001121}
1122EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1123
Takashi Iwai2a439522011-07-26 09:52:50 +02001124#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001125/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1126static void restore_shutup_pins(struct hda_codec *codec)
1127{
1128 int i;
1129 if (!codec->pins_shutup)
1130 return;
1131 if (codec->bus->shutdown)
1132 return;
1133 for (i = 0; i < codec->init_pins.used; i++) {
1134 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1135 snd_hda_codec_write(codec, pin->nid, 0,
1136 AC_VERB_SET_PIN_WIDGET_CONTROL,
1137 pin->ctrl);
1138 }
1139 codec->pins_shutup = 0;
1140}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001141#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001142
Takashi Iwai01751f52007-08-10 16:59:39 +02001143static void init_hda_cache(struct hda_cache_rec *cache,
1144 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001145static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001146
Takashi Iwai3be14142009-02-20 14:11:16 +01001147/* restore the initial pin cfgs and release all pincfg lists */
1148static void restore_init_pincfgs(struct hda_codec *codec)
1149{
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001151 * so that only the values in init_pins are restored
1152 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001153 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001154#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001155 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001156#endif
1157 restore_pincfgs(codec);
1158 snd_array_free(&codec->init_pins);
1159}
1160
Takashi Iwai54d17402005-11-21 16:33:22 +01001161/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001162 * audio-converter setup caches
1163 */
1164struct hda_cvt_setup {
1165 hda_nid_t nid;
1166 u8 stream_tag;
1167 u8 channel_id;
1168 u16 format_id;
1169 unsigned char active; /* cvt is currently used */
1170 unsigned char dirty; /* setups should be cleared */
1171};
1172
1173/* get or create a cache entry for the given audio converter NID */
1174static struct hda_cvt_setup *
1175get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1176{
1177 struct hda_cvt_setup *p;
1178 int i;
1179
1180 for (i = 0; i < codec->cvt_setups.used; i++) {
1181 p = snd_array_elem(&codec->cvt_setups, i);
1182 if (p->nid == nid)
1183 return p;
1184 }
1185 p = snd_array_new(&codec->cvt_setups);
1186 if (p)
1187 p->nid = nid;
1188 return p;
1189}
1190
1191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 * codec destructor
1193 */
1194static void snd_hda_codec_free(struct hda_codec *codec)
1195{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001196 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return;
Takashi Iwai354e5502012-06-26 15:00:20 +02001198 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001199 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001200#ifdef CONFIG_SND_HDA_POWER_SAVE
1201 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001202 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001205 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001206 snd_array_free(&codec->nids);
Takashi Iwai354e5502012-06-26 15:00:20 +02001207 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001208 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001209 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 codec->bus->caddr_tbl[codec->addr] = NULL;
1211 if (codec->patch_ops.free)
1212 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001213 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001214 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001215 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001216 kfree(codec->vendor_name);
1217 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001218 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001219 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 kfree(codec);
1221}
1222
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001223static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1224 unsigned int power_state);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/**
1227 * snd_hda_codec_new - create a HDA codec
1228 * @bus: the bus to assign
1229 * @codec_addr: the codec address
1230 * @codecp: the pointer to store the generated codec
1231 *
1232 * Returns 0 if successful, or a negative error code.
1233 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001234int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1235 unsigned int codec_addr,
1236 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001239 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 int err;
1241
Takashi Iwaida3cec32008-08-08 17:12:14 +02001242 if (snd_BUG_ON(!bus))
1243 return -EINVAL;
1244 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001248 snd_printk(KERN_ERR "hda_codec: "
1249 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EBUSY;
1251 }
1252
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001253 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (codec == NULL) {
1255 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1256 return -ENOMEM;
1257 }
1258
1259 codec->bus = bus;
1260 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001261 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001262 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001263 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001264 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001265 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1266 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001267 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001268 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001269 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001270 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001271 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001272 if (codec->bus->modelname) {
1273 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1274 if (!codec->modelname) {
1275 snd_hda_codec_free(codec);
1276 return -ENODEV;
1277 }
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Takashi Iwaicb53c622007-08-10 17:21:45 +02001280#ifdef CONFIG_SND_HDA_POWER_SAVE
1281 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1282 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1283 * the caller has to power down appropriatley after initialization
1284 * phase.
1285 */
1286 hda_keep_power_on(codec);
1287#endif
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 list_add_tail(&codec->list, &bus->codec_list);
1290 bus->caddr_tbl[codec_addr] = codec;
1291
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001294 if (codec->vendor_id == -1)
1295 /* read again, hopefully the access method was corrected
1296 * in the last read...
1297 */
1298 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1301 AC_PAR_SUBSYSTEM_ID);
1302 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1303 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001305 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001306 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001307 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = -ENODEV;
1309 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1313 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001314 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001315 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001316 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001317 err = read_pin_defaults(codec);
1318 if (err < 0)
1319 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001320
Takashi Iwai0ba21762007-04-16 11:29:14 +02001321 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001322 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001323 codec->subsystem_id =
1324 snd_hda_codec_read(codec, nid, 0,
1325 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001326 }
1327
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001328 /* power-up all before initialization */
1329 hda_set_power_state(codec,
1330 codec->afg ? codec->afg : codec->mfg,
1331 AC_PWRST_D0);
1332
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333 snd_hda_codec_proc_new(codec);
1334
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001335 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336
1337 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1338 codec->subsystem_id, codec->revision_id);
1339 snd_component_add(codec->bus->card, component);
1340
1341 if (codecp)
1342 *codecp = codec;
1343 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001344
1345 error:
1346 snd_hda_codec_free(codec);
1347 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001348}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001349EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001350
Takashi Iwaid5191e52009-11-16 14:58:17 +01001351/**
1352 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1353 * @codec: the HDA codec
1354 *
1355 * Start parsing of the given codec tree and (re-)initialize the whole
1356 * patch instance.
1357 *
1358 * Returns 0 if successful or a negative error code.
1359 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001360int snd_hda_codec_configure(struct hda_codec *codec)
1361{
1362 int err;
1363
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001364 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001365 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001366 err = get_codec_name(codec);
1367 if (err < 0)
1368 return err;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Takashi Iwai82467612007-07-27 19:15:54 +02001371 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001373 goto patched;
1374 }
Takashi Iwai82467612007-07-27 19:15:54 +02001375 if (codec->preset && codec->preset->patch) {
1376 err = codec->preset->patch(codec);
1377 goto patched;
1378 }
1379
1380 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001381 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001382 if (err < 0)
1383 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001384
1385 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001386 if (!err && codec->patch_ops.unsol_event)
1387 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001388 /* audio codec should override the mixer name */
1389 if (!err && (codec->afg || !*codec->bus->card->mixername))
1390 snprintf(codec->bus->card->mixername,
1391 sizeof(codec->bus->card->mixername),
1392 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001395EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397/**
1398 * snd_hda_codec_setup_stream - set up the codec for streaming
1399 * @codec: the CODEC to set up
1400 * @nid: the NID to set up
1401 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1402 * @channel_id: channel id to pass, zero based.
1403 * @format: stream format.
1404 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1406 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int channel_id, int format)
1408{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001409 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001410 struct hda_cvt_setup *p;
1411 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001412 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001413 int i;
1414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001416 return;
1417
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 snd_printdd("hda_codec_setup_stream: "
1419 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001421 p = get_hda_cvt_setup(codec, nid);
1422 if (!p)
1423 return;
1424 /* update the stream-id if changed */
1425 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1426 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1427 newval = (stream_tag << 4) | channel_id;
1428 if (oldval != newval)
1429 snd_hda_codec_write(codec, nid, 0,
1430 AC_VERB_SET_CHANNEL_STREAMID,
1431 newval);
1432 p->stream_tag = stream_tag;
1433 p->channel_id = channel_id;
1434 }
1435 /* update the format-id if changed */
1436 if (p->format_id != format) {
1437 oldval = snd_hda_codec_read(codec, nid, 0,
1438 AC_VERB_GET_STREAM_FORMAT, 0);
1439 if (oldval != format) {
1440 msleep(1);
1441 snd_hda_codec_write(codec, nid, 0,
1442 AC_VERB_SET_STREAM_FORMAT,
1443 format);
1444 }
1445 p->format_id = format;
1446 }
1447 p->active = 1;
1448 p->dirty = 0;
1449
1450 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001451 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001452 list_for_each_entry(c, &codec->bus->codec_list, list) {
1453 for (i = 0; i < c->cvt_setups.used; i++) {
1454 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001455 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001456 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001457 p->dirty = 1;
1458 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001461EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Takashi Iwaif0cea792010-08-13 11:56:53 +02001463static void really_cleanup_stream(struct hda_codec *codec,
1464 struct hda_cvt_setup *q);
1465
Takashi Iwaid5191e52009-11-16 14:58:17 +01001466/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001468 * @codec: the CODEC to clean up
1469 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001470 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001471 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001472void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1473 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001474{
Takashi Iwaieb541332010-08-06 13:48:11 +02001475 struct hda_cvt_setup *p;
1476
Takashi Iwai888afa12008-03-18 09:57:50 +01001477 if (!nid)
1478 return;
1479
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001480 if (codec->no_sticky_stream)
1481 do_now = 1;
1482
Takashi Iwai888afa12008-03-18 09:57:50 +01001483 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001484 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485 if (p) {
1486 /* here we just clear the active flag when do_now isn't set;
1487 * actual clean-ups will be done later in
1488 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1489 */
1490 if (do_now)
1491 really_cleanup_stream(codec, p);
1492 else
1493 p->active = 0;
1494 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001495}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001496EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001497
Takashi Iwaieb541332010-08-06 13:48:11 +02001498static void really_cleanup_stream(struct hda_codec *codec,
1499 struct hda_cvt_setup *q)
1500{
1501 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001502 if (q->stream_tag || q->channel_id)
1503 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1504 if (q->format_id)
1505 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1506);
Takashi Iwaieb541332010-08-06 13:48:11 +02001507 memset(q, 0, sizeof(*q));
1508 q->nid = nid;
1509}
1510
1511/* clean up the all conflicting obsolete streams */
1512static void purify_inactive_streams(struct hda_codec *codec)
1513{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001514 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001515 int i;
1516
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001517 list_for_each_entry(c, &codec->bus->codec_list, list) {
1518 for (i = 0; i < c->cvt_setups.used; i++) {
1519 struct hda_cvt_setup *p;
1520 p = snd_array_elem(&c->cvt_setups, i);
1521 if (p->dirty)
1522 really_cleanup_stream(c, p);
1523 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001524 }
1525}
1526
Takashi Iwai2a439522011-07-26 09:52:50 +02001527#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001528/* clean up all streams; called from suspend */
1529static void hda_cleanup_all_streams(struct hda_codec *codec)
1530{
1531 int i;
1532
1533 for (i = 0; i < codec->cvt_setups.used; i++) {
1534 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1535 if (p->stream_tag)
1536 really_cleanup_stream(codec, p);
1537 }
1538}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001539#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/*
1542 * amp access functions
1543 */
1544
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001545/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001546#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001547#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001548#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1549#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001551#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001554static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001555 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwai01751f52007-08-10 16:59:39 +02001557 memset(cache, 0, sizeof(*cache));
1558 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001559 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001560}
1561
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001562static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001563{
Takashi Iwai603c4012008-07-30 15:01:44 +02001564 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001568static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwai01751f52007-08-10 16:59:39 +02001570 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1571 u16 cur = cache->hash[idx];
1572 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001575 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (info->key == key)
1577 return info;
1578 cur = info->next;
1579 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001580 return NULL;
1581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001583/* query the hash. allocate an entry if not found. */
1584static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1585 u32 key)
1586{
1587 struct hda_cache_head *info = get_hash(cache, key);
1588 if (!info) {
1589 u16 idx, cur;
1590 /* add a new hash entry */
1591 info = snd_array_new(&cache->buf);
1592 if (!info)
1593 return NULL;
1594 cur = snd_array_index(&cache->buf, info);
1595 info->key = key;
1596 info->val = 0;
1597 idx = key % (u16)ARRAY_SIZE(cache->hash);
1598 info->next = cache->hash[idx];
1599 cache->hash[idx] = cur;
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return info;
1602}
1603
Takashi Iwai01751f52007-08-10 16:59:39 +02001604/* query and allocate an amp hash entry */
1605static inline struct hda_amp_info *
1606get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1607{
1608 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1609}
1610
Takashi Iwaid5191e52009-11-16 14:58:17 +01001611/**
1612 * query_amp_caps - query AMP capabilities
1613 * @codec: the HD-auio codec
1614 * @nid: the NID to query
1615 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1616 *
1617 * Query AMP capabilities for the given widget and direction.
1618 * Returns the obtained capability bits.
1619 *
1620 * When cap bits have been already read, this doesn't read again but
1621 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001623u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Takashi Iwai0ba21762007-04-16 11:29:14 +02001627 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1628 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001630 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001633 info->amp_caps = snd_hda_param_read(codec, nid,
1634 direction == HDA_OUTPUT ?
1635 AC_PAR_AMP_OUT_CAP :
1636 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001637 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001638 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 return info->amp_caps;
1641}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001642EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Takashi Iwaid5191e52009-11-16 14:58:17 +01001644/**
1645 * snd_hda_override_amp_caps - Override the AMP capabilities
1646 * @codec: the CODEC to clean up
1647 * @nid: the NID to clean up
1648 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1649 * @caps: the capability bits to set
1650 *
1651 * Override the cached AMP caps bits value by the given one.
1652 * This function is useful if the driver needs to adjust the AMP ranges,
1653 * e.g. limit to 0dB, etc.
1654 *
1655 * Returns zero if successful or a negative error code.
1656 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001657int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1658 unsigned int caps)
1659{
1660 struct hda_amp_info *info;
1661
1662 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1663 if (!info)
1664 return -EINVAL;
1665 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001666 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001667 return 0;
1668}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001669EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001670
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001671static unsigned int
1672query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1673 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001674{
1675 struct hda_amp_info *info;
1676
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001677 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001678 if (!info)
1679 return 0;
1680 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001681 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001682 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001683 }
1684 return info->amp_caps;
1685}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001686
1687static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1688{
1689 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1690}
1691
Takashi Iwaid5191e52009-11-16 14:58:17 +01001692/**
1693 * snd_hda_query_pin_caps - Query PIN capabilities
1694 * @codec: the HD-auio codec
1695 * @nid: the NID to query
1696 *
1697 * Query PIN capabilities for the given widget.
1698 * Returns the obtained capability bits.
1699 *
1700 * When cap bits have been already read, this doesn't read again but
1701 * returns the cached value.
1702 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001703u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1704{
1705 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1706 read_pin_cap);
1707}
Takashi Iwai1327a322009-03-23 13:07:47 +01001708EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1709
Wu Fengguang864f92b2009-11-18 12:38:02 +08001710/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001711 * snd_hda_override_pin_caps - Override the pin capabilities
1712 * @codec: the CODEC
1713 * @nid: the NID to override
1714 * @caps: the capability bits to set
1715 *
1716 * Override the cached PIN capabilitiy bits value by the given one.
1717 *
1718 * Returns zero if successful or a negative error code.
1719 */
1720int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1721 unsigned int caps)
1722{
1723 struct hda_amp_info *info;
1724 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1725 if (!info)
1726 return -ENOMEM;
1727 info->amp_caps = caps;
1728 info->head.val |= INFO_AMP_CAPS;
1729 return 0;
1730}
1731EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/*
1734 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001735 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001737static unsigned int get_vol_mute(struct hda_codec *codec,
1738 struct hda_amp_info *info, hda_nid_t nid,
1739 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 u32 val, parm;
1742
Takashi Iwai01751f52007-08-10 16:59:39 +02001743 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001744 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1747 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1748 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001749 val = snd_hda_codec_read(codec, nid, 0,
1750 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001752 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001753 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001757 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001759static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001760 hda_nid_t nid, int ch, int direction, int index,
1761 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 u32 parm;
1764
1765 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1766 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1767 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001768 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1769 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1770 ; /* set the zero value as a fake mute */
1771 else
1772 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001774 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Takashi Iwaid5191e52009-11-16 14:58:17 +01001777/**
1778 * snd_hda_codec_amp_read - Read AMP value
1779 * @codec: HD-audio codec
1780 * @nid: NID to read the AMP value
1781 * @ch: channel (left=0 or right=1)
1782 * @direction: #HDA_INPUT or #HDA_OUTPUT
1783 * @index: the index value (only for input direction)
1784 *
1785 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
Takashi Iwai834be882006-03-01 14:16:17 +01001787int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1788 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001790 struct hda_amp_info *info;
1791 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1792 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001794 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001796EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Takashi Iwaid5191e52009-11-16 14:58:17 +01001798/**
1799 * snd_hda_codec_amp_update - update the AMP value
1800 * @codec: HD-audio codec
1801 * @nid: NID to read the AMP value
1802 * @ch: channel (left=0 or right=1)
1803 * @direction: #HDA_INPUT or #HDA_OUTPUT
1804 * @idx: the index value (only for input direction)
1805 * @mask: bit mask to set
1806 * @val: the bits value to set
1807 *
1808 * Update the AMP value with a bit mask.
1809 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001810 */
Takashi Iwai834be882006-03-01 14:16:17 +01001811int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1812 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001814 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001815
Takashi Iwai0ba21762007-04-16 11:29:14 +02001816 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1817 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001819 if (snd_BUG_ON(mask & ~0xff))
1820 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001821 val &= mask;
1822 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001823 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001825 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 1;
1827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001828EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Takashi Iwaid5191e52009-11-16 14:58:17 +01001830/**
1831 * snd_hda_codec_amp_stereo - update the AMP stereo values
1832 * @codec: HD-audio codec
1833 * @nid: NID to read the AMP value
1834 * @direction: #HDA_INPUT or #HDA_OUTPUT
1835 * @idx: the index value (only for input direction)
1836 * @mask: bit mask to set
1837 * @val: the bits value to set
1838 *
1839 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1840 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001841 */
1842int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1843 int direction, int idx, int mask, int val)
1844{
1845 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001846
1847 if (snd_BUG_ON(mask & ~0xff))
1848 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001849 for (ch = 0; ch < 2; ch++)
1850 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1851 idx, mask, val);
1852 return ret;
1853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001855
Takashi Iwai2a439522011-07-26 09:52:50 +02001856#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001857/**
1858 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1859 * @codec: HD-audio codec
1860 *
1861 * Resume the all amp commands from the cache.
1862 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001863void snd_hda_codec_resume_amp(struct hda_codec *codec)
1864{
Takashi Iwai603c4012008-07-30 15:01:44 +02001865 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001866 int i;
1867
Takashi Iwai603c4012008-07-30 15:01:44 +02001868 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001869 u32 key = buffer->head.key;
1870 hda_nid_t nid;
1871 unsigned int idx, dir, ch;
1872 if (!key)
1873 continue;
1874 nid = key & 0xff;
1875 idx = (key >> 16) & 0xff;
1876 dir = (key >> 24) & 0xff;
1877 for (ch = 0; ch < 2; ch++) {
1878 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1879 continue;
1880 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1881 buffer->vol[ch]);
1882 }
1883 }
1884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001885EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001886#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001888static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1889 unsigned int ofs)
1890{
1891 u32 caps = query_amp_caps(codec, nid, dir);
1892 /* get num steps */
1893 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1894 if (ofs < caps)
1895 caps -= ofs;
1896 return caps;
1897}
1898
Takashi Iwaid5191e52009-11-16 14:58:17 +01001899/**
1900 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1901 *
1902 * The control element is supposed to have the private_value field
1903 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1904 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001905int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1909 u16 nid = get_amp_nid(kcontrol);
1910 u8 chs = get_amp_channels(kcontrol);
1911 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001912 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001914 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1915 uinfo->count = chs == 3 ? 2 : 1;
1916 uinfo->value.integer.min = 0;
1917 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1918 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001919 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001920 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1921 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return -EINVAL;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001926EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001928
1929static inline unsigned int
1930read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1931 int ch, int dir, int idx, unsigned int ofs)
1932{
1933 unsigned int val;
1934 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1935 val &= HDA_AMP_VOLMASK;
1936 if (val >= ofs)
1937 val -= ofs;
1938 else
1939 val = 0;
1940 return val;
1941}
1942
1943static inline int
1944update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1945 int ch, int dir, int idx, unsigned int ofs,
1946 unsigned int val)
1947{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001948 unsigned int maxval;
1949
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001950 if (val > 0)
1951 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001952 /* ofs = 0: raw max value */
1953 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001954 if (val > maxval)
1955 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001956 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1957 HDA_AMP_VOLMASK, val);
1958}
1959
Takashi Iwaid5191e52009-11-16 14:58:17 +01001960/**
1961 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1962 *
1963 * The control element is supposed to have the private_value field
1964 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1965 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001966int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1970 hda_nid_t nid = get_amp_nid(kcontrol);
1971 int chs = get_amp_channels(kcontrol);
1972 int dir = get_amp_direction(kcontrol);
1973 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001974 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 long *valp = ucontrol->value.integer.value;
1976
1977 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001978 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001980 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return 0;
1982}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001983EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Takashi Iwaid5191e52009-11-16 14:58:17 +01001985/**
1986 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1987 *
1988 * The control element is supposed to have the private_value field
1989 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001991int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1992 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1995 hda_nid_t nid = get_amp_nid(kcontrol);
1996 int chs = get_amp_channels(kcontrol);
1997 int dir = get_amp_direction(kcontrol);
1998 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 long *valp = ucontrol->value.integer.value;
2001 int change = 0;
2002
Takashi Iwaicb53c622007-08-10 17:21:45 +02002003 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002004 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002005 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002006 valp++;
2007 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002008 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002009 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002010 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return change;
2012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002013EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Takashi Iwaid5191e52009-11-16 14:58:17 +01002015/**
2016 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2017 *
2018 * The control element is supposed to have the private_value field
2019 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2020 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002021int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2022 unsigned int size, unsigned int __user *_tlv)
2023{
2024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2025 hda_nid_t nid = get_amp_nid(kcontrol);
2026 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002028 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002029 u32 caps, val1, val2;
2030
2031 if (size < 4 * sizeof(unsigned int))
2032 return -ENOMEM;
2033 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002034 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2035 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002036 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002037 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002038 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002039 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002040 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002041 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2042 return -EFAULT;
2043 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2044 return -EFAULT;
2045 if (put_user(val1, _tlv + 2))
2046 return -EFAULT;
2047 if (put_user(val2, _tlv + 3))
2048 return -EFAULT;
2049 return 0;
2050}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002051EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002052
Takashi Iwaid5191e52009-11-16 14:58:17 +01002053/**
2054 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2055 * @codec: HD-audio codec
2056 * @nid: NID of a reference widget
2057 * @dir: #HDA_INPUT or #HDA_OUTPUT
2058 * @tlv: TLV data to be stored, at least 4 elements
2059 *
2060 * Set (static) TLV data for a virtual master volume using the AMP caps
2061 * obtained from the reference NID.
2062 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002063 */
2064void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2065 unsigned int *tlv)
2066{
2067 u32 caps;
2068 int nums, step;
2069
2070 caps = query_amp_caps(codec, nid, dir);
2071 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2072 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2073 step = (step + 1) * 25;
2074 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2075 tlv[1] = 2 * sizeof(unsigned int);
2076 tlv[2] = -nums * step;
2077 tlv[3] = step;
2078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002079EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002080
2081/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002082static struct snd_kcontrol *
2083_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2084 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002085{
2086 struct snd_ctl_elem_id id;
2087 memset(&id, 0, sizeof(id));
2088 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002089 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002090 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2091 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002092 strcpy(id.name, name);
2093 return snd_ctl_find_id(codec->bus->card, &id);
2094}
2095
Takashi Iwaid5191e52009-11-16 14:58:17 +01002096/**
2097 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2098 * @codec: HD-audio codec
2099 * @name: ctl id name string
2100 *
2101 * Get the control element with the given id string and IFACE_MIXER.
2102 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002103struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2104 const char *name)
2105{
2106 return _snd_hda_find_mixer_ctl(codec, name, 0);
2107}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002108EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002109
Takashi Iwai1afe2062010-12-23 10:17:52 +01002110static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2111{
2112 int idx;
2113 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2114 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2115 return idx;
2116 }
2117 return -EBUSY;
2118}
2119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002121 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002122 * @codec: HD-audio codec
2123 * @nid: corresponding NID (optional)
2124 * @kctl: the control element to assign
2125 *
2126 * Add the given control element to an array inside the codec instance.
2127 * All control elements belonging to a codec are supposed to be added
2128 * by this function so that a proper clean-up works at the free or
2129 * reconfiguration time.
2130 *
2131 * If non-zero @nid is passed, the NID is assigned to the control element.
2132 * The assignment is shown in the codec proc file.
2133 *
2134 * snd_hda_ctl_add() checks the control subdev id field whether
2135 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002136 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2137 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002138 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002139int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2140 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002141{
2142 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002143 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002144 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002145
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002146 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002147 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002148 if (nid == 0)
2149 nid = get_amp_nid_(kctl->private_value);
2150 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002151 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2152 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002153 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002154 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002155 err = snd_ctl_add(codec->bus->card, kctl);
2156 if (err < 0)
2157 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002158 item = snd_array_new(&codec->mixers);
2159 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002160 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002161 item->kctl = kctl;
2162 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002163 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002164 return 0;
2165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002166EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002169 * snd_hda_add_nid - Assign a NID to a control element
2170 * @codec: HD-audio codec
2171 * @nid: corresponding NID (optional)
2172 * @kctl: the control element to assign
2173 * @index: index to kctl
2174 *
2175 * Add the given control element to an array inside the codec instance.
2176 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2177 * NID:KCTL mapping - for example "Capture Source" selector.
2178 */
2179int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2180 unsigned int index, hda_nid_t nid)
2181{
2182 struct hda_nid_item *item;
2183
2184 if (nid > 0) {
2185 item = snd_array_new(&codec->nids);
2186 if (!item)
2187 return -ENOMEM;
2188 item->kctl = kctl;
2189 item->index = index;
2190 item->nid = nid;
2191 return 0;
2192 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002193 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2194 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002195 return -EINVAL;
2196}
2197EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2198
2199/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002200 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2201 * @codec: HD-audio codec
2202 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002203void snd_hda_ctls_clear(struct hda_codec *codec)
2204{
2205 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002206 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002207 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002208 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002209 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002210 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002211}
2212
Takashi Iwaia65d6292009-02-23 16:57:04 +01002213/* pseudo device locking
2214 * toggle card->shutdown to allow/disallow the device access (as a hack)
2215 */
2216static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002217{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002218 spin_lock(&card->files_lock);
2219 if (card->shutdown) {
2220 spin_unlock(&card->files_lock);
2221 return -EINVAL;
2222 }
2223 card->shutdown = 1;
2224 spin_unlock(&card->files_lock);
2225 return 0;
2226}
2227
2228static void hda_unlock_devices(struct snd_card *card)
2229{
2230 spin_lock(&card->files_lock);
2231 card->shutdown = 0;
2232 spin_unlock(&card->files_lock);
2233}
2234
Takashi Iwaid5191e52009-11-16 14:58:17 +01002235/**
2236 * snd_hda_codec_reset - Clear all objects assigned to the codec
2237 * @codec: HD-audio codec
2238 *
2239 * This frees the all PCM and control elements assigned to the codec, and
2240 * clears the caches and restores the pin default configurations.
2241 *
2242 * When a device is being used, it returns -EBSY. If successfully freed,
2243 * returns zero.
2244 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002245int snd_hda_codec_reset(struct hda_codec *codec)
2246{
2247 struct snd_card *card = codec->bus->card;
2248 int i, pcm;
2249
2250 if (hda_lock_devices(card) < 0)
2251 return -EBUSY;
2252 /* check whether the codec isn't used by any mixer or PCM streams */
2253 if (!list_empty(&card->ctl_files)) {
2254 hda_unlock_devices(card);
2255 return -EBUSY;
2256 }
2257 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2258 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2259 if (!cpcm->pcm)
2260 continue;
2261 if (cpcm->pcm->streams[0].substream_opened ||
2262 cpcm->pcm->streams[1].substream_opened) {
2263 hda_unlock_devices(card);
2264 return -EBUSY;
2265 }
2266 }
2267
2268 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002269
2270#ifdef CONFIG_SND_HDA_POWER_SAVE
2271 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002272 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002273#endif
2274 snd_hda_ctls_clear(codec);
2275 /* relase PCMs */
2276 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002277 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002278 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002279 clear_bit(codec->pcm_info[i].device,
2280 codec->bus->pcm_dev_bits);
2281 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002282 }
2283 if (codec->patch_ops.free)
2284 codec->patch_ops.free(codec);
Takashi Iwai6172ace2012-09-10 09:39:31 +02002285 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002286 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002287 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002288 codec->spec = NULL;
2289 free_hda_cache(&codec->amp_cache);
2290 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002291 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2292 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002293 /* free only driver_pins so that init_pins + user_pins are restored */
2294 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002295 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002296 codec->num_pcms = 0;
2297 codec->pcm_info = NULL;
2298 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002299 codec->slave_dig_outs = NULL;
2300 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002301 module_put(codec->owner);
2302 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002303
2304 /* allow device access again */
2305 hda_unlock_devices(card);
2306 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002307}
2308
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002309typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2310
2311/* apply the function to all matching slave ctls in the mixer list */
2312static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002313 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002314{
2315 struct hda_nid_item *items;
2316 const char * const *s;
2317 int i, err;
2318
2319 items = codec->mixers.list;
2320 for (i = 0; i < codec->mixers.used; i++) {
2321 struct snd_kcontrol *sctl = items[i].kctl;
2322 if (!sctl || !sctl->id.name ||
2323 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2324 continue;
2325 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002326 char tmpname[sizeof(sctl->id.name)];
2327 const char *name = *s;
2328 if (suffix) {
2329 snprintf(tmpname, sizeof(tmpname), "%s %s",
2330 name, suffix);
2331 name = tmpname;
2332 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002333 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002334 err = func(data, sctl);
2335 if (err)
2336 return err;
2337 break;
2338 }
2339 }
2340 }
2341 return 0;
2342}
2343
2344static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2345{
2346 return 1;
2347}
2348
Takashi Iwai18478e82012-03-09 17:51:10 +01002349/* guess the value corresponding to 0dB */
2350static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2351{
2352 int _tlv[4];
2353 const int *tlv = NULL;
2354 int val = -1;
2355
2356 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2357 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2358 mm_segment_t fs = get_fs();
2359 set_fs(get_ds());
2360 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2361 tlv = _tlv;
2362 set_fs(fs);
2363 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2364 tlv = kctl->tlv.p;
2365 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2366 val = -tlv[2] / tlv[3];
2367 return val;
2368}
2369
2370/* call kctl->put with the given value(s) */
2371static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2372{
2373 struct snd_ctl_elem_value *ucontrol;
2374 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2375 if (!ucontrol)
2376 return -ENOMEM;
2377 ucontrol->value.integer.value[0] = val;
2378 ucontrol->value.integer.value[1] = val;
2379 kctl->put(kctl, ucontrol);
2380 kfree(ucontrol);
2381 return 0;
2382}
2383
2384/* initialize the slave volume with 0dB */
2385static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2386{
2387 int offset = get_kctl_0dB_offset(slave);
2388 if (offset > 0)
2389 put_kctl_with_value(slave, offset);
2390 return 0;
2391}
2392
2393/* unmute the slave */
2394static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2395{
2396 return put_kctl_with_value(slave, 1);
2397}
2398
Takashi Iwaid5191e52009-11-16 14:58:17 +01002399/**
2400 * snd_hda_add_vmaster - create a virtual master control and add slaves
2401 * @codec: HD-audio codec
2402 * @name: vmaster control name
2403 * @tlv: TLV data (optional)
2404 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002405 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002406 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002407 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002408 *
2409 * Create a virtual master control with the given name. The TLV data
2410 * must be either NULL or a valid data.
2411 *
2412 * @slaves is a NULL-terminated array of strings, each of which is a
2413 * slave control name. All controls with these names are assigned to
2414 * the new virtual master control.
2415 *
2416 * This function returns zero if successful or a negative error code.
2417 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002418int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002419 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002420 const char *suffix, bool init_slave_vol,
2421 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002422{
2423 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002424 int err;
2425
Takashi Iwai29e58532012-03-12 12:25:03 +01002426 if (ctl_ret)
2427 *ctl_ret = NULL;
2428
Takashi Iwai9322ca52012-02-03 14:28:01 +01002429 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002430 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002431 snd_printdd("No slave found for %s\n", name);
2432 return 0;
2433 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002434 kctl = snd_ctl_make_virtual_master(name, tlv);
2435 if (!kctl)
2436 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002437 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002438 if (err < 0)
2439 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002440
Takashi Iwai9322ca52012-02-03 14:28:01 +01002441 err = map_slaves(codec, slaves, suffix,
2442 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002443 if (err < 0)
2444 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002445
2446 /* init with master mute & zero volume */
2447 put_kctl_with_value(kctl, 0);
2448 if (init_slave_vol)
2449 map_slaves(codec, slaves, suffix,
2450 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2451
Takashi Iwai29e58532012-03-12 12:25:03 +01002452 if (ctl_ret)
2453 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002454 return 0;
2455}
Takashi Iwai18478e82012-03-09 17:51:10 +01002456EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002457
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002458/*
2459 * mute-LED control using vmaster
2460 */
2461static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_info *uinfo)
2463{
2464 static const char * const texts[] = {
2465 "Off", "On", "Follow Master"
2466 };
2467 unsigned int index;
2468
2469 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2470 uinfo->count = 1;
2471 uinfo->value.enumerated.items = 3;
2472 index = uinfo->value.enumerated.item;
2473 if (index >= 3)
2474 index = 2;
2475 strcpy(uinfo->value.enumerated.name, texts[index]);
2476 return 0;
2477}
2478
2479static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2481{
2482 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2483 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2484 return 0;
2485}
2486
2487static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2491 unsigned int old_mode = hook->mute_mode;
2492
2493 hook->mute_mode = ucontrol->value.enumerated.item[0];
2494 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2495 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2496 if (old_mode == hook->mute_mode)
2497 return 0;
2498 snd_hda_sync_vmaster_hook(hook);
2499 return 1;
2500}
2501
2502static struct snd_kcontrol_new vmaster_mute_mode = {
2503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2504 .name = "Mute-LED Mode",
2505 .info = vmaster_mute_mode_info,
2506 .get = vmaster_mute_mode_get,
2507 .put = vmaster_mute_mode_put,
2508};
2509
2510/*
2511 * Add a mute-LED hook with the given vmaster switch kctl
2512 * "Mute-LED Mode" control is automatically created and associated with
2513 * the given hook.
2514 */
2515int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002516 struct hda_vmaster_mute_hook *hook,
2517 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002518{
2519 struct snd_kcontrol *kctl;
2520
2521 if (!hook->hook || !hook->sw_kctl)
2522 return 0;
2523 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2524 hook->codec = codec;
2525 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002526 if (!expose_enum_ctl)
2527 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002528 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2529 if (!kctl)
2530 return -ENOMEM;
2531 return snd_hda_ctl_add(codec, 0, kctl);
2532}
2533EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2534
2535/*
2536 * Call the hook with the current value for synchronization
2537 * Should be called in init callback
2538 */
2539void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2540{
2541 if (!hook->hook || !hook->codec)
2542 return;
2543 switch (hook->mute_mode) {
2544 case HDA_VMUTE_FOLLOW_MASTER:
2545 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2546 break;
2547 default:
2548 hook->hook(hook->codec, hook->mute_mode);
2549 break;
2550 }
2551}
2552EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2553
2554
Takashi Iwaid5191e52009-11-16 14:58:17 +01002555/**
2556 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2557 *
2558 * The control element is supposed to have the private_value field
2559 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2560 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002561int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 int chs = get_amp_channels(kcontrol);
2565
2566 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2567 uinfo->count = chs == 3 ? 2 : 1;
2568 uinfo->value.integer.min = 0;
2569 uinfo->value.integer.max = 1;
2570 return 0;
2571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002572EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Takashi Iwaid5191e52009-11-16 14:58:17 +01002574/**
2575 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2576 *
2577 * The control element is supposed to have the private_value field
2578 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2579 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002580int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 hda_nid_t nid = get_amp_nid(kcontrol);
2585 int chs = get_amp_channels(kcontrol);
2586 int dir = get_amp_direction(kcontrol);
2587 int idx = get_amp_index(kcontrol);
2588 long *valp = ucontrol->value.integer.value;
2589
2590 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002591 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002592 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002594 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002595 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002598EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Takashi Iwaid5191e52009-11-16 14:58:17 +01002600/**
2601 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2602 *
2603 * The control element is supposed to have the private_value field
2604 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2605 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = get_amp_nid(kcontrol);
2611 int chs = get_amp_channels(kcontrol);
2612 int dir = get_amp_direction(kcontrol);
2613 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 long *valp = ucontrol->value.integer.value;
2615 int change = 0;
2616
Takashi Iwaicb53c622007-08-10 17:21:45 +02002617 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002618 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002619 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002620 HDA_AMP_MUTE,
2621 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002622 valp++;
2623 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002624 if (chs & 2)
2625 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002626 HDA_AMP_MUTE,
2627 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002628 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002629 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return change;
2631}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002632EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Takashi Iwai67d634c2009-11-16 15:35:59 +01002634#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002635/**
2636 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2637 *
2638 * This function calls snd_hda_enable_beep_device(), which behaves differently
2639 * depending on beep_mode option.
2640 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002641int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
2644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2645 long *valp = ucontrol->value.integer.value;
2646
2647 snd_hda_enable_beep_device(codec, *valp);
2648 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2649}
2650EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002651#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
Takashi Iwai985be542005-11-02 18:26:49 +01002654 * bound volume controls
2655 *
2656 * bind multiple volumes (# indices, from 0)
2657 */
2658
2659#define AMP_VAL_IDX_SHIFT 19
2660#define AMP_VAL_IDX_MASK (0x0f<<19)
2661
Takashi Iwaid5191e52009-11-16 14:58:17 +01002662/**
2663 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2664 *
2665 * The control element is supposed to have the private_value field
2666 * set up via HDA_BIND_MUTE*() macros.
2667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002668int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002670{
2671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 unsigned long pval;
2673 int err;
2674
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002675 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002676 pval = kcontrol->private_value;
2677 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2678 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2679 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002680 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002681 return err;
2682}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002683EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002684
Takashi Iwaid5191e52009-11-16 14:58:17 +01002685/**
2686 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2687 *
2688 * The control element is supposed to have the private_value field
2689 * set up via HDA_BIND_MUTE*() macros.
2690 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002693{
2694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2695 unsigned long pval;
2696 int i, indices, err = 0, change = 0;
2697
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002698 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002699 pval = kcontrol->private_value;
2700 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2701 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002702 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2703 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002704 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2705 if (err < 0)
2706 break;
2707 change |= err;
2708 }
2709 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002710 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002711 return err < 0 ? err : change;
2712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002713EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002714
Takashi Iwaid5191e52009-11-16 14:58:17 +01002715/**
2716 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2717 *
2718 * The control element is supposed to have the private_value field
2719 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002720 */
2721int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_info *uinfo)
2723{
2724 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2725 struct hda_bind_ctls *c;
2726 int err;
2727
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002728 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002729 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002730 kcontrol->private_value = *c->values;
2731 err = c->ops->info(kcontrol, uinfo);
2732 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002733 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002734 return err;
2735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002736EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002737
Takashi Iwaid5191e52009-11-16 14:58:17 +01002738/**
2739 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2740 *
2741 * The control element is supposed to have the private_value field
2742 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2743 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002744int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
2747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 struct hda_bind_ctls *c;
2749 int err;
2750
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002751 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002752 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002753 kcontrol->private_value = *c->values;
2754 err = c->ops->get(kcontrol, ucontrol);
2755 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002756 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002757 return err;
2758}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002759EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002760
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761/**
2762 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2763 *
2764 * The control element is supposed to have the private_value field
2765 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2766 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002767int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct hda_bind_ctls *c;
2772 unsigned long *vals;
2773 int err = 0, change = 0;
2774
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002776 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 for (vals = c->values; *vals; vals++) {
2778 kcontrol->private_value = *vals;
2779 err = c->ops->put(kcontrol, ucontrol);
2780 if (err < 0)
2781 break;
2782 change |= err;
2783 }
2784 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002785 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002786 return err < 0 ? err : change;
2787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002788EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002789
Takashi Iwaid5191e52009-11-16 14:58:17 +01002790/**
2791 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2792 *
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_BIND_VOL() macro.
2795 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002796int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2797 unsigned int size, unsigned int __user *tlv)
2798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 struct hda_bind_ctls *c;
2801 int err;
2802
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002804 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002805 kcontrol->private_value = *c->values;
2806 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2807 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002808 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809 return err;
2810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002812
2813struct hda_ctl_ops snd_hda_bind_vol = {
2814 .info = snd_hda_mixer_amp_volume_info,
2815 .get = snd_hda_mixer_amp_volume_get,
2816 .put = snd_hda_mixer_amp_volume_put,
2817 .tlv = snd_hda_mixer_amp_tlv
2818};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002820
2821struct hda_ctl_ops snd_hda_bind_sw = {
2822 .info = snd_hda_mixer_amp_switch_info,
2823 .get = snd_hda_mixer_amp_switch_get,
2824 .put = snd_hda_mixer_amp_switch_put,
2825 .tlv = snd_hda_mixer_amp_tlv
2826};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002828
2829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * SPDIF out controls
2831 */
2832
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2837 uinfo->count = 1;
2838 return 0;
2839}
2840
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2845 IEC958_AES0_NONAUDIO |
2846 IEC958_AES0_CON_EMPHASIS_5015 |
2847 IEC958_AES0_CON_NOT_COPYRIGHT;
2848 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2849 IEC958_AES1_CON_ORIGINAL;
2850 return 0;
2851}
2852
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2857 IEC958_AES0_NONAUDIO |
2858 IEC958_AES0_PRO_EMPHASIS_5015;
2859 return 0;
2860}
2861
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002866 int idx = kcontrol->private_value;
2867 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Stephen Warren7c935972011-06-01 11:14:17 -06002869 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2870 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2871 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2872 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 return 0;
2875}
2876
2877/* convert from SPDIF status bits to HDA SPDIF bits
2878 * bit 0 (DigEn) is always set zero (to be filled later)
2879 */
2880static unsigned short convert_from_spdif_status(unsigned int sbits)
2881{
2882 unsigned short val = 0;
2883
2884 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002885 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002887 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002889 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2890 IEC958_AES0_PRO_EMPHASIS_5015)
2891 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002893 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2894 IEC958_AES0_CON_EMPHASIS_5015)
2895 val |= AC_DIG1_EMPHASIS;
2896 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2897 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002899 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2901 }
2902 return val;
2903}
2904
2905/* convert to SPDIF status bits from HDA SPDIF bits
2906 */
2907static unsigned int convert_to_spdif_status(unsigned short val)
2908{
2909 unsigned int sbits = 0;
2910
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 sbits |= IEC958_AES0_PROFESSIONAL;
2915 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaie9781452013-03-20 15:42:00 +01002916 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2918 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002923 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2925 sbits |= val & (0x7f << 8);
2926 }
2927 return sbits;
2928}
2929
Takashi Iwai2f728532008-09-25 16:32:41 +02002930/* set digital convert verbs both for the given NID and its slaves */
2931static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2932 int verb, int val)
2933{
Takashi Iwaidda14412011-05-02 11:29:30 +02002934 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002935
Takashi Iwai9e976972008-11-25 08:17:20 +01002936 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002937 d = codec->slave_dig_outs;
2938 if (!d)
2939 return;
2940 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002941 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002942}
2943
2944static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2945 int dig1, int dig2)
2946{
2947 if (dig1 != -1)
2948 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2949 if (dig2 != -1)
2950 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2951}
2952
Takashi Iwai0ba21762007-04-16 11:29:14 +02002953static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2954 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002957 int idx = kcontrol->private_value;
2958 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2959 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 unsigned short val;
2961 int change;
2962
Ingo Molnar62932df2006-01-16 16:34:20 +01002963 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002964 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2966 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2967 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002968 val = convert_from_spdif_status(spdif->status);
2969 val |= spdif->ctls & 1;
2970 change = spdif->ctls != val;
2971 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002972 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002973 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002974 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return change;
2976}
2977
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002978#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Takashi Iwai0ba21762007-04-16 11:29:14 +02002980static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002984 int idx = kcontrol->private_value;
2985 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Stephen Warren7c935972011-06-01 11:14:17 -06002987 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return 0;
2989}
2990
Stephen Warren74b654c2011-06-01 11:14:18 -06002991static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2992 int dig1, int dig2)
2993{
2994 set_dig_out_convert(codec, nid, dig1, dig2);
2995 /* unmute amp switch (if any) */
2996 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2997 (dig1 & AC_DIG1_ENABLE))
2998 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2999 HDA_AMP_MUTE, 0);
3000}
3001
Takashi Iwai0ba21762007-04-16 11:29:14 +02003002static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003006 int idx = kcontrol->private_value;
3007 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3008 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 unsigned short val;
3010 int change;
3011
Ingo Molnar62932df2006-01-16 16:34:20 +01003012 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06003013 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003016 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003017 spdif->ctls = val;
3018 if (change && nid != (u16)-1)
3019 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003020 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return change;
3022}
3023
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003024static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 {
3026 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3027 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003028 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 .info = snd_hda_spdif_mask_info,
3030 .get = snd_hda_spdif_cmask_get,
3031 },
3032 {
3033 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3034 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003035 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .info = snd_hda_spdif_mask_info,
3037 .get = snd_hda_spdif_pmask_get,
3038 },
3039 {
3040 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003041 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .info = snd_hda_spdif_mask_info,
3043 .get = snd_hda_spdif_default_get,
3044 .put = snd_hda_spdif_default_put,
3045 },
3046 {
3047 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003048 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 .info = snd_hda_spdif_out_switch_info,
3050 .get = snd_hda_spdif_out_switch_get,
3051 .put = snd_hda_spdif_out_switch_put,
3052 },
3053 { } /* end */
3054};
3055
3056/**
3057 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3058 * @codec: the HDA codec
3059 * @nid: audio out widget NID
3060 *
3061 * Creates controls related with the SPDIF output.
3062 * Called from each patch supporting the SPDIF out.
3063 *
3064 * Returns 0 if successful, or a negative error code.
3065 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003066int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3067 hda_nid_t associated_nid,
3068 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003071 struct snd_kcontrol *kctl;
3072 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003073 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003074 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Takashi Iwai1afe2062010-12-23 10:17:52 +01003076 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3077 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003078 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3079 return -EBUSY;
3080 }
Stephen Warren7c935972011-06-01 11:14:17 -06003081 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3083 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003084 if (!kctl)
3085 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003086 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003087 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003088 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return err;
3091 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003092 spdif->nid = cvt_nid;
3093 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003094 AC_VERB_GET_DIGI_CONVERT_1, 0);
3095 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return 0;
3097}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003098EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Stephen Warren7c935972011-06-01 11:14:17 -06003100struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3101 hda_nid_t nid)
3102{
3103 int i;
3104 for (i = 0; i < codec->spdif_out.used; i++) {
3105 struct hda_spdif_out *spdif =
3106 snd_array_elem(&codec->spdif_out, i);
3107 if (spdif->nid == nid)
3108 return spdif;
3109 }
3110 return NULL;
3111}
3112EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3113
Stephen Warren74b654c2011-06-01 11:14:18 -06003114void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3115{
3116 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3117
3118 mutex_lock(&codec->spdif_mutex);
3119 spdif->nid = (u16)-1;
3120 mutex_unlock(&codec->spdif_mutex);
3121}
3122EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3123
3124void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3125{
3126 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3127 unsigned short val;
3128
3129 mutex_lock(&codec->spdif_mutex);
3130 if (spdif->nid != nid) {
3131 spdif->nid = nid;
3132 val = spdif->ctls;
3133 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3134 }
3135 mutex_unlock(&codec->spdif_mutex);
3136}
3137EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003140 * SPDIF sharing with analog output
3141 */
3142static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
3145 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3146 ucontrol->value.integer.value[0] = mout->share_spdif;
3147 return 0;
3148}
3149
3150static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
3152{
3153 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3154 mout->share_spdif = !!ucontrol->value.integer.value[0];
3155 return 0;
3156}
3157
3158static struct snd_kcontrol_new spdif_share_sw = {
3159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3160 .name = "IEC958 Default PCM Playback Switch",
3161 .info = snd_ctl_boolean_mono_info,
3162 .get = spdif_share_sw_get,
3163 .put = spdif_share_sw_put,
3164};
3165
Takashi Iwaid5191e52009-11-16 14:58:17 +01003166/**
3167 * snd_hda_create_spdif_share_sw - create Default PCM switch
3168 * @codec: the HDA codec
3169 * @mout: multi-out instance
3170 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003171int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3172 struct hda_multi_out *mout)
3173{
3174 if (!mout->dig_out_nid)
3175 return 0;
3176 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003177 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3178 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003180EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003181
3182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 * SPDIF input
3184 */
3185
3186#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3187
Takashi Iwai0ba21762007-04-16 11:29:14 +02003188static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192
3193 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3194 return 0;
3195}
3196
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3201 hda_nid_t nid = kcontrol->private_value;
3202 unsigned int val = !!ucontrol->value.integer.value[0];
3203 int change;
3204
Ingo Molnar62932df2006-01-16 16:34:20 +01003205 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003207 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003209 snd_hda_codec_write_cache(codec, nid, 0,
3210 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003212 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return change;
3214}
3215
Takashi Iwai0ba21762007-04-16 11:29:14 +02003216static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3220 hda_nid_t nid = kcontrol->private_value;
3221 unsigned short val;
3222 unsigned int sbits;
3223
Andrew Paprocki3982d172007-12-19 12:13:44 +01003224 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 sbits = convert_to_spdif_status(val);
3226 ucontrol->value.iec958.status[0] = sbits;
3227 ucontrol->value.iec958.status[1] = sbits >> 8;
3228 ucontrol->value.iec958.status[2] = sbits >> 16;
3229 ucontrol->value.iec958.status[3] = sbits >> 24;
3230 return 0;
3231}
3232
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003233static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 {
3235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003236 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 .info = snd_hda_spdif_in_switch_info,
3238 .get = snd_hda_spdif_in_switch_get,
3239 .put = snd_hda_spdif_in_switch_put,
3240 },
3241 {
3242 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003244 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .info = snd_hda_spdif_mask_info,
3246 .get = snd_hda_spdif_in_status_get,
3247 },
3248 { } /* end */
3249};
3250
3251/**
3252 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3253 * @codec: the HDA codec
3254 * @nid: audio in widget NID
3255 *
3256 * Creates controls related with the SPDIF input.
3257 * Called from each patch supporting the SPDIF in.
3258 *
3259 * Returns 0 if successful, or a negative error code.
3260 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003261int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003264 struct snd_kcontrol *kctl;
3265 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003266 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Takashi Iwai1afe2062010-12-23 10:17:52 +01003268 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3269 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003270 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3271 return -EBUSY;
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3274 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003275 if (!kctl)
3276 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003278 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003279 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return err;
3281 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003283 snd_hda_codec_read(codec, nid, 0,
3284 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003285 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return 0;
3287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003288EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Takashi Iwai2a439522011-07-26 09:52:50 +02003290#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003291/*
3292 * command cache
3293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003295/* build a 32bit cache key with the widget id and the command parameter */
3296#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3297#define get_cmd_cache_nid(key) ((key) & 0xff)
3298#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3299
3300/**
3301 * snd_hda_codec_write_cache - send a single command with caching
3302 * @codec: the HDA codec
3303 * @nid: NID to send the command
3304 * @direct: direct flag
3305 * @verb: the verb to send
3306 * @parm: the parameter for the verb
3307 *
3308 * Send a single command without waiting for response.
3309 *
3310 * Returns 0 if successful, or a negative error code.
3311 */
3312int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3313 int direct, unsigned int verb, unsigned int parm)
3314{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003315 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3316 struct hda_cache_head *c;
3317 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003318
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003319 if (err < 0)
3320 return err;
3321 /* parm may contain the verb stuff for get/set amp */
3322 verb = verb | (parm >> 8);
3323 parm &= 0xff;
3324 key = build_cmd_cache_key(nid, verb);
3325 mutex_lock(&codec->bus->cmd_mutex);
3326 c = get_alloc_hash(&codec->cmd_cache, key);
3327 if (c)
3328 c->val = parm;
3329 mutex_unlock(&codec->bus->cmd_mutex);
3330 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003331}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003332EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003333
Takashi Iwaid5191e52009-11-16 14:58:17 +01003334/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003335 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3336 * @codec: the HDA codec
3337 * @nid: NID to send the command
3338 * @direct: direct flag
3339 * @verb: the verb to send
3340 * @parm: the parameter for the verb
3341 *
3342 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3343 * command if the parameter is already identical with the cached value.
3344 * If not, it sends the command and refreshes the cache.
3345 *
3346 * Returns 0 if successful, or a negative error code.
3347 */
3348int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3349 int direct, unsigned int verb, unsigned int parm)
3350{
3351 struct hda_cache_head *c;
3352 u32 key;
3353
3354 /* parm may contain the verb stuff for get/set amp */
3355 verb = verb | (parm >> 8);
3356 parm &= 0xff;
3357 key = build_cmd_cache_key(nid, verb);
3358 mutex_lock(&codec->bus->cmd_mutex);
3359 c = get_hash(&codec->cmd_cache, key);
3360 if (c && c->val == parm) {
3361 mutex_unlock(&codec->bus->cmd_mutex);
3362 return 0;
3363 }
3364 mutex_unlock(&codec->bus->cmd_mutex);
3365 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3366}
3367EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3368
3369/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003370 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3371 * @codec: HD-audio codec
3372 *
3373 * Execute all verbs recorded in the command caches to resume.
3374 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003375void snd_hda_codec_resume_cache(struct hda_codec *codec)
3376{
Takashi Iwai603c4012008-07-30 15:01:44 +02003377 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003378 int i;
3379
Takashi Iwai603c4012008-07-30 15:01:44 +02003380 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003381 u32 key = buffer->key;
3382 if (!key)
3383 continue;
3384 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3385 get_cmd_cache_cmd(key), buffer->val);
3386 }
3387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003388EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003389
3390/**
3391 * snd_hda_sequence_write_cache - sequence writes with caching
3392 * @codec: the HDA codec
3393 * @seq: VERB array to send
3394 *
3395 * Send the commands sequentially from the given array.
3396 * Thte commands are recorded on cache for power-save and resume.
3397 * The array must be terminated with NID=0.
3398 */
3399void snd_hda_sequence_write_cache(struct hda_codec *codec,
3400 const struct hda_verb *seq)
3401{
3402 for (; seq->nid; seq++)
3403 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3404 seq->param);
3405}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003406EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003407#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003408
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003409void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3410 unsigned int power_state,
3411 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003412{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003413 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003414 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003415
Takashi Iwaicb53c622007-08-10 17:21:45 +02003416 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003417 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003418 if (!(wcaps & AC_WCAP_POWER))
3419 continue;
3420 /* don't power down the widget if it controls eapd and
3421 * EAPD_BTLENABLE is set.
3422 */
3423 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3424 get_wcaps_type(wcaps) == AC_WID_PIN &&
3425 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3426 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003427 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003428 if (eapd & 0x02)
3429 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003430 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003431 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3432 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003433 }
3434
Takashi Iwaicb53c622007-08-10 17:21:45 +02003435 if (power_state == AC_PWRST_D0) {
3436 unsigned long end_time;
3437 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003438 /* wait until the codec reachs to D0 */
3439 end_time = jiffies + msecs_to_jiffies(500);
3440 do {
3441 state = snd_hda_codec_read(codec, fg, 0,
3442 AC_VERB_GET_POWER_STATE, 0);
3443 if (state == power_state)
3444 break;
3445 msleep(1);
3446 } while (time_after_eq(end_time, jiffies));
3447 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003448}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003449EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3450
3451/*
3452 * set power state of the codec
3453 */
3454static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3455 unsigned int power_state)
3456{
3457 if (codec->patch_ops.set_power_state) {
3458 codec->patch_ops.set_power_state(codec, fg, power_state);
3459 return;
3460 }
3461
3462 /* this delay seems necessary to avoid click noise at power-down */
3463 if (power_state == AC_PWRST_D3)
3464 msleep(100);
3465 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3466 power_state);
3467 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3468}
Takashi Iwai54d17402005-11-21 16:33:22 +01003469
Takashi Iwai11aeff02008-07-30 15:01:46 +02003470#ifdef CONFIG_SND_HDA_HWDEP
3471/* execute additional init verbs */
3472static void hda_exec_init_verbs(struct hda_codec *codec)
3473{
3474 if (codec->init_verbs.list)
3475 snd_hda_sequence_write(codec, codec->init_verbs.list);
3476}
3477#else
3478static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3479#endif
3480
Takashi Iwai2a439522011-07-26 09:52:50 +02003481#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003482/*
3483 * call suspend and power-down; used both from PM and power-save
3484 */
3485static void hda_call_codec_suspend(struct hda_codec *codec)
3486{
3487 if (codec->patch_ops.suspend)
3488 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003489 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003490 hda_set_power_state(codec,
3491 codec->afg ? codec->afg : codec->mfg,
3492 AC_PWRST_D3);
3493#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003494 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003495 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003496 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003497 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003498 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003499#endif
3500}
3501
3502/*
3503 * kick up codec; used both from PM and power-save
3504 */
3505static void hda_call_codec_resume(struct hda_codec *codec)
3506{
3507 hda_set_power_state(codec,
3508 codec->afg ? codec->afg : codec->mfg,
3509 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003510 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003511 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003512 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003513 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003514 if (codec->patch_ops.resume)
3515 codec->patch_ops.resume(codec);
3516 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003517 if (codec->patch_ops.init)
3518 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003519 snd_hda_codec_resume_amp(codec);
3520 snd_hda_codec_resume_cache(codec);
3521 }
3522}
Takashi Iwai2a439522011-07-26 09:52:50 +02003523#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003524
Takashi Iwai54d17402005-11-21 16:33:22 +01003525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526/**
3527 * snd_hda_build_controls - build mixer controls
3528 * @bus: the BUS
3529 *
3530 * Creates mixer controls for each codec included in the bus.
3531 *
3532 * Returns 0 if successful, otherwise a negative error code.
3533 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003534int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003536 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Takashi Iwai0ba21762007-04-16 11:29:14 +02003538 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003539 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003540 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003541 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003542 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003543 err = snd_hda_codec_reset(codec);
3544 if (err < 0) {
3545 printk(KERN_ERR
3546 "hda_codec: cannot revert codec\n");
3547 return err;
3548 }
3549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003551 return 0;
3552}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003553EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003555int snd_hda_codec_build_controls(struct hda_codec *codec)
3556{
3557 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003558 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003559 /* continue to initialize... */
3560 if (codec->patch_ops.init)
3561 err = codec->patch_ops.init(codec);
3562 if (!err && codec->patch_ops.build_controls)
3563 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003564 if (err < 0)
3565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return 0;
3567}
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569/*
3570 * stream formats
3571 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003572struct hda_rate_tbl {
3573 unsigned int hz;
3574 unsigned int alsa_bits;
3575 unsigned int hda_fmt;
3576};
3577
Takashi Iwai92f10b32010-08-03 14:21:00 +02003578/* rate = base * mult / div */
3579#define HDA_RATE(base, mult, div) \
3580 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3581 (((div) - 1) << AC_FMT_DIV_SHIFT))
3582
Takashi Iwaibefdf312005-08-22 13:57:55 +02003583static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003585
3586 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003587 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3588 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3589 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3590 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3591 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3592 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3593 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3594 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3595 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3596 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3597 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003598#define AC_PAR_PCM_RATE_BITS 11
3599 /* up to bits 10, 384kHZ isn't supported properly */
3600
3601 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003602 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003603
Takashi Iwaibefdf312005-08-22 13:57:55 +02003604 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605};
3606
3607/**
3608 * snd_hda_calc_stream_format - calculate format bitset
3609 * @rate: the sample rate
3610 * @channels: the number of channels
3611 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3612 * @maxbps: the max. bps
3613 *
3614 * Calculate the format bitset from the given rate, channels and th PCM format.
3615 *
3616 * Return zero if invalid.
3617 */
3618unsigned int snd_hda_calc_stream_format(unsigned int rate,
3619 unsigned int channels,
3620 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003621 unsigned int maxbps,
3622 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 int i;
3625 unsigned int val = 0;
3626
Takashi Iwaibefdf312005-08-22 13:57:55 +02003627 for (i = 0; rate_bits[i].hz; i++)
3628 if (rate_bits[i].hz == rate) {
3629 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 break;
3631 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003632 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 snd_printdd("invalid rate %d\n", rate);
3634 return 0;
3635 }
3636
3637 if (channels == 0 || channels > 8) {
3638 snd_printdd("invalid channels %d\n", channels);
3639 return 0;
3640 }
3641 val |= channels - 1;
3642
3643 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003644 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003645 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003646 break;
3647 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003648 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 case 20:
3651 case 24:
3652 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003653 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003654 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003656 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003658 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 break;
3660 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003661 snd_printdd("invalid format width %d\n",
3662 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return 0;
3664 }
3665
Anssi Hannula32c168c2010-08-03 13:28:57 +03003666 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003667 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return val;
3670}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003671EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003673static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3674{
3675 unsigned int val = 0;
3676 if (nid != codec->afg &&
3677 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3678 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3679 if (!val || val == -1)
3680 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3681 if (!val || val == -1)
3682 return 0;
3683 return val;
3684}
3685
3686static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3687{
3688 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3689 get_pcm_param);
3690}
3691
3692static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3693{
3694 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3695 if (!streams || streams == -1)
3696 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3697 if (!streams || streams == -1)
3698 return 0;
3699 return streams;
3700}
3701
3702static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3703{
3704 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3705 get_stream_param);
3706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/**
3709 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3710 * @codec: the HDA codec
3711 * @nid: NID to query
3712 * @ratesp: the pointer to store the detected rate bitflags
3713 * @formatsp: the pointer to store the detected formats
3714 * @bpsp: the pointer to store the detected format widths
3715 *
3716 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3717 * or @bsps argument is ignored.
3718 *
3719 * Returns 0 if successful, otherwise a negative error code.
3720 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003721int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3723{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003724 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003726 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003727 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 if (ratesp) {
3730 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003731 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003733 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003735 if (rates == 0) {
3736 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3737 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3738 nid, val,
3739 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3740 return -EIO;
3741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 *ratesp = rates;
3743 }
3744
3745 if (formatsp || bpsp) {
3746 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003747 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003749 streams = query_stream_param(codec, nid);
3750 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 bps = 0;
3754 if (streams & AC_SUPFMT_PCM) {
3755 if (val & AC_SUPPCM_BITS_8) {
3756 formats |= SNDRV_PCM_FMTBIT_U8;
3757 bps = 8;
3758 }
3759 if (val & AC_SUPPCM_BITS_16) {
3760 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3761 bps = 16;
3762 }
3763 if (wcaps & AC_WCAP_DIGITAL) {
3764 if (val & AC_SUPPCM_BITS_32)
3765 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3766 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3767 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3768 if (val & AC_SUPPCM_BITS_24)
3769 bps = 24;
3770 else if (val & AC_SUPPCM_BITS_20)
3771 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003772 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3773 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3775 if (val & AC_SUPPCM_BITS_32)
3776 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 else if (val & AC_SUPPCM_BITS_24)
3778 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02003779 else if (val & AC_SUPPCM_BITS_20)
3780 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003783 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003785 if (!bps)
3786 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003787 }
3788 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003789 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* temporary hack: we have still no proper support
3791 * for the direct AC3 stream...
3792 */
3793 formats |= SNDRV_PCM_FMTBIT_U8;
3794 bps = 8;
3795 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003796 if (formats == 0) {
3797 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3798 "(nid=0x%x, val=0x%x, ovrd=%i, "
3799 "streams=0x%x)\n",
3800 nid, val,
3801 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3802 streams);
3803 return -EIO;
3804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 if (formatsp)
3806 *formatsp = formats;
3807 if (bpsp)
3808 *bpsp = bps;
3809 }
3810
3811 return 0;
3812}
Stephen Warren384a48d2011-06-01 11:14:21 -06003813EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003816 * snd_hda_is_supported_format - Check the validity of the format
3817 * @codec: HD-audio codec
3818 * @nid: NID to check
3819 * @format: the HD-audio format value to check
3820 *
3821 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 *
3823 * Returns 1 if supported, 0 if not.
3824 */
3825int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3826 unsigned int format)
3827{
3828 int i;
3829 unsigned int val = 0, rate, stream;
3830
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003831 val = query_pcm_param(codec, nid);
3832 if (!val)
3833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003836 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003837 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (val & (1 << i))
3839 break;
3840 return 0;
3841 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003842 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return 0;
3844
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003845 stream = query_stream_param(codec, nid);
3846 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return 0;
3848
3849 if (stream & AC_SUPFMT_PCM) {
3850 switch (format & 0xf0) {
3851 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003852 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return 0;
3854 break;
3855 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003856 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return 0;
3858 break;
3859 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return 0;
3862 break;
3863 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003864 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return 0;
3866 break;
3867 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003868 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return 0;
3870 break;
3871 default:
3872 return 0;
3873 }
3874 } else {
3875 /* FIXME: check for float32 and AC3? */
3876 }
3877
3878 return 1;
3879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003880EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882/*
3883 * PCM stuff
3884 */
3885static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3886 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003887 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
3889 return 0;
3890}
3891
3892static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3893 struct hda_codec *codec,
3894 unsigned int stream_tag,
3895 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003896 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3899 return 0;
3900}
3901
3902static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3903 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003904 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
Takashi Iwai888afa12008-03-18 09:57:50 +01003906 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return 0;
3908}
3909
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003910static int set_pcm_default_values(struct hda_codec *codec,
3911 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003913 int err;
3914
Takashi Iwai0ba21762007-04-16 11:29:14 +02003915 /* query support PCM information from the given NID */
3916 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003917 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003918 info->rates ? NULL : &info->rates,
3919 info->formats ? NULL : &info->formats,
3920 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003921 if (err < 0)
3922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
3924 if (info->ops.open == NULL)
3925 info->ops.open = hda_pcm_default_open_close;
3926 if (info->ops.close == NULL)
3927 info->ops.close = hda_pcm_default_open_close;
3928 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003929 if (snd_BUG_ON(!info->nid))
3930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 info->ops.prepare = hda_pcm_default_prepare;
3932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003934 if (snd_BUG_ON(!info->nid))
3935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 info->ops.cleanup = hda_pcm_default_cleanup;
3937 }
3938 return 0;
3939}
3940
Takashi Iwaieb541332010-08-06 13:48:11 +02003941/*
3942 * codec prepare/cleanup entries
3943 */
3944int snd_hda_codec_prepare(struct hda_codec *codec,
3945 struct hda_pcm_stream *hinfo,
3946 unsigned int stream,
3947 unsigned int format,
3948 struct snd_pcm_substream *substream)
3949{
3950 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003951 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003952 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3953 if (ret >= 0)
3954 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003955 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003956 return ret;
3957}
3958EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3959
3960void snd_hda_codec_cleanup(struct hda_codec *codec,
3961 struct hda_pcm_stream *hinfo,
3962 struct snd_pcm_substream *substream)
3963{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003964 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003965 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003966 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003967}
3968EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3969
Takashi Iwaid5191e52009-11-16 14:58:17 +01003970/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003971const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3972 "Audio", "SPDIF", "HDMI", "Modem"
3973};
3974
Takashi Iwai176d5332008-07-30 15:01:44 +02003975/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003976 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003977 *
3978 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003979 */
3980static int get_empty_pcm_device(struct hda_bus *bus, int type)
3981{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003982 /* audio device indices; not linear to keep compatibility */
3983 static int audio_idx[HDA_PCM_NTYPES][5] = {
3984 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3985 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003986 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003987 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003988 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003989 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003990
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003991 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003992 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3993 return -EINVAL;
3994 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003995
3996 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3997 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3998 return audio_idx[type][i];
3999
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004000 /* non-fixed slots starting from 10 */
4001 for (i = 10; i < 32; i++) {
4002 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4003 return i;
4004 }
4005
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004006 snd_printk(KERN_WARNING "Too many %s devices\n",
4007 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004008 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004009}
4010
4011/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004012 * attach a new PCM stream
4013 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004014static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004015{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004016 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004017 struct hda_pcm_stream *info;
4018 int stream, err;
4019
Takashi Iwaib91f0802008-11-04 08:43:08 +01004020 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004021 return -EINVAL;
4022 for (stream = 0; stream < 2; stream++) {
4023 info = &pcm->stream[stream];
4024 if (info->substreams) {
4025 err = set_pcm_default_values(codec, info);
4026 if (err < 0)
4027 return err;
4028 }
4029 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004030 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004031}
4032
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004033/* assign all PCMs of the given codec */
4034int snd_hda_codec_build_pcms(struct hda_codec *codec)
4035{
4036 unsigned int pcm;
4037 int err;
4038
4039 if (!codec->num_pcms) {
4040 if (!codec->patch_ops.build_pcms)
4041 return 0;
4042 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004043 if (err < 0) {
4044 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004045 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004046 err = snd_hda_codec_reset(codec);
4047 if (err < 0) {
4048 printk(KERN_ERR
4049 "hda_codec: cannot revert codec\n");
4050 return err;
4051 }
4052 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004053 }
4054 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4055 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4056 int dev;
4057
4058 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004059 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004060
4061 if (!cpcm->pcm) {
4062 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4063 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004064 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004065 cpcm->device = dev;
4066 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004067 if (err < 0) {
4068 printk(KERN_ERR "hda_codec: cannot attach "
4069 "PCM stream %d for codec #%d\n",
4070 dev, codec->addr);
4071 continue; /* no fatal error */
4072 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004073 }
4074 }
4075 return 0;
4076}
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078/**
4079 * snd_hda_build_pcms - build PCM information
4080 * @bus: the BUS
4081 *
4082 * Create PCM information for each codec included in the bus.
4083 *
4084 * The build_pcms codec patch is requested to set up codec->num_pcms and
4085 * codec->pcm_info properly. The array is referred by the top-level driver
4086 * to create its PCM instances.
4087 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4088 * callback.
4089 *
4090 * At least, substreams, channels_min and channels_max must be filled for
4091 * each stream. substreams = 0 indicates that the stream doesn't exist.
4092 * When rates and/or formats are zero, the supported values are queried
4093 * from the given nid. The nid is used also by the default ops.prepare
4094 * and ops.cleanup callbacks.
4095 *
4096 * The driver needs to call ops.open in its open callback. Similarly,
4097 * ops.close is supposed to be called in the close callback.
4098 * ops.prepare should be called in the prepare or hw_params callback
4099 * with the proper parameters for set up.
4100 * ops.cleanup should be called in hw_free for clean up of streams.
4101 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004102 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004104int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004106 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Takashi Iwai0ba21762007-04-16 11:29:14 +02004108 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004109 int err = snd_hda_codec_build_pcms(codec);
4110 if (err < 0)
4111 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
4113 return 0;
4114}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004115EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117/**
4118 * snd_hda_check_board_config - compare the current codec with the config table
4119 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004120 * @num_configs: number of config enums
4121 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * @tbl: configuration table, terminated by null entries
4123 *
4124 * Compares the modelname or PCI subsystem id of the current codec with the
4125 * given configuration table. If a matching entry is found, returns its
4126 * config value (supposed to be 0 or positive).
4127 *
4128 * If no entries are matching, the function returns a negative value.
4129 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004130int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004131 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004132 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004134 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004135 int i;
4136 for (i = 0; i < num_configs; i++) {
4137 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004138 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004139 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4140 "selected\n", models[i]);
4141 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 }
4143 }
4144 }
4145
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004146 if (!codec->bus->pci || !tbl)
4147 return -1;
4148
4149 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4150 if (!tbl)
4151 return -1;
4152 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004153#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004154 char tmp[10];
4155 const char *model = NULL;
4156 if (models)
4157 model = models[tbl->value];
4158 if (!model) {
4159 sprintf(tmp, "#%d", tbl->value);
4160 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004162 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4163 "for config %x:%x (%s)\n",
4164 model, tbl->subvendor, tbl->subdevice,
4165 (tbl->name ? tbl->name : "Unknown device"));
4166#endif
4167 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
4169 return -1;
4170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004171EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004174 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004175 subsystem ID with the
4176 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004177
4178 This is important for Gateway notebooks with SB450 HDA Audio
4179 where the vendor ID of the PCI device is:
4180 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4181 and the vendor/subvendor are found only at the codec.
4182
4183 * @codec: the HDA codec
4184 * @num_configs: number of config enums
4185 * @models: array of model name strings
4186 * @tbl: configuration table, terminated by null entries
4187 *
4188 * Compares the modelname or PCI subsystem id of the current codec with the
4189 * given configuration table. If a matching entry is found, returns its
4190 * config value (supposed to be 0 or positive).
4191 *
4192 * If no entries are matching, the function returns a negative value.
4193 */
4194int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004195 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004196 const struct snd_pci_quirk *tbl)
4197{
4198 const struct snd_pci_quirk *q;
4199
4200 /* Search for codec ID */
4201 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004202 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4203 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4204 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004205 break;
4206 }
4207
4208 if (!q->subvendor)
4209 return -1;
4210
4211 tbl = q;
4212
4213 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004214#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004215 char tmp[10];
4216 const char *model = NULL;
4217 if (models)
4218 model = models[tbl->value];
4219 if (!model) {
4220 sprintf(tmp, "#%d", tbl->value);
4221 model = tmp;
4222 }
4223 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4224 "for config %x:%x (%s)\n",
4225 model, tbl->subvendor, tbl->subdevice,
4226 (tbl->name ? tbl->name : "Unknown device"));
4227#endif
4228 return tbl->value;
4229 }
4230 return -1;
4231}
4232EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4233
4234/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 * snd_hda_add_new_ctls - create controls from the array
4236 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004237 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 *
4239 * This helper function creates and add new controls in the given array.
4240 * The array must be terminated with an empty entry as terminator.
4241 *
4242 * Returns 0 if successful, or a negative error code.
4243 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004244int snd_hda_add_new_ctls(struct hda_codec *codec,
4245 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004247 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004250 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004251 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004252 if (knew->iface == -1) /* skip this codec private value */
4253 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004254 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004255 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004256 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004257 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004258 if (addr > 0)
4259 kctl->id.device = addr;
4260 if (idx > 0)
4261 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004262 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004263 if (!err)
4264 break;
4265 /* try first with another device index corresponding to
4266 * the codec addr; if it still fails (or it's the
4267 * primary codec), then try another control index
4268 */
4269 if (!addr && codec->addr)
4270 addr = codec->addr;
4271 else if (!idx && !knew->index) {
4272 idx = find_empty_mixer_ctl_idx(codec,
4273 knew->name);
4274 if (idx <= 0)
4275 return err;
4276 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004277 return err;
4278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 }
4280 return 0;
4281}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004282EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Takashi Iwaicb53c622007-08-10 17:21:45 +02004284#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004285static void hda_power_work(struct work_struct *work)
4286{
4287 struct hda_codec *codec =
4288 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004289 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004290
Maxim Levitsky2e492462007-09-03 15:26:57 +02004291 if (!codec->power_on || codec->power_count) {
4292 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004293 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004294 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004295
Takashi Iwaid66fee52011-08-02 15:39:31 +02004296 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004297 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004298 if (bus->ops.pm_notify)
4299 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004300}
4301
4302static void hda_keep_power_on(struct hda_codec *codec)
4303{
4304 codec->power_count++;
4305 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004306 codec->power_jiffies = jiffies;
4307}
4308
Takashi Iwaid5191e52009-11-16 14:58:17 +01004309/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004310void snd_hda_update_power_acct(struct hda_codec *codec)
4311{
4312 unsigned long delta = jiffies - codec->power_jiffies;
4313 if (codec->power_on)
4314 codec->power_on_acct += delta;
4315 else
4316 codec->power_off_acct += delta;
4317 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004318}
4319
Takashi Iwaid5191e52009-11-16 14:58:17 +01004320/**
4321 * snd_hda_power_up - Power-up the codec
4322 * @codec: HD-audio codec
4323 *
4324 * Increment the power-up counter and power up the hardware really when
4325 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004326 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004327void snd_hda_power_up(struct hda_codec *codec)
4328{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004329 struct hda_bus *bus = codec->bus;
4330
Takashi Iwaicb53c622007-08-10 17:21:45 +02004331 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004332 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004333 return;
4334
Takashi Iwaid66fee52011-08-02 15:39:31 +02004335 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004336 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004337 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004338 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004339 if (bus->ops.pm_notify)
4340 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004341 hda_call_codec_resume(codec);
4342 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004343 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004345EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004346
4347#define power_save(codec) \
4348 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004349
Takashi Iwaid5191e52009-11-16 14:58:17 +01004350/**
4351 * snd_hda_power_down - Power-down the codec
4352 * @codec: HD-audio codec
4353 *
4354 * Decrement the power-up counter and schedules the power-off work if
4355 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004356 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004357void snd_hda_power_down(struct hda_codec *codec)
4358{
4359 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004360 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004361 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004362 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004363 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004364 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004365 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004366 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004367}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004368EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004369
Takashi Iwaid5191e52009-11-16 14:58:17 +01004370/**
4371 * snd_hda_check_amp_list_power - Check the amp list and update the power
4372 * @codec: HD-audio codec
4373 * @check: the object containing an AMP list and the status
4374 * @nid: NID to check / update
4375 *
4376 * Check whether the given NID is in the amp list. If it's in the list,
4377 * check the current AMP status, and update the the power-status according
4378 * to the mute status.
4379 *
4380 * This function is supposed to be set or called from the check_power_status
4381 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004382 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004383int snd_hda_check_amp_list_power(struct hda_codec *codec,
4384 struct hda_loopback_check *check,
4385 hda_nid_t nid)
4386{
Takashi Iwai031024e2011-05-02 11:29:30 +02004387 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004388 int ch, v;
4389
4390 if (!check->amplist)
4391 return 0;
4392 for (p = check->amplist; p->nid; p++) {
4393 if (p->nid == nid)
4394 break;
4395 }
4396 if (!p->nid)
4397 return 0; /* nothing changed */
4398
4399 for (p = check->amplist; p->nid; p++) {
4400 for (ch = 0; ch < 2; ch++) {
4401 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4402 p->idx);
4403 if (!(v & HDA_AMP_MUTE) && v > 0) {
4404 if (!check->power_on) {
4405 check->power_on = 1;
4406 snd_hda_power_up(codec);
4407 }
4408 return 1;
4409 }
4410 }
4411 }
4412 if (check->power_on) {
4413 check->power_on = 0;
4414 snd_hda_power_down(codec);
4415 }
4416 return 0;
4417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004418EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004421/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004422 * Channel mode helper
4423 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004424
4425/**
4426 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4427 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004428int snd_hda_ch_mode_info(struct hda_codec *codec,
4429 struct snd_ctl_elem_info *uinfo,
4430 const struct hda_channel_mode *chmode,
4431 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004432{
4433 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4434 uinfo->count = 1;
4435 uinfo->value.enumerated.items = num_chmodes;
4436 if (uinfo->value.enumerated.item >= num_chmodes)
4437 uinfo->value.enumerated.item = num_chmodes - 1;
4438 sprintf(uinfo->value.enumerated.name, "%dch",
4439 chmode[uinfo->value.enumerated.item].channels);
4440 return 0;
4441}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004442EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004443
Takashi Iwaid5191e52009-11-16 14:58:17 +01004444/**
4445 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4446 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004447int snd_hda_ch_mode_get(struct hda_codec *codec,
4448 struct snd_ctl_elem_value *ucontrol,
4449 const struct hda_channel_mode *chmode,
4450 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004451 int max_channels)
4452{
4453 int i;
4454
4455 for (i = 0; i < num_chmodes; i++) {
4456 if (max_channels == chmode[i].channels) {
4457 ucontrol->value.enumerated.item[0] = i;
4458 break;
4459 }
4460 }
4461 return 0;
4462}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004463EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004464
Takashi Iwaid5191e52009-11-16 14:58:17 +01004465/**
4466 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4467 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004468int snd_hda_ch_mode_put(struct hda_codec *codec,
4469 struct snd_ctl_elem_value *ucontrol,
4470 const struct hda_channel_mode *chmode,
4471 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004472 int *max_channelsp)
4473{
4474 unsigned int mode;
4475
4476 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004477 if (mode >= num_chmodes)
4478 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004479 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004480 return 0;
4481 /* change the current channel setting */
4482 *max_channelsp = chmode[mode].channels;
4483 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004484 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004485 return 1;
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/*
4490 * input MUX helper
4491 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004492
4493/**
4494 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4495 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004496int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4497 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
4499 unsigned int index;
4500
4501 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4502 uinfo->count = 1;
4503 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004504 if (!imux->num_items)
4505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 index = uinfo->value.enumerated.item;
4507 if (index >= imux->num_items)
4508 index = imux->num_items - 1;
4509 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4510 return 0;
4511}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004512EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Takashi Iwaid5191e52009-11-16 14:58:17 +01004514/**
4515 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4516 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004517int snd_hda_input_mux_put(struct hda_codec *codec,
4518 const struct hda_input_mux *imux,
4519 struct snd_ctl_elem_value *ucontrol,
4520 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 unsigned int *cur_val)
4522{
4523 unsigned int idx;
4524
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004525 if (!imux->num_items)
4526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 idx = ucontrol->value.enumerated.item[0];
4528 if (idx >= imux->num_items)
4529 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004530 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004532 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4533 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 *cur_val = idx;
4535 return 1;
4536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004537EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539
4540/*
4541 * Multi-channel / digital-out PCM helper functions
4542 */
4543
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004544/* setup SPDIF output stream */
4545static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4546 unsigned int stream_tag, unsigned int format)
4547{
Stephen Warren7c935972011-06-01 11:14:17 -06004548 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4549
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004550 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004551 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004552 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004553 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004554 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004555 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004556 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004557 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004558 for (d = codec->slave_dig_outs; *d; d++)
4559 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4560 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004561 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004562 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004563 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004564 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004565 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004566}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004567
Takashi Iwai2f728532008-09-25 16:32:41 +02004568static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4569{
4570 snd_hda_codec_cleanup_stream(codec, nid);
4571 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004572 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004573 for (d = codec->slave_dig_outs; *d; d++)
4574 snd_hda_codec_cleanup_stream(codec, *d);
4575 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004576}
4577
Takashi Iwaid5191e52009-11-16 14:58:17 +01004578/**
4579 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4580 * @bus: HD-audio bus
4581 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004582void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4583{
4584 struct hda_codec *codec;
4585
4586 if (!bus)
4587 return;
4588 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004589 if (hda_codec_is_power_on(codec) &&
4590 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004591 codec->patch_ops.reboot_notify(codec);
4592 }
4593}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004594EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004595
Takashi Iwaid5191e52009-11-16 14:58:17 +01004596/**
4597 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004599int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4600 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601{
Ingo Molnar62932df2006-01-16 16:34:20 +01004602 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004603 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4604 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004605 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004607 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return 0;
4609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004610EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Takashi Iwaid5191e52009-11-16 14:58:17 +01004612/**
4613 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4614 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004615int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4616 struct hda_multi_out *mout,
4617 unsigned int stream_tag,
4618 unsigned int format,
4619 struct snd_pcm_substream *substream)
4620{
4621 mutex_lock(&codec->spdif_mutex);
4622 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4623 mutex_unlock(&codec->spdif_mutex);
4624 return 0;
4625}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004626EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004627
Takashi Iwaid5191e52009-11-16 14:58:17 +01004628/**
4629 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4630 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004631int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4632 struct hda_multi_out *mout)
4633{
4634 mutex_lock(&codec->spdif_mutex);
4635 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4636 mutex_unlock(&codec->spdif_mutex);
4637 return 0;
4638}
4639EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4640
Takashi Iwaid5191e52009-11-16 14:58:17 +01004641/**
4642 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004644int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4645 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Ingo Molnar62932df2006-01-16 16:34:20 +01004647 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004649 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 return 0;
4651}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004652EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Takashi Iwaid5191e52009-11-16 14:58:17 +01004654/**
4655 * snd_hda_multi_out_analog_open - open analog outputs
4656 *
4657 * Open analog outputs and set up the hw-constraints.
4658 * If the digital outputs can be opened as slave, open the digital
4659 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004661int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4662 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004663 struct snd_pcm_substream *substream,
4664 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Takashi Iwai9a081602008-02-12 18:37:26 +01004666 struct snd_pcm_runtime *runtime = substream->runtime;
4667 runtime->hw.channels_max = mout->max_channels;
4668 if (mout->dig_out_nid) {
4669 if (!mout->analog_rates) {
4670 mout->analog_rates = hinfo->rates;
4671 mout->analog_formats = hinfo->formats;
4672 mout->analog_maxbps = hinfo->maxbps;
4673 } else {
4674 runtime->hw.rates = mout->analog_rates;
4675 runtime->hw.formats = mout->analog_formats;
4676 hinfo->maxbps = mout->analog_maxbps;
4677 }
4678 if (!mout->spdif_rates) {
4679 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4680 &mout->spdif_rates,
4681 &mout->spdif_formats,
4682 &mout->spdif_maxbps);
4683 }
4684 mutex_lock(&codec->spdif_mutex);
4685 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004686 if ((runtime->hw.rates & mout->spdif_rates) &&
4687 (runtime->hw.formats & mout->spdif_formats)) {
4688 runtime->hw.rates &= mout->spdif_rates;
4689 runtime->hw.formats &= mout->spdif_formats;
4690 if (mout->spdif_maxbps < hinfo->maxbps)
4691 hinfo->maxbps = mout->spdif_maxbps;
4692 } else {
4693 mout->share_spdif = 0;
4694 /* FIXME: need notify? */
4695 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004696 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004697 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4700 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004702EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Takashi Iwaid5191e52009-11-16 14:58:17 +01004704/**
4705 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4706 *
4707 * Set up the i/o for analog out.
4708 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004710int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4711 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 unsigned int stream_tag,
4713 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004714 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Takashi Iwaidda14412011-05-02 11:29:30 +02004716 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004718 struct hda_spdif_out *spdif =
4719 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 int i;
4721
Ingo Molnar62932df2006-01-16 16:34:20 +01004722 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004723 if (mout->dig_out_nid && mout->share_spdif &&
4724 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004726 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4727 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004728 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004730 setup_dig_out_stream(codec, mout->dig_out_nid,
4731 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 } else {
4733 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004734 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004737 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004740 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4741 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004742 if (!mout->no_share_stream &&
4743 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004745 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4746 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004747 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004748 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4749 if (!mout->no_share_stream && mout->hp_out_nid[i])
4750 snd_hda_codec_setup_stream(codec,
4751 mout->hp_out_nid[i],
4752 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004753 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004754 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004755 snd_hda_codec_setup_stream(codec,
4756 mout->extra_out_nid[i],
4757 stream_tag, 0, format);
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* surrounds */
4760 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004761 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004762 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4763 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004764 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004765 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4766 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 }
4768 return 0;
4769}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004770EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Takashi Iwaid5191e52009-11-16 14:58:17 +01004772/**
4773 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004775int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4776 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Takashi Iwaidda14412011-05-02 11:29:30 +02004778 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 int i;
4780
4781 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004782 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004784 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004785 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4786 if (mout->hp_out_nid[i])
4787 snd_hda_codec_cleanup_stream(codec,
4788 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004789 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4790 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004791 snd_hda_codec_cleanup_stream(codec,
4792 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004793 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004795 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 mout->dig_out_used = 0;
4797 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004798 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return 0;
4800}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004801EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004803/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004804 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004805 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004806
Takashi Iwaidda14412011-05-02 11:29:30 +02004807static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004808{
4809 for (; *list; list++)
4810 if (*list == nid)
4811 return 1;
4812 return 0;
4813}
4814
Steve Longerbeam81937d32007-05-08 15:33:03 +02004815
4816/*
4817 * Sort an associated group of pins according to their sequence numbers.
4818 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004819static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004820 int num_pins)
4821{
4822 int i, j;
4823 short seq;
4824 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004825
Steve Longerbeam81937d32007-05-08 15:33:03 +02004826 for (i = 0; i < num_pins; i++) {
4827 for (j = i + 1; j < num_pins; j++) {
4828 if (sequences[i] > sequences[j]) {
4829 seq = sequences[i];
4830 sequences[i] = sequences[j];
4831 sequences[j] = seq;
4832 nid = pins[i];
4833 pins[i] = pins[j];
4834 pins[j] = nid;
4835 }
4836 }
4837 }
4838}
4839
4840
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004841/* add the found input-pin to the cfg->inputs[] table */
4842static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4843 int type)
4844{
4845 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4846 cfg->inputs[cfg->num_inputs].pin = nid;
4847 cfg->inputs[cfg->num_inputs].type = type;
4848 cfg->num_inputs++;
4849 }
4850}
4851
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004852/* sort inputs in the order of AUTO_PIN_* type */
4853static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4854{
4855 int i, j;
4856
4857 for (i = 0; i < cfg->num_inputs; i++) {
4858 for (j = i + 1; j < cfg->num_inputs; j++) {
4859 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4860 struct auto_pin_cfg_item tmp;
4861 tmp = cfg->inputs[i];
4862 cfg->inputs[i] = cfg->inputs[j];
4863 cfg->inputs[j] = tmp;
4864 }
4865 }
4866 }
4867}
4868
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004869/* Reorder the surround channels
4870 * ALSA sequence is front/surr/clfe/side
4871 * HDA sequence is:
4872 * 4-ch: front/surr => OK as it is
4873 * 6-ch: front/clfe/surr
4874 * 8-ch: front/clfe/rear/side|fc
4875 */
4876static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4877{
4878 hda_nid_t nid;
4879
4880 switch (nums) {
4881 case 3:
4882 case 4:
4883 nid = pins[1];
4884 pins[1] = pins[2];
4885 pins[2] = nid;
4886 break;
4887 }
4888}
4889
Takashi Iwai82bc9552006-03-21 11:24:42 +01004890/*
4891 * Parse all pin widgets and store the useful pin nids to cfg
4892 *
4893 * The number of line-outs or any primary output is stored in line_outs,
4894 * and the corresponding output pins are assigned to line_out_pins[],
4895 * in the order of front, rear, CLFE, side, ...
4896 *
4897 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004898 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004899 * is detected, one of speaker of HP pins is assigned as the primary
4900 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4901 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004902 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004903 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004904 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4905 * respectively.
4906 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004907int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4908 struct auto_pin_cfg *cfg,
4909 const hda_nid_t *ignore_nids,
4910 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004911{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004912 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004913 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004914 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4915 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004916 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004917 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004918
4919 memset(cfg, 0, sizeof(*cfg));
4920
Steve Longerbeam81937d32007-05-08 15:33:03 +02004921 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4922 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004923 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004924 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004925
Takashi Iwai2f451d22011-11-10 12:36:46 +01004926 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004927 end_nid = codec->start_nid + codec->num_nodes;
4928 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004929 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004930 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004931 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004932 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004933
4934 /* read all default configuration for pin complex */
4935 if (wid_type != AC_WID_PIN)
4936 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004937 /* ignore the given nids (e.g. pc-beep returns error) */
4938 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4939 continue;
4940
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004941 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01004942 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4943 AC_DEFCFG_MISC_NO_PRESENCE))
4944 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004945 conn = get_defcfg_connect(def_conf);
4946 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004947 continue;
4948 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004949 dev = get_defcfg_device(def_conf);
4950
4951 /* workaround for buggy BIOS setups */
4952 if (dev == AC_JACK_LINE_OUT) {
4953 if (conn == AC_JACK_PORT_FIXED)
4954 dev = AC_JACK_SPEAKER;
4955 }
4956
4957 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004958 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004959 seq = get_defcfg_sequence(def_conf);
4960 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004961
4962 if (!(wid_caps & AC_WCAP_STEREO))
4963 if (!cfg->mono_out_pin)
4964 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004965 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004966 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004967 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004968 assoc_line_out = assoc;
4969 else if (assoc_line_out != assoc)
4970 continue;
4971 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4972 continue;
4973 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004974 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004975 cfg->line_outs++;
4976 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004977 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004978 seq = get_defcfg_sequence(def_conf);
4979 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004980 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4981 continue;
4982 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004983 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004984 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004985 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004986 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004987 seq = get_defcfg_sequence(def_conf);
4988 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004989 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4990 continue;
4991 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004992 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004993 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004994 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004995 case AC_JACK_MIC_IN:
4996 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004997 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004998 case AC_JACK_LINE_IN:
4999 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005000 break;
5001 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005002 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005003 break;
5004 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005005 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005006 break;
5007 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005008 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005009 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5010 continue;
5011 cfg->dig_out_pins[cfg->dig_outs] = nid;
5012 cfg->dig_out_type[cfg->dig_outs] =
5013 (loc == AC_JACK_LOC_HDMI) ?
5014 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5015 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005016 break;
5017 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005018 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005019 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01005020 if (loc == AC_JACK_LOC_HDMI)
5021 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5022 else
5023 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005024 break;
5025 }
5026 }
5027
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005028 /* FIX-UP:
5029 * If no line-out is defined but multiple HPs are found,
5030 * some of them might be the real line-outs.
5031 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005032 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5033 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005034 int i = 0;
5035 while (i < cfg->hp_outs) {
5036 /* The real HPs should have the sequence 0x0f */
5037 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5038 i++;
5039 continue;
5040 }
5041 /* Move it to the line-out table */
5042 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5043 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5044 cfg->line_outs++;
5045 cfg->hp_outs--;
5046 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5047 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02005048 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005049 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5050 }
Takashi Iwai03642c92010-09-08 15:28:19 +02005051 memset(cfg->hp_pins + cfg->hp_outs, 0,
5052 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01005053 if (!cfg->hp_outs)
5054 cfg->line_out_type = AUTO_PIN_HP_OUT;
5055
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005056 }
5057
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005058 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02005059 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5060 cfg->line_outs);
5061 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5062 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01005063 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5064 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005065
Steve Longerbeam81937d32007-05-08 15:33:03 +02005066 /*
5067 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5068 * as a primary output
5069 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005070 if (!cfg->line_outs &&
5071 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02005072 if (cfg->speaker_outs) {
5073 cfg->line_outs = cfg->speaker_outs;
5074 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5075 sizeof(cfg->speaker_pins));
5076 cfg->speaker_outs = 0;
5077 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5078 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5079 } else if (cfg->hp_outs) {
5080 cfg->line_outs = cfg->hp_outs;
5081 memcpy(cfg->line_out_pins, cfg->hp_pins,
5082 sizeof(cfg->hp_pins));
5083 cfg->hp_outs = 0;
5084 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5085 cfg->line_out_type = AUTO_PIN_HP_OUT;
5086 }
5087 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005088
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02005089 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5090 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5091 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005092
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02005093 sort_autocfg_input_pins(cfg);
5094
Takashi Iwai82bc9552006-03-21 11:24:42 +01005095 /*
5096 * debug prints of the parsed results
5097 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02005098 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 +01005099 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5100 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02005101 cfg->line_out_pins[4],
5102 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5103 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5104 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01005105 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5106 cfg->speaker_outs, cfg->speaker_pins[0],
5107 cfg->speaker_pins[1], cfg->speaker_pins[2],
5108 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005109 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5110 cfg->hp_outs, cfg->hp_pins[0],
5111 cfg->hp_pins[1], cfg->hp_pins[2],
5112 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005113 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005114 if (cfg->dig_outs)
5115 snd_printd(" dig-out=0x%x/0x%x\n",
5116 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005117 snd_printd(" inputs:");
5118 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02005119 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02005120 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005121 cfg->inputs[i].pin);
5122 }
5123 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01005124 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01005125 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005126
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005127 return 0;
5128}
Takashi Iwaid025feb2011-08-23 15:24:39 +02005129EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005130
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005131int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005132{
5133 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005134 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005135 if (conn == AC_JACK_PORT_NONE)
5136 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005137 /* Windows may claim the internal mic to be BOTH, too */
5138 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005139 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005140 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005141 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005142 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005143 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005144 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005145 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005146 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005147 return INPUT_PIN_ATTR_FRONT;
5148 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005149}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005150EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005151
Takashi Iwai990061c2010-09-09 22:08:44 +02005152/**
5153 * hda_get_input_pin_label - Give a label for the given input pin
5154 *
5155 * When check_location is true, the function checks the pin location
5156 * for mic and line-in pins, and set an appropriate prefix like "Front",
5157 * "Rear", "Internal".
5158 */
5159
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005160static const char *hda_get_input_pin_label(struct hda_codec *codec,
5161 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02005162{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005163 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005164 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005165 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5166 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005167 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005168
Takashi Iwai10a20af2010-09-09 16:28:02 +02005169 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005170
5171 switch (get_defcfg_device(def_conf)) {
5172 case AC_JACK_MIC_IN:
5173 if (!check_location)
5174 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005175 attr = snd_hda_get_input_pin_attr(def_conf);
5176 if (!attr)
5177 return "None";
5178 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005179 case AC_JACK_LINE_IN:
5180 if (!check_location)
5181 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005182 attr = snd_hda_get_input_pin_attr(def_conf);
5183 if (!attr)
5184 return "None";
5185 if (attr == INPUT_PIN_ATTR_DOCK)
5186 return "Dock Line";
5187 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005188 case AC_JACK_AUX:
5189 return "Aux";
5190 case AC_JACK_CD:
5191 return "CD";
5192 case AC_JACK_SPDIF_IN:
5193 return "SPDIF In";
5194 case AC_JACK_DIG_OTHER_IN:
5195 return "Digital In";
5196 default:
5197 return "Misc";
5198 }
5199}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005200
Takashi Iwaia1c98512010-09-09 21:36:27 +02005201/* Check whether the location prefix needs to be added to the label.
5202 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5203 * have to put "Front" prefix to each label. In such a case, returns false.
5204 */
5205static int check_mic_location_need(struct hda_codec *codec,
5206 const struct auto_pin_cfg *cfg,
5207 int input)
5208{
5209 unsigned int defc;
5210 int i, attr, attr2;
5211
5212 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005213 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005214 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005215 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005216 return 1;
5217
5218 attr = 0;
5219 for (i = 0; i < cfg->num_inputs; i++) {
5220 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005221 attr2 = snd_hda_get_input_pin_attr(defc);
5222 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005223 if (attr && attr != attr2)
5224 return 1; /* different locations found */
5225 attr = attr2;
5226 }
5227 }
5228 return 0;
5229}
5230
Takashi Iwai990061c2010-09-09 22:08:44 +02005231/**
5232 * hda_get_autocfg_input_label - Get a label for the given input
5233 *
5234 * Get a label for the given input pin defined by the autocfg item.
5235 * Unlike hda_get_input_pin_label(), this function checks all inputs
5236 * defined in autocfg and avoids the redundant mic/line prefix as much as
5237 * possible.
5238 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005239const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5240 const struct auto_pin_cfg *cfg,
5241 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005242{
5243 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005244 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005245
Takashi Iwai10a20af2010-09-09 16:28:02 +02005246 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5247 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5248 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005249 if (has_multiple_pins && type == AUTO_PIN_MIC)
5250 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005251 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5252 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005253}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005254EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5255
Takashi Iwai358b6e62011-11-21 14:34:20 +01005256/* return the position of NID in the list, or -1 if not found */
5257static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5258{
5259 int i;
5260 for (i = 0; i < nums; i++)
5261 if (list[i] == nid)
5262 return i;
5263 return -1;
5264}
5265
Takashi Iwai201e06f2011-11-16 15:33:26 +01005266/* get a unique suffix or an index number */
5267static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5268 int num_pins, int *indexp)
5269{
5270 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005271 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005272 };
5273 int i;
5274
Takashi Iwai358b6e62011-11-21 14:34:20 +01005275 i = find_idx_in_nid_list(nid, pins, num_pins);
5276 if (i < 0)
5277 return NULL;
5278 if (num_pins == 1)
5279 return "";
5280 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5281 if (indexp)
5282 *indexp = i;
5283 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005284 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005285 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005286}
5287
5288static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5289 const struct auto_pin_cfg *cfg,
5290 const char *name, char *label, int maxlen,
5291 int *indexp)
5292{
5293 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5294 int attr = snd_hda_get_input_pin_attr(def_conf);
5295 const char *pfx = "", *sfx = "";
5296
5297 /* handle as a speaker if it's a fixed line-out */
Takashi Iwaie49a3432012-03-01 18:14:41 +01005298 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005299 name = "Speaker";
5300 /* check the location */
5301 switch (attr) {
5302 case INPUT_PIN_ATTR_DOCK:
5303 pfx = "Dock ";
5304 break;
5305 case INPUT_PIN_ATTR_FRONT:
5306 pfx = "Front ";
5307 break;
5308 }
5309 if (cfg) {
5310 /* try to give a unique suffix if needed */
5311 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5312 indexp);
5313 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005314 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5315 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005316 if (!sfx) {
5317 /* don't add channel suffix for Headphone controls */
5318 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5319 cfg->hp_outs);
5320 if (idx >= 0)
5321 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005322 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005323 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005324 }
5325 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5326 return 1;
5327}
5328
Takashi Iwai990061c2010-09-09 22:08:44 +02005329/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005330 * snd_hda_get_pin_label - Get a label for the given I/O pin
5331 *
5332 * Get a label for the given pin. This function works for both input and
5333 * output pins. When @cfg is given as non-NULL, the function tries to get
5334 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005335 *
5336 * This function tries to give a unique label string for the pin as much as
5337 * possible. For example, when the multiple line-outs are present, it adds
5338 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5339 * If no unique name with a suffix is available and @indexp is non-NULL, the
5340 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005341 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005342int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5343 const struct auto_pin_cfg *cfg,
5344 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005345{
5346 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005347 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005348 int i;
5349
Takashi Iwai201e06f2011-11-16 15:33:26 +01005350 if (indexp)
5351 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005352 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005353 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005354
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005355 switch (get_defcfg_device(def_conf)) {
5356 case AC_JACK_LINE_OUT:
Takashi Iwaie49a3432012-03-01 18:14:41 +01005357 return fill_audio_out_name(codec, nid, cfg, "Line Out",
Takashi Iwai201e06f2011-11-16 15:33:26 +01005358 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005359 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005360 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5361 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005362 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005363 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5364 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005365 case AC_JACK_SPDIF_OUT:
5366 case AC_JACK_DIG_OTHER_OUT:
5367 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005368 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005369 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005370 name = "SPDIF";
5371 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005372 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5373 cfg->dig_outs);
5374 if (i >= 0)
5375 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005376 }
5377 break;
5378 default:
5379 if (cfg) {
5380 for (i = 0; i < cfg->num_inputs; i++) {
5381 if (cfg->inputs[i].pin != nid)
5382 continue;
5383 name = hda_get_autocfg_input_label(codec, cfg, i);
5384 if (name)
5385 break;
5386 }
5387 }
5388 if (!name)
5389 name = hda_get_input_pin_label(codec, nid, true);
5390 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005391 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005392 if (!name)
5393 return 0;
5394 strlcpy(label, name, maxlen);
5395 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005396}
5397EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5398
Takashi Iwai990061c2010-09-09 22:08:44 +02005399/**
5400 * snd_hda_add_imux_item - Add an item to input_mux
5401 *
5402 * When the same label is used already in the existing items, the number
5403 * suffix is appended to the label. This label index number is stored
5404 * to type_idx when non-NULL pointer is given.
5405 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005406int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5407 int index, int *type_idx)
5408{
5409 int i, label_idx = 0;
5410 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5411 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5412 return -EINVAL;
5413 }
5414 for (i = 0; i < imux->num_items; i++) {
5415 if (!strncmp(label, imux->items[i].label, strlen(label)))
5416 label_idx++;
5417 }
5418 if (type_idx)
5419 *type_idx = label_idx;
5420 if (label_idx > 0)
5421 snprintf(imux->items[imux->num_items].label,
5422 sizeof(imux->items[imux->num_items].label),
5423 "%s %d", label, label_idx);
5424 else
5425 strlcpy(imux->items[imux->num_items].label, label,
5426 sizeof(imux->items[imux->num_items].label));
5427 imux->items[imux->num_items].index = index;
5428 imux->num_items++;
5429 return 0;
5430}
5431EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005432
Takashi Iwai4a471b72005-12-07 13:56:29 +01005433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434#ifdef CONFIG_PM
5435/*
5436 * power management
5437 */
5438
5439/**
5440 * snd_hda_suspend - suspend the codecs
5441 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 *
5443 * Returns 0 if successful.
5444 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005445int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005447 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Takashi Iwai0ba21762007-04-16 11:29:14 +02005449 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005450 if (hda_codec_is_power_on(codec))
5451 hda_call_codec_suspend(codec);
5452 if (codec->patch_ops.post_suspend)
5453 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
5455 return 0;
5456}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005457EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459/**
5460 * snd_hda_resume - resume the codecs
5461 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 *
5463 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005464 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005465 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005466 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 */
5468int snd_hda_resume(struct hda_bus *bus)
5469{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005470 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Takashi Iwai0ba21762007-04-16 11:29:14 +02005472 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005473 if (codec->patch_ops.pre_resume)
5474 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005475 if (snd_hda_codec_needs_resume(codec))
5476 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005480EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005481#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005482
5483/*
5484 * generic arrays
5485 */
5486
Takashi Iwaid5191e52009-11-16 14:58:17 +01005487/**
5488 * snd_array_new - get a new element from the given array
5489 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005490 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005491 * Get a new element from the given array. If it exceeds the
5492 * pre-allocated array size, re-allocate the array.
5493 *
5494 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005495 */
5496void *snd_array_new(struct snd_array *array)
5497{
5498 if (array->used >= array->alloced) {
5499 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005500 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005501 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005502 void *nlist;
5503 if (snd_BUG_ON(num >= 4096))
5504 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005505 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005506 if (!nlist)
5507 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005508 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005509 array->list = nlist;
5510 array->alloced = num;
5511 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005512 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005513}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005514EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005515
Takashi Iwaid5191e52009-11-16 14:58:17 +01005516/**
5517 * snd_array_free - free the given array elements
5518 * @array: the array object
5519 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005520void snd_array_free(struct snd_array *array)
5521{
5522 kfree(array->list);
5523 array->used = 0;
5524 array->alloced = 0;
5525 array->list = NULL;
5526}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005527EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005528
Takashi Iwaid5191e52009-11-16 14:58:17 +01005529/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005530 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5531 * @pcm: PCM caps bits
5532 * @buf: the string buffer to write
5533 * @buflen: the max buffer length
5534 *
5535 * used by hda_proc.c and hda_eld.c
5536 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005537void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5538{
5539 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5540 int i, j;
5541
5542 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5543 if (pcm & (AC_SUPPCM_BITS_8 << i))
5544 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5545
5546 buf[j] = '\0'; /* necessary when j == 0 */
5547}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005548EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005549
5550MODULE_DESCRIPTION("HDA codec core");
5551MODULE_LICENSE("GPL");