blob: 8bd34320ef91f0a6d883a2fc1468db63bec164c3 [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",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
169 };
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
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200620 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 unsol = bus->unsol;
622 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->wp = wp;
627
628 wp <<= 1;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
631
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100636EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800639 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Howellsc4028952006-11-22 14:57:56 +0000641static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howellsc4028952006-11-22 14:57:56 +0000643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
648
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
651 unsol->rp = rp;
652 rp <<= 1;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
660 }
661}
662
663/*
664 * initialize unsolicited queue
665 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200666static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct hda_bus_unsolicited *unsol;
669
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100670 if (bus->unsol) /* already initialized */
671 return 0;
672
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200674 if (!unsol) {
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&unsol->work, process_unsol_events);
680 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus->unsol = unsol;
682 return 0;
683}
684
685/*
686 * destructor
687 */
688static void snd_hda_codec_free(struct hda_codec *codec);
689
690static int snd_hda_bus_free(struct hda_bus *bus)
691{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Takashi Iwai0ba21762007-04-16 11:29:14 +0200694 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 if (bus->workq)
697 flush_workqueue(bus->workq);
698 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_hda_codec_free(codec);
702 }
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100705 if (bus->workq)
706 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(bus);
708 return 0;
709}
710
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100711static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100714 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return snd_hda_bus_free(bus);
716}
717
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718#ifdef CONFIG_SND_HDA_HWDEP
719static int snd_hda_bus_dev_register(struct snd_device *device)
720{
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100725 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200726 }
727 return 0;
728}
729#else
730#define snd_hda_bus_dev_register NULL
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
738 *
739 * Returns 0 if successful, or a negative error code.
740 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100741int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct hda_bus *bus;
746 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100747 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200748 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .dev_free = snd_hda_bus_dev_free,
750 };
751
Takashi Iwaida3cec32008-08-08 17:12:14 +0200752 if (snd_BUG_ON(!temp))
753 return -EINVAL;
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (busp)
758 *busp = NULL;
759
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (bus == NULL) {
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
763 return -ENOMEM;
764 }
765
766 bus->card = card;
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100770 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 bus->ops = temp->ops;
772
Ingo Molnar62932df2006-01-16 16:34:20 +0100773 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200774 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&bus->codec_list);
776
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
782 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 kfree(bus);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 snd_hda_bus_free(bus);
790 return err;
791 }
792 if (busp)
793 *busp = bus;
794 return 0;
795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100796EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798#ifdef CONFIG_SND_HDA_GENERIC
799#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200800 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200801#else
802#define is_generic_config(codec) 0
803#endif
804
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * find a matching codec preset
813 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200815find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
819 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai82467612007-07-27 19:15:54 +0200821 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 return NULL; /* use the generic parser */
823
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 again:
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
829 continue;
830 }
831 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100833 if (preset->afg && preset->afg != codec->afg)
834 continue;
835 if (preset->mfg && preset->mfg != codec->mfg)
836 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200839 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847 module_put(tbl->owner);
848 }
849 mutex_unlock(&preset_mutex);
850
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
852 char name[32];
853 if (!mod_requested)
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
855 codec->vendor_id);
856 else
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
860 mod_requested++;
861 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return NULL;
864}
865
866/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 char tmp[16];
875
876 if (codec->vendor_name)
877 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
881 vendor = c->name;
882 break;
883 }
884 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200885 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sprintf(tmp, "Generic %04x", vendor_id);
887 vendor = tmp;
888 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
891 return -ENOMEM;
892
893 get_chip_name:
894 if (codec->chip_name)
895 return 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 else {
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 }
903 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200904 return -ENOMEM;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200909 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200913 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hda_nid_t nid;
915
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200918 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200919 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200920 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200921 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200925 break;
926 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200930 break;
931 default:
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100938 * read widget caps for each widget and store in cache
939 */
940static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
941{
942 int i;
943 hda_nid_t nid;
944
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 &codec->start_nid);
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100949 return -ENOMEM;
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
954 return 0;
955}
956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* read all pin default configurations and save codec->init_pins */
958static int read_pin_defaults(struct hda_codec *codec)
959{
960 int i;
961 hda_nid_t nid = codec->start_nid;
962
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200966 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967 if (wid_type != AC_WID_PIN)
968 continue;
969 pin = snd_array_new(&codec->init_pins);
970 if (!pin)
971 return -ENOMEM;
972 pin->nid = nid;
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
977 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 }
979 return 0;
980}
981
982/* look up the given pin config list and return the item matching with NID */
983static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
985 hda_nid_t nid)
986{
987 int i;
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
990 if (pin->nid == nid)
991 return pin;
992 }
993 return NULL;
994}
995
996/* write a config value for the given NID */
997static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
998 unsigned int cfg)
999{
1000 int i;
1001 for (i = 0; i < 4; i++) {
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1004 cfg & 0xff);
1005 cfg >>= 8;
1006 }
1007}
1008
1009/* set the current pin config value for the given NID.
1010 * the value is cached, and read via snd_hda_codec_get_pincfg()
1011 */
1012int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1013 hda_nid_t nid, unsigned int cfg)
1014{
1015 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001016 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001017
Takashi Iwaib82855a2009-12-27 11:24:56 +01001018 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1019 return -EINVAL;
1020
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001021 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 pin = look_up_pincfg(codec, list, nid);
1023 if (!pin) {
1024 pin = snd_array_new(list);
1025 if (!pin)
1026 return -ENOMEM;
1027 pin->nid = nid;
1028 }
1029 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001030
1031 /* change only when needed; e.g. if the pincfg is already present
1032 * in user_pins[], don't write it
1033 */
1034 cfg = snd_hda_codec_get_pincfg(codec, nid);
1035 if (oldcfg != cfg)
1036 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001037 return 0;
1038}
1039
Takashi Iwaid5191e52009-11-16 14:58:17 +01001040/**
1041 * snd_hda_codec_set_pincfg - Override a pin default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to set the pin config
1044 * @cfg: the pin default config value
1045 *
1046 * Override a pin default configuration value in the cache.
1047 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1048 * priority than the real hardware value.
1049 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001050int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1051 hda_nid_t nid, unsigned int cfg)
1052{
Takashi Iwai346ff702009-02-23 09:42:57 +01001053 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001054}
1055EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1056
Takashi Iwaid5191e52009-11-16 14:58:17 +01001057/**
1058 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1059 * @codec: the HDA codec
1060 * @nid: NID to get the pin config
1061 *
1062 * Get the current pin config value of the given pin NID.
1063 * If the pincfg value is cached or overridden via sysfs or driver,
1064 * returns the cached value.
1065 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001066unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1067{
1068 struct hda_pincfg *pin;
1069
Takashi Iwai3be14142009-02-20 14:11:16 +01001070#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 if (pin)
1073 return pin->cfg;
1074#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001075 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1076 if (pin)
1077 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1079 if (pin)
1080 return pin->cfg;
1081 return 0;
1082}
1083EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1084
1085/* restore all current pin configs */
1086static void restore_pincfgs(struct hda_codec *codec)
1087{
1088 int i;
1089 for (i = 0; i < codec->init_pins.used; i++) {
1090 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1091 set_pincfg(codec, pin->nid,
1092 snd_hda_codec_get_pincfg(codec, pin->nid));
1093 }
1094}
Takashi Iwai54d17402005-11-21 16:33:22 +01001095
Takashi Iwai92ee6162009-12-27 11:18:59 +01001096/**
1097 * snd_hda_shutup_pins - Shut up all pins
1098 * @codec: the HDA codec
1099 *
1100 * Clear all pin controls to shup up before suspend for avoiding click noise.
1101 * The controls aren't cached so that they can be resumed properly.
1102 */
1103void snd_hda_shutup_pins(struct hda_codec *codec)
1104{
1105 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106 /* don't shut up pins when unloading the driver; otherwise it breaks
1107 * the default pin setup at the next load of the driver
1108 */
1109 if (codec->bus->shutdown)
1110 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001111 for (i = 0; i < codec->init_pins.used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1113 /* use read here for syncing after issuing each verb */
1114 snd_hda_codec_read(codec, pin->nid, 0,
1115 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1116 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001117 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001118}
1119EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1120
Takashi Iwai2a439522011-07-26 09:52:50 +02001121#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001122/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1123static void restore_shutup_pins(struct hda_codec *codec)
1124{
1125 int i;
1126 if (!codec->pins_shutup)
1127 return;
1128 if (codec->bus->shutdown)
1129 return;
1130 for (i = 0; i < codec->init_pins.used; i++) {
1131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1132 snd_hda_codec_write(codec, pin->nid, 0,
1133 AC_VERB_SET_PIN_WIDGET_CONTROL,
1134 pin->ctrl);
1135 }
1136 codec->pins_shutup = 0;
1137}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001138#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001139
Takashi Iwai01751f52007-08-10 16:59:39 +02001140static void init_hda_cache(struct hda_cache_rec *cache,
1141 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001142static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143
Takashi Iwai3be14142009-02-20 14:11:16 +01001144/* restore the initial pin cfgs and release all pincfg lists */
1145static void restore_init_pincfgs(struct hda_codec *codec)
1146{
Takashi Iwai346ff702009-02-23 09:42:57 +01001147 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001148 * so that only the values in init_pins are restored
1149 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001152 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001153#endif
1154 restore_pincfgs(codec);
1155 snd_array_free(&codec->init_pins);
1156}
1157
Takashi Iwai54d17402005-11-21 16:33:22 +01001158/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001159 * audio-converter setup caches
1160 */
1161struct hda_cvt_setup {
1162 hda_nid_t nid;
1163 u8 stream_tag;
1164 u8 channel_id;
1165 u16 format_id;
1166 unsigned char active; /* cvt is currently used */
1167 unsigned char dirty; /* setups should be cleared */
1168};
1169
1170/* get or create a cache entry for the given audio converter NID */
1171static struct hda_cvt_setup *
1172get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1173{
1174 struct hda_cvt_setup *p;
1175 int i;
1176
1177 for (i = 0; i < codec->cvt_setups.used; i++) {
1178 p = snd_array_elem(&codec->cvt_setups, i);
1179 if (p->nid == nid)
1180 return p;
1181 }
1182 p = snd_array_new(&codec->cvt_setups);
1183 if (p)
1184 p->nid = nid;
1185 return p;
1186}
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * codec destructor
1190 */
1191static void snd_hda_codec_free(struct hda_codec *codec)
1192{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001193 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001195 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#ifdef CONFIG_SND_HDA_POWER_SAVE
1197 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001198 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001201 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001202 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001208 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001209 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001210 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001211 kfree(codec->vendor_name);
1212 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001213 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001214 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(codec);
1216}
1217
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001218static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1219 unsigned int power_state);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
1222 * snd_hda_codec_new - create a HDA codec
1223 * @bus: the bus to assign
1224 * @codec_addr: the codec address
1225 * @codecp: the pointer to store the generated codec
1226 *
1227 * Returns 0 if successful, or a negative error code.
1228 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001229int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1230 unsigned int codec_addr,
1231 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001234 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
1236
Takashi Iwaida3cec32008-08-08 17:12:14 +02001237 if (snd_BUG_ON(!bus))
1238 return -EINVAL;
1239 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243 snd_printk(KERN_ERR "hda_codec: "
1244 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EBUSY;
1246 }
1247
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001248 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (codec == NULL) {
1250 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1251 return -ENOMEM;
1252 }
1253
1254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001258 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001259 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001260 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001262 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001263 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001264 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001265 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#ifdef CONFIG_SND_HDA_POWER_SAVE
1269 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1270 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1271 * the caller has to power down appropriatley after initialization
1272 * phase.
1273 */
1274 hda_keep_power_on(codec);
1275#endif
1276
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001277 if (codec->bus->modelname) {
1278 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1279 if (!codec->modelname) {
1280 snd_hda_codec_free(codec);
1281 return -ENODEV;
1282 }
1283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 list_add_tail(&codec->list, &bus->codec_list);
1286 bus->caddr_tbl[codec_addr] = codec;
1287
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001290 if (codec->vendor_id == -1)
1291 /* read again, hopefully the access method was corrected
1292 * in the last read...
1293 */
1294 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_SUBSYSTEM_ID);
1298 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001301 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001302 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001303 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = -ENODEV;
1305 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1309 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001310 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001311 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001312 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001313 err = read_pin_defaults(codec);
1314 if (err < 0)
1315 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001316
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001318 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319 codec->subsystem_id =
1320 snd_hda_codec_read(codec, nid, 0,
1321 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001322 }
1323
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001324 /* power-up all before initialization */
1325 hda_set_power_state(codec,
1326 codec->afg ? codec->afg : codec->mfg,
1327 AC_PWRST_D0);
1328
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001329 snd_hda_codec_proc_new(codec);
1330
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332
1333 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1334 codec->subsystem_id, codec->revision_id);
1335 snd_component_add(codec->bus->card, component);
1336
1337 if (codecp)
1338 *codecp = codec;
1339 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001340
1341 error:
1342 snd_hda_codec_free(codec);
1343 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001345EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001346
Takashi Iwaid5191e52009-11-16 14:58:17 +01001347/**
1348 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1349 * @codec: the HDA codec
1350 *
1351 * Start parsing of the given codec tree and (re-)initialize the whole
1352 * patch instance.
1353 *
1354 * Returns 0 if successful or a negative error code.
1355 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001356int snd_hda_codec_configure(struct hda_codec *codec)
1357{
1358 int err;
1359
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001360 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001361 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001362 err = get_codec_name(codec);
1363 if (err < 0)
1364 return err;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001369 goto patched;
1370 }
Takashi Iwai82467612007-07-27 19:15:54 +02001371 if (codec->preset && codec->preset->patch) {
1372 err = codec->preset->patch(codec);
1373 goto patched;
1374 }
1375
1376 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001377 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001378 if (err < 0)
1379 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001380
1381 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001382 if (!err && codec->patch_ops.unsol_event)
1383 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001384 /* audio codec should override the mixer name */
1385 if (!err && (codec->afg || !*codec->bus->card->mixername))
1386 snprintf(codec->bus->card->mixername,
1387 sizeof(codec->bus->card->mixername),
1388 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001391EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393/**
1394 * snd_hda_codec_setup_stream - set up the codec for streaming
1395 * @codec: the CODEC to set up
1396 * @nid: the NID to set up
1397 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1398 * @channel_id: channel id to pass, zero based.
1399 * @format: stream format.
1400 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1402 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 int channel_id, int format)
1404{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001405 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001406 struct hda_cvt_setup *p;
1407 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001408 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001409 int i;
1410
Takashi Iwai0ba21762007-04-16 11:29:14 +02001411 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001412 return;
1413
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414 snd_printdd("hda_codec_setup_stream: "
1415 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001417 p = get_hda_cvt_setup(codec, nid);
1418 if (!p)
1419 return;
1420 /* update the stream-id if changed */
1421 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1422 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1423 newval = (stream_tag << 4) | channel_id;
1424 if (oldval != newval)
1425 snd_hda_codec_write(codec, nid, 0,
1426 AC_VERB_SET_CHANNEL_STREAMID,
1427 newval);
1428 p->stream_tag = stream_tag;
1429 p->channel_id = channel_id;
1430 }
1431 /* update the format-id if changed */
1432 if (p->format_id != format) {
1433 oldval = snd_hda_codec_read(codec, nid, 0,
1434 AC_VERB_GET_STREAM_FORMAT, 0);
1435 if (oldval != format) {
1436 msleep(1);
1437 snd_hda_codec_write(codec, nid, 0,
1438 AC_VERB_SET_STREAM_FORMAT,
1439 format);
1440 }
1441 p->format_id = format;
1442 }
1443 p->active = 1;
1444 p->dirty = 0;
1445
1446 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001447 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001448 list_for_each_entry(c, &codec->bus->codec_list, list) {
1449 for (i = 0; i < c->cvt_setups.used; i++) {
1450 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001451 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001452 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001453 p->dirty = 1;
1454 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001457EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Takashi Iwaif0cea792010-08-13 11:56:53 +02001459static void really_cleanup_stream(struct hda_codec *codec,
1460 struct hda_cvt_setup *q);
1461
Takashi Iwaid5191e52009-11-16 14:58:17 +01001462/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001463 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001464 * @codec: the CODEC to clean up
1465 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001466 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001467 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001468void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1469 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001470{
Takashi Iwaieb541332010-08-06 13:48:11 +02001471 struct hda_cvt_setup *p;
1472
Takashi Iwai888afa12008-03-18 09:57:50 +01001473 if (!nid)
1474 return;
1475
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001476 if (codec->no_sticky_stream)
1477 do_now = 1;
1478
Takashi Iwai888afa12008-03-18 09:57:50 +01001479 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001480 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001481 if (p) {
1482 /* here we just clear the active flag when do_now isn't set;
1483 * actual clean-ups will be done later in
1484 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1485 */
1486 if (do_now)
1487 really_cleanup_stream(codec, p);
1488 else
1489 p->active = 0;
1490 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001491}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001492EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001493
Takashi Iwaieb541332010-08-06 13:48:11 +02001494static void really_cleanup_stream(struct hda_codec *codec,
1495 struct hda_cvt_setup *q)
1496{
1497 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001498 if (q->stream_tag || q->channel_id)
1499 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1500 if (q->format_id)
1501 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1502);
Takashi Iwaieb541332010-08-06 13:48:11 +02001503 memset(q, 0, sizeof(*q));
1504 q->nid = nid;
1505}
1506
1507/* clean up the all conflicting obsolete streams */
1508static void purify_inactive_streams(struct hda_codec *codec)
1509{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001510 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001511 int i;
1512
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001513 list_for_each_entry(c, &codec->bus->codec_list, list) {
1514 for (i = 0; i < c->cvt_setups.used; i++) {
1515 struct hda_cvt_setup *p;
1516 p = snd_array_elem(&c->cvt_setups, i);
1517 if (p->dirty)
1518 really_cleanup_stream(c, p);
1519 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001520 }
1521}
1522
Takashi Iwai2a439522011-07-26 09:52:50 +02001523#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001524/* clean up all streams; called from suspend */
1525static void hda_cleanup_all_streams(struct hda_codec *codec)
1526{
1527 int i;
1528
1529 for (i = 0; i < codec->cvt_setups.used; i++) {
1530 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1531 if (p->stream_tag)
1532 really_cleanup_stream(codec, p);
1533 }
1534}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001535#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
1538 * amp access functions
1539 */
1540
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001541/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001542#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001543#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001544#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1545#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001547#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001550static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001551 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai01751f52007-08-10 16:59:39 +02001553 memset(cache, 0, sizeof(*cache));
1554 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001555 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001556}
1557
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001558static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001559{
Takashi Iwai603c4012008-07-30 15:01:44 +02001560 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001564static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Takashi Iwai01751f52007-08-10 16:59:39 +02001566 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1567 u16 cur = cache->hash[idx];
1568 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001571 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (info->key == key)
1573 return info;
1574 cur = info->next;
1575 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001576 return NULL;
1577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001579/* query the hash. allocate an entry if not found. */
1580static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1581 u32 key)
1582{
1583 struct hda_cache_head *info = get_hash(cache, key);
1584 if (!info) {
1585 u16 idx, cur;
1586 /* add a new hash entry */
1587 info = snd_array_new(&cache->buf);
1588 if (!info)
1589 return NULL;
1590 cur = snd_array_index(&cache->buf, info);
1591 info->key = key;
1592 info->val = 0;
1593 idx = key % (u16)ARRAY_SIZE(cache->hash);
1594 info->next = cache->hash[idx];
1595 cache->hash[idx] = cur;
1596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return info;
1598}
1599
Takashi Iwai01751f52007-08-10 16:59:39 +02001600/* query and allocate an amp hash entry */
1601static inline struct hda_amp_info *
1602get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1603{
1604 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1605}
1606
Takashi Iwaid5191e52009-11-16 14:58:17 +01001607/**
1608 * query_amp_caps - query AMP capabilities
1609 * @codec: the HD-auio codec
1610 * @nid: the NID to query
1611 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1612 *
1613 * Query AMP capabilities for the given widget and direction.
1614 * Returns the obtained capability bits.
1615 *
1616 * When cap bits have been already read, this doesn't read again but
1617 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001619u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001621 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Takashi Iwai0ba21762007-04-16 11:29:14 +02001623 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1624 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001626 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001627 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001629 info->amp_caps = snd_hda_param_read(codec, nid,
1630 direction == HDA_OUTPUT ?
1631 AC_PAR_AMP_OUT_CAP :
1632 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001633 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001634 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 return info->amp_caps;
1637}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001638EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Takashi Iwaid5191e52009-11-16 14:58:17 +01001640/**
1641 * snd_hda_override_amp_caps - Override the AMP capabilities
1642 * @codec: the CODEC to clean up
1643 * @nid: the NID to clean up
1644 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1645 * @caps: the capability bits to set
1646 *
1647 * Override the cached AMP caps bits value by the given one.
1648 * This function is useful if the driver needs to adjust the AMP ranges,
1649 * e.g. limit to 0dB, etc.
1650 *
1651 * Returns zero if successful or a negative error code.
1652 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001653int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1654 unsigned int caps)
1655{
1656 struct hda_amp_info *info;
1657
1658 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1659 if (!info)
1660 return -EINVAL;
1661 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001662 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001663 return 0;
1664}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001665EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001666
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001667static unsigned int
1668query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1669 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001670{
1671 struct hda_amp_info *info;
1672
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001673 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001674 if (!info)
1675 return 0;
1676 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001677 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001678 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001679 }
1680 return info->amp_caps;
1681}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001682
1683static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1684{
1685 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1686}
1687
Takashi Iwaid5191e52009-11-16 14:58:17 +01001688/**
1689 * snd_hda_query_pin_caps - Query PIN capabilities
1690 * @codec: the HD-auio codec
1691 * @nid: the NID to query
1692 *
1693 * Query PIN capabilities for the given widget.
1694 * Returns the obtained capability bits.
1695 *
1696 * When cap bits have been already read, this doesn't read again but
1697 * returns the cached value.
1698 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001699u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1700{
1701 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1702 read_pin_cap);
1703}
Takashi Iwai1327a322009-03-23 13:07:47 +01001704EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1705
Wu Fengguang864f92b2009-11-18 12:38:02 +08001706/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001707 * snd_hda_override_pin_caps - Override the pin capabilities
1708 * @codec: the CODEC
1709 * @nid: the NID to override
1710 * @caps: the capability bits to set
1711 *
1712 * Override the cached PIN capabilitiy bits value by the given one.
1713 *
1714 * Returns zero if successful or a negative error code.
1715 */
1716int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1717 unsigned int caps)
1718{
1719 struct hda_amp_info *info;
1720 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1721 if (!info)
1722 return -ENOMEM;
1723 info->amp_caps = caps;
1724 info->head.val |= INFO_AMP_CAPS;
1725 return 0;
1726}
1727EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001731 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001733static unsigned int get_vol_mute(struct hda_codec *codec,
1734 struct hda_amp_info *info, hda_nid_t nid,
1735 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 u32 val, parm;
1738
Takashi Iwai01751f52007-08-10 16:59:39 +02001739 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001740 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1743 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1744 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001745 val = snd_hda_codec_read(codec, nid, 0,
1746 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001748 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001749 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001753 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001755static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001756 hda_nid_t nid, int ch, int direction, int index,
1757 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 u32 parm;
1760
1761 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1762 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1763 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001764 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1765 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1766 ; /* set the zero value as a fake mute */
1767 else
1768 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001770 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Takashi Iwaid5191e52009-11-16 14:58:17 +01001773/**
1774 * snd_hda_codec_amp_read - Read AMP value
1775 * @codec: HD-audio codec
1776 * @nid: NID to read the AMP value
1777 * @ch: channel (left=0 or right=1)
1778 * @direction: #HDA_INPUT or #HDA_OUTPUT
1779 * @index: the index value (only for input direction)
1780 *
1781 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Takashi Iwai834be882006-03-01 14:16:17 +01001783int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1784 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001786 struct hda_amp_info *info;
1787 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1788 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001790 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001792EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Takashi Iwaid5191e52009-11-16 14:58:17 +01001794/**
1795 * snd_hda_codec_amp_update - update the AMP value
1796 * @codec: HD-audio codec
1797 * @nid: NID to read the AMP value
1798 * @ch: channel (left=0 or right=1)
1799 * @direction: #HDA_INPUT or #HDA_OUTPUT
1800 * @idx: the index value (only for input direction)
1801 * @mask: bit mask to set
1802 * @val: the bits value to set
1803 *
1804 * Update the AMP value with a bit mask.
1805 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001806 */
Takashi Iwai834be882006-03-01 14:16:17 +01001807int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1808 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001810 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001811
Takashi Iwai0ba21762007-04-16 11:29:14 +02001812 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1813 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001815 if (snd_BUG_ON(mask & ~0xff))
1816 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001817 val &= mask;
1818 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001819 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001821 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return 1;
1823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001824EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Takashi Iwaid5191e52009-11-16 14:58:17 +01001826/**
1827 * snd_hda_codec_amp_stereo - update the AMP stereo values
1828 * @codec: HD-audio codec
1829 * @nid: NID to read the AMP value
1830 * @direction: #HDA_INPUT or #HDA_OUTPUT
1831 * @idx: the index value (only for input direction)
1832 * @mask: bit mask to set
1833 * @val: the bits value to set
1834 *
1835 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1836 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001837 */
1838int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1839 int direction, int idx, int mask, int val)
1840{
1841 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001842
1843 if (snd_BUG_ON(mask & ~0xff))
1844 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001845 for (ch = 0; ch < 2; ch++)
1846 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1847 idx, mask, val);
1848 return ret;
1849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001850EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001851
Takashi Iwai2a439522011-07-26 09:52:50 +02001852#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001853/**
1854 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1855 * @codec: HD-audio codec
1856 *
1857 * Resume the all amp commands from the cache.
1858 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001859void snd_hda_codec_resume_amp(struct hda_codec *codec)
1860{
Takashi Iwai603c4012008-07-30 15:01:44 +02001861 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001862 int i;
1863
Takashi Iwai603c4012008-07-30 15:01:44 +02001864 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001865 u32 key = buffer->head.key;
1866 hda_nid_t nid;
1867 unsigned int idx, dir, ch;
1868 if (!key)
1869 continue;
1870 nid = key & 0xff;
1871 idx = (key >> 16) & 0xff;
1872 dir = (key >> 24) & 0xff;
1873 for (ch = 0; ch < 2; ch++) {
1874 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1875 continue;
1876 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1877 buffer->vol[ch]);
1878 }
1879 }
1880}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001881EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001882#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001884static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1885 unsigned int ofs)
1886{
1887 u32 caps = query_amp_caps(codec, nid, dir);
1888 /* get num steps */
1889 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1890 if (ofs < caps)
1891 caps -= ofs;
1892 return caps;
1893}
1894
Takashi Iwaid5191e52009-11-16 14:58:17 +01001895/**
1896 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1897 *
1898 * The control element is supposed to have the private_value field
1899 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1900 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001901int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1902 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1905 u16 nid = get_amp_nid(kcontrol);
1906 u8 chs = get_amp_channels(kcontrol);
1907 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001908 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001910 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1911 uinfo->count = chs == 3 ? 2 : 1;
1912 uinfo->value.integer.min = 0;
1913 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1914 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001915 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001916 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1917 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return -EINVAL;
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return 0;
1921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001922EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001924
1925static inline unsigned int
1926read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1927 int ch, int dir, int idx, unsigned int ofs)
1928{
1929 unsigned int val;
1930 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1931 val &= HDA_AMP_VOLMASK;
1932 if (val >= ofs)
1933 val -= ofs;
1934 else
1935 val = 0;
1936 return val;
1937}
1938
1939static inline int
1940update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1941 int ch, int dir, int idx, unsigned int ofs,
1942 unsigned int val)
1943{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001944 unsigned int maxval;
1945
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001946 if (val > 0)
1947 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001948 /* ofs = 0: raw max value */
1949 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001950 if (val > maxval)
1951 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001952 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1953 HDA_AMP_VOLMASK, val);
1954}
1955
Takashi Iwaid5191e52009-11-16 14:58:17 +01001956/**
1957 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1958 *
1959 * The control element is supposed to have the private_value field
1960 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1961 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001962int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1963 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1966 hda_nid_t nid = get_amp_nid(kcontrol);
1967 int chs = get_amp_channels(kcontrol);
1968 int dir = get_amp_direction(kcontrol);
1969 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001970 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 long *valp = ucontrol->value.integer.value;
1972
1973 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001974 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001976 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001979EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
1982 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1983 *
1984 * The control element is supposed to have the private_value field
1985 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1986 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001987int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1991 hda_nid_t nid = get_amp_nid(kcontrol);
1992 int chs = get_amp_channels(kcontrol);
1993 int dir = get_amp_direction(kcontrol);
1994 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001995 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 long *valp = ucontrol->value.integer.value;
1997 int change = 0;
1998
Takashi Iwaicb53c622007-08-10 17:21:45 +02001999 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002000 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002001 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002002 valp++;
2003 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002004 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002005 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002006 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return change;
2008}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002009EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Takashi Iwaid5191e52009-11-16 14:58:17 +01002011/**
2012 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2013 *
2014 * The control element is supposed to have the private_value field
2015 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2016 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002017int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2018 unsigned int size, unsigned int __user *_tlv)
2019{
2020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2021 hda_nid_t nid = get_amp_nid(kcontrol);
2022 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002023 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002024 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002025 u32 caps, val1, val2;
2026
2027 if (size < 4 * sizeof(unsigned int))
2028 return -ENOMEM;
2029 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002030 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2031 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002032 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002033 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002034 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002035 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002036 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002037 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2038 return -EFAULT;
2039 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2040 return -EFAULT;
2041 if (put_user(val1, _tlv + 2))
2042 return -EFAULT;
2043 if (put_user(val2, _tlv + 3))
2044 return -EFAULT;
2045 return 0;
2046}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002047EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002048
Takashi Iwaid5191e52009-11-16 14:58:17 +01002049/**
2050 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2051 * @codec: HD-audio codec
2052 * @nid: NID of a reference widget
2053 * @dir: #HDA_INPUT or #HDA_OUTPUT
2054 * @tlv: TLV data to be stored, at least 4 elements
2055 *
2056 * Set (static) TLV data for a virtual master volume using the AMP caps
2057 * obtained from the reference NID.
2058 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002059 */
2060void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2061 unsigned int *tlv)
2062{
2063 u32 caps;
2064 int nums, step;
2065
2066 caps = query_amp_caps(codec, nid, dir);
2067 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2068 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2069 step = (step + 1) * 25;
2070 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2071 tlv[1] = 2 * sizeof(unsigned int);
2072 tlv[2] = -nums * step;
2073 tlv[3] = step;
2074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002075EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002076
2077/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002078static struct snd_kcontrol *
2079_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2080 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002081{
2082 struct snd_ctl_elem_id id;
2083 memset(&id, 0, sizeof(id));
2084 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002085 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002086 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2087 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002088 strcpy(id.name, name);
2089 return snd_ctl_find_id(codec->bus->card, &id);
2090}
2091
Takashi Iwaid5191e52009-11-16 14:58:17 +01002092/**
2093 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2094 * @codec: HD-audio codec
2095 * @name: ctl id name string
2096 *
2097 * Get the control element with the given id string and IFACE_MIXER.
2098 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002099struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2100 const char *name)
2101{
2102 return _snd_hda_find_mixer_ctl(codec, name, 0);
2103}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002104EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002105
Takashi Iwai1afe2062010-12-23 10:17:52 +01002106static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2107{
2108 int idx;
2109 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2110 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2111 return idx;
2112 }
2113 return -EBUSY;
2114}
2115
Takashi Iwaid5191e52009-11-16 14:58:17 +01002116/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002117 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002118 * @codec: HD-audio codec
2119 * @nid: corresponding NID (optional)
2120 * @kctl: the control element to assign
2121 *
2122 * Add the given control element to an array inside the codec instance.
2123 * All control elements belonging to a codec are supposed to be added
2124 * by this function so that a proper clean-up works at the free or
2125 * reconfiguration time.
2126 *
2127 * If non-zero @nid is passed, the NID is assigned to the control element.
2128 * The assignment is shown in the codec proc file.
2129 *
2130 * snd_hda_ctl_add() checks the control subdev id field whether
2131 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002132 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2133 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002134 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002135int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2136 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002137{
2138 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002139 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002140 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002141
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002142 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002143 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002144 if (nid == 0)
2145 nid = get_amp_nid_(kctl->private_value);
2146 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002147 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2148 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002149 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002150 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002151 err = snd_ctl_add(codec->bus->card, kctl);
2152 if (err < 0)
2153 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002154 item = snd_array_new(&codec->mixers);
2155 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002156 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002157 item->kctl = kctl;
2158 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002159 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002160 return 0;
2161}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002162EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002163
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002165 * snd_hda_add_nid - Assign a NID to a control element
2166 * @codec: HD-audio codec
2167 * @nid: corresponding NID (optional)
2168 * @kctl: the control element to assign
2169 * @index: index to kctl
2170 *
2171 * Add the given control element to an array inside the codec instance.
2172 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2173 * NID:KCTL mapping - for example "Capture Source" selector.
2174 */
2175int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2176 unsigned int index, hda_nid_t nid)
2177{
2178 struct hda_nid_item *item;
2179
2180 if (nid > 0) {
2181 item = snd_array_new(&codec->nids);
2182 if (!item)
2183 return -ENOMEM;
2184 item->kctl = kctl;
2185 item->index = index;
2186 item->nid = nid;
2187 return 0;
2188 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002189 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2190 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002191 return -EINVAL;
2192}
2193EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2194
2195/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2197 * @codec: HD-audio codec
2198 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002199void snd_hda_ctls_clear(struct hda_codec *codec)
2200{
2201 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002202 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002203 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002204 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002205 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002206 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002207}
2208
Takashi Iwaia65d6292009-02-23 16:57:04 +01002209/* pseudo device locking
2210 * toggle card->shutdown to allow/disallow the device access (as a hack)
2211 */
2212static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002213{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002214 spin_lock(&card->files_lock);
2215 if (card->shutdown) {
2216 spin_unlock(&card->files_lock);
2217 return -EINVAL;
2218 }
2219 card->shutdown = 1;
2220 spin_unlock(&card->files_lock);
2221 return 0;
2222}
2223
2224static void hda_unlock_devices(struct snd_card *card)
2225{
2226 spin_lock(&card->files_lock);
2227 card->shutdown = 0;
2228 spin_unlock(&card->files_lock);
2229}
2230
Takashi Iwaid5191e52009-11-16 14:58:17 +01002231/**
2232 * snd_hda_codec_reset - Clear all objects assigned to the codec
2233 * @codec: HD-audio codec
2234 *
2235 * This frees the all PCM and control elements assigned to the codec, and
2236 * clears the caches and restores the pin default configurations.
2237 *
2238 * When a device is being used, it returns -EBSY. If successfully freed,
2239 * returns zero.
2240 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002241int snd_hda_codec_reset(struct hda_codec *codec)
2242{
2243 struct snd_card *card = codec->bus->card;
2244 int i, pcm;
2245
2246 if (hda_lock_devices(card) < 0)
2247 return -EBUSY;
2248 /* check whether the codec isn't used by any mixer or PCM streams */
2249 if (!list_empty(&card->ctl_files)) {
2250 hda_unlock_devices(card);
2251 return -EBUSY;
2252 }
2253 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2254 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2255 if (!cpcm->pcm)
2256 continue;
2257 if (cpcm->pcm->streams[0].substream_opened ||
2258 cpcm->pcm->streams[1].substream_opened) {
2259 hda_unlock_devices(card);
2260 return -EBUSY;
2261 }
2262 }
2263
2264 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002265
2266#ifdef CONFIG_SND_HDA_POWER_SAVE
2267 cancel_delayed_work(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002268 codec->power_on = 0;
2269 codec->power_transition = 0;
2270 codec->power_jiffies = jiffies;
Takashi Iwai6acaed32009-01-12 10:09:24 +01002271 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002272#endif
2273 snd_hda_ctls_clear(codec);
2274 /* relase PCMs */
2275 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002276 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002277 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002278 clear_bit(codec->pcm_info[i].device,
2279 codec->bus->pcm_dev_bits);
2280 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002281 }
2282 if (codec->patch_ops.free)
2283 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002284 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002285 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002286 codec->spec = NULL;
2287 free_hda_cache(&codec->amp_cache);
2288 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002289 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2290 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002291 /* free only driver_pins so that init_pins + user_pins are restored */
2292 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002293 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002294 codec->num_pcms = 0;
2295 codec->pcm_info = NULL;
2296 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002297 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2298 codec->slave_dig_outs = NULL;
2299 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002300 module_put(codec->owner);
2301 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002302
2303 /* allow device access again */
2304 hda_unlock_devices(card);
2305 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002306}
2307
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002308typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2309
2310/* apply the function to all matching slave ctls in the mixer list */
2311static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002312 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002313{
2314 struct hda_nid_item *items;
2315 const char * const *s;
2316 int i, err;
2317
2318 items = codec->mixers.list;
2319 for (i = 0; i < codec->mixers.used; i++) {
2320 struct snd_kcontrol *sctl = items[i].kctl;
2321 if (!sctl || !sctl->id.name ||
2322 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2323 continue;
2324 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002325 char tmpname[sizeof(sctl->id.name)];
2326 const char *name = *s;
2327 if (suffix) {
2328 snprintf(tmpname, sizeof(tmpname), "%s %s",
2329 name, suffix);
2330 name = tmpname;
2331 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002332 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002333 err = func(data, sctl);
2334 if (err)
2335 return err;
2336 break;
2337 }
2338 }
2339 }
2340 return 0;
2341}
2342
2343static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2344{
2345 return 1;
2346}
2347
Takashi Iwai18478e82012-03-09 17:51:10 +01002348/* guess the value corresponding to 0dB */
2349static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2350{
2351 int _tlv[4];
2352 const int *tlv = NULL;
2353 int val = -1;
2354
2355 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2356 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2357 mm_segment_t fs = get_fs();
2358 set_fs(get_ds());
2359 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2360 tlv = _tlv;
2361 set_fs(fs);
2362 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2363 tlv = kctl->tlv.p;
2364 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2365 val = -tlv[2] / tlv[3];
2366 return val;
2367}
2368
2369/* call kctl->put with the given value(s) */
2370static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2371{
2372 struct snd_ctl_elem_value *ucontrol;
2373 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2374 if (!ucontrol)
2375 return -ENOMEM;
2376 ucontrol->value.integer.value[0] = val;
2377 ucontrol->value.integer.value[1] = val;
2378 kctl->put(kctl, ucontrol);
2379 kfree(ucontrol);
2380 return 0;
2381}
2382
2383/* initialize the slave volume with 0dB */
2384static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2385{
2386 int offset = get_kctl_0dB_offset(slave);
2387 if (offset > 0)
2388 put_kctl_with_value(slave, offset);
2389 return 0;
2390}
2391
2392/* unmute the slave */
2393static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2394{
2395 return put_kctl_with_value(slave, 1);
2396}
2397
Takashi Iwaid5191e52009-11-16 14:58:17 +01002398/**
2399 * snd_hda_add_vmaster - create a virtual master control and add slaves
2400 * @codec: HD-audio codec
2401 * @name: vmaster control name
2402 * @tlv: TLV data (optional)
2403 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002404 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002405 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002406 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002407 *
2408 * Create a virtual master control with the given name. The TLV data
2409 * must be either NULL or a valid data.
2410 *
2411 * @slaves is a NULL-terminated array of strings, each of which is a
2412 * slave control name. All controls with these names are assigned to
2413 * the new virtual master control.
2414 *
2415 * This function returns zero if successful or a negative error code.
2416 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002417int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002418 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002419 const char *suffix, bool init_slave_vol,
2420 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002421{
2422 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002423 int err;
2424
Takashi Iwai29e58532012-03-12 12:25:03 +01002425 if (ctl_ret)
2426 *ctl_ret = NULL;
2427
Takashi Iwai9322ca52012-02-03 14:28:01 +01002428 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002429 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002430 snd_printdd("No slave found for %s\n", name);
2431 return 0;
2432 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002433 kctl = snd_ctl_make_virtual_master(name, tlv);
2434 if (!kctl)
2435 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002436 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002437 if (err < 0)
2438 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002439
Takashi Iwai9322ca52012-02-03 14:28:01 +01002440 err = map_slaves(codec, slaves, suffix,
2441 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002442 if (err < 0)
2443 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002444
2445 /* init with master mute & zero volume */
2446 put_kctl_with_value(kctl, 0);
2447 if (init_slave_vol)
2448 map_slaves(codec, slaves, suffix,
2449 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2450
Takashi Iwai29e58532012-03-12 12:25:03 +01002451 if (ctl_ret)
2452 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002453 return 0;
2454}
Takashi Iwai18478e82012-03-09 17:51:10 +01002455EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002456
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002457/*
2458 * mute-LED control using vmaster
2459 */
2460static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_info *uinfo)
2462{
2463 static const char * const texts[] = {
2464 "Off", "On", "Follow Master"
2465 };
2466 unsigned int index;
2467
2468 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2469 uinfo->count = 1;
2470 uinfo->value.enumerated.items = 3;
2471 index = uinfo->value.enumerated.item;
2472 if (index >= 3)
2473 index = 2;
2474 strcpy(uinfo->value.enumerated.name, texts[index]);
2475 return 0;
2476}
2477
2478static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
2480{
2481 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2482 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2483 return 0;
2484}
2485
2486static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_value *ucontrol)
2488{
2489 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2490 unsigned int old_mode = hook->mute_mode;
2491
2492 hook->mute_mode = ucontrol->value.enumerated.item[0];
2493 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2494 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2495 if (old_mode == hook->mute_mode)
2496 return 0;
2497 snd_hda_sync_vmaster_hook(hook);
2498 return 1;
2499}
2500
2501static struct snd_kcontrol_new vmaster_mute_mode = {
2502 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2503 .name = "Mute-LED Mode",
2504 .info = vmaster_mute_mode_info,
2505 .get = vmaster_mute_mode_get,
2506 .put = vmaster_mute_mode_put,
2507};
2508
2509/*
2510 * Add a mute-LED hook with the given vmaster switch kctl
2511 * "Mute-LED Mode" control is automatically created and associated with
2512 * the given hook.
2513 */
2514int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002515 struct hda_vmaster_mute_hook *hook,
2516 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002517{
2518 struct snd_kcontrol *kctl;
2519
2520 if (!hook->hook || !hook->sw_kctl)
2521 return 0;
2522 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2523 hook->codec = codec;
2524 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002525 if (!expose_enum_ctl)
2526 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002527 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2528 if (!kctl)
2529 return -ENOMEM;
2530 return snd_hda_ctl_add(codec, 0, kctl);
2531}
2532EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2533
2534/*
2535 * Call the hook with the current value for synchronization
2536 * Should be called in init callback
2537 */
2538void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2539{
2540 if (!hook->hook || !hook->codec)
2541 return;
2542 switch (hook->mute_mode) {
2543 case HDA_VMUTE_FOLLOW_MASTER:
2544 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2545 break;
2546 default:
2547 hook->hook(hook->codec, hook->mute_mode);
2548 break;
2549 }
2550}
2551EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2552
2553
Takashi Iwaid5191e52009-11-16 14:58:17 +01002554/**
2555 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2556 *
2557 * The control element is supposed to have the private_value field
2558 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2559 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002560int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 int chs = get_amp_channels(kcontrol);
2564
2565 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2566 uinfo->count = chs == 3 ? 2 : 1;
2567 uinfo->value.integer.min = 0;
2568 uinfo->value.integer.max = 1;
2569 return 0;
2570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002571EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Takashi Iwaid5191e52009-11-16 14:58:17 +01002573/**
2574 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2575 *
2576 * The control element is supposed to have the private_value field
2577 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2578 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583 hda_nid_t nid = get_amp_nid(kcontrol);
2584 int chs = get_amp_channels(kcontrol);
2585 int dir = get_amp_direction(kcontrol);
2586 int idx = get_amp_index(kcontrol);
2587 long *valp = ucontrol->value.integer.value;
2588
2589 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002590 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002591 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002593 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002594 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return 0;
2596}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002597EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Takashi Iwaid5191e52009-11-16 14:58:17 +01002599/**
2600 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2601 *
2602 * The control element is supposed to have the private_value field
2603 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2604 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2609 hda_nid_t nid = get_amp_nid(kcontrol);
2610 int chs = get_amp_channels(kcontrol);
2611 int dir = get_amp_direction(kcontrol);
2612 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 long *valp = ucontrol->value.integer.value;
2614 int change = 0;
2615
Takashi Iwaicb53c622007-08-10 17:21:45 +02002616 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002617 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002618 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002619 HDA_AMP_MUTE,
2620 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002621 valp++;
2622 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002623 if (chs & 2)
2624 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002625 HDA_AMP_MUTE,
2626 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002627 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002628 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return change;
2630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002631EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Takashi Iwai67d634c2009-11-16 15:35:59 +01002633#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002634/**
2635 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2636 *
2637 * This function calls snd_hda_enable_beep_device(), which behaves differently
2638 * depending on beep_mode option.
2639 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002640int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2641 struct snd_ctl_elem_value *ucontrol)
2642{
2643 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2644 long *valp = ucontrol->value.integer.value;
2645
2646 snd_hda_enable_beep_device(codec, *valp);
2647 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2648}
2649EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002650#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652/*
Takashi Iwai985be542005-11-02 18:26:49 +01002653 * bound volume controls
2654 *
2655 * bind multiple volumes (# indices, from 0)
2656 */
2657
2658#define AMP_VAL_IDX_SHIFT 19
2659#define AMP_VAL_IDX_MASK (0x0f<<19)
2660
Takashi Iwaid5191e52009-11-16 14:58:17 +01002661/**
2662 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2663 *
2664 * The control element is supposed to have the private_value field
2665 * set up via HDA_BIND_MUTE*() macros.
2666 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002667int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002669{
2670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2671 unsigned long pval;
2672 int err;
2673
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002674 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002675 pval = kcontrol->private_value;
2676 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2677 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2678 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002679 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002680 return err;
2681}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002682EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002683
Takashi Iwaid5191e52009-11-16 14:58:17 +01002684/**
2685 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2686 *
2687 * The control element is supposed to have the private_value field
2688 * set up via HDA_BIND_MUTE*() macros.
2689 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002692{
2693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2694 unsigned long pval;
2695 int i, indices, err = 0, change = 0;
2696
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002697 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002698 pval = kcontrol->private_value;
2699 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2700 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002701 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2702 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002703 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2704 if (err < 0)
2705 break;
2706 change |= err;
2707 }
2708 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002709 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002710 return err < 0 ? err : change;
2711}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002712EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002713
Takashi Iwaid5191e52009-11-16 14:58:17 +01002714/**
2715 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2716 *
2717 * The control element is supposed to have the private_value field
2718 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002719 */
2720int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2721 struct snd_ctl_elem_info *uinfo)
2722{
2723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2724 struct hda_bind_ctls *c;
2725 int err;
2726
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002727 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002728 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002729 kcontrol->private_value = *c->values;
2730 err = c->ops->info(kcontrol, uinfo);
2731 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002732 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002733 return err;
2734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002735EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002736
Takashi Iwaid5191e52009-11-16 14:58:17 +01002737/**
2738 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2739 *
2740 * The control element is supposed to have the private_value field
2741 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2742 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002743int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2745{
2746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2747 struct hda_bind_ctls *c;
2748 int err;
2749
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002750 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002751 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002752 kcontrol->private_value = *c->values;
2753 err = c->ops->get(kcontrol, ucontrol);
2754 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002755 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002756 return err;
2757}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002758EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002759
Takashi Iwaid5191e52009-11-16 14:58:17 +01002760/**
2761 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2762 *
2763 * The control element is supposed to have the private_value field
2764 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2765 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002766int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
2768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 struct hda_bind_ctls *c;
2771 unsigned long *vals;
2772 int err = 0, change = 0;
2773
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002774 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002775 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002776 for (vals = c->values; *vals; vals++) {
2777 kcontrol->private_value = *vals;
2778 err = c->ops->put(kcontrol, ucontrol);
2779 if (err < 0)
2780 break;
2781 change |= err;
2782 }
2783 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002784 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002785 return err < 0 ? err : change;
2786}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002787EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002788
Takashi Iwaid5191e52009-11-16 14:58:17 +01002789/**
2790 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2791 *
2792 * The control element is supposed to have the private_value field
2793 * set up via HDA_BIND_VOL() macro.
2794 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002795int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2796 unsigned int size, unsigned int __user *tlv)
2797{
2798 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2799 struct hda_bind_ctls *c;
2800 int err;
2801
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002802 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002803 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002804 kcontrol->private_value = *c->values;
2805 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2806 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002807 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002808 return err;
2809}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002810EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002811
2812struct hda_ctl_ops snd_hda_bind_vol = {
2813 .info = snd_hda_mixer_amp_volume_info,
2814 .get = snd_hda_mixer_amp_volume_get,
2815 .put = snd_hda_mixer_amp_volume_put,
2816 .tlv = snd_hda_mixer_amp_tlv
2817};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002818EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002819
2820struct hda_ctl_ops snd_hda_bind_sw = {
2821 .info = snd_hda_mixer_amp_switch_info,
2822 .get = snd_hda_mixer_amp_switch_get,
2823 .put = snd_hda_mixer_amp_switch_put,
2824 .tlv = snd_hda_mixer_amp_tlv
2825};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002826EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002827
2828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * SPDIF out controls
2830 */
2831
Takashi Iwai0ba21762007-04-16 11:29:14 +02002832static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2836 uinfo->count = 1;
2837 return 0;
2838}
2839
Takashi Iwai0ba21762007-04-16 11:29:14 +02002840static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2844 IEC958_AES0_NONAUDIO |
2845 IEC958_AES0_CON_EMPHASIS_5015 |
2846 IEC958_AES0_CON_NOT_COPYRIGHT;
2847 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2848 IEC958_AES1_CON_ORIGINAL;
2849 return 0;
2850}
2851
Takashi Iwai0ba21762007-04-16 11:29:14 +02002852static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2856 IEC958_AES0_NONAUDIO |
2857 IEC958_AES0_PRO_EMPHASIS_5015;
2858 return 0;
2859}
2860
Takashi Iwai0ba21762007-04-16 11:29:14 +02002861static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002865 int idx = kcontrol->private_value;
2866 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Stephen Warren7c9359762011-06-01 11:14:17 -06002868 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2869 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2870 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2871 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 return 0;
2874}
2875
2876/* convert from SPDIF status bits to HDA SPDIF bits
2877 * bit 0 (DigEn) is always set zero (to be filled later)
2878 */
2879static unsigned short convert_from_spdif_status(unsigned int sbits)
2880{
2881 unsigned short val = 0;
2882
2883 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002884 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2889 IEC958_AES0_PRO_EMPHASIS_5015)
2890 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002892 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2893 IEC958_AES0_CON_EMPHASIS_5015)
2894 val |= AC_DIG1_EMPHASIS;
2895 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2896 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002898 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2900 }
2901 return val;
2902}
2903
2904/* convert to SPDIF status bits from HDA SPDIF bits
2905 */
2906static unsigned int convert_to_spdif_status(unsigned short val)
2907{
2908 unsigned int sbits = 0;
2909
Takashi Iwai0ba21762007-04-16 11:29:14 +02002910 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002912 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 sbits |= IEC958_AES0_PROFESSIONAL;
2914 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002915 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2917 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002918 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002920 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002922 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2924 sbits |= val & (0x7f << 8);
2925 }
2926 return sbits;
2927}
2928
Takashi Iwai2f728532008-09-25 16:32:41 +02002929/* set digital convert verbs both for the given NID and its slaves */
2930static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2931 int verb, int val)
2932{
Takashi Iwaidda14412011-05-02 11:29:30 +02002933 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002934
Takashi Iwai9e976972008-11-25 08:17:20 +01002935 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002936 d = codec->slave_dig_outs;
2937 if (!d)
2938 return;
2939 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002940 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002941}
2942
2943static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2944 int dig1, int dig2)
2945{
2946 if (dig1 != -1)
2947 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2948 if (dig2 != -1)
2949 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2950}
2951
Takashi Iwai0ba21762007-04-16 11:29:14 +02002952static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002956 int idx = kcontrol->private_value;
2957 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2958 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 unsigned short val;
2960 int change;
2961
Ingo Molnar62932df2006-01-16 16:34:20 +01002962 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002963 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2965 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2966 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002967 val = convert_from_spdif_status(spdif->status);
2968 val |= spdif->ctls & 1;
2969 change = spdif->ctls != val;
2970 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002971 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002972 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002973 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return change;
2975}
2976
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002977#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Takashi Iwai0ba21762007-04-16 11:29:14 +02002979static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2980 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002983 int idx = kcontrol->private_value;
2984 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Stephen Warren7c9359762011-06-01 11:14:17 -06002986 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return 0;
2988}
2989
Stephen Warren74b654c2011-06-01 11:14:18 -06002990static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2991 int dig1, int dig2)
2992{
2993 set_dig_out_convert(codec, nid, dig1, dig2);
2994 /* unmute amp switch (if any) */
2995 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2996 (dig1 & AC_DIG1_ENABLE))
2997 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2998 HDA_AMP_MUTE, 0);
2999}
3000
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3002 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003005 int idx = kcontrol->private_value;
3006 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3007 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 unsigned short val;
3009 int change;
3010
Ingo Molnar62932df2006-01-16 16:34:20 +01003011 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06003012 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003014 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003015 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003016 spdif->ctls = val;
3017 if (change && nid != (u16)-1)
3018 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003019 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return change;
3021}
3022
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003023static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 {
3025 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3026 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003027 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 .info = snd_hda_spdif_mask_info,
3029 .get = snd_hda_spdif_cmask_get,
3030 },
3031 {
3032 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003034 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 .info = snd_hda_spdif_mask_info,
3036 .get = snd_hda_spdif_pmask_get,
3037 },
3038 {
3039 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003040 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .info = snd_hda_spdif_mask_info,
3042 .get = snd_hda_spdif_default_get,
3043 .put = snd_hda_spdif_default_put,
3044 },
3045 {
3046 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003047 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 .info = snd_hda_spdif_out_switch_info,
3049 .get = snd_hda_spdif_out_switch_get,
3050 .put = snd_hda_spdif_out_switch_put,
3051 },
3052 { } /* end */
3053};
3054
3055/**
3056 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3057 * @codec: the HDA codec
3058 * @nid: audio out widget NID
3059 *
3060 * Creates controls related with the SPDIF output.
3061 * Called from each patch supporting the SPDIF out.
3062 *
3063 * Returns 0 if successful, or a negative error code.
3064 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003065int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3066 hda_nid_t associated_nid,
3067 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003070 struct snd_kcontrol *kctl;
3071 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003072 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003073 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Takashi Iwai1afe2062010-12-23 10:17:52 +01003075 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3076 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003077 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3078 return -EBUSY;
3079 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003080 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3082 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003083 if (!kctl)
3084 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003085 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003086 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003087 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003088 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return err;
3090 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003091 spdif->nid = cvt_nid;
3092 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003093 AC_VERB_GET_DIGI_CONVERT_1, 0);
3094 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return 0;
3096}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003097EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Stephen Warren7c9359762011-06-01 11:14:17 -06003099struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3100 hda_nid_t nid)
3101{
3102 int i;
3103 for (i = 0; i < codec->spdif_out.used; i++) {
3104 struct hda_spdif_out *spdif =
3105 snd_array_elem(&codec->spdif_out, i);
3106 if (spdif->nid == nid)
3107 return spdif;
3108 }
3109 return NULL;
3110}
3111EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3112
Stephen Warren74b654c2011-06-01 11:14:18 -06003113void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3114{
3115 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3116
3117 mutex_lock(&codec->spdif_mutex);
3118 spdif->nid = (u16)-1;
3119 mutex_unlock(&codec->spdif_mutex);
3120}
3121EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3122
3123void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3124{
3125 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3126 unsigned short val;
3127
3128 mutex_lock(&codec->spdif_mutex);
3129 if (spdif->nid != nid) {
3130 spdif->nid = nid;
3131 val = spdif->ctls;
3132 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3133 }
3134 mutex_unlock(&codec->spdif_mutex);
3135}
3136EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003139 * SPDIF sharing with analog output
3140 */
3141static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_value *ucontrol)
3143{
3144 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3145 ucontrol->value.integer.value[0] = mout->share_spdif;
3146 return 0;
3147}
3148
3149static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3150 struct snd_ctl_elem_value *ucontrol)
3151{
3152 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3153 mout->share_spdif = !!ucontrol->value.integer.value[0];
3154 return 0;
3155}
3156
3157static struct snd_kcontrol_new spdif_share_sw = {
3158 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3159 .name = "IEC958 Default PCM Playback Switch",
3160 .info = snd_ctl_boolean_mono_info,
3161 .get = spdif_share_sw_get,
3162 .put = spdif_share_sw_put,
3163};
3164
Takashi Iwaid5191e52009-11-16 14:58:17 +01003165/**
3166 * snd_hda_create_spdif_share_sw - create Default PCM switch
3167 * @codec: the HDA codec
3168 * @mout: multi-out instance
3169 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003170int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3171 struct hda_multi_out *mout)
3172{
3173 if (!mout->dig_out_nid)
3174 return 0;
3175 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003176 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3177 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003179EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003180
3181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 * SPDIF input
3183 */
3184
3185#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3186
Takashi Iwai0ba21762007-04-16 11:29:14 +02003187static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3188 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3191
3192 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3193 return 0;
3194}
3195
Takashi Iwai0ba21762007-04-16 11:29:14 +02003196static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
3199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3200 hda_nid_t nid = kcontrol->private_value;
3201 unsigned int val = !!ucontrol->value.integer.value[0];
3202 int change;
3203
Ingo Molnar62932df2006-01-16 16:34:20 +01003204 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003206 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003208 snd_hda_codec_write_cache(codec, nid, 0,
3209 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003211 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return change;
3213}
3214
Takashi Iwai0ba21762007-04-16 11:29:14 +02003215static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3216 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3219 hda_nid_t nid = kcontrol->private_value;
3220 unsigned short val;
3221 unsigned int sbits;
3222
Andrew Paprocki3982d172007-12-19 12:13:44 +01003223 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 sbits = convert_to_spdif_status(val);
3225 ucontrol->value.iec958.status[0] = sbits;
3226 ucontrol->value.iec958.status[1] = sbits >> 8;
3227 ucontrol->value.iec958.status[2] = sbits >> 16;
3228 ucontrol->value.iec958.status[3] = sbits >> 24;
3229 return 0;
3230}
3231
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003232static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 {
3234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003235 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 .info = snd_hda_spdif_in_switch_info,
3237 .get = snd_hda_spdif_in_switch_get,
3238 .put = snd_hda_spdif_in_switch_put,
3239 },
3240 {
3241 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3242 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003243 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 .info = snd_hda_spdif_mask_info,
3245 .get = snd_hda_spdif_in_status_get,
3246 },
3247 { } /* end */
3248};
3249
3250/**
3251 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3252 * @codec: the HDA codec
3253 * @nid: audio in widget NID
3254 *
3255 * Creates controls related with the SPDIF input.
3256 * Called from each patch supporting the SPDIF in.
3257 *
3258 * Returns 0 if successful, or a negative error code.
3259 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003260int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003263 struct snd_kcontrol *kctl;
3264 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003265 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Takashi Iwai1afe2062010-12-23 10:17:52 +01003267 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3268 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003269 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3270 return -EBUSY;
3271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3273 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003274 if (!kctl)
3275 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003277 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003278 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return err;
3280 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003281 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003282 snd_hda_codec_read(codec, nid, 0,
3283 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003284 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return 0;
3286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003287EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Takashi Iwai2a439522011-07-26 09:52:50 +02003289#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003290/*
3291 * command cache
3292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003294/* build a 32bit cache key with the widget id and the command parameter */
3295#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3296#define get_cmd_cache_nid(key) ((key) & 0xff)
3297#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3298
3299/**
3300 * snd_hda_codec_write_cache - send a single command with caching
3301 * @codec: the HDA codec
3302 * @nid: NID to send the command
3303 * @direct: direct flag
3304 * @verb: the verb to send
3305 * @parm: the parameter for the verb
3306 *
3307 * Send a single command without waiting for response.
3308 *
3309 * Returns 0 if successful, or a negative error code.
3310 */
3311int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3312 int direct, unsigned int verb, unsigned int parm)
3313{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003314 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3315 struct hda_cache_head *c;
3316 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003317
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003318 if (err < 0)
3319 return err;
3320 /* parm may contain the verb stuff for get/set amp */
3321 verb = verb | (parm >> 8);
3322 parm &= 0xff;
3323 key = build_cmd_cache_key(nid, verb);
3324 mutex_lock(&codec->bus->cmd_mutex);
3325 c = get_alloc_hash(&codec->cmd_cache, key);
3326 if (c)
3327 c->val = parm;
3328 mutex_unlock(&codec->bus->cmd_mutex);
3329 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003330}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003331EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003332
Takashi Iwaid5191e52009-11-16 14:58:17 +01003333/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003334 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3335 * @codec: the HDA codec
3336 * @nid: NID to send the command
3337 * @direct: direct flag
3338 * @verb: the verb to send
3339 * @parm: the parameter for the verb
3340 *
3341 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3342 * command if the parameter is already identical with the cached value.
3343 * If not, it sends the command and refreshes the cache.
3344 *
3345 * Returns 0 if successful, or a negative error code.
3346 */
3347int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3348 int direct, unsigned int verb, unsigned int parm)
3349{
3350 struct hda_cache_head *c;
3351 u32 key;
3352
3353 /* parm may contain the verb stuff for get/set amp */
3354 verb = verb | (parm >> 8);
3355 parm &= 0xff;
3356 key = build_cmd_cache_key(nid, verb);
3357 mutex_lock(&codec->bus->cmd_mutex);
3358 c = get_hash(&codec->cmd_cache, key);
3359 if (c && c->val == parm) {
3360 mutex_unlock(&codec->bus->cmd_mutex);
3361 return 0;
3362 }
3363 mutex_unlock(&codec->bus->cmd_mutex);
3364 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3365}
3366EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3367
3368/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003369 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3370 * @codec: HD-audio codec
3371 *
3372 * Execute all verbs recorded in the command caches to resume.
3373 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003374void snd_hda_codec_resume_cache(struct hda_codec *codec)
3375{
Takashi Iwai603c4012008-07-30 15:01:44 +02003376 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003377 int i;
3378
Takashi Iwai603c4012008-07-30 15:01:44 +02003379 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003380 u32 key = buffer->key;
3381 if (!key)
3382 continue;
3383 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3384 get_cmd_cache_cmd(key), buffer->val);
3385 }
3386}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003387EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003388
3389/**
3390 * snd_hda_sequence_write_cache - sequence writes with caching
3391 * @codec: the HDA codec
3392 * @seq: VERB array to send
3393 *
3394 * Send the commands sequentially from the given array.
3395 * Thte commands are recorded on cache for power-save and resume.
3396 * The array must be terminated with NID=0.
3397 */
3398void snd_hda_sequence_write_cache(struct hda_codec *codec,
3399 const struct hda_verb *seq)
3400{
3401 for (; seq->nid; seq++)
3402 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3403 seq->param);
3404}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003405EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003406#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003407
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003408void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3409 unsigned int power_state,
3410 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003411{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003412 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003413 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003414
Takashi Iwaicb53c622007-08-10 17:21:45 +02003415 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003416 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003417 if (!(wcaps & AC_WCAP_POWER))
3418 continue;
3419 /* don't power down the widget if it controls eapd and
3420 * EAPD_BTLENABLE is set.
3421 */
3422 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3423 get_wcaps_type(wcaps) == AC_WID_PIN &&
3424 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3425 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003426 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003427 if (eapd & 0x02)
3428 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003429 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003430 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3431 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003432 }
3433
Takashi Iwaicb53c622007-08-10 17:21:45 +02003434 if (power_state == AC_PWRST_D0) {
3435 unsigned long end_time;
3436 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003437 /* wait until the codec reachs to D0 */
3438 end_time = jiffies + msecs_to_jiffies(500);
3439 do {
3440 state = snd_hda_codec_read(codec, fg, 0,
3441 AC_VERB_GET_POWER_STATE, 0);
3442 if (state == power_state)
3443 break;
3444 msleep(1);
3445 } while (time_after_eq(end_time, jiffies));
3446 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003447}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003448EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3449
3450/*
3451 * set power state of the codec
3452 */
3453static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3454 unsigned int power_state)
3455{
3456 if (codec->patch_ops.set_power_state) {
3457 codec->patch_ops.set_power_state(codec, fg, power_state);
3458 return;
3459 }
3460
3461 /* this delay seems necessary to avoid click noise at power-down */
3462 if (power_state == AC_PWRST_D3)
3463 msleep(100);
3464 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3465 power_state);
3466 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3467}
Takashi Iwai54d17402005-11-21 16:33:22 +01003468
Takashi Iwai11aeff02008-07-30 15:01:46 +02003469#ifdef CONFIG_SND_HDA_HWDEP
3470/* execute additional init verbs */
3471static void hda_exec_init_verbs(struct hda_codec *codec)
3472{
3473 if (codec->init_verbs.list)
3474 snd_hda_sequence_write(codec, codec->init_verbs.list);
3475}
3476#else
3477static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3478#endif
3479
Takashi Iwai2a439522011-07-26 09:52:50 +02003480#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003481/*
3482 * call suspend and power-down; used both from PM and power-save
3483 */
3484static void hda_call_codec_suspend(struct hda_codec *codec)
3485{
3486 if (codec->patch_ops.suspend)
3487 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003488 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003489 hda_set_power_state(codec,
3490 codec->afg ? codec->afg : codec->mfg,
3491 AC_PWRST_D3);
3492#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003493 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003494 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003495 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003496 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003497 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003498#endif
3499}
3500
3501/*
3502 * kick up codec; used both from PM and power-save
3503 */
3504static void hda_call_codec_resume(struct hda_codec *codec)
3505{
Takashi Iwai7f308302012-05-08 16:52:23 +02003506 /* set as if powered on for avoiding re-entering the resume
3507 * in the resume / power-save sequence
3508 */
3509 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003510 hda_set_power_state(codec,
3511 codec->afg ? codec->afg : codec->mfg,
3512 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003513 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003514 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003515 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003516 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003517 if (codec->patch_ops.resume)
3518 codec->patch_ops.resume(codec);
3519 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003520 if (codec->patch_ops.init)
3521 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003522 snd_hda_codec_resume_amp(codec);
3523 snd_hda_codec_resume_cache(codec);
3524 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003525 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003526}
Takashi Iwai2a439522011-07-26 09:52:50 +02003527#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003528
Takashi Iwai54d17402005-11-21 16:33:22 +01003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530/**
3531 * snd_hda_build_controls - build mixer controls
3532 * @bus: the BUS
3533 *
3534 * Creates mixer controls for each codec included in the bus.
3535 *
3536 * Returns 0 if successful, otherwise a negative error code.
3537 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003538int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003540 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Takashi Iwai0ba21762007-04-16 11:29:14 +02003542 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003543 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003544 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003545 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003546 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003547 err = snd_hda_codec_reset(codec);
3548 if (err < 0) {
3549 printk(KERN_ERR
3550 "hda_codec: cannot revert codec\n");
3551 return err;
3552 }
3553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003555 return 0;
3556}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003557EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003559int snd_hda_codec_build_controls(struct hda_codec *codec)
3560{
3561 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003562 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003563 /* continue to initialize... */
3564 if (codec->patch_ops.init)
3565 err = codec->patch_ops.init(codec);
3566 if (!err && codec->patch_ops.build_controls)
3567 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003568 if (err < 0)
3569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571}
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573/*
3574 * stream formats
3575 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003576struct hda_rate_tbl {
3577 unsigned int hz;
3578 unsigned int alsa_bits;
3579 unsigned int hda_fmt;
3580};
3581
Takashi Iwai92f10b32010-08-03 14:21:00 +02003582/* rate = base * mult / div */
3583#define HDA_RATE(base, mult, div) \
3584 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3585 (((div) - 1) << AC_FMT_DIV_SHIFT))
3586
Takashi Iwaibefdf312005-08-22 13:57:55 +02003587static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003589
3590 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003591 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3592 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3593 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3594 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3595 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3596 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3597 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3598 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3599 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3600 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3601 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003602#define AC_PAR_PCM_RATE_BITS 11
3603 /* up to bits 10, 384kHZ isn't supported properly */
3604
3605 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003606 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003607
Takashi Iwaibefdf312005-08-22 13:57:55 +02003608 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609};
3610
3611/**
3612 * snd_hda_calc_stream_format - calculate format bitset
3613 * @rate: the sample rate
3614 * @channels: the number of channels
3615 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3616 * @maxbps: the max. bps
3617 *
3618 * Calculate the format bitset from the given rate, channels and th PCM format.
3619 *
3620 * Return zero if invalid.
3621 */
3622unsigned int snd_hda_calc_stream_format(unsigned int rate,
3623 unsigned int channels,
3624 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003625 unsigned int maxbps,
3626 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 int i;
3629 unsigned int val = 0;
3630
Takashi Iwaibefdf312005-08-22 13:57:55 +02003631 for (i = 0; rate_bits[i].hz; i++)
3632 if (rate_bits[i].hz == rate) {
3633 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
3635 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003636 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 snd_printdd("invalid rate %d\n", rate);
3638 return 0;
3639 }
3640
3641 if (channels == 0 || channels > 8) {
3642 snd_printdd("invalid channels %d\n", channels);
3643 return 0;
3644 }
3645 val |= channels - 1;
3646
3647 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003648 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003649 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003650 break;
3651 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003652 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 case 20:
3655 case 24:
3656 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003657 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003658 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003660 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003662 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
3664 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003665 snd_printdd("invalid format width %d\n",
3666 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return 0;
3668 }
3669
Anssi Hannula32c168c2010-08-03 13:28:57 +03003670 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003671 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return val;
3674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003675EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003677static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3678{
3679 unsigned int val = 0;
3680 if (nid != codec->afg &&
3681 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3682 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3683 if (!val || val == -1)
3684 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3685 if (!val || val == -1)
3686 return 0;
3687 return val;
3688}
3689
3690static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3691{
3692 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3693 get_pcm_param);
3694}
3695
3696static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3697{
3698 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3699 if (!streams || streams == -1)
3700 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3701 if (!streams || streams == -1)
3702 return 0;
3703 return streams;
3704}
3705
3706static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3707{
3708 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3709 get_stream_param);
3710}
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712/**
3713 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3714 * @codec: the HDA codec
3715 * @nid: NID to query
3716 * @ratesp: the pointer to store the detected rate bitflags
3717 * @formatsp: the pointer to store the detected formats
3718 * @bpsp: the pointer to store the detected format widths
3719 *
3720 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3721 * or @bsps argument is ignored.
3722 *
3723 * Returns 0 if successful, otherwise a negative error code.
3724 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003725int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3727{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003728 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003730 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003731 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 if (ratesp) {
3734 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003735 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003737 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003739 if (rates == 0) {
3740 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3741 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3742 nid, val,
3743 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3744 return -EIO;
3745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 *ratesp = rates;
3747 }
3748
3749 if (formatsp || bpsp) {
3750 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003751 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003753 streams = query_stream_param(codec, nid);
3754 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 bps = 0;
3758 if (streams & AC_SUPFMT_PCM) {
3759 if (val & AC_SUPPCM_BITS_8) {
3760 formats |= SNDRV_PCM_FMTBIT_U8;
3761 bps = 8;
3762 }
3763 if (val & AC_SUPPCM_BITS_16) {
3764 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3765 bps = 16;
3766 }
3767 if (wcaps & AC_WCAP_DIGITAL) {
3768 if (val & AC_SUPPCM_BITS_32)
3769 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3770 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3771 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3772 if (val & AC_SUPPCM_BITS_24)
3773 bps = 24;
3774 else if (val & AC_SUPPCM_BITS_20)
3775 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003776 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3777 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3779 if (val & AC_SUPPCM_BITS_32)
3780 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 else if (val & AC_SUPPCM_BITS_24)
3782 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003783 else if (val & AC_SUPPCM_BITS_20)
3784 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003787 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003789 if (!bps)
3790 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003791 }
3792 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003793 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 /* temporary hack: we have still no proper support
3795 * for the direct AC3 stream...
3796 */
3797 formats |= SNDRV_PCM_FMTBIT_U8;
3798 bps = 8;
3799 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003800 if (formats == 0) {
3801 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3802 "(nid=0x%x, val=0x%x, ovrd=%i, "
3803 "streams=0x%x)\n",
3804 nid, val,
3805 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3806 streams);
3807 return -EIO;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 if (formatsp)
3810 *formatsp = formats;
3811 if (bpsp)
3812 *bpsp = bps;
3813 }
3814
3815 return 0;
3816}
Stephen Warren384a48d2011-06-01 11:14:21 -06003817EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003820 * snd_hda_is_supported_format - Check the validity of the format
3821 * @codec: HD-audio codec
3822 * @nid: NID to check
3823 * @format: the HD-audio format value to check
3824 *
3825 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 *
3827 * Returns 1 if supported, 0 if not.
3828 */
3829int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3830 unsigned int format)
3831{
3832 int i;
3833 unsigned int val = 0, rate, stream;
3834
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003835 val = query_pcm_param(codec, nid);
3836 if (!val)
3837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003840 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003841 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 if (val & (1 << i))
3843 break;
3844 return 0;
3845 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003846 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return 0;
3848
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003849 stream = query_stream_param(codec, nid);
3850 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return 0;
3852
3853 if (stream & AC_SUPFMT_PCM) {
3854 switch (format & 0xf0) {
3855 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003856 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return 0;
3858 break;
3859 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return 0;
3862 break;
3863 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003864 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return 0;
3866 break;
3867 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003868 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return 0;
3870 break;
3871 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003872 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return 0;
3874 break;
3875 default:
3876 return 0;
3877 }
3878 } else {
3879 /* FIXME: check for float32 and AC3? */
3880 }
3881
3882 return 1;
3883}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003884EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886/*
3887 * PCM stuff
3888 */
3889static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3890 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003891 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892{
3893 return 0;
3894}
3895
3896static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3897 struct hda_codec *codec,
3898 unsigned int stream_tag,
3899 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003900 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901{
3902 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3903 return 0;
3904}
3905
3906static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3907 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003908 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
Takashi Iwai888afa12008-03-18 09:57:50 +01003910 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return 0;
3912}
3913
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003914static int set_pcm_default_values(struct hda_codec *codec,
3915 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003917 int err;
3918
Takashi Iwai0ba21762007-04-16 11:29:14 +02003919 /* query support PCM information from the given NID */
3920 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003921 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003922 info->rates ? NULL : &info->rates,
3923 info->formats ? NULL : &info->formats,
3924 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003925 if (err < 0)
3926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
3928 if (info->ops.open == NULL)
3929 info->ops.open = hda_pcm_default_open_close;
3930 if (info->ops.close == NULL)
3931 info->ops.close = hda_pcm_default_open_close;
3932 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003933 if (snd_BUG_ON(!info->nid))
3934 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 info->ops.prepare = hda_pcm_default_prepare;
3936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003938 if (snd_BUG_ON(!info->nid))
3939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 info->ops.cleanup = hda_pcm_default_cleanup;
3941 }
3942 return 0;
3943}
3944
Takashi Iwaieb541332010-08-06 13:48:11 +02003945/*
3946 * codec prepare/cleanup entries
3947 */
3948int snd_hda_codec_prepare(struct hda_codec *codec,
3949 struct hda_pcm_stream *hinfo,
3950 unsigned int stream,
3951 unsigned int format,
3952 struct snd_pcm_substream *substream)
3953{
3954 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003955 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003956 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3957 if (ret >= 0)
3958 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003959 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003960 return ret;
3961}
3962EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3963
3964void snd_hda_codec_cleanup(struct hda_codec *codec,
3965 struct hda_pcm_stream *hinfo,
3966 struct snd_pcm_substream *substream)
3967{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003968 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003969 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003970 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003971}
3972EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3973
Takashi Iwaid5191e52009-11-16 14:58:17 +01003974/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003975const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3976 "Audio", "SPDIF", "HDMI", "Modem"
3977};
3978
Takashi Iwai176d5332008-07-30 15:01:44 +02003979/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003980 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003981 *
3982 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003983 */
3984static int get_empty_pcm_device(struct hda_bus *bus, int type)
3985{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003986 /* audio device indices; not linear to keep compatibility */
3987 static int audio_idx[HDA_PCM_NTYPES][5] = {
3988 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3989 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003990 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003991 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003992 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003993 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003994
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003995 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003996 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3997 return -EINVAL;
3998 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003999
4000 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4001 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4002 return audio_idx[type][i];
4003
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004004 /* non-fixed slots starting from 10 */
4005 for (i = 10; i < 32; i++) {
4006 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4007 return i;
4008 }
4009
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004010 snd_printk(KERN_WARNING "Too many %s devices\n",
4011 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004012 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004013}
4014
4015/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004016 * attach a new PCM stream
4017 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004018static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004019{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004020 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004021 struct hda_pcm_stream *info;
4022 int stream, err;
4023
Takashi Iwaib91f0802008-11-04 08:43:08 +01004024 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004025 return -EINVAL;
4026 for (stream = 0; stream < 2; stream++) {
4027 info = &pcm->stream[stream];
4028 if (info->substreams) {
4029 err = set_pcm_default_values(codec, info);
4030 if (err < 0)
4031 return err;
4032 }
4033 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004034 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004035}
4036
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004037/* assign all PCMs of the given codec */
4038int snd_hda_codec_build_pcms(struct hda_codec *codec)
4039{
4040 unsigned int pcm;
4041 int err;
4042
4043 if (!codec->num_pcms) {
4044 if (!codec->patch_ops.build_pcms)
4045 return 0;
4046 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004047 if (err < 0) {
4048 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004049 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004050 err = snd_hda_codec_reset(codec);
4051 if (err < 0) {
4052 printk(KERN_ERR
4053 "hda_codec: cannot revert codec\n");
4054 return err;
4055 }
4056 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004057 }
4058 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4059 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4060 int dev;
4061
4062 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004063 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004064
4065 if (!cpcm->pcm) {
4066 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4067 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004068 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004069 cpcm->device = dev;
4070 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004071 if (err < 0) {
4072 printk(KERN_ERR "hda_codec: cannot attach "
4073 "PCM stream %d for codec #%d\n",
4074 dev, codec->addr);
4075 continue; /* no fatal error */
4076 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004077 }
4078 }
4079 return 0;
4080}
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082/**
4083 * snd_hda_build_pcms - build PCM information
4084 * @bus: the BUS
4085 *
4086 * Create PCM information for each codec included in the bus.
4087 *
4088 * The build_pcms codec patch is requested to set up codec->num_pcms and
4089 * codec->pcm_info properly. The array is referred by the top-level driver
4090 * to create its PCM instances.
4091 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4092 * callback.
4093 *
4094 * At least, substreams, channels_min and channels_max must be filled for
4095 * each stream. substreams = 0 indicates that the stream doesn't exist.
4096 * When rates and/or formats are zero, the supported values are queried
4097 * from the given nid. The nid is used also by the default ops.prepare
4098 * and ops.cleanup callbacks.
4099 *
4100 * The driver needs to call ops.open in its open callback. Similarly,
4101 * ops.close is supposed to be called in the close callback.
4102 * ops.prepare should be called in the prepare or hw_params callback
4103 * with the proper parameters for set up.
4104 * ops.cleanup should be called in hw_free for clean up of streams.
4105 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004106 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004108int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Takashi Iwai0ba21762007-04-16 11:29:14 +02004112 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004113 int err = snd_hda_codec_build_pcms(codec);
4114 if (err < 0)
4115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117 return 0;
4118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004119EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121/**
4122 * snd_hda_check_board_config - compare the current codec with the config table
4123 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004124 * @num_configs: number of config enums
4125 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 * @tbl: configuration table, terminated by null entries
4127 *
4128 * Compares the modelname or PCI subsystem id of the current codec with the
4129 * given configuration table. If a matching entry is found, returns its
4130 * config value (supposed to be 0 or positive).
4131 *
4132 * If no entries are matching, the function returns a negative value.
4133 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004134int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004135 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004136 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004138 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004139 int i;
4140 for (i = 0; i < num_configs; i++) {
4141 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004142 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004143 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4144 "selected\n", models[i]);
4145 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
4147 }
4148 }
4149
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004150 if (!codec->bus->pci || !tbl)
4151 return -1;
4152
4153 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4154 if (!tbl)
4155 return -1;
4156 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004157#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004158 char tmp[10];
4159 const char *model = NULL;
4160 if (models)
4161 model = models[tbl->value];
4162 if (!model) {
4163 sprintf(tmp, "#%d", tbl->value);
4164 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004166 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4167 "for config %x:%x (%s)\n",
4168 model, tbl->subvendor, tbl->subdevice,
4169 (tbl->name ? tbl->name : "Unknown device"));
4170#endif
4171 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173 return -1;
4174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004175EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004178 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004179 subsystem ID with the
4180 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004181
4182 This is important for Gateway notebooks with SB450 HDA Audio
4183 where the vendor ID of the PCI device is:
4184 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4185 and the vendor/subvendor are found only at the codec.
4186
4187 * @codec: the HDA codec
4188 * @num_configs: number of config enums
4189 * @models: array of model name strings
4190 * @tbl: configuration table, terminated by null entries
4191 *
4192 * Compares the modelname or PCI subsystem id of the current codec with the
4193 * given configuration table. If a matching entry is found, returns its
4194 * config value (supposed to be 0 or positive).
4195 *
4196 * If no entries are matching, the function returns a negative value.
4197 */
4198int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004199 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004200 const struct snd_pci_quirk *tbl)
4201{
4202 const struct snd_pci_quirk *q;
4203
4204 /* Search for codec ID */
4205 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004206 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4207 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4208 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004209 break;
4210 }
4211
4212 if (!q->subvendor)
4213 return -1;
4214
4215 tbl = q;
4216
4217 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004218#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004219 char tmp[10];
4220 const char *model = NULL;
4221 if (models)
4222 model = models[tbl->value];
4223 if (!model) {
4224 sprintf(tmp, "#%d", tbl->value);
4225 model = tmp;
4226 }
4227 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4228 "for config %x:%x (%s)\n",
4229 model, tbl->subvendor, tbl->subdevice,
4230 (tbl->name ? tbl->name : "Unknown device"));
4231#endif
4232 return tbl->value;
4233 }
4234 return -1;
4235}
4236EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4237
4238/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 * snd_hda_add_new_ctls - create controls from the array
4240 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004241 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 *
4243 * This helper function creates and add new controls in the given array.
4244 * The array must be terminated with an empty entry as terminator.
4245 *
4246 * Returns 0 if successful, or a negative error code.
4247 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004248int snd_hda_add_new_ctls(struct hda_codec *codec,
4249 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004251 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004254 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004255 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004256 if (knew->iface == -1) /* skip this codec private value */
4257 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004258 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004259 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004260 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004261 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004262 if (addr > 0)
4263 kctl->id.device = addr;
4264 if (idx > 0)
4265 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004266 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004267 if (!err)
4268 break;
4269 /* try first with another device index corresponding to
4270 * the codec addr; if it still fails (or it's the
4271 * primary codec), then try another control index
4272 */
4273 if (!addr && codec->addr)
4274 addr = codec->addr;
4275 else if (!idx && !knew->index) {
4276 idx = find_empty_mixer_ctl_idx(codec,
4277 knew->name);
4278 if (idx <= 0)
4279 return err;
4280 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004281 return err;
4282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284 return 0;
4285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004286EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Takashi Iwaicb53c622007-08-10 17:21:45 +02004288#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004289static void hda_power_work(struct work_struct *work)
4290{
4291 struct hda_codec *codec =
4292 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004293 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004294
Maxim Levitsky2e492462007-09-03 15:26:57 +02004295 if (!codec->power_on || codec->power_count) {
4296 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004297 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004298 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004299
Takashi Iwaid66fee52011-08-02 15:39:31 +02004300 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004301 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004302 if (bus->ops.pm_notify)
4303 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004304}
4305
4306static void hda_keep_power_on(struct hda_codec *codec)
4307{
4308 codec->power_count++;
4309 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004310 codec->power_jiffies = jiffies;
4311}
4312
Takashi Iwaid5191e52009-11-16 14:58:17 +01004313/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004314void snd_hda_update_power_acct(struct hda_codec *codec)
4315{
4316 unsigned long delta = jiffies - codec->power_jiffies;
4317 if (codec->power_on)
4318 codec->power_on_acct += delta;
4319 else
4320 codec->power_off_acct += delta;
4321 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004322}
4323
Takashi Iwaid5191e52009-11-16 14:58:17 +01004324/**
4325 * snd_hda_power_up - Power-up the codec
4326 * @codec: HD-audio codec
4327 *
4328 * Increment the power-up counter and power up the hardware really when
4329 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004330 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004331void snd_hda_power_up(struct hda_codec *codec)
4332{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004333 struct hda_bus *bus = codec->bus;
4334
Takashi Iwaicb53c622007-08-10 17:21:45 +02004335 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004336 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004337 return;
4338
Takashi Iwaid66fee52011-08-02 15:39:31 +02004339 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004340 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004341 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004342 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004343 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004344 if (bus->ops.pm_notify)
4345 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004346 hda_call_codec_resume(codec);
4347 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004348 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004350EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004351
4352#define power_save(codec) \
4353 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004354
Takashi Iwaid5191e52009-11-16 14:58:17 +01004355/**
4356 * snd_hda_power_down - Power-down the codec
4357 * @codec: HD-audio codec
4358 *
4359 * Decrement the power-up counter and schedules the power-off work if
4360 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004361 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004362void snd_hda_power_down(struct hda_codec *codec)
4363{
4364 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004365 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004366 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004367 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004368 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004369 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004370 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004371 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004373EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004374
Takashi Iwaid5191e52009-11-16 14:58:17 +01004375/**
4376 * snd_hda_check_amp_list_power - Check the amp list and update the power
4377 * @codec: HD-audio codec
4378 * @check: the object containing an AMP list and the status
4379 * @nid: NID to check / update
4380 *
4381 * Check whether the given NID is in the amp list. If it's in the list,
4382 * check the current AMP status, and update the the power-status according
4383 * to the mute status.
4384 *
4385 * This function is supposed to be set or called from the check_power_status
4386 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004387 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004388int snd_hda_check_amp_list_power(struct hda_codec *codec,
4389 struct hda_loopback_check *check,
4390 hda_nid_t nid)
4391{
Takashi Iwai031024e2011-05-02 11:29:30 +02004392 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004393 int ch, v;
4394
4395 if (!check->amplist)
4396 return 0;
4397 for (p = check->amplist; p->nid; p++) {
4398 if (p->nid == nid)
4399 break;
4400 }
4401 if (!p->nid)
4402 return 0; /* nothing changed */
4403
4404 for (p = check->amplist; p->nid; p++) {
4405 for (ch = 0; ch < 2; ch++) {
4406 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4407 p->idx);
4408 if (!(v & HDA_AMP_MUTE) && v > 0) {
4409 if (!check->power_on) {
4410 check->power_on = 1;
4411 snd_hda_power_up(codec);
4412 }
4413 return 1;
4414 }
4415 }
4416 }
4417 if (check->power_on) {
4418 check->power_on = 0;
4419 snd_hda_power_down(codec);
4420 }
4421 return 0;
4422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004423EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004426/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004427 * Channel mode helper
4428 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004429
4430/**
4431 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4432 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004433int snd_hda_ch_mode_info(struct hda_codec *codec,
4434 struct snd_ctl_elem_info *uinfo,
4435 const struct hda_channel_mode *chmode,
4436 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004437{
4438 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4439 uinfo->count = 1;
4440 uinfo->value.enumerated.items = num_chmodes;
4441 if (uinfo->value.enumerated.item >= num_chmodes)
4442 uinfo->value.enumerated.item = num_chmodes - 1;
4443 sprintf(uinfo->value.enumerated.name, "%dch",
4444 chmode[uinfo->value.enumerated.item].channels);
4445 return 0;
4446}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004447EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004448
Takashi Iwaid5191e52009-11-16 14:58:17 +01004449/**
4450 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4451 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004452int snd_hda_ch_mode_get(struct hda_codec *codec,
4453 struct snd_ctl_elem_value *ucontrol,
4454 const struct hda_channel_mode *chmode,
4455 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004456 int max_channels)
4457{
4458 int i;
4459
4460 for (i = 0; i < num_chmodes; i++) {
4461 if (max_channels == chmode[i].channels) {
4462 ucontrol->value.enumerated.item[0] = i;
4463 break;
4464 }
4465 }
4466 return 0;
4467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004468EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004469
Takashi Iwaid5191e52009-11-16 14:58:17 +01004470/**
4471 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4472 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004473int snd_hda_ch_mode_put(struct hda_codec *codec,
4474 struct snd_ctl_elem_value *ucontrol,
4475 const struct hda_channel_mode *chmode,
4476 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004477 int *max_channelsp)
4478{
4479 unsigned int mode;
4480
4481 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004482 if (mode >= num_chmodes)
4483 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004484 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004485 return 0;
4486 /* change the current channel setting */
4487 *max_channelsp = chmode[mode].channels;
4488 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004489 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004490 return 1;
4491}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004492EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494/*
4495 * input MUX helper
4496 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004497
4498/**
4499 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4500 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004501int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4502 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
4504 unsigned int index;
4505
4506 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4507 uinfo->count = 1;
4508 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004509 if (!imux->num_items)
4510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 index = uinfo->value.enumerated.item;
4512 if (index >= imux->num_items)
4513 index = imux->num_items - 1;
4514 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4515 return 0;
4516}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004517EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Takashi Iwaid5191e52009-11-16 14:58:17 +01004519/**
4520 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4521 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004522int snd_hda_input_mux_put(struct hda_codec *codec,
4523 const struct hda_input_mux *imux,
4524 struct snd_ctl_elem_value *ucontrol,
4525 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 unsigned int *cur_val)
4527{
4528 unsigned int idx;
4529
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004530 if (!imux->num_items)
4531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 idx = ucontrol->value.enumerated.item[0];
4533 if (idx >= imux->num_items)
4534 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004535 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004537 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4538 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 *cur_val = idx;
4540 return 1;
4541}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004542EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544
4545/*
4546 * Multi-channel / digital-out PCM helper functions
4547 */
4548
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004549/* setup SPDIF output stream */
4550static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4551 unsigned int stream_tag, unsigned int format)
4552{
Stephen Warren7c9359762011-06-01 11:14:17 -06004553 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4554
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004555 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004556 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004557 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004558 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004559 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004560 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004561 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004562 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004563 for (d = codec->slave_dig_outs; *d; d++)
4564 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4565 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004566 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004567 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004568 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004569 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004570 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004571}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004572
Takashi Iwai2f728532008-09-25 16:32:41 +02004573static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4574{
4575 snd_hda_codec_cleanup_stream(codec, nid);
4576 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004577 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004578 for (d = codec->slave_dig_outs; *d; d++)
4579 snd_hda_codec_cleanup_stream(codec, *d);
4580 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004581}
4582
Takashi Iwaid5191e52009-11-16 14:58:17 +01004583/**
4584 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4585 * @bus: HD-audio bus
4586 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004587void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4588{
4589 struct hda_codec *codec;
4590
4591 if (!bus)
4592 return;
4593 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004594 if (hda_codec_is_power_on(codec) &&
4595 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004596 codec->patch_ops.reboot_notify(codec);
4597 }
4598}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004599EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004600
Takashi Iwaid5191e52009-11-16 14:58:17 +01004601/**
4602 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004604int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4605 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Ingo Molnar62932df2006-01-16 16:34:20 +01004607 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004608 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4609 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004610 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004612 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return 0;
4614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004615EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Takashi Iwaid5191e52009-11-16 14:58:17 +01004617/**
4618 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4619 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004620int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4621 struct hda_multi_out *mout,
4622 unsigned int stream_tag,
4623 unsigned int format,
4624 struct snd_pcm_substream *substream)
4625{
4626 mutex_lock(&codec->spdif_mutex);
4627 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4628 mutex_unlock(&codec->spdif_mutex);
4629 return 0;
4630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004631EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004632
Takashi Iwaid5191e52009-11-16 14:58:17 +01004633/**
4634 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4635 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004636int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4637 struct hda_multi_out *mout)
4638{
4639 mutex_lock(&codec->spdif_mutex);
4640 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4641 mutex_unlock(&codec->spdif_mutex);
4642 return 0;
4643}
4644EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4645
Takashi Iwaid5191e52009-11-16 14:58:17 +01004646/**
4647 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004649int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4650 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Ingo Molnar62932df2006-01-16 16:34:20 +01004652 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004654 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return 0;
4656}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004657EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Takashi Iwaid5191e52009-11-16 14:58:17 +01004659/**
4660 * snd_hda_multi_out_analog_open - open analog outputs
4661 *
4662 * Open analog outputs and set up the hw-constraints.
4663 * If the digital outputs can be opened as slave, open the digital
4664 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004666int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4667 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004668 struct snd_pcm_substream *substream,
4669 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Takashi Iwai9a081602008-02-12 18:37:26 +01004671 struct snd_pcm_runtime *runtime = substream->runtime;
4672 runtime->hw.channels_max = mout->max_channels;
4673 if (mout->dig_out_nid) {
4674 if (!mout->analog_rates) {
4675 mout->analog_rates = hinfo->rates;
4676 mout->analog_formats = hinfo->formats;
4677 mout->analog_maxbps = hinfo->maxbps;
4678 } else {
4679 runtime->hw.rates = mout->analog_rates;
4680 runtime->hw.formats = mout->analog_formats;
4681 hinfo->maxbps = mout->analog_maxbps;
4682 }
4683 if (!mout->spdif_rates) {
4684 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4685 &mout->spdif_rates,
4686 &mout->spdif_formats,
4687 &mout->spdif_maxbps);
4688 }
4689 mutex_lock(&codec->spdif_mutex);
4690 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004691 if ((runtime->hw.rates & mout->spdif_rates) &&
4692 (runtime->hw.formats & mout->spdif_formats)) {
4693 runtime->hw.rates &= mout->spdif_rates;
4694 runtime->hw.formats &= mout->spdif_formats;
4695 if (mout->spdif_maxbps < hinfo->maxbps)
4696 hinfo->maxbps = mout->spdif_maxbps;
4697 } else {
4698 mout->share_spdif = 0;
4699 /* FIXME: need notify? */
4700 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004701 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004702 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4705 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4706}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004707EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Takashi Iwaid5191e52009-11-16 14:58:17 +01004709/**
4710 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4711 *
4712 * Set up the i/o for analog out.
4713 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004715int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4716 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 unsigned int stream_tag,
4718 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004719 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Takashi Iwaidda14412011-05-02 11:29:30 +02004721 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004723 struct hda_spdif_out *spdif =
4724 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 int i;
4726
Ingo Molnar62932df2006-01-16 16:34:20 +01004727 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004728 if (mout->dig_out_nid && mout->share_spdif &&
4729 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004731 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4732 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004733 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004735 setup_dig_out_stream(codec, mout->dig_out_nid,
4736 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 } else {
4738 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004739 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
4741 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004742 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004745 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4746 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004747 if (!mout->no_share_stream &&
4748 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004750 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4751 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004752 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004753 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4754 if (!mout->no_share_stream && mout->hp_out_nid[i])
4755 snd_hda_codec_setup_stream(codec,
4756 mout->hp_out_nid[i],
4757 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004758 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004759 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004760 snd_hda_codec_setup_stream(codec,
4761 mout->extra_out_nid[i],
4762 stream_tag, 0, format);
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* surrounds */
4765 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004766 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004767 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4768 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004769 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004770 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4771 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
4773 return 0;
4774}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004775EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Takashi Iwaid5191e52009-11-16 14:58:17 +01004777/**
4778 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004780int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4781 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Takashi Iwaidda14412011-05-02 11:29:30 +02004783 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 int i;
4785
4786 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004787 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004789 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004790 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4791 if (mout->hp_out_nid[i])
4792 snd_hda_codec_cleanup_stream(codec,
4793 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004794 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4795 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004796 snd_hda_codec_cleanup_stream(codec,
4797 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004798 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004800 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 mout->dig_out_used = 0;
4802 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004803 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return 0;
4805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004806EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Takashi Iwai47408602012-04-20 13:06:53 +02004808/**
4809 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4810 *
4811 * Guess the suitable VREF pin bits to be set as the pin-control value.
4812 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4813 */
4814unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4815{
4816 unsigned int pincap;
4817 unsigned int oldval;
4818 oldval = snd_hda_codec_read(codec, pin, 0,
4819 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4820 pincap = snd_hda_query_pin_caps(codec, pin);
4821 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4822 /* Exception: if the default pin setup is vref50, we give it priority */
4823 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4824 return AC_PINCTL_VREF_80;
4825 else if (pincap & AC_PINCAP_VREF_50)
4826 return AC_PINCTL_VREF_50;
4827 else if (pincap & AC_PINCAP_VREF_100)
4828 return AC_PINCTL_VREF_100;
4829 else if (pincap & AC_PINCAP_VREF_GRD)
4830 return AC_PINCTL_VREF_GRD;
4831 return AC_PINCTL_VREF_HIZ;
4832}
4833EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4834
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004835int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4836 unsigned int val, bool cached)
4837{
4838 if (val) {
4839 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004840 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004841 if (!(cap & AC_PINCAP_OUT))
4842 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4843 else if ((val & AC_PINCTL_HP_EN) &&
4844 !(cap & AC_PINCAP_HP_DRV))
4845 val &= ~AC_PINCTL_HP_EN;
4846 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004847 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004848 if (!(cap & AC_PINCAP_IN))
4849 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4850 }
4851 }
4852 if (cached)
4853 return snd_hda_codec_update_cache(codec, pin, 0,
4854 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4855 else
4856 return snd_hda_codec_write(codec, pin, 0,
4857 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4858}
4859EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4860
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004861/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004862 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004863 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004864
Takashi Iwaidda14412011-05-02 11:29:30 +02004865static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004866{
4867 for (; *list; list++)
4868 if (*list == nid)
4869 return 1;
4870 return 0;
4871}
4872
Steve Longerbeam81937d32007-05-08 15:33:03 +02004873
4874/*
4875 * Sort an associated group of pins according to their sequence numbers.
4876 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004877static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004878 int num_pins)
4879{
4880 int i, j;
4881 short seq;
4882 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004883
Steve Longerbeam81937d32007-05-08 15:33:03 +02004884 for (i = 0; i < num_pins; i++) {
4885 for (j = i + 1; j < num_pins; j++) {
4886 if (sequences[i] > sequences[j]) {
4887 seq = sequences[i];
4888 sequences[i] = sequences[j];
4889 sequences[j] = seq;
4890 nid = pins[i];
4891 pins[i] = pins[j];
4892 pins[j] = nid;
4893 }
4894 }
4895 }
4896}
4897
4898
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004899/* add the found input-pin to the cfg->inputs[] table */
4900static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4901 int type)
4902{
4903 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4904 cfg->inputs[cfg->num_inputs].pin = nid;
4905 cfg->inputs[cfg->num_inputs].type = type;
4906 cfg->num_inputs++;
4907 }
4908}
4909
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004910/* sort inputs in the order of AUTO_PIN_* type */
4911static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4912{
4913 int i, j;
4914
4915 for (i = 0; i < cfg->num_inputs; i++) {
4916 for (j = i + 1; j < cfg->num_inputs; j++) {
4917 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4918 struct auto_pin_cfg_item tmp;
4919 tmp = cfg->inputs[i];
4920 cfg->inputs[i] = cfg->inputs[j];
4921 cfg->inputs[j] = tmp;
4922 }
4923 }
4924 }
4925}
4926
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004927/* Reorder the surround channels
4928 * ALSA sequence is front/surr/clfe/side
4929 * HDA sequence is:
4930 * 4-ch: front/surr => OK as it is
4931 * 6-ch: front/clfe/surr
4932 * 8-ch: front/clfe/rear/side|fc
4933 */
4934static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4935{
4936 hda_nid_t nid;
4937
4938 switch (nums) {
4939 case 3:
4940 case 4:
4941 nid = pins[1];
4942 pins[1] = pins[2];
4943 pins[2] = nid;
4944 break;
4945 }
4946}
4947
Takashi Iwai82bc9552006-03-21 11:24:42 +01004948/*
4949 * Parse all pin widgets and store the useful pin nids to cfg
4950 *
4951 * The number of line-outs or any primary output is stored in line_outs,
4952 * and the corresponding output pins are assigned to line_out_pins[],
4953 * in the order of front, rear, CLFE, side, ...
4954 *
4955 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004956 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004957 * is detected, one of speaker of HP pins is assigned as the primary
4958 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4959 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004960 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004961 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004962 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4963 * respectively.
4964 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004965int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4966 struct auto_pin_cfg *cfg,
4967 const hda_nid_t *ignore_nids,
4968 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004969{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004970 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004971 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004972 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4973 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004974 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004975 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004976
4977 memset(cfg, 0, sizeof(*cfg));
4978
Steve Longerbeam81937d32007-05-08 15:33:03 +02004979 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4980 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004981 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004982 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004983
Takashi Iwai2f451d22011-11-10 12:36:46 +01004984 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004985 end_nid = codec->start_nid + codec->num_nodes;
4986 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004987 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004988 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004989 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004990 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004991
4992 /* read all default configuration for pin complex */
4993 if (wid_type != AC_WID_PIN)
4994 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004995 /* ignore the given nids (e.g. pc-beep returns error) */
4996 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4997 continue;
4998
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004999 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01005000 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
5001 AC_DEFCFG_MISC_NO_PRESENCE))
5002 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02005003 conn = get_defcfg_connect(def_conf);
5004 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005005 continue;
5006 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02005007 dev = get_defcfg_device(def_conf);
5008
5009 /* workaround for buggy BIOS setups */
5010 if (dev == AC_JACK_LINE_OUT) {
5011 if (conn == AC_JACK_PORT_FIXED)
5012 dev = AC_JACK_SPEAKER;
5013 }
5014
5015 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005016 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005017 seq = get_defcfg_sequence(def_conf);
5018 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005019
5020 if (!(wid_caps & AC_WCAP_STEREO))
5021 if (!cfg->mono_out_pin)
5022 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02005023 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005024 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02005025 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005026 assoc_line_out = assoc;
5027 else if (assoc_line_out != assoc)
5028 continue;
5029 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
5030 continue;
5031 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02005032 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005033 cfg->line_outs++;
5034 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01005035 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02005036 seq = get_defcfg_sequence(def_conf);
5037 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005038 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
5039 continue;
5040 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02005041 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01005042 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01005043 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005044 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01005045 seq = get_defcfg_sequence(def_conf);
5046 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005047 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
5048 continue;
5049 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01005050 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005051 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005052 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02005053 case AC_JACK_MIC_IN:
5054 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005055 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02005056 case AC_JACK_LINE_IN:
5057 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005058 break;
5059 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005060 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005061 break;
5062 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005063 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005064 break;
5065 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005066 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005067 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5068 continue;
5069 cfg->dig_out_pins[cfg->dig_outs] = nid;
5070 cfg->dig_out_type[cfg->dig_outs] =
5071 (loc == AC_JACK_LOC_HDMI) ?
5072 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5073 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005074 break;
5075 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005076 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005077 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01005078 if (loc == AC_JACK_LOC_HDMI)
5079 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5080 else
5081 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005082 break;
5083 }
5084 }
5085
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005086 /* FIX-UP:
5087 * If no line-out is defined but multiple HPs are found,
5088 * some of them might be the real line-outs.
5089 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005090 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5091 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005092 int i = 0;
5093 while (i < cfg->hp_outs) {
5094 /* The real HPs should have the sequence 0x0f */
5095 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5096 i++;
5097 continue;
5098 }
5099 /* Move it to the line-out table */
5100 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5101 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5102 cfg->line_outs++;
5103 cfg->hp_outs--;
5104 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5105 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02005106 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005107 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5108 }
Takashi Iwai03642c92010-09-08 15:28:19 +02005109 memset(cfg->hp_pins + cfg->hp_outs, 0,
5110 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01005111 if (!cfg->hp_outs)
5112 cfg->line_out_type = AUTO_PIN_HP_OUT;
5113
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005114 }
5115
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005116 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02005117 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5118 cfg->line_outs);
5119 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5120 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01005121 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5122 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005123
Steve Longerbeam81937d32007-05-08 15:33:03 +02005124 /*
5125 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5126 * as a primary output
5127 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005128 if (!cfg->line_outs &&
5129 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02005130 if (cfg->speaker_outs) {
5131 cfg->line_outs = cfg->speaker_outs;
5132 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5133 sizeof(cfg->speaker_pins));
5134 cfg->speaker_outs = 0;
5135 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5136 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5137 } else if (cfg->hp_outs) {
5138 cfg->line_outs = cfg->hp_outs;
5139 memcpy(cfg->line_out_pins, cfg->hp_pins,
5140 sizeof(cfg->hp_pins));
5141 cfg->hp_outs = 0;
5142 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5143 cfg->line_out_type = AUTO_PIN_HP_OUT;
5144 }
5145 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005146
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02005147 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5148 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5149 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005150
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02005151 sort_autocfg_input_pins(cfg);
5152
Takashi Iwai82bc9552006-03-21 11:24:42 +01005153 /*
5154 * debug prints of the parsed results
5155 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02005156 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 +01005157 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5158 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02005159 cfg->line_out_pins[4],
5160 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5161 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5162 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01005163 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5164 cfg->speaker_outs, cfg->speaker_pins[0],
5165 cfg->speaker_pins[1], cfg->speaker_pins[2],
5166 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005167 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5168 cfg->hp_outs, cfg->hp_pins[0],
5169 cfg->hp_pins[1], cfg->hp_pins[2],
5170 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005171 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005172 if (cfg->dig_outs)
5173 snd_printd(" dig-out=0x%x/0x%x\n",
5174 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005175 snd_printd(" inputs:");
5176 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02005177 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02005178 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005179 cfg->inputs[i].pin);
5180 }
5181 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01005182 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01005183 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005184
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005185 return 0;
5186}
Takashi Iwaid025feb2011-08-23 15:24:39 +02005187EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005188
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005189int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005190{
5191 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005192 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005193 if (conn == AC_JACK_PORT_NONE)
5194 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005195 /* Windows may claim the internal mic to be BOTH, too */
5196 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005197 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005198 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005199 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005200 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005201 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005202 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005203 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005204 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005205 return INPUT_PIN_ATTR_FRONT;
5206 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005207}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005208EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005209
Takashi Iwai990061c2010-09-09 22:08:44 +02005210/**
5211 * hda_get_input_pin_label - Give a label for the given input pin
5212 *
5213 * When check_location is true, the function checks the pin location
5214 * for mic and line-in pins, and set an appropriate prefix like "Front",
5215 * "Rear", "Internal".
5216 */
5217
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005218static const char *hda_get_input_pin_label(struct hda_codec *codec,
5219 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02005220{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005221 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005222 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005223 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5224 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005225 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005226
Takashi Iwai10a20af2010-09-09 16:28:02 +02005227 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005228
5229 switch (get_defcfg_device(def_conf)) {
5230 case AC_JACK_MIC_IN:
5231 if (!check_location)
5232 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005233 attr = snd_hda_get_input_pin_attr(def_conf);
5234 if (!attr)
5235 return "None";
5236 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005237 case AC_JACK_LINE_IN:
5238 if (!check_location)
5239 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005240 attr = snd_hda_get_input_pin_attr(def_conf);
5241 if (!attr)
5242 return "None";
5243 if (attr == INPUT_PIN_ATTR_DOCK)
5244 return "Dock Line";
5245 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005246 case AC_JACK_AUX:
5247 return "Aux";
5248 case AC_JACK_CD:
5249 return "CD";
5250 case AC_JACK_SPDIF_IN:
5251 return "SPDIF In";
5252 case AC_JACK_DIG_OTHER_IN:
5253 return "Digital In";
5254 default:
5255 return "Misc";
5256 }
5257}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005258
Takashi Iwaia1c98512010-09-09 21:36:27 +02005259/* Check whether the location prefix needs to be added to the label.
5260 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5261 * have to put "Front" prefix to each label. In such a case, returns false.
5262 */
5263static int check_mic_location_need(struct hda_codec *codec,
5264 const struct auto_pin_cfg *cfg,
5265 int input)
5266{
5267 unsigned int defc;
5268 int i, attr, attr2;
5269
5270 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005271 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005272 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005273 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005274 return 1;
5275
5276 attr = 0;
5277 for (i = 0; i < cfg->num_inputs; i++) {
5278 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005279 attr2 = snd_hda_get_input_pin_attr(defc);
5280 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005281 if (attr && attr != attr2)
5282 return 1; /* different locations found */
5283 attr = attr2;
5284 }
5285 }
5286 return 0;
5287}
5288
Takashi Iwai990061c2010-09-09 22:08:44 +02005289/**
5290 * hda_get_autocfg_input_label - Get a label for the given input
5291 *
5292 * Get a label for the given input pin defined by the autocfg item.
5293 * Unlike hda_get_input_pin_label(), this function checks all inputs
5294 * defined in autocfg and avoids the redundant mic/line prefix as much as
5295 * possible.
5296 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005297const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5298 const struct auto_pin_cfg *cfg,
5299 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005300{
5301 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005302 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005303
Takashi Iwai10a20af2010-09-09 16:28:02 +02005304 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5305 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5306 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005307 if (has_multiple_pins && type == AUTO_PIN_MIC)
5308 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005309 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5310 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005311}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005312EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5313
Takashi Iwai358b6e62011-11-21 14:34:20 +01005314/* return the position of NID in the list, or -1 if not found */
5315static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5316{
5317 int i;
5318 for (i = 0; i < nums; i++)
5319 if (list[i] == nid)
5320 return i;
5321 return -1;
5322}
5323
Takashi Iwai201e06f2011-11-16 15:33:26 +01005324/* get a unique suffix or an index number */
5325static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5326 int num_pins, int *indexp)
5327{
5328 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005329 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005330 };
5331 int i;
5332
Takashi Iwai358b6e62011-11-21 14:34:20 +01005333 i = find_idx_in_nid_list(nid, pins, num_pins);
5334 if (i < 0)
5335 return NULL;
5336 if (num_pins == 1)
5337 return "";
5338 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5339 if (indexp)
5340 *indexp = i;
5341 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005342 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005343 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005344}
5345
5346static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5347 const struct auto_pin_cfg *cfg,
5348 const char *name, char *label, int maxlen,
5349 int *indexp)
5350{
5351 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5352 int attr = snd_hda_get_input_pin_attr(def_conf);
5353 const char *pfx = "", *sfx = "";
5354
5355 /* handle as a speaker if it's a fixed line-out */
Takashi Iwaie49a3432012-03-01 18:14:41 +01005356 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005357 name = "Speaker";
5358 /* check the location */
5359 switch (attr) {
5360 case INPUT_PIN_ATTR_DOCK:
5361 pfx = "Dock ";
5362 break;
5363 case INPUT_PIN_ATTR_FRONT:
5364 pfx = "Front ";
5365 break;
5366 }
5367 if (cfg) {
5368 /* try to give a unique suffix if needed */
5369 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5370 indexp);
5371 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005372 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5373 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005374 if (!sfx) {
5375 /* don't add channel suffix for Headphone controls */
5376 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5377 cfg->hp_outs);
5378 if (idx >= 0)
5379 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005380 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005381 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005382 }
5383 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5384 return 1;
5385}
5386
Takashi Iwai990061c2010-09-09 22:08:44 +02005387/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005388 * snd_hda_get_pin_label - Get a label for the given I/O pin
5389 *
5390 * Get a label for the given pin. This function works for both input and
5391 * output pins. When @cfg is given as non-NULL, the function tries to get
5392 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005393 *
5394 * This function tries to give a unique label string for the pin as much as
5395 * possible. For example, when the multiple line-outs are present, it adds
5396 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5397 * If no unique name with a suffix is available and @indexp is non-NULL, the
5398 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005399 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005400int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5401 const struct auto_pin_cfg *cfg,
5402 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005403{
5404 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005405 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005406 int i;
5407
Takashi Iwai201e06f2011-11-16 15:33:26 +01005408 if (indexp)
5409 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005410 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005411 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005412
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005413 switch (get_defcfg_device(def_conf)) {
5414 case AC_JACK_LINE_OUT:
Takashi Iwaie49a3432012-03-01 18:14:41 +01005415 return fill_audio_out_name(codec, nid, cfg, "Line Out",
Takashi Iwai201e06f2011-11-16 15:33:26 +01005416 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005417 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005418 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5419 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005420 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005421 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5422 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005423 case AC_JACK_SPDIF_OUT:
5424 case AC_JACK_DIG_OTHER_OUT:
5425 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005426 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005427 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005428 name = "SPDIF";
5429 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005430 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5431 cfg->dig_outs);
5432 if (i >= 0)
5433 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005434 }
5435 break;
5436 default:
5437 if (cfg) {
5438 for (i = 0; i < cfg->num_inputs; i++) {
5439 if (cfg->inputs[i].pin != nid)
5440 continue;
5441 name = hda_get_autocfg_input_label(codec, cfg, i);
5442 if (name)
5443 break;
5444 }
5445 }
5446 if (!name)
5447 name = hda_get_input_pin_label(codec, nid, true);
5448 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005449 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005450 if (!name)
5451 return 0;
5452 strlcpy(label, name, maxlen);
5453 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005454}
5455EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5456
Takashi Iwai990061c2010-09-09 22:08:44 +02005457/**
5458 * snd_hda_add_imux_item - Add an item to input_mux
5459 *
5460 * When the same label is used already in the existing items, the number
5461 * suffix is appended to the label. This label index number is stored
5462 * to type_idx when non-NULL pointer is given.
5463 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005464int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5465 int index, int *type_idx)
5466{
5467 int i, label_idx = 0;
5468 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5469 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5470 return -EINVAL;
5471 }
5472 for (i = 0; i < imux->num_items; i++) {
5473 if (!strncmp(label, imux->items[i].label, strlen(label)))
5474 label_idx++;
5475 }
5476 if (type_idx)
5477 *type_idx = label_idx;
5478 if (label_idx > 0)
5479 snprintf(imux->items[imux->num_items].label,
5480 sizeof(imux->items[imux->num_items].label),
5481 "%s %d", label, label_idx);
5482 else
5483 strlcpy(imux->items[imux->num_items].label, label,
5484 sizeof(imux->items[imux->num_items].label));
5485 imux->items[imux->num_items].index = index;
5486 imux->num_items++;
5487 return 0;
5488}
5489EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005490
Takashi Iwai4a471b72005-12-07 13:56:29 +01005491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492#ifdef CONFIG_PM
5493/*
5494 * power management
5495 */
5496
5497/**
5498 * snd_hda_suspend - suspend the codecs
5499 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 *
5501 * Returns 0 if successful.
5502 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005503int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005505 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Takashi Iwai0ba21762007-04-16 11:29:14 +02005507 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005508 if (hda_codec_is_power_on(codec))
5509 hda_call_codec_suspend(codec);
5510 if (codec->patch_ops.post_suspend)
5511 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 }
5513 return 0;
5514}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005515EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517/**
5518 * snd_hda_resume - resume the codecs
5519 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 *
5521 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005522 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005523 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005524 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 */
5526int snd_hda_resume(struct hda_bus *bus)
5527{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005528 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Takashi Iwai0ba21762007-04-16 11:29:14 +02005530 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005531 if (codec->patch_ops.pre_resume)
5532 codec->patch_ops.pre_resume(codec);
Takashi Iwai7f308302012-05-08 16:52:23 +02005533 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return 0;
5536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005537EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005538#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005539
5540/*
5541 * generic arrays
5542 */
5543
Takashi Iwaid5191e52009-11-16 14:58:17 +01005544/**
5545 * snd_array_new - get a new element from the given array
5546 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005547 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005548 * Get a new element from the given array. If it exceeds the
5549 * pre-allocated array size, re-allocate the array.
5550 *
5551 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005552 */
5553void *snd_array_new(struct snd_array *array)
5554{
5555 if (array->used >= array->alloced) {
5556 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005557 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005558 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005559 void *nlist;
5560 if (snd_BUG_ON(num >= 4096))
5561 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005562 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005563 if (!nlist)
5564 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005565 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005566 array->list = nlist;
5567 array->alloced = num;
5568 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005569 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005571EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005572
Takashi Iwaid5191e52009-11-16 14:58:17 +01005573/**
5574 * snd_array_free - free the given array elements
5575 * @array: the array object
5576 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005577void snd_array_free(struct snd_array *array)
5578{
5579 kfree(array->list);
5580 array->used = 0;
5581 array->alloced = 0;
5582 array->list = NULL;
5583}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005584EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005585
Takashi Iwaid5191e52009-11-16 14:58:17 +01005586/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005587 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5588 * @pcm: PCM caps bits
5589 * @buf: the string buffer to write
5590 * @buflen: the max buffer length
5591 *
5592 * used by hda_proc.c and hda_eld.c
5593 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005594void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5595{
5596 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5597 int i, j;
5598
5599 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5600 if (pcm & (AC_SUPPCM_BITS_8 << i))
5601 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5602
5603 buf[j] = '\0'; /* necessary when j == 0 */
5604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005605EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005606
5607MODULE_DESCRIPTION("HDA codec core");
5608MODULE_LICENSE("GPL");