blob: 28e070ad3daca8f5a97f244ee6dcda7545c24dba [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 Iwaic8b6bf9b2005-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 Iwaib94d3532008-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 Iwaic8b6bf9b2005-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 Iwai354e5502012-06-26 15:00:20 +02001195 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001196 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001197#ifdef CONFIG_SND_HDA_POWER_SAVE
1198 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001199 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001202 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001203 snd_array_free(&codec->nids);
Takashi Iwai354e5502012-06-26 15:00:20 +02001204 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001205 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001206 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 codec->bus->caddr_tbl[codec->addr] = NULL;
1208 if (codec->patch_ops.free)
1209 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001210 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001211 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001212 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001213 kfree(codec->vendor_name);
1214 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001215 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001216 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kfree(codec);
1218}
1219
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001220static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1221 unsigned int power_state);
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/**
1224 * snd_hda_codec_new - create a HDA codec
1225 * @bus: the bus to assign
1226 * @codec_addr: the codec address
1227 * @codecp: the pointer to store the generated codec
1228 *
1229 * Returns 0 if successful, or a negative error code.
1230 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001231int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1232 unsigned int codec_addr,
1233 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001236 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int err;
1238
Takashi Iwaida3cec32008-08-08 17:12:14 +02001239 if (snd_BUG_ON(!bus))
1240 return -EINVAL;
1241 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001245 snd_printk(KERN_ERR "hda_codec: "
1246 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -EBUSY;
1248 }
1249
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001250 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (codec == NULL) {
1252 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1253 return -ENOMEM;
1254 }
1255
1256 codec->bus = bus;
1257 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001258 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001259 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001260 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001261 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001262 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1263 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001264 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001265 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001266 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001267 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001268 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001269 if (codec->bus->modelname) {
1270 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1271 if (!codec->modelname) {
1272 snd_hda_codec_free(codec);
1273 return -ENODEV;
1274 }
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Takashi Iwaicb53c622007-08-10 17:21:45 +02001277#ifdef CONFIG_SND_HDA_POWER_SAVE
1278 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1279 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1280 * the caller has to power down appropriatley after initialization
1281 * phase.
1282 */
1283 hda_keep_power_on(codec);
1284#endif
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_add_tail(&codec->list, &bus->codec_list);
1287 bus->caddr_tbl[codec_addr] = codec;
1288
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001291 if (codec->vendor_id == -1)
1292 /* read again, hopefully the access method was corrected
1293 * in the last read...
1294 */
1295 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001297 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_SUBSYSTEM_ID);
1299 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1300 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001302 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001304 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 err = -ENODEV;
1306 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1310 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = read_pin_defaults(codec);
1315 if (err < 0)
1316 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001319 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001320 codec->subsystem_id =
1321 snd_hda_codec_read(codec, nid, 0,
1322 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001323 }
1324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
1326 hda_set_power_state(codec,
1327 codec->afg ? codec->afg : codec->mfg,
1328 AC_PWRST_D0);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_codec_proc_new(codec);
1331
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333
1334 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1335 codec->subsystem_id, codec->revision_id);
1336 snd_component_add(codec->bus->card, component);
1337
1338 if (codecp)
1339 *codecp = codec;
1340 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001341
1342 error:
1343 snd_hda_codec_free(codec);
1344 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001346EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348/**
1349 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1350 * @codec: the HDA codec
1351 *
1352 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * patch instance.
1354 *
1355 * Returns 0 if successful or a negative error code.
1356 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001357int snd_hda_codec_configure(struct hda_codec *codec)
1358{
1359 int err;
1360
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001361 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001363 err = get_codec_name(codec);
1364 if (err < 0)
1365 return err;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001370 goto patched;
1371 }
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (codec->preset && codec->preset->patch) {
1373 err = codec->preset->patch(codec);
1374 goto patched;
1375 }
1376
1377 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001378 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001379 if (err < 0)
1380 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001381
1382 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 if (!err && codec->patch_ops.unsol_event)
1384 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001385 /* audio codec should override the mixer name */
1386 if (!err && (codec->afg || !*codec->bus->card->mixername))
1387 snprintf(codec->bus->card->mixername,
1388 sizeof(codec->bus->card->mixername),
1389 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001392EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/**
1395 * snd_hda_codec_setup_stream - set up the codec for streaming
1396 * @codec: the CODEC to set up
1397 * @nid: the NID to set up
1398 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1399 * @channel_id: channel id to pass, zero based.
1400 * @format: stream format.
1401 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1403 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int channel_id, int format)
1405{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001406 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001407 struct hda_cvt_setup *p;
1408 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001409 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001410 int i;
1411
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001413 return;
1414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 snd_printdd("hda_codec_setup_stream: "
1416 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001418 p = get_hda_cvt_setup(codec, nid);
1419 if (!p)
1420 return;
1421 /* update the stream-id if changed */
1422 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1423 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1424 newval = (stream_tag << 4) | channel_id;
1425 if (oldval != newval)
1426 snd_hda_codec_write(codec, nid, 0,
1427 AC_VERB_SET_CHANNEL_STREAMID,
1428 newval);
1429 p->stream_tag = stream_tag;
1430 p->channel_id = channel_id;
1431 }
1432 /* update the format-id if changed */
1433 if (p->format_id != format) {
1434 oldval = snd_hda_codec_read(codec, nid, 0,
1435 AC_VERB_GET_STREAM_FORMAT, 0);
1436 if (oldval != format) {
1437 msleep(1);
1438 snd_hda_codec_write(codec, nid, 0,
1439 AC_VERB_SET_STREAM_FORMAT,
1440 format);
1441 }
1442 p->format_id = format;
1443 }
1444 p->active = 1;
1445 p->dirty = 0;
1446
1447 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001448 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001449 list_for_each_entry(c, &codec->bus->codec_list, list) {
1450 for (i = 0; i < c->cvt_setups.used; i++) {
1451 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001452 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001453 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001454 p->dirty = 1;
1455 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001458EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460static void really_cleanup_stream(struct hda_codec *codec,
1461 struct hda_cvt_setup *q);
1462
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001465 * @codec: the CODEC to clean up
1466 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001468 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001469void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1470 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001471{
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 struct hda_cvt_setup *p;
1473
Takashi Iwai888afa12008-03-18 09:57:50 +01001474 if (!nid)
1475 return;
1476
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001477 if (codec->no_sticky_stream)
1478 do_now = 1;
1479
Takashi Iwai888afa12008-03-18 09:57:50 +01001480 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001481 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001482 if (p) {
1483 /* here we just clear the active flag when do_now isn't set;
1484 * actual clean-ups will be done later in
1485 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1486 */
1487 if (do_now)
1488 really_cleanup_stream(codec, p);
1489 else
1490 p->active = 0;
1491 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001492}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001493EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001494
Takashi Iwaieb541332010-08-06 13:48:11 +02001495static void really_cleanup_stream(struct hda_codec *codec,
1496 struct hda_cvt_setup *q)
1497{
1498 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001499 if (q->stream_tag || q->channel_id)
1500 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1501 if (q->format_id)
1502 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1503);
Takashi Iwaieb541332010-08-06 13:48:11 +02001504 memset(q, 0, sizeof(*q));
1505 q->nid = nid;
1506}
1507
1508/* clean up the all conflicting obsolete streams */
1509static void purify_inactive_streams(struct hda_codec *codec)
1510{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001511 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001512 int i;
1513
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001514 list_for_each_entry(c, &codec->bus->codec_list, list) {
1515 for (i = 0; i < c->cvt_setups.used; i++) {
1516 struct hda_cvt_setup *p;
1517 p = snd_array_elem(&c->cvt_setups, i);
1518 if (p->dirty)
1519 really_cleanup_stream(c, p);
1520 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 }
1522}
1523
Takashi Iwai2a439522011-07-26 09:52:50 +02001524#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001525/* clean up all streams; called from suspend */
1526static void hda_cleanup_all_streams(struct hda_codec *codec)
1527{
1528 int i;
1529
1530 for (i = 0; i < codec->cvt_setups.used; i++) {
1531 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1532 if (p->stream_tag)
1533 really_cleanup_stream(codec, p);
1534 }
1535}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001536#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
1539 * amp access functions
1540 */
1541
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001542/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001543#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001544#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001545#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1546#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001548#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001551static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001552 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai01751f52007-08-10 16:59:39 +02001554 memset(cache, 0, sizeof(*cache));
1555 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001556 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001557}
1558
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001559static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001560{
Takashi Iwai603c4012008-07-30 15:01:44 +02001561 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001565static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai01751f52007-08-10 16:59:39 +02001567 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1568 u16 cur = cache->hash[idx];
1569 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001572 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (info->key == key)
1574 return info;
1575 cur = info->next;
1576 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001577 return NULL;
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001580/* query the hash. allocate an entry if not found. */
1581static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1582 u32 key)
1583{
1584 struct hda_cache_head *info = get_hash(cache, key);
1585 if (!info) {
1586 u16 idx, cur;
1587 /* add a new hash entry */
1588 info = snd_array_new(&cache->buf);
1589 if (!info)
1590 return NULL;
1591 cur = snd_array_index(&cache->buf, info);
1592 info->key = key;
1593 info->val = 0;
1594 idx = key % (u16)ARRAY_SIZE(cache->hash);
1595 info->next = cache->hash[idx];
1596 cache->hash[idx] = cur;
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return info;
1599}
1600
Takashi Iwai01751f52007-08-10 16:59:39 +02001601/* query and allocate an amp hash entry */
1602static inline struct hda_amp_info *
1603get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1604{
1605 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1606}
1607
Takashi Iwaid5191e52009-11-16 14:58:17 +01001608/**
1609 * query_amp_caps - query AMP capabilities
1610 * @codec: the HD-auio codec
1611 * @nid: the NID to query
1612 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1613 *
1614 * Query AMP capabilities for the given widget and direction.
1615 * Returns the obtained capability bits.
1616 *
1617 * When cap bits have been already read, this doesn't read again but
1618 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001620u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1625 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001627 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 info->amp_caps = snd_hda_param_read(codec, nid,
1631 direction == HDA_OUTPUT ?
1632 AC_PAR_AMP_OUT_CAP :
1633 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001634 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001635 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 return info->amp_caps;
1638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001639EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Takashi Iwaid5191e52009-11-16 14:58:17 +01001641/**
1642 * snd_hda_override_amp_caps - Override the AMP capabilities
1643 * @codec: the CODEC to clean up
1644 * @nid: the NID to clean up
1645 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1646 * @caps: the capability bits to set
1647 *
1648 * Override the cached AMP caps bits value by the given one.
1649 * This function is useful if the driver needs to adjust the AMP ranges,
1650 * e.g. limit to 0dB, etc.
1651 *
1652 * Returns zero if successful or a negative error code.
1653 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001654int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1655 unsigned int caps)
1656{
1657 struct hda_amp_info *info;
1658
1659 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1660 if (!info)
1661 return -EINVAL;
1662 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001663 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001664 return 0;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001667
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001668static unsigned int
1669query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1670 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001671{
1672 struct hda_amp_info *info;
1673
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001674 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001675 if (!info)
1676 return 0;
1677 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001678 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001679 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001680 }
1681 return info->amp_caps;
1682}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001683
1684static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1685{
1686 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1687}
1688
Takashi Iwaid5191e52009-11-16 14:58:17 +01001689/**
1690 * snd_hda_query_pin_caps - Query PIN capabilities
1691 * @codec: the HD-auio codec
1692 * @nid: the NID to query
1693 *
1694 * Query PIN capabilities for the given widget.
1695 * Returns the obtained capability bits.
1696 *
1697 * When cap bits have been already read, this doesn't read again but
1698 * returns the cached value.
1699 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001700u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1701{
1702 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1703 read_pin_cap);
1704}
Takashi Iwai1327a322009-03-23 13:07:47 +01001705EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1706
Wu Fengguang864f92b2009-11-18 12:38:02 +08001707/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001708 * snd_hda_override_pin_caps - Override the pin capabilities
1709 * @codec: the CODEC
1710 * @nid: the NID to override
1711 * @caps: the capability bits to set
1712 *
1713 * Override the cached PIN capabilitiy bits value by the given one.
1714 *
1715 * Returns zero if successful or a negative error code.
1716 */
1717int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1718 unsigned int caps)
1719{
1720 struct hda_amp_info *info;
1721 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1722 if (!info)
1723 return -ENOMEM;
1724 info->amp_caps = caps;
1725 info->head.val |= INFO_AMP_CAPS;
1726 return 0;
1727}
1728EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001732 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734static unsigned int get_vol_mute(struct hda_codec *codec,
1735 struct hda_amp_info *info, hda_nid_t nid,
1736 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 u32 val, parm;
1739
Takashi Iwai01751f52007-08-10 16:59:39 +02001740 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001741 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1744 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1745 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001746 val = snd_hda_codec_read(codec, nid, 0,
1747 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001749 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001750 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001754 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001756static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001757 hda_nid_t nid, int ch, int direction, int index,
1758 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 u32 parm;
1761
1762 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1763 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1764 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001765 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1766 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1767 ; /* set the zero value as a fake mute */
1768 else
1769 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001771 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Takashi Iwaid5191e52009-11-16 14:58:17 +01001774/**
1775 * snd_hda_codec_amp_read - Read AMP value
1776 * @codec: HD-audio codec
1777 * @nid: NID to read the AMP value
1778 * @ch: channel (left=0 or right=1)
1779 * @direction: #HDA_INPUT or #HDA_OUTPUT
1780 * @index: the index value (only for input direction)
1781 *
1782 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Takashi Iwai834be882006-03-01 14:16:17 +01001784int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1785 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787 struct hda_amp_info *info;
1788 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1789 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001791 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001793EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Takashi Iwaid5191e52009-11-16 14:58:17 +01001795/**
1796 * snd_hda_codec_amp_update - update the AMP value
1797 * @codec: HD-audio codec
1798 * @nid: NID to read the AMP value
1799 * @ch: channel (left=0 or right=1)
1800 * @direction: #HDA_INPUT or #HDA_OUTPUT
1801 * @idx: the index value (only for input direction)
1802 * @mask: bit mask to set
1803 * @val: the bits value to set
1804 *
1805 * Update the AMP value with a bit mask.
1806 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001807 */
Takashi Iwai834be882006-03-01 14:16:17 +01001808int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1809 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001812
Takashi Iwai0ba21762007-04-16 11:29:14 +02001813 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1814 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001816 if (snd_BUG_ON(mask & ~0xff))
1817 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001818 val &= mask;
1819 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001820 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001822 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return 1;
1824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001825EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Takashi Iwaid5191e52009-11-16 14:58:17 +01001827/**
1828 * snd_hda_codec_amp_stereo - update the AMP stereo values
1829 * @codec: HD-audio codec
1830 * @nid: NID to read the AMP value
1831 * @direction: #HDA_INPUT or #HDA_OUTPUT
1832 * @idx: the index value (only for input direction)
1833 * @mask: bit mask to set
1834 * @val: the bits value to set
1835 *
1836 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1837 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001838 */
1839int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1840 int direction, int idx, int mask, int val)
1841{
1842 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001843
1844 if (snd_BUG_ON(mask & ~0xff))
1845 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001846 for (ch = 0; ch < 2; ch++)
1847 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1848 idx, mask, val);
1849 return ret;
1850}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001851EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001852
Takashi Iwai2a439522011-07-26 09:52:50 +02001853#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001854/**
1855 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1856 * @codec: HD-audio codec
1857 *
1858 * Resume the all amp commands from the cache.
1859 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001860void snd_hda_codec_resume_amp(struct hda_codec *codec)
1861{
Takashi Iwai603c4012008-07-30 15:01:44 +02001862 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001863 int i;
1864
Takashi Iwai603c4012008-07-30 15:01:44 +02001865 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001866 u32 key = buffer->head.key;
1867 hda_nid_t nid;
1868 unsigned int idx, dir, ch;
1869 if (!key)
1870 continue;
1871 nid = key & 0xff;
1872 idx = (key >> 16) & 0xff;
1873 dir = (key >> 24) & 0xff;
1874 for (ch = 0; ch < 2; ch++) {
1875 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1876 continue;
1877 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1878 buffer->vol[ch]);
1879 }
1880 }
1881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001882EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001883#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001885static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1886 unsigned int ofs)
1887{
1888 u32 caps = query_amp_caps(codec, nid, dir);
1889 /* get num steps */
1890 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1891 if (ofs < caps)
1892 caps -= ofs;
1893 return caps;
1894}
1895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1898 *
1899 * The control element is supposed to have the private_value field
1900 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1901 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001902int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 u16 nid = get_amp_nid(kcontrol);
1907 u8 chs = get_amp_channels(kcontrol);
1908 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001909 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1912 uinfo->count = chs == 3 ? 2 : 1;
1913 uinfo->value.integer.min = 0;
1914 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1915 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001916 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001917 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1918 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EINVAL;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001923EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001925
1926static inline unsigned int
1927read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1928 int ch, int dir, int idx, unsigned int ofs)
1929{
1930 unsigned int val;
1931 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1932 val &= HDA_AMP_VOLMASK;
1933 if (val >= ofs)
1934 val -= ofs;
1935 else
1936 val = 0;
1937 return val;
1938}
1939
1940static inline int
1941update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1942 int ch, int dir, int idx, unsigned int ofs,
1943 unsigned int val)
1944{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001945 unsigned int maxval;
1946
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001947 if (val > 0)
1948 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001949 /* ofs = 0: raw max value */
1950 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001951 if (val > maxval)
1952 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001953 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1954 HDA_AMP_VOLMASK, val);
1955}
1956
Takashi Iwaid5191e52009-11-16 14:58:17 +01001957/**
1958 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1959 *
1960 * The control element is supposed to have the private_value field
1961 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1962 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001963int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1967 hda_nid_t nid = get_amp_nid(kcontrol);
1968 int chs = get_amp_channels(kcontrol);
1969 int dir = get_amp_direction(kcontrol);
1970 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001971 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 long *valp = ucontrol->value.integer.value;
1973
1974 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001977 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001980EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Takashi Iwaid5191e52009-11-16 14:58:17 +01001982/**
1983 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1984 *
1985 * The control element is supposed to have the private_value field
1986 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001988int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 hda_nid_t nid = get_amp_nid(kcontrol);
1993 int chs = get_amp_channels(kcontrol);
1994 int dir = get_amp_direction(kcontrol);
1995 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001996 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 long *valp = ucontrol->value.integer.value;
1998 int change = 0;
1999
Takashi Iwaicb53c622007-08-10 17:21:45 +02002000 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002001 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002003 valp++;
2004 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002005 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002007 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return change;
2009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002010EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Takashi Iwaid5191e52009-11-16 14:58:17 +01002012/**
2013 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2014 *
2015 * The control element is supposed to have the private_value field
2016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2017 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002018int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2019 unsigned int size, unsigned int __user *_tlv)
2020{
2021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2022 hda_nid_t nid = get_amp_nid(kcontrol);
2023 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002025 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002026 u32 caps, val1, val2;
2027
2028 if (size < 4 * sizeof(unsigned int))
2029 return -ENOMEM;
2030 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002031 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2032 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002033 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002034 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002035 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002036 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002037 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002038 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2039 return -EFAULT;
2040 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2041 return -EFAULT;
2042 if (put_user(val1, _tlv + 2))
2043 return -EFAULT;
2044 if (put_user(val2, _tlv + 3))
2045 return -EFAULT;
2046 return 0;
2047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002048EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
2051 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2052 * @codec: HD-audio codec
2053 * @nid: NID of a reference widget
2054 * @dir: #HDA_INPUT or #HDA_OUTPUT
2055 * @tlv: TLV data to be stored, at least 4 elements
2056 *
2057 * Set (static) TLV data for a virtual master volume using the AMP caps
2058 * obtained from the reference NID.
2059 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002060 */
2061void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2062 unsigned int *tlv)
2063{
2064 u32 caps;
2065 int nums, step;
2066
2067 caps = query_amp_caps(codec, nid, dir);
2068 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2069 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2070 step = (step + 1) * 25;
2071 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2072 tlv[1] = 2 * sizeof(unsigned int);
2073 tlv[2] = -nums * step;
2074 tlv[3] = step;
2075}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002076EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002077
2078/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002079static struct snd_kcontrol *
2080_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2081 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002082{
2083 struct snd_ctl_elem_id id;
2084 memset(&id, 0, sizeof(id));
2085 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002086 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002087 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2088 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002089 strcpy(id.name, name);
2090 return snd_ctl_find_id(codec->bus->card, &id);
2091}
2092
Takashi Iwaid5191e52009-11-16 14:58:17 +01002093/**
2094 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2095 * @codec: HD-audio codec
2096 * @name: ctl id name string
2097 *
2098 * Get the control element with the given id string and IFACE_MIXER.
2099 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002100struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2101 const char *name)
2102{
2103 return _snd_hda_find_mixer_ctl(codec, name, 0);
2104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002105EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002106
Takashi Iwai1afe2062010-12-23 10:17:52 +01002107static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2108{
2109 int idx;
2110 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2111 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2112 return idx;
2113 }
2114 return -EBUSY;
2115}
2116
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002118 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002119 * @codec: HD-audio codec
2120 * @nid: corresponding NID (optional)
2121 * @kctl: the control element to assign
2122 *
2123 * Add the given control element to an array inside the codec instance.
2124 * All control elements belonging to a codec are supposed to be added
2125 * by this function so that a proper clean-up works at the free or
2126 * reconfiguration time.
2127 *
2128 * If non-zero @nid is passed, the NID is assigned to the control element.
2129 * The assignment is shown in the codec proc file.
2130 *
2131 * snd_hda_ctl_add() checks the control subdev id field whether
2132 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002133 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2134 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002135 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002136int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2137 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002138{
2139 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002140 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002141 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002142
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002144 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002145 if (nid == 0)
2146 nid = get_amp_nid_(kctl->private_value);
2147 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002148 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2149 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002150 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002151 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002152 err = snd_ctl_add(codec->bus->card, kctl);
2153 if (err < 0)
2154 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002155 item = snd_array_new(&codec->mixers);
2156 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002157 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002158 item->kctl = kctl;
2159 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002160 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002161 return 0;
2162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002163EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002164
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002166 * snd_hda_add_nid - Assign a NID to a control element
2167 * @codec: HD-audio codec
2168 * @nid: corresponding NID (optional)
2169 * @kctl: the control element to assign
2170 * @index: index to kctl
2171 *
2172 * Add the given control element to an array inside the codec instance.
2173 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2174 * NID:KCTL mapping - for example "Capture Source" selector.
2175 */
2176int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2177 unsigned int index, hda_nid_t nid)
2178{
2179 struct hda_nid_item *item;
2180
2181 if (nid > 0) {
2182 item = snd_array_new(&codec->nids);
2183 if (!item)
2184 return -ENOMEM;
2185 item->kctl = kctl;
2186 item->index = index;
2187 item->nid = nid;
2188 return 0;
2189 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002190 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2191 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002192 return -EINVAL;
2193}
2194EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2195
2196/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2198 * @codec: HD-audio codec
2199 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002200void snd_hda_ctls_clear(struct hda_codec *codec)
2201{
2202 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002203 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002205 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002207 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002208}
2209
Takashi Iwaia65d6292009-02-23 16:57:04 +01002210/* pseudo device locking
2211 * toggle card->shutdown to allow/disallow the device access (as a hack)
2212 */
2213static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002214{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002215 spin_lock(&card->files_lock);
2216 if (card->shutdown) {
2217 spin_unlock(&card->files_lock);
2218 return -EINVAL;
2219 }
2220 card->shutdown = 1;
2221 spin_unlock(&card->files_lock);
2222 return 0;
2223}
2224
2225static void hda_unlock_devices(struct snd_card *card)
2226{
2227 spin_lock(&card->files_lock);
2228 card->shutdown = 0;
2229 spin_unlock(&card->files_lock);
2230}
2231
Takashi Iwaid5191e52009-11-16 14:58:17 +01002232/**
2233 * snd_hda_codec_reset - Clear all objects assigned to the codec
2234 * @codec: HD-audio codec
2235 *
2236 * This frees the all PCM and control elements assigned to the codec, and
2237 * clears the caches and restores the pin default configurations.
2238 *
2239 * When a device is being used, it returns -EBSY. If successfully freed,
2240 * returns zero.
2241 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002242int snd_hda_codec_reset(struct hda_codec *codec)
2243{
2244 struct snd_card *card = codec->bus->card;
2245 int i, pcm;
2246
2247 if (hda_lock_devices(card) < 0)
2248 return -EBUSY;
2249 /* check whether the codec isn't used by any mixer or PCM streams */
2250 if (!list_empty(&card->ctl_files)) {
2251 hda_unlock_devices(card);
2252 return -EBUSY;
2253 }
2254 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2255 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2256 if (!cpcm->pcm)
2257 continue;
2258 if (cpcm->pcm->streams[0].substream_opened ||
2259 cpcm->pcm->streams[1].substream_opened) {
2260 hda_unlock_devices(card);
2261 return -EBUSY;
2262 }
2263 }
2264
2265 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002266
2267#ifdef CONFIG_SND_HDA_POWER_SAVE
2268 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002269 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002270#endif
2271 snd_hda_ctls_clear(codec);
2272 /* relase PCMs */
2273 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002274 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002275 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002276 clear_bit(codec->pcm_info[i].device,
2277 codec->bus->pcm_dev_bits);
2278 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002279 }
2280 if (codec->patch_ops.free)
2281 codec->patch_ops.free(codec);
Takashi Iwai6172ace2012-09-10 09:39:31 +02002282 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002283 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002284 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002285 codec->spec = NULL;
2286 free_hda_cache(&codec->amp_cache);
2287 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002288 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2289 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002290 /* free only driver_pins so that init_pins + user_pins are restored */
2291 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002292 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002293 codec->num_pcms = 0;
2294 codec->pcm_info = NULL;
2295 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002296 codec->slave_dig_outs = NULL;
2297 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002298 module_put(codec->owner);
2299 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002300
2301 /* allow device access again */
2302 hda_unlock_devices(card);
2303 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002304}
2305
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002306typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2307
2308/* apply the function to all matching slave ctls in the mixer list */
2309static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002310 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002311{
2312 struct hda_nid_item *items;
2313 const char * const *s;
2314 int i, err;
2315
2316 items = codec->mixers.list;
2317 for (i = 0; i < codec->mixers.used; i++) {
2318 struct snd_kcontrol *sctl = items[i].kctl;
2319 if (!sctl || !sctl->id.name ||
2320 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2321 continue;
2322 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002323 char tmpname[sizeof(sctl->id.name)];
2324 const char *name = *s;
2325 if (suffix) {
2326 snprintf(tmpname, sizeof(tmpname), "%s %s",
2327 name, suffix);
2328 name = tmpname;
2329 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002330 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002331 err = func(data, sctl);
2332 if (err)
2333 return err;
2334 break;
2335 }
2336 }
2337 }
2338 return 0;
2339}
2340
2341static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2342{
2343 return 1;
2344}
2345
Takashi Iwai18478e82012-03-09 17:51:10 +01002346/* guess the value corresponding to 0dB */
2347static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2348{
2349 int _tlv[4];
2350 const int *tlv = NULL;
2351 int val = -1;
2352
2353 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2354 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2355 mm_segment_t fs = get_fs();
2356 set_fs(get_ds());
2357 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2358 tlv = _tlv;
2359 set_fs(fs);
2360 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2361 tlv = kctl->tlv.p;
2362 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2363 val = -tlv[2] / tlv[3];
2364 return val;
2365}
2366
2367/* call kctl->put with the given value(s) */
2368static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2369{
2370 struct snd_ctl_elem_value *ucontrol;
2371 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2372 if (!ucontrol)
2373 return -ENOMEM;
2374 ucontrol->value.integer.value[0] = val;
2375 ucontrol->value.integer.value[1] = val;
2376 kctl->put(kctl, ucontrol);
2377 kfree(ucontrol);
2378 return 0;
2379}
2380
2381/* initialize the slave volume with 0dB */
2382static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2383{
2384 int offset = get_kctl_0dB_offset(slave);
2385 if (offset > 0)
2386 put_kctl_with_value(slave, offset);
2387 return 0;
2388}
2389
2390/* unmute the slave */
2391static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2392{
2393 return put_kctl_with_value(slave, 1);
2394}
2395
Takashi Iwaid5191e52009-11-16 14:58:17 +01002396/**
2397 * snd_hda_add_vmaster - create a virtual master control and add slaves
2398 * @codec: HD-audio codec
2399 * @name: vmaster control name
2400 * @tlv: TLV data (optional)
2401 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002402 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002403 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002404 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002405 *
2406 * Create a virtual master control with the given name. The TLV data
2407 * must be either NULL or a valid data.
2408 *
2409 * @slaves is a NULL-terminated array of strings, each of which is a
2410 * slave control name. All controls with these names are assigned to
2411 * the new virtual master control.
2412 *
2413 * This function returns zero if successful or a negative error code.
2414 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002415int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002416 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002417 const char *suffix, bool init_slave_vol,
2418 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002419{
2420 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002421 int err;
2422
Takashi Iwai29e58532012-03-12 12:25:03 +01002423 if (ctl_ret)
2424 *ctl_ret = NULL;
2425
Takashi Iwai9322ca52012-02-03 14:28:01 +01002426 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002427 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002428 snd_printdd("No slave found for %s\n", name);
2429 return 0;
2430 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002431 kctl = snd_ctl_make_virtual_master(name, tlv);
2432 if (!kctl)
2433 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002434 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002435 if (err < 0)
2436 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002437
Takashi Iwai9322ca52012-02-03 14:28:01 +01002438 err = map_slaves(codec, slaves, suffix,
2439 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002440 if (err < 0)
2441 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002442
2443 /* init with master mute & zero volume */
2444 put_kctl_with_value(kctl, 0);
2445 if (init_slave_vol)
2446 map_slaves(codec, slaves, suffix,
2447 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2448
Takashi Iwai29e58532012-03-12 12:25:03 +01002449 if (ctl_ret)
2450 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002451 return 0;
2452}
Takashi Iwai18478e82012-03-09 17:51:10 +01002453EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002454
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002455/*
2456 * mute-LED control using vmaster
2457 */
2458static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_info *uinfo)
2460{
2461 static const char * const texts[] = {
2462 "Off", "On", "Follow Master"
2463 };
2464 unsigned int index;
2465
2466 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2467 uinfo->count = 1;
2468 uinfo->value.enumerated.items = 3;
2469 index = uinfo->value.enumerated.item;
2470 if (index >= 3)
2471 index = 2;
2472 strcpy(uinfo->value.enumerated.name, texts[index]);
2473 return 0;
2474}
2475
2476static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
2479 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2480 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2481 return 0;
2482}
2483
2484static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
2487 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2488 unsigned int old_mode = hook->mute_mode;
2489
2490 hook->mute_mode = ucontrol->value.enumerated.item[0];
2491 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2492 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2493 if (old_mode == hook->mute_mode)
2494 return 0;
2495 snd_hda_sync_vmaster_hook(hook);
2496 return 1;
2497}
2498
2499static struct snd_kcontrol_new vmaster_mute_mode = {
2500 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2501 .name = "Mute-LED Mode",
2502 .info = vmaster_mute_mode_info,
2503 .get = vmaster_mute_mode_get,
2504 .put = vmaster_mute_mode_put,
2505};
2506
2507/*
2508 * Add a mute-LED hook with the given vmaster switch kctl
2509 * "Mute-LED Mode" control is automatically created and associated with
2510 * the given hook.
2511 */
2512int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002513 struct hda_vmaster_mute_hook *hook,
2514 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002515{
2516 struct snd_kcontrol *kctl;
2517
2518 if (!hook->hook || !hook->sw_kctl)
2519 return 0;
2520 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2521 hook->codec = codec;
2522 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002523 if (!expose_enum_ctl)
2524 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002525 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2526 if (!kctl)
2527 return -ENOMEM;
2528 return snd_hda_ctl_add(codec, 0, kctl);
2529}
2530EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2531
2532/*
2533 * Call the hook with the current value for synchronization
2534 * Should be called in init callback
2535 */
2536void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2537{
2538 if (!hook->hook || !hook->codec)
2539 return;
2540 switch (hook->mute_mode) {
2541 case HDA_VMUTE_FOLLOW_MASTER:
2542 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2543 break;
2544 default:
2545 hook->hook(hook->codec, hook->mute_mode);
2546 break;
2547 }
2548}
2549EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2550
2551
Takashi Iwaid5191e52009-11-16 14:58:17 +01002552/**
2553 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2554 *
2555 * The control element is supposed to have the private_value field
2556 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2557 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002558int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 int chs = get_amp_channels(kcontrol);
2562
2563 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2564 uinfo->count = chs == 3 ? 2 : 1;
2565 uinfo->value.integer.min = 0;
2566 uinfo->value.integer.max = 1;
2567 return 0;
2568}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002569EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Takashi Iwaid5191e52009-11-16 14:58:17 +01002571/**
2572 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2573 *
2574 * The control element is supposed to have the private_value field
2575 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2576 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002577int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2578 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2581 hda_nid_t nid = get_amp_nid(kcontrol);
2582 int chs = get_amp_channels(kcontrol);
2583 int dir = get_amp_direction(kcontrol);
2584 int idx = get_amp_index(kcontrol);
2585 long *valp = ucontrol->value.integer.value;
2586
2587 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002588 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002589 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002591 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002592 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return 0;
2594}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002595EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Takashi Iwaid5191e52009-11-16 14:58:17 +01002597/**
2598 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2599 *
2600 * The control element is supposed to have the private_value field
2601 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2602 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002603int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2604 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2607 hda_nid_t nid = get_amp_nid(kcontrol);
2608 int chs = get_amp_channels(kcontrol);
2609 int dir = get_amp_direction(kcontrol);
2610 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 long *valp = ucontrol->value.integer.value;
2612 int change = 0;
2613
Takashi Iwaicb53c622007-08-10 17:21:45 +02002614 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002615 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002616 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002617 HDA_AMP_MUTE,
2618 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002619 valp++;
2620 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002621 if (chs & 2)
2622 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002623 HDA_AMP_MUTE,
2624 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002625 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002626 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return change;
2628}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002629EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Takashi Iwai67d634c2009-11-16 15:35:59 +01002631#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002632/**
2633 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2634 *
2635 * This function calls snd_hda_enable_beep_device(), which behaves differently
2636 * depending on beep_mode option.
2637 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002638int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_value *ucontrol)
2640{
2641 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2642 long *valp = ucontrol->value.integer.value;
2643
2644 snd_hda_enable_beep_device(codec, *valp);
2645 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2646}
2647EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002648#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650/*
Takashi Iwai985be542005-11-02 18:26:49 +01002651 * bound volume controls
2652 *
2653 * bind multiple volumes (# indices, from 0)
2654 */
2655
2656#define AMP_VAL_IDX_SHIFT 19
2657#define AMP_VAL_IDX_MASK (0x0f<<19)
2658
Takashi Iwaid5191e52009-11-16 14:58:17 +01002659/**
2660 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2661 *
2662 * The control element is supposed to have the private_value field
2663 * set up via HDA_BIND_MUTE*() macros.
2664 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002665int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002667{
2668 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2669 unsigned long pval;
2670 int err;
2671
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002672 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002673 pval = kcontrol->private_value;
2674 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2675 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2676 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002677 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002678 return err;
2679}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002680EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002681
Takashi Iwaid5191e52009-11-16 14:58:17 +01002682/**
2683 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2684 *
2685 * The control element is supposed to have the private_value field
2686 * set up via HDA_BIND_MUTE*() macros.
2687 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002688int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002690{
2691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692 unsigned long pval;
2693 int i, indices, err = 0, change = 0;
2694
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002695 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002696 pval = kcontrol->private_value;
2697 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2698 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002699 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2700 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002701 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2702 if (err < 0)
2703 break;
2704 change |= err;
2705 }
2706 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002707 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002708 return err < 0 ? err : change;
2709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002710EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002711
Takashi Iwaid5191e52009-11-16 14:58:17 +01002712/**
2713 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2714 *
2715 * The control element is supposed to have the private_value field
2716 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002717 */
2718int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2719 struct snd_ctl_elem_info *uinfo)
2720{
2721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2722 struct hda_bind_ctls *c;
2723 int err;
2724
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002725 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002726 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002727 kcontrol->private_value = *c->values;
2728 err = c->ops->info(kcontrol, uinfo);
2729 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002730 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002731 return err;
2732}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002733EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002734
Takashi Iwaid5191e52009-11-16 14:58:17 +01002735/**
2736 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2737 *
2738 * The control element is supposed to have the private_value field
2739 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2740 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002741int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2743{
2744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2745 struct hda_bind_ctls *c;
2746 int err;
2747
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002748 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002749 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002750 kcontrol->private_value = *c->values;
2751 err = c->ops->get(kcontrol, ucontrol);
2752 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002753 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002754 return err;
2755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002756EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002757
Takashi Iwaid5191e52009-11-16 14:58:17 +01002758/**
2759 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2760 *
2761 * The control element is supposed to have the private_value field
2762 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2763 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002764int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768 struct hda_bind_ctls *c;
2769 unsigned long *vals;
2770 int err = 0, change = 0;
2771
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002772 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002773 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002774 for (vals = c->values; *vals; vals++) {
2775 kcontrol->private_value = *vals;
2776 err = c->ops->put(kcontrol, ucontrol);
2777 if (err < 0)
2778 break;
2779 change |= err;
2780 }
2781 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002782 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002783 return err < 0 ? err : change;
2784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002785EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002786
Takashi Iwaid5191e52009-11-16 14:58:17 +01002787/**
2788 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2789 *
2790 * The control element is supposed to have the private_value field
2791 * set up via HDA_BIND_VOL() macro.
2792 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002793int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2794 unsigned int size, unsigned int __user *tlv)
2795{
2796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2797 struct hda_bind_ctls *c;
2798 int err;
2799
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002800 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002801 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002802 kcontrol->private_value = *c->values;
2803 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2804 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002805 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002806 return err;
2807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002808EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809
2810struct hda_ctl_ops snd_hda_bind_vol = {
2811 .info = snd_hda_mixer_amp_volume_info,
2812 .get = snd_hda_mixer_amp_volume_get,
2813 .put = snd_hda_mixer_amp_volume_put,
2814 .tlv = snd_hda_mixer_amp_tlv
2815};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002816EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002817
2818struct hda_ctl_ops snd_hda_bind_sw = {
2819 .info = snd_hda_mixer_amp_switch_info,
2820 .get = snd_hda_mixer_amp_switch_get,
2821 .put = snd_hda_mixer_amp_switch_put,
2822 .tlv = snd_hda_mixer_amp_tlv
2823};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002824EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002825
2826/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 * SPDIF out controls
2828 */
2829
Takashi Iwai0ba21762007-04-16 11:29:14 +02002830static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2834 uinfo->count = 1;
2835 return 0;
2836}
2837
Takashi Iwai0ba21762007-04-16 11:29:14 +02002838static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2842 IEC958_AES0_NONAUDIO |
2843 IEC958_AES0_CON_EMPHASIS_5015 |
2844 IEC958_AES0_CON_NOT_COPYRIGHT;
2845 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2846 IEC958_AES1_CON_ORIGINAL;
2847 return 0;
2848}
2849
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2854 IEC958_AES0_NONAUDIO |
2855 IEC958_AES0_PRO_EMPHASIS_5015;
2856 return 0;
2857}
2858
Takashi Iwai0ba21762007-04-16 11:29:14 +02002859static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
2862 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002863 int idx = kcontrol->private_value;
2864 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Stephen Warren7c935972011-06-01 11:14:17 -06002866 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2867 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2868 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2869 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 return 0;
2872}
2873
2874/* convert from SPDIF status bits to HDA SPDIF bits
2875 * bit 0 (DigEn) is always set zero (to be filled later)
2876 */
2877static unsigned short convert_from_spdif_status(unsigned int sbits)
2878{
2879 unsigned short val = 0;
2880
2881 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002884 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2887 IEC958_AES0_PRO_EMPHASIS_5015)
2888 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002890 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2891 IEC958_AES0_CON_EMPHASIS_5015)
2892 val |= AC_DIG1_EMPHASIS;
2893 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2894 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002896 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2898 }
2899 return val;
2900}
2901
2902/* convert to SPDIF status bits from HDA SPDIF bits
2903 */
2904static unsigned int convert_to_spdif_status(unsigned short val)
2905{
2906 unsigned int sbits = 0;
2907
Takashi Iwai0ba21762007-04-16 11:29:14 +02002908 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002910 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 sbits |= IEC958_AES0_PROFESSIONAL;
2912 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaie9781452013-03-20 15:42:00 +01002913 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2915 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002918 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002920 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2922 sbits |= val & (0x7f << 8);
2923 }
2924 return sbits;
2925}
2926
Takashi Iwai2f728532008-09-25 16:32:41 +02002927/* set digital convert verbs both for the given NID and its slaves */
2928static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2929 int verb, int val)
2930{
Takashi Iwaidda14412011-05-02 11:29:30 +02002931 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002932
Takashi Iwai9e976972008-11-25 08:17:20 +01002933 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002934 d = codec->slave_dig_outs;
2935 if (!d)
2936 return;
2937 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002938 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002939}
2940
2941static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2942 int dig1, int dig2)
2943{
2944 if (dig1 != -1)
2945 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2946 if (dig2 != -1)
2947 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2948}
2949
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002954 int idx = kcontrol->private_value;
2955 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2956 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 unsigned short val;
2958 int change;
2959
Ingo Molnar62932df2006-01-16 16:34:20 +01002960 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002961 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2963 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2964 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002965 val = convert_from_spdif_status(spdif->status);
2966 val |= spdif->ctls & 1;
2967 change = spdif->ctls != val;
2968 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002969 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002970 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002971 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return change;
2973}
2974
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002975#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Takashi Iwai0ba21762007-04-16 11:29:14 +02002977static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002981 int idx = kcontrol->private_value;
2982 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Stephen Warren7c935972011-06-01 11:14:17 -06002984 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return 0;
2986}
2987
Stephen Warren74b654c2011-06-01 11:14:18 -06002988static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2989 int dig1, int dig2)
2990{
2991 set_dig_out_convert(codec, nid, dig1, dig2);
2992 /* unmute amp switch (if any) */
2993 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2994 (dig1 & AC_DIG1_ENABLE))
2995 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2996 HDA_AMP_MUTE, 0);
2997}
2998
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
3002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003003 int idx = kcontrol->private_value;
3004 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3005 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 unsigned short val;
3007 int change;
3008
Ingo Molnar62932df2006-01-16 16:34:20 +01003009 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06003010 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003012 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003013 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003014 spdif->ctls = val;
3015 if (change && nid != (u16)-1)
3016 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003017 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return change;
3019}
3020
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003021static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 {
3023 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3024 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003025 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 .info = snd_hda_spdif_mask_info,
3027 .get = snd_hda_spdif_cmask_get,
3028 },
3029 {
3030 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3031 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003032 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 .info = snd_hda_spdif_mask_info,
3034 .get = snd_hda_spdif_pmask_get,
3035 },
3036 {
3037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003038 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 .info = snd_hda_spdif_mask_info,
3040 .get = snd_hda_spdif_default_get,
3041 .put = snd_hda_spdif_default_put,
3042 },
3043 {
3044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003045 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 .info = snd_hda_spdif_out_switch_info,
3047 .get = snd_hda_spdif_out_switch_get,
3048 .put = snd_hda_spdif_out_switch_put,
3049 },
3050 { } /* end */
3051};
3052
3053/**
3054 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3055 * @codec: the HDA codec
3056 * @nid: audio out widget NID
3057 *
3058 * Creates controls related with the SPDIF output.
3059 * Called from each patch supporting the SPDIF out.
3060 *
3061 * Returns 0 if successful, or a negative error code.
3062 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003063int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3064 hda_nid_t associated_nid,
3065 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003068 struct snd_kcontrol *kctl;
3069 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003070 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003071 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Takashi Iwai1afe2062010-12-23 10:17:52 +01003073 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3074 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003075 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3076 return -EBUSY;
3077 }
Stephen Warren7c935972011-06-01 11:14:17 -06003078 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3080 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003081 if (!kctl)
3082 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003083 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003084 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003085 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003086 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return err;
3088 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003089 spdif->nid = cvt_nid;
3090 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003091 AC_VERB_GET_DIGI_CONVERT_1, 0);
3092 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 0;
3094}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003095EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Stephen Warren7c935972011-06-01 11:14:17 -06003097struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3098 hda_nid_t nid)
3099{
3100 int i;
3101 for (i = 0; i < codec->spdif_out.used; i++) {
3102 struct hda_spdif_out *spdif =
3103 snd_array_elem(&codec->spdif_out, i);
3104 if (spdif->nid == nid)
3105 return spdif;
3106 }
3107 return NULL;
3108}
3109EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3110
Stephen Warren74b654c2011-06-01 11:14:18 -06003111void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3112{
3113 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3114
3115 mutex_lock(&codec->spdif_mutex);
3116 spdif->nid = (u16)-1;
3117 mutex_unlock(&codec->spdif_mutex);
3118}
3119EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3120
3121void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3122{
3123 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3124 unsigned short val;
3125
3126 mutex_lock(&codec->spdif_mutex);
3127 if (spdif->nid != nid) {
3128 spdif->nid = nid;
3129 val = spdif->ctls;
3130 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3131 }
3132 mutex_unlock(&codec->spdif_mutex);
3133}
3134EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003137 * SPDIF sharing with analog output
3138 */
3139static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3141{
3142 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3143 ucontrol->value.integer.value[0] = mout->share_spdif;
3144 return 0;
3145}
3146
3147static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3148 struct snd_ctl_elem_value *ucontrol)
3149{
3150 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3151 mout->share_spdif = !!ucontrol->value.integer.value[0];
3152 return 0;
3153}
3154
3155static struct snd_kcontrol_new spdif_share_sw = {
3156 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3157 .name = "IEC958 Default PCM Playback Switch",
3158 .info = snd_ctl_boolean_mono_info,
3159 .get = spdif_share_sw_get,
3160 .put = spdif_share_sw_put,
3161};
3162
Takashi Iwaid5191e52009-11-16 14:58:17 +01003163/**
3164 * snd_hda_create_spdif_share_sw - create Default PCM switch
3165 * @codec: the HDA codec
3166 * @mout: multi-out instance
3167 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003168int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3169 struct hda_multi_out *mout)
3170{
3171 if (!mout->dig_out_nid)
3172 return 0;
3173 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003174 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3175 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003177EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003178
3179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 * SPDIF input
3181 */
3182
3183#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3184
Takashi Iwai0ba21762007-04-16 11:29:14 +02003185static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3189
3190 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3191 return 0;
3192}
3193
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3198 hda_nid_t nid = kcontrol->private_value;
3199 unsigned int val = !!ucontrol->value.integer.value[0];
3200 int change;
3201
Ingo Molnar62932df2006-01-16 16:34:20 +01003202 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003204 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003206 snd_hda_codec_write_cache(codec, nid, 0,
3207 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003209 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return change;
3211}
3212
Takashi Iwai0ba21762007-04-16 11:29:14 +02003213static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3217 hda_nid_t nid = kcontrol->private_value;
3218 unsigned short val;
3219 unsigned int sbits;
3220
Andrew Paprocki3982d172007-12-19 12:13:44 +01003221 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 sbits = convert_to_spdif_status(val);
3223 ucontrol->value.iec958.status[0] = sbits;
3224 ucontrol->value.iec958.status[1] = sbits >> 8;
3225 ucontrol->value.iec958.status[2] = sbits >> 16;
3226 ucontrol->value.iec958.status[3] = sbits >> 24;
3227 return 0;
3228}
3229
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003230static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 {
3232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003233 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 .info = snd_hda_spdif_in_switch_info,
3235 .get = snd_hda_spdif_in_switch_get,
3236 .put = snd_hda_spdif_in_switch_put,
3237 },
3238 {
3239 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003241 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 .info = snd_hda_spdif_mask_info,
3243 .get = snd_hda_spdif_in_status_get,
3244 },
3245 { } /* end */
3246};
3247
3248/**
3249 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3250 * @codec: the HDA codec
3251 * @nid: audio in widget NID
3252 *
3253 * Creates controls related with the SPDIF input.
3254 * Called from each patch supporting the SPDIF in.
3255 *
3256 * Returns 0 if successful, or a negative error code.
3257 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003258int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003261 struct snd_kcontrol *kctl;
3262 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003263 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Takashi Iwai1afe2062010-12-23 10:17:52 +01003265 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3266 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003267 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3268 return -EBUSY;
3269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3271 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003272 if (!kctl)
3273 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003275 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003276 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return err;
3278 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003279 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003280 snd_hda_codec_read(codec, nid, 0,
3281 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return 0;
3284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003285EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Takashi Iwai2a439522011-07-26 09:52:50 +02003287#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003288/*
3289 * command cache
3290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003292/* build a 32bit cache key with the widget id and the command parameter */
3293#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3294#define get_cmd_cache_nid(key) ((key) & 0xff)
3295#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3296
3297/**
3298 * snd_hda_codec_write_cache - send a single command with caching
3299 * @codec: the HDA codec
3300 * @nid: NID to send the command
3301 * @direct: direct flag
3302 * @verb: the verb to send
3303 * @parm: the parameter for the verb
3304 *
3305 * Send a single command without waiting for response.
3306 *
3307 * Returns 0 if successful, or a negative error code.
3308 */
3309int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3310 int direct, unsigned int verb, unsigned int parm)
3311{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003312 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3313 struct hda_cache_head *c;
3314 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003315
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003316 if (err < 0)
3317 return err;
3318 /* parm may contain the verb stuff for get/set amp */
3319 verb = verb | (parm >> 8);
3320 parm &= 0xff;
3321 key = build_cmd_cache_key(nid, verb);
3322 mutex_lock(&codec->bus->cmd_mutex);
3323 c = get_alloc_hash(&codec->cmd_cache, key);
3324 if (c)
3325 c->val = parm;
3326 mutex_unlock(&codec->bus->cmd_mutex);
3327 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003329EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003330
Takashi Iwaid5191e52009-11-16 14:58:17 +01003331/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003332 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3333 * @codec: the HDA codec
3334 * @nid: NID to send the command
3335 * @direct: direct flag
3336 * @verb: the verb to send
3337 * @parm: the parameter for the verb
3338 *
3339 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3340 * command if the parameter is already identical with the cached value.
3341 * If not, it sends the command and refreshes the cache.
3342 *
3343 * Returns 0 if successful, or a negative error code.
3344 */
3345int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3346 int direct, unsigned int verb, unsigned int parm)
3347{
3348 struct hda_cache_head *c;
3349 u32 key;
3350
3351 /* parm may contain the verb stuff for get/set amp */
3352 verb = verb | (parm >> 8);
3353 parm &= 0xff;
3354 key = build_cmd_cache_key(nid, verb);
3355 mutex_lock(&codec->bus->cmd_mutex);
3356 c = get_hash(&codec->cmd_cache, key);
3357 if (c && c->val == parm) {
3358 mutex_unlock(&codec->bus->cmd_mutex);
3359 return 0;
3360 }
3361 mutex_unlock(&codec->bus->cmd_mutex);
3362 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3363}
3364EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3365
3366/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003367 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3368 * @codec: HD-audio codec
3369 *
3370 * Execute all verbs recorded in the command caches to resume.
3371 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003372void snd_hda_codec_resume_cache(struct hda_codec *codec)
3373{
Takashi Iwai603c4012008-07-30 15:01:44 +02003374 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003375 int i;
3376
Takashi Iwai603c4012008-07-30 15:01:44 +02003377 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003378 u32 key = buffer->key;
3379 if (!key)
3380 continue;
3381 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3382 get_cmd_cache_cmd(key), buffer->val);
3383 }
3384}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003385EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003386
3387/**
3388 * snd_hda_sequence_write_cache - sequence writes with caching
3389 * @codec: the HDA codec
3390 * @seq: VERB array to send
3391 *
3392 * Send the commands sequentially from the given array.
3393 * Thte commands are recorded on cache for power-save and resume.
3394 * The array must be terminated with NID=0.
3395 */
3396void snd_hda_sequence_write_cache(struct hda_codec *codec,
3397 const struct hda_verb *seq)
3398{
3399 for (; seq->nid; seq++)
3400 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3401 seq->param);
3402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003403EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003404#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003405
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003406void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3407 unsigned int power_state,
3408 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003409{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003410 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003411 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003412
Takashi Iwaicb53c622007-08-10 17:21:45 +02003413 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003414 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003415 if (!(wcaps & AC_WCAP_POWER))
3416 continue;
3417 /* don't power down the widget if it controls eapd and
3418 * EAPD_BTLENABLE is set.
3419 */
3420 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3421 get_wcaps_type(wcaps) == AC_WID_PIN &&
3422 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3423 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003424 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003425 if (eapd & 0x02)
3426 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003427 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003428 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3429 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003430 }
3431
Takashi Iwaicb53c622007-08-10 17:21:45 +02003432 if (power_state == AC_PWRST_D0) {
3433 unsigned long end_time;
3434 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003435 /* wait until the codec reachs to D0 */
3436 end_time = jiffies + msecs_to_jiffies(500);
3437 do {
3438 state = snd_hda_codec_read(codec, fg, 0,
3439 AC_VERB_GET_POWER_STATE, 0);
3440 if (state == power_state)
3441 break;
3442 msleep(1);
3443 } while (time_after_eq(end_time, jiffies));
3444 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003445}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003446EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3447
3448/*
3449 * set power state of the codec
3450 */
3451static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3452 unsigned int power_state)
3453{
3454 if (codec->patch_ops.set_power_state) {
3455 codec->patch_ops.set_power_state(codec, fg, power_state);
3456 return;
3457 }
3458
3459 /* this delay seems necessary to avoid click noise at power-down */
3460 if (power_state == AC_PWRST_D3)
3461 msleep(100);
3462 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3463 power_state);
3464 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3465}
Takashi Iwai54d17402005-11-21 16:33:22 +01003466
Takashi Iwai11aeff02008-07-30 15:01:46 +02003467#ifdef CONFIG_SND_HDA_HWDEP
3468/* execute additional init verbs */
3469static void hda_exec_init_verbs(struct hda_codec *codec)
3470{
3471 if (codec->init_verbs.list)
3472 snd_hda_sequence_write(codec, codec->init_verbs.list);
3473}
3474#else
3475static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3476#endif
3477
Takashi Iwai2a439522011-07-26 09:52:50 +02003478#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003479/*
3480 * call suspend and power-down; used both from PM and power-save
3481 */
3482static void hda_call_codec_suspend(struct hda_codec *codec)
3483{
3484 if (codec->patch_ops.suspend)
3485 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003486 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003487 hda_set_power_state(codec,
3488 codec->afg ? codec->afg : codec->mfg,
3489 AC_PWRST_D3);
3490#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003491 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003492 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003493 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003494 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003495 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003496#endif
3497}
3498
3499/*
3500 * kick up codec; used both from PM and power-save
3501 */
3502static void hda_call_codec_resume(struct hda_codec *codec)
3503{
3504 hda_set_power_state(codec,
3505 codec->afg ? codec->afg : codec->mfg,
3506 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003507 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003508 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003509 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003510 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003511 if (codec->patch_ops.resume)
3512 codec->patch_ops.resume(codec);
3513 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003514 if (codec->patch_ops.init)
3515 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003516 snd_hda_codec_resume_amp(codec);
3517 snd_hda_codec_resume_cache(codec);
3518 }
3519}
Takashi Iwai2a439522011-07-26 09:52:50 +02003520#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003521
Takashi Iwai54d17402005-11-21 16:33:22 +01003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523/**
3524 * snd_hda_build_controls - build mixer controls
3525 * @bus: the BUS
3526 *
3527 * Creates mixer controls for each codec included in the bus.
3528 *
3529 * Returns 0 if successful, otherwise a negative error code.
3530 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003531int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003533 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Takashi Iwai0ba21762007-04-16 11:29:14 +02003535 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003536 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003537 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003538 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003539 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003540 err = snd_hda_codec_reset(codec);
3541 if (err < 0) {
3542 printk(KERN_ERR
3543 "hda_codec: cannot revert codec\n");
3544 return err;
3545 }
3546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003548 return 0;
3549}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003550EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003552int snd_hda_codec_build_controls(struct hda_codec *codec)
3553{
3554 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003555 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003556 /* continue to initialize... */
3557 if (codec->patch_ops.init)
3558 err = codec->patch_ops.init(codec);
3559 if (!err && codec->patch_ops.build_controls)
3560 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003561 if (err < 0)
3562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return 0;
3564}
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566/*
3567 * stream formats
3568 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003569struct hda_rate_tbl {
3570 unsigned int hz;
3571 unsigned int alsa_bits;
3572 unsigned int hda_fmt;
3573};
3574
Takashi Iwai92f10b32010-08-03 14:21:00 +02003575/* rate = base * mult / div */
3576#define HDA_RATE(base, mult, div) \
3577 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3578 (((div) - 1) << AC_FMT_DIV_SHIFT))
3579
Takashi Iwaibefdf312005-08-22 13:57:55 +02003580static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003582
3583 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003584 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3585 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3586 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3587 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3588 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3589 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3590 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3591 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3592 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3593 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3594 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003595#define AC_PAR_PCM_RATE_BITS 11
3596 /* up to bits 10, 384kHZ isn't supported properly */
3597
3598 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003599 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003600
Takashi Iwaibefdf312005-08-22 13:57:55 +02003601 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602};
3603
3604/**
3605 * snd_hda_calc_stream_format - calculate format bitset
3606 * @rate: the sample rate
3607 * @channels: the number of channels
3608 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3609 * @maxbps: the max. bps
3610 *
3611 * Calculate the format bitset from the given rate, channels and th PCM format.
3612 *
3613 * Return zero if invalid.
3614 */
3615unsigned int snd_hda_calc_stream_format(unsigned int rate,
3616 unsigned int channels,
3617 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003618 unsigned int maxbps,
3619 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 int i;
3622 unsigned int val = 0;
3623
Takashi Iwaibefdf312005-08-22 13:57:55 +02003624 for (i = 0; rate_bits[i].hz; i++)
3625 if (rate_bits[i].hz == rate) {
3626 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 break;
3628 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003629 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 snd_printdd("invalid rate %d\n", rate);
3631 return 0;
3632 }
3633
3634 if (channels == 0 || channels > 8) {
3635 snd_printdd("invalid channels %d\n", channels);
3636 return 0;
3637 }
3638 val |= channels - 1;
3639
3640 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003641 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003642 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003643 break;
3644 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003645 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 case 20:
3648 case 24:
3649 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003650 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003651 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003653 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003655 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 break;
3657 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003658 snd_printdd("invalid format width %d\n",
3659 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return 0;
3661 }
3662
Anssi Hannula32c168c2010-08-03 13:28:57 +03003663 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003664 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return val;
3667}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003668EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003670static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3671{
3672 unsigned int val = 0;
3673 if (nid != codec->afg &&
3674 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3675 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3676 if (!val || val == -1)
3677 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3678 if (!val || val == -1)
3679 return 0;
3680 return val;
3681}
3682
3683static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3684{
3685 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3686 get_pcm_param);
3687}
3688
3689static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3690{
3691 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3692 if (!streams || streams == -1)
3693 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3694 if (!streams || streams == -1)
3695 return 0;
3696 return streams;
3697}
3698
3699static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3700{
3701 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3702 get_stream_param);
3703}
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705/**
3706 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3707 * @codec: the HDA codec
3708 * @nid: NID to query
3709 * @ratesp: the pointer to store the detected rate bitflags
3710 * @formatsp: the pointer to store the detected formats
3711 * @bpsp: the pointer to store the detected format widths
3712 *
3713 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3714 * or @bsps argument is ignored.
3715 *
3716 * Returns 0 if successful, otherwise a negative error code.
3717 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003718int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3720{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003721 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003723 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003724 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 if (ratesp) {
3727 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003728 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003730 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003732 if (rates == 0) {
3733 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3734 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3735 nid, val,
3736 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3737 return -EIO;
3738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 *ratesp = rates;
3740 }
3741
3742 if (formatsp || bpsp) {
3743 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003744 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003746 streams = query_stream_param(codec, nid);
3747 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 bps = 0;
3751 if (streams & AC_SUPFMT_PCM) {
3752 if (val & AC_SUPPCM_BITS_8) {
3753 formats |= SNDRV_PCM_FMTBIT_U8;
3754 bps = 8;
3755 }
3756 if (val & AC_SUPPCM_BITS_16) {
3757 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3758 bps = 16;
3759 }
3760 if (wcaps & AC_WCAP_DIGITAL) {
3761 if (val & AC_SUPPCM_BITS_32)
3762 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3763 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3764 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3765 if (val & AC_SUPPCM_BITS_24)
3766 bps = 24;
3767 else if (val & AC_SUPPCM_BITS_20)
3768 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003769 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3770 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3772 if (val & AC_SUPPCM_BITS_32)
3773 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 else if (val & AC_SUPPCM_BITS_24)
3775 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02003776 else if (val & AC_SUPPCM_BITS_20)
3777 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003780 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003782 if (!bps)
3783 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003784 }
3785 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003786 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 /* temporary hack: we have still no proper support
3788 * for the direct AC3 stream...
3789 */
3790 formats |= SNDRV_PCM_FMTBIT_U8;
3791 bps = 8;
3792 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003793 if (formats == 0) {
3794 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3795 "(nid=0x%x, val=0x%x, ovrd=%i, "
3796 "streams=0x%x)\n",
3797 nid, val,
3798 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3799 streams);
3800 return -EIO;
3801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 if (formatsp)
3803 *formatsp = formats;
3804 if (bpsp)
3805 *bpsp = bps;
3806 }
3807
3808 return 0;
3809}
Stephen Warren384a48d2011-06-01 11:14:21 -06003810EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
3812/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003813 * snd_hda_is_supported_format - Check the validity of the format
3814 * @codec: HD-audio codec
3815 * @nid: NID to check
3816 * @format: the HD-audio format value to check
3817 *
3818 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 *
3820 * Returns 1 if supported, 0 if not.
3821 */
3822int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3823 unsigned int format)
3824{
3825 int i;
3826 unsigned int val = 0, rate, stream;
3827
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003828 val = query_pcm_param(codec, nid);
3829 if (!val)
3830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003833 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003834 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (val & (1 << i))
3836 break;
3837 return 0;
3838 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003839 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return 0;
3841
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003842 stream = query_stream_param(codec, nid);
3843 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return 0;
3845
3846 if (stream & AC_SUPFMT_PCM) {
3847 switch (format & 0xf0) {
3848 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003849 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return 0;
3851 break;
3852 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003853 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 return 0;
3855 break;
3856 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003857 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return 0;
3859 break;
3860 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003861 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return 0;
3863 break;
3864 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003865 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 0;
3867 break;
3868 default:
3869 return 0;
3870 }
3871 } else {
3872 /* FIXME: check for float32 and AC3? */
3873 }
3874
3875 return 1;
3876}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003877EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879/*
3880 * PCM stuff
3881 */
3882static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3883 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003884 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
3886 return 0;
3887}
3888
3889static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3890 struct hda_codec *codec,
3891 unsigned int stream_tag,
3892 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003893 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
3895 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3896 return 0;
3897}
3898
3899static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3900 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003901 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Takashi Iwai888afa12008-03-18 09:57:50 +01003903 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return 0;
3905}
3906
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003907static int set_pcm_default_values(struct hda_codec *codec,
3908 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003910 int err;
3911
Takashi Iwai0ba21762007-04-16 11:29:14 +02003912 /* query support PCM information from the given NID */
3913 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003914 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003915 info->rates ? NULL : &info->rates,
3916 info->formats ? NULL : &info->formats,
3917 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003918 if (err < 0)
3919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
3921 if (info->ops.open == NULL)
3922 info->ops.open = hda_pcm_default_open_close;
3923 if (info->ops.close == NULL)
3924 info->ops.close = hda_pcm_default_open_close;
3925 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003926 if (snd_BUG_ON(!info->nid))
3927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 info->ops.prepare = hda_pcm_default_prepare;
3929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003931 if (snd_BUG_ON(!info->nid))
3932 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 info->ops.cleanup = hda_pcm_default_cleanup;
3934 }
3935 return 0;
3936}
3937
Takashi Iwaieb541332010-08-06 13:48:11 +02003938/*
3939 * codec prepare/cleanup entries
3940 */
3941int snd_hda_codec_prepare(struct hda_codec *codec,
3942 struct hda_pcm_stream *hinfo,
3943 unsigned int stream,
3944 unsigned int format,
3945 struct snd_pcm_substream *substream)
3946{
3947 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003948 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003949 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3950 if (ret >= 0)
3951 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003952 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003953 return ret;
3954}
3955EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3956
3957void snd_hda_codec_cleanup(struct hda_codec *codec,
3958 struct hda_pcm_stream *hinfo,
3959 struct snd_pcm_substream *substream)
3960{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003961 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003962 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003963 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003964}
3965EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3966
Takashi Iwaid5191e52009-11-16 14:58:17 +01003967/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003968const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3969 "Audio", "SPDIF", "HDMI", "Modem"
3970};
3971
Takashi Iwai176d5332008-07-30 15:01:44 +02003972/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003973 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003974 *
3975 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003976 */
3977static int get_empty_pcm_device(struct hda_bus *bus, int type)
3978{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003979 /* audio device indices; not linear to keep compatibility */
3980 static int audio_idx[HDA_PCM_NTYPES][5] = {
3981 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3982 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003983 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003984 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003985 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003986 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003987
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003988 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003989 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3990 return -EINVAL;
3991 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003992
3993 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3994 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3995 return audio_idx[type][i];
3996
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003997 /* non-fixed slots starting from 10 */
3998 for (i = 10; i < 32; i++) {
3999 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4000 return i;
4001 }
4002
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004003 snd_printk(KERN_WARNING "Too many %s devices\n",
4004 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004005 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004006}
4007
4008/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004009 * attach a new PCM stream
4010 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004011static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004012{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004013 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004014 struct hda_pcm_stream *info;
4015 int stream, err;
4016
Takashi Iwaib91f0802008-11-04 08:43:08 +01004017 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004018 return -EINVAL;
4019 for (stream = 0; stream < 2; stream++) {
4020 info = &pcm->stream[stream];
4021 if (info->substreams) {
4022 err = set_pcm_default_values(codec, info);
4023 if (err < 0)
4024 return err;
4025 }
4026 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004027 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004028}
4029
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004030/* assign all PCMs of the given codec */
4031int snd_hda_codec_build_pcms(struct hda_codec *codec)
4032{
4033 unsigned int pcm;
4034 int err;
4035
4036 if (!codec->num_pcms) {
4037 if (!codec->patch_ops.build_pcms)
4038 return 0;
4039 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004040 if (err < 0) {
4041 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004042 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004043 err = snd_hda_codec_reset(codec);
4044 if (err < 0) {
4045 printk(KERN_ERR
4046 "hda_codec: cannot revert codec\n");
4047 return err;
4048 }
4049 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004050 }
4051 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4052 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4053 int dev;
4054
4055 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004056 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004057
4058 if (!cpcm->pcm) {
4059 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4060 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004061 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004062 cpcm->device = dev;
4063 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004064 if (err < 0) {
4065 printk(KERN_ERR "hda_codec: cannot attach "
4066 "PCM stream %d for codec #%d\n",
4067 dev, codec->addr);
4068 continue; /* no fatal error */
4069 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004070 }
4071 }
4072 return 0;
4073}
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075/**
4076 * snd_hda_build_pcms - build PCM information
4077 * @bus: the BUS
4078 *
4079 * Create PCM information for each codec included in the bus.
4080 *
4081 * The build_pcms codec patch is requested to set up codec->num_pcms and
4082 * codec->pcm_info properly. The array is referred by the top-level driver
4083 * to create its PCM instances.
4084 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4085 * callback.
4086 *
4087 * At least, substreams, channels_min and channels_max must be filled for
4088 * each stream. substreams = 0 indicates that the stream doesn't exist.
4089 * When rates and/or formats are zero, the supported values are queried
4090 * from the given nid. The nid is used also by the default ops.prepare
4091 * and ops.cleanup callbacks.
4092 *
4093 * The driver needs to call ops.open in its open callback. Similarly,
4094 * ops.close is supposed to be called in the close callback.
4095 * ops.prepare should be called in the prepare or hw_params callback
4096 * with the proper parameters for set up.
4097 * ops.cleanup should be called in hw_free for clean up of streams.
4098 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004099 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004101int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004103 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Takashi Iwai0ba21762007-04-16 11:29:14 +02004105 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004106 int err = snd_hda_codec_build_pcms(codec);
4107 if (err < 0)
4108 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 }
4110 return 0;
4111}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004112EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114/**
4115 * snd_hda_check_board_config - compare the current codec with the config table
4116 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004117 * @num_configs: number of config enums
4118 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 * @tbl: configuration table, terminated by null entries
4120 *
4121 * Compares the modelname or PCI subsystem id of the current codec with the
4122 * given configuration table. If a matching entry is found, returns its
4123 * config value (supposed to be 0 or positive).
4124 *
4125 * If no entries are matching, the function returns a negative value.
4126 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004127int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004128 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004129 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004131 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004132 int i;
4133 for (i = 0; i < num_configs; i++) {
4134 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004135 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004136 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4137 "selected\n", models[i]);
4138 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
4140 }
4141 }
4142
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004143 if (!codec->bus->pci || !tbl)
4144 return -1;
4145
4146 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4147 if (!tbl)
4148 return -1;
4149 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004150#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004151 char tmp[10];
4152 const char *model = NULL;
4153 if (models)
4154 model = models[tbl->value];
4155 if (!model) {
4156 sprintf(tmp, "#%d", tbl->value);
4157 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004159 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4160 "for config %x:%x (%s)\n",
4161 model, tbl->subvendor, tbl->subdevice,
4162 (tbl->name ? tbl->name : "Unknown device"));
4163#endif
4164 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
4166 return -1;
4167}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004168EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004171 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004172 subsystem ID with the
4173 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004174
4175 This is important for Gateway notebooks with SB450 HDA Audio
4176 where the vendor ID of the PCI device is:
4177 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4178 and the vendor/subvendor are found only at the codec.
4179
4180 * @codec: the HDA codec
4181 * @num_configs: number of config enums
4182 * @models: array of model name strings
4183 * @tbl: configuration table, terminated by null entries
4184 *
4185 * Compares the modelname or PCI subsystem id of the current codec with the
4186 * given configuration table. If a matching entry is found, returns its
4187 * config value (supposed to be 0 or positive).
4188 *
4189 * If no entries are matching, the function returns a negative value.
4190 */
4191int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004192 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004193 const struct snd_pci_quirk *tbl)
4194{
4195 const struct snd_pci_quirk *q;
4196
4197 /* Search for codec ID */
4198 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004199 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4200 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4201 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004202 break;
4203 }
4204
4205 if (!q->subvendor)
4206 return -1;
4207
4208 tbl = q;
4209
4210 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004211#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004212 char tmp[10];
4213 const char *model = NULL;
4214 if (models)
4215 model = models[tbl->value];
4216 if (!model) {
4217 sprintf(tmp, "#%d", tbl->value);
4218 model = tmp;
4219 }
4220 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4221 "for config %x:%x (%s)\n",
4222 model, tbl->subvendor, tbl->subdevice,
4223 (tbl->name ? tbl->name : "Unknown device"));
4224#endif
4225 return tbl->value;
4226 }
4227 return -1;
4228}
4229EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4230
4231/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 * snd_hda_add_new_ctls - create controls from the array
4233 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004234 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 *
4236 * This helper function creates and add new controls in the given array.
4237 * The array must be terminated with an empty entry as terminator.
4238 *
4239 * Returns 0 if successful, or a negative error code.
4240 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004241int snd_hda_add_new_ctls(struct hda_codec *codec,
4242 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004244 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004247 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004248 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004249 if (knew->iface == -1) /* skip this codec private value */
4250 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004251 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004252 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004253 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004254 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004255 if (addr > 0)
4256 kctl->id.device = addr;
4257 if (idx > 0)
4258 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004259 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004260 if (!err)
4261 break;
4262 /* try first with another device index corresponding to
4263 * the codec addr; if it still fails (or it's the
4264 * primary codec), then try another control index
4265 */
4266 if (!addr && codec->addr)
4267 addr = codec->addr;
4268 else if (!idx && !knew->index) {
4269 idx = find_empty_mixer_ctl_idx(codec,
4270 knew->name);
4271 if (idx <= 0)
4272 return err;
4273 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004274 return err;
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
4277 return 0;
4278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004279EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Takashi Iwaicb53c622007-08-10 17:21:45 +02004281#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004282static void hda_power_work(struct work_struct *work)
4283{
4284 struct hda_codec *codec =
4285 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004286 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004287
Maxim Levitsky2e492462007-09-03 15:26:57 +02004288 if (!codec->power_on || codec->power_count) {
4289 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004290 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004291 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004292
Takashi Iwaid66fee52011-08-02 15:39:31 +02004293 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004294 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004295 if (bus->ops.pm_notify)
4296 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004297}
4298
4299static void hda_keep_power_on(struct hda_codec *codec)
4300{
4301 codec->power_count++;
4302 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004303 codec->power_jiffies = jiffies;
4304}
4305
Takashi Iwaid5191e52009-11-16 14:58:17 +01004306/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004307void snd_hda_update_power_acct(struct hda_codec *codec)
4308{
4309 unsigned long delta = jiffies - codec->power_jiffies;
4310 if (codec->power_on)
4311 codec->power_on_acct += delta;
4312 else
4313 codec->power_off_acct += delta;
4314 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004315}
4316
Takashi Iwaid5191e52009-11-16 14:58:17 +01004317/**
4318 * snd_hda_power_up - Power-up the codec
4319 * @codec: HD-audio codec
4320 *
4321 * Increment the power-up counter and power up the hardware really when
4322 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004323 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004324void snd_hda_power_up(struct hda_codec *codec)
4325{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004326 struct hda_bus *bus = codec->bus;
4327
Takashi Iwaicb53c622007-08-10 17:21:45 +02004328 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004329 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004330 return;
4331
Takashi Iwaid66fee52011-08-02 15:39:31 +02004332 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004333 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004334 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004335 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004336 if (bus->ops.pm_notify)
4337 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004338 hda_call_codec_resume(codec);
4339 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004340 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004342EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004343
4344#define power_save(codec) \
4345 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004346
Takashi Iwaid5191e52009-11-16 14:58:17 +01004347/**
4348 * snd_hda_power_down - Power-down the codec
4349 * @codec: HD-audio codec
4350 *
4351 * Decrement the power-up counter and schedules the power-off work if
4352 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004353 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004354void snd_hda_power_down(struct hda_codec *codec)
4355{
4356 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004357 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004358 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004359 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004360 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004361 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004362 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004363 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004364}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004365EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004366
Takashi Iwaid5191e52009-11-16 14:58:17 +01004367/**
4368 * snd_hda_check_amp_list_power - Check the amp list and update the power
4369 * @codec: HD-audio codec
4370 * @check: the object containing an AMP list and the status
4371 * @nid: NID to check / update
4372 *
4373 * Check whether the given NID is in the amp list. If it's in the list,
4374 * check the current AMP status, and update the the power-status according
4375 * to the mute status.
4376 *
4377 * This function is supposed to be set or called from the check_power_status
4378 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004379 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004380int snd_hda_check_amp_list_power(struct hda_codec *codec,
4381 struct hda_loopback_check *check,
4382 hda_nid_t nid)
4383{
Takashi Iwai031024e2011-05-02 11:29:30 +02004384 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004385 int ch, v;
4386
4387 if (!check->amplist)
4388 return 0;
4389 for (p = check->amplist; p->nid; p++) {
4390 if (p->nid == nid)
4391 break;
4392 }
4393 if (!p->nid)
4394 return 0; /* nothing changed */
4395
4396 for (p = check->amplist; p->nid; p++) {
4397 for (ch = 0; ch < 2; ch++) {
4398 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4399 p->idx);
4400 if (!(v & HDA_AMP_MUTE) && v > 0) {
4401 if (!check->power_on) {
4402 check->power_on = 1;
4403 snd_hda_power_up(codec);
4404 }
4405 return 1;
4406 }
4407 }
4408 }
4409 if (check->power_on) {
4410 check->power_on = 0;
4411 snd_hda_power_down(codec);
4412 }
4413 return 0;
4414}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004415EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004416#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004418/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004419 * Channel mode helper
4420 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004421
4422/**
4423 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4424 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004425int snd_hda_ch_mode_info(struct hda_codec *codec,
4426 struct snd_ctl_elem_info *uinfo,
4427 const struct hda_channel_mode *chmode,
4428 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004429{
4430 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4431 uinfo->count = 1;
4432 uinfo->value.enumerated.items = num_chmodes;
4433 if (uinfo->value.enumerated.item >= num_chmodes)
4434 uinfo->value.enumerated.item = num_chmodes - 1;
4435 sprintf(uinfo->value.enumerated.name, "%dch",
4436 chmode[uinfo->value.enumerated.item].channels);
4437 return 0;
4438}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004439EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004440
Takashi Iwaid5191e52009-11-16 14:58:17 +01004441/**
4442 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4443 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004444int snd_hda_ch_mode_get(struct hda_codec *codec,
4445 struct snd_ctl_elem_value *ucontrol,
4446 const struct hda_channel_mode *chmode,
4447 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004448 int max_channels)
4449{
4450 int i;
4451
4452 for (i = 0; i < num_chmodes; i++) {
4453 if (max_channels == chmode[i].channels) {
4454 ucontrol->value.enumerated.item[0] = i;
4455 break;
4456 }
4457 }
4458 return 0;
4459}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004460EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004461
Takashi Iwaid5191e52009-11-16 14:58:17 +01004462/**
4463 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4464 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004465int snd_hda_ch_mode_put(struct hda_codec *codec,
4466 struct snd_ctl_elem_value *ucontrol,
4467 const struct hda_channel_mode *chmode,
4468 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004469 int *max_channelsp)
4470{
4471 unsigned int mode;
4472
4473 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004474 if (mode >= num_chmodes)
4475 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004476 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004477 return 0;
4478 /* change the current channel setting */
4479 *max_channelsp = chmode[mode].channels;
4480 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004481 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004482 return 1;
4483}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004484EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486/*
4487 * input MUX helper
4488 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004489
4490/**
4491 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4492 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004493int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4494 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
4496 unsigned int index;
4497
4498 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4499 uinfo->count = 1;
4500 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004501 if (!imux->num_items)
4502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 index = uinfo->value.enumerated.item;
4504 if (index >= imux->num_items)
4505 index = imux->num_items - 1;
4506 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4507 return 0;
4508}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004509EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Takashi Iwaid5191e52009-11-16 14:58:17 +01004511/**
4512 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4513 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004514int snd_hda_input_mux_put(struct hda_codec *codec,
4515 const struct hda_input_mux *imux,
4516 struct snd_ctl_elem_value *ucontrol,
4517 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 unsigned int *cur_val)
4519{
4520 unsigned int idx;
4521
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004522 if (!imux->num_items)
4523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 idx = ucontrol->value.enumerated.item[0];
4525 if (idx >= imux->num_items)
4526 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004527 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004529 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4530 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 *cur_val = idx;
4532 return 1;
4533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004534EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536
4537/*
4538 * Multi-channel / digital-out PCM helper functions
4539 */
4540
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004541/* setup SPDIF output stream */
4542static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4543 unsigned int stream_tag, unsigned int format)
4544{
Stephen Warren7c935972011-06-01 11:14:17 -06004545 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4546
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004547 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004548 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004549 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004550 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004551 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004552 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004553 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004554 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004555 for (d = codec->slave_dig_outs; *d; d++)
4556 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4557 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004558 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004559 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004560 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004561 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004562 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004563}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004564
Takashi Iwai2f728532008-09-25 16:32:41 +02004565static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4566{
4567 snd_hda_codec_cleanup_stream(codec, nid);
4568 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004569 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004570 for (d = codec->slave_dig_outs; *d; d++)
4571 snd_hda_codec_cleanup_stream(codec, *d);
4572 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004573}
4574
Takashi Iwaid5191e52009-11-16 14:58:17 +01004575/**
4576 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4577 * @bus: HD-audio bus
4578 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004579void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4580{
4581 struct hda_codec *codec;
4582
4583 if (!bus)
4584 return;
4585 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004586 if (hda_codec_is_power_on(codec) &&
4587 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004588 codec->patch_ops.reboot_notify(codec);
4589 }
4590}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004591EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004592
Takashi Iwaid5191e52009-11-16 14:58:17 +01004593/**
4594 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004596int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4597 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Ingo Molnar62932df2006-01-16 16:34:20 +01004599 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004600 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4601 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004602 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004604 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return 0;
4606}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004607EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Takashi Iwaid5191e52009-11-16 14:58:17 +01004609/**
4610 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4611 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004612int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4613 struct hda_multi_out *mout,
4614 unsigned int stream_tag,
4615 unsigned int format,
4616 struct snd_pcm_substream *substream)
4617{
4618 mutex_lock(&codec->spdif_mutex);
4619 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4620 mutex_unlock(&codec->spdif_mutex);
4621 return 0;
4622}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004623EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004624
Takashi Iwaid5191e52009-11-16 14:58:17 +01004625/**
4626 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4627 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004628int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4629 struct hda_multi_out *mout)
4630{
4631 mutex_lock(&codec->spdif_mutex);
4632 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4633 mutex_unlock(&codec->spdif_mutex);
4634 return 0;
4635}
4636EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4637
Takashi Iwaid5191e52009-11-16 14:58:17 +01004638/**
4639 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004641int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4642 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Ingo Molnar62932df2006-01-16 16:34:20 +01004644 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004646 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return 0;
4648}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004649EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Takashi Iwaid5191e52009-11-16 14:58:17 +01004651/**
4652 * snd_hda_multi_out_analog_open - open analog outputs
4653 *
4654 * Open analog outputs and set up the hw-constraints.
4655 * If the digital outputs can be opened as slave, open the digital
4656 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004658int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4659 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004660 struct snd_pcm_substream *substream,
4661 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Takashi Iwai9a081602008-02-12 18:37:26 +01004663 struct snd_pcm_runtime *runtime = substream->runtime;
4664 runtime->hw.channels_max = mout->max_channels;
4665 if (mout->dig_out_nid) {
4666 if (!mout->analog_rates) {
4667 mout->analog_rates = hinfo->rates;
4668 mout->analog_formats = hinfo->formats;
4669 mout->analog_maxbps = hinfo->maxbps;
4670 } else {
4671 runtime->hw.rates = mout->analog_rates;
4672 runtime->hw.formats = mout->analog_formats;
4673 hinfo->maxbps = mout->analog_maxbps;
4674 }
4675 if (!mout->spdif_rates) {
4676 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4677 &mout->spdif_rates,
4678 &mout->spdif_formats,
4679 &mout->spdif_maxbps);
4680 }
4681 mutex_lock(&codec->spdif_mutex);
4682 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004683 if ((runtime->hw.rates & mout->spdif_rates) &&
4684 (runtime->hw.formats & mout->spdif_formats)) {
4685 runtime->hw.rates &= mout->spdif_rates;
4686 runtime->hw.formats &= mout->spdif_formats;
4687 if (mout->spdif_maxbps < hinfo->maxbps)
4688 hinfo->maxbps = mout->spdif_maxbps;
4689 } else {
4690 mout->share_spdif = 0;
4691 /* FIXME: need notify? */
4692 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004693 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004694 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4697 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4698}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004699EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Takashi Iwaid5191e52009-11-16 14:58:17 +01004701/**
4702 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4703 *
4704 * Set up the i/o for analog out.
4705 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004707int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4708 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 unsigned int stream_tag,
4710 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004711 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Takashi Iwaidda14412011-05-02 11:29:30 +02004713 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004715 struct hda_spdif_out *spdif =
4716 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 int i;
4718
Ingo Molnar62932df2006-01-16 16:34:20 +01004719 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004720 if (mout->dig_out_nid && mout->share_spdif &&
4721 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004723 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4724 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004725 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004727 setup_dig_out_stream(codec, mout->dig_out_nid,
4728 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 } else {
4730 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004731 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
4733 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004734 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004737 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4738 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004739 if (!mout->no_share_stream &&
4740 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004742 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4743 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004744 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004745 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4746 if (!mout->no_share_stream && mout->hp_out_nid[i])
4747 snd_hda_codec_setup_stream(codec,
4748 mout->hp_out_nid[i],
4749 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004750 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004751 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004752 snd_hda_codec_setup_stream(codec,
4753 mout->extra_out_nid[i],
4754 stream_tag, 0, format);
4755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 /* surrounds */
4757 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004758 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004759 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4760 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004761 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004762 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4763 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
4765 return 0;
4766}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004767EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Takashi Iwaid5191e52009-11-16 14:58:17 +01004769/**
4770 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004772int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4773 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Takashi Iwaidda14412011-05-02 11:29:30 +02004775 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 int i;
4777
4778 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004779 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004781 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004782 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4783 if (mout->hp_out_nid[i])
4784 snd_hda_codec_cleanup_stream(codec,
4785 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004786 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4787 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004788 snd_hda_codec_cleanup_stream(codec,
4789 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004790 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004792 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 mout->dig_out_used = 0;
4794 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004795 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 return 0;
4797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004798EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004800/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004801 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004802 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004803
Takashi Iwaidda14412011-05-02 11:29:30 +02004804static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004805{
4806 for (; *list; list++)
4807 if (*list == nid)
4808 return 1;
4809 return 0;
4810}
4811
Steve Longerbeam81937d32007-05-08 15:33:03 +02004812
4813/*
4814 * Sort an associated group of pins according to their sequence numbers.
4815 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004816static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004817 int num_pins)
4818{
4819 int i, j;
4820 short seq;
4821 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004822
Steve Longerbeam81937d32007-05-08 15:33:03 +02004823 for (i = 0; i < num_pins; i++) {
4824 for (j = i + 1; j < num_pins; j++) {
4825 if (sequences[i] > sequences[j]) {
4826 seq = sequences[i];
4827 sequences[i] = sequences[j];
4828 sequences[j] = seq;
4829 nid = pins[i];
4830 pins[i] = pins[j];
4831 pins[j] = nid;
4832 }
4833 }
4834 }
4835}
4836
4837
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004838/* add the found input-pin to the cfg->inputs[] table */
4839static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4840 int type)
4841{
4842 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4843 cfg->inputs[cfg->num_inputs].pin = nid;
4844 cfg->inputs[cfg->num_inputs].type = type;
4845 cfg->num_inputs++;
4846 }
4847}
4848
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004849/* sort inputs in the order of AUTO_PIN_* type */
4850static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4851{
4852 int i, j;
4853
4854 for (i = 0; i < cfg->num_inputs; i++) {
4855 for (j = i + 1; j < cfg->num_inputs; j++) {
4856 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4857 struct auto_pin_cfg_item tmp;
4858 tmp = cfg->inputs[i];
4859 cfg->inputs[i] = cfg->inputs[j];
4860 cfg->inputs[j] = tmp;
4861 }
4862 }
4863 }
4864}
4865
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004866/* Reorder the surround channels
4867 * ALSA sequence is front/surr/clfe/side
4868 * HDA sequence is:
4869 * 4-ch: front/surr => OK as it is
4870 * 6-ch: front/clfe/surr
4871 * 8-ch: front/clfe/rear/side|fc
4872 */
4873static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4874{
4875 hda_nid_t nid;
4876
4877 switch (nums) {
4878 case 3:
4879 case 4:
4880 nid = pins[1];
4881 pins[1] = pins[2];
4882 pins[2] = nid;
4883 break;
4884 }
4885}
4886
Takashi Iwai82bc9552006-03-21 11:24:42 +01004887/*
4888 * Parse all pin widgets and store the useful pin nids to cfg
4889 *
4890 * The number of line-outs or any primary output is stored in line_outs,
4891 * and the corresponding output pins are assigned to line_out_pins[],
4892 * in the order of front, rear, CLFE, side, ...
4893 *
4894 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004895 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004896 * is detected, one of speaker of HP pins is assigned as the primary
4897 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4898 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004899 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004900 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004901 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4902 * respectively.
4903 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004904int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4905 struct auto_pin_cfg *cfg,
4906 const hda_nid_t *ignore_nids,
4907 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004908{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004909 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004910 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004911 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4912 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004913 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004914 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004915
4916 memset(cfg, 0, sizeof(*cfg));
4917
Steve Longerbeam81937d32007-05-08 15:33:03 +02004918 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4919 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004920 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004921 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004922
Takashi Iwai2f451d22011-11-10 12:36:46 +01004923 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004924 end_nid = codec->start_nid + codec->num_nodes;
4925 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004926 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004927 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004928 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004929 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004930
4931 /* read all default configuration for pin complex */
4932 if (wid_type != AC_WID_PIN)
4933 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004934 /* ignore the given nids (e.g. pc-beep returns error) */
4935 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4936 continue;
4937
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004938 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01004939 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4940 AC_DEFCFG_MISC_NO_PRESENCE))
4941 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004942 conn = get_defcfg_connect(def_conf);
4943 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004944 continue;
4945 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004946 dev = get_defcfg_device(def_conf);
4947
4948 /* workaround for buggy BIOS setups */
4949 if (dev == AC_JACK_LINE_OUT) {
4950 if (conn == AC_JACK_PORT_FIXED)
4951 dev = AC_JACK_SPEAKER;
4952 }
4953
4954 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004955 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004956 seq = get_defcfg_sequence(def_conf);
4957 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004958
4959 if (!(wid_caps & AC_WCAP_STEREO))
4960 if (!cfg->mono_out_pin)
4961 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004962 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004963 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004964 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004965 assoc_line_out = assoc;
4966 else if (assoc_line_out != assoc)
4967 continue;
4968 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4969 continue;
4970 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004971 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004972 cfg->line_outs++;
4973 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004974 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004975 seq = get_defcfg_sequence(def_conf);
4976 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004977 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4978 continue;
4979 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004980 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004981 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004982 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004983 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004984 seq = get_defcfg_sequence(def_conf);
4985 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004986 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4987 continue;
4988 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004989 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004990 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004991 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004992 case AC_JACK_MIC_IN:
4993 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004994 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004995 case AC_JACK_LINE_IN:
4996 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004997 break;
4998 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004999 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005000 break;
5001 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005002 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005003 break;
5004 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005005 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005006 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5007 continue;
5008 cfg->dig_out_pins[cfg->dig_outs] = nid;
5009 cfg->dig_out_type[cfg->dig_outs] =
5010 (loc == AC_JACK_LOC_HDMI) ?
5011 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5012 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005013 break;
5014 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005015 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005016 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01005017 if (loc == AC_JACK_LOC_HDMI)
5018 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5019 else
5020 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005021 break;
5022 }
5023 }
5024
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005025 /* FIX-UP:
5026 * If no line-out is defined but multiple HPs are found,
5027 * some of them might be the real line-outs.
5028 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005029 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5030 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005031 int i = 0;
5032 while (i < cfg->hp_outs) {
5033 /* The real HPs should have the sequence 0x0f */
5034 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5035 i++;
5036 continue;
5037 }
5038 /* Move it to the line-out table */
5039 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5040 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5041 cfg->line_outs++;
5042 cfg->hp_outs--;
5043 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5044 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02005045 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005046 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5047 }
Takashi Iwai03642c92010-09-08 15:28:19 +02005048 memset(cfg->hp_pins + cfg->hp_outs, 0,
5049 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01005050 if (!cfg->hp_outs)
5051 cfg->line_out_type = AUTO_PIN_HP_OUT;
5052
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005053 }
5054
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005055 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02005056 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5057 cfg->line_outs);
5058 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5059 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01005060 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5061 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005062
Steve Longerbeam81937d32007-05-08 15:33:03 +02005063 /*
5064 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5065 * as a primary output
5066 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005067 if (!cfg->line_outs &&
5068 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02005069 if (cfg->speaker_outs) {
5070 cfg->line_outs = cfg->speaker_outs;
5071 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5072 sizeof(cfg->speaker_pins));
5073 cfg->speaker_outs = 0;
5074 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5075 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5076 } else if (cfg->hp_outs) {
5077 cfg->line_outs = cfg->hp_outs;
5078 memcpy(cfg->line_out_pins, cfg->hp_pins,
5079 sizeof(cfg->hp_pins));
5080 cfg->hp_outs = 0;
5081 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5082 cfg->line_out_type = AUTO_PIN_HP_OUT;
5083 }
5084 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005085
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02005086 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5087 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5088 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005089
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02005090 sort_autocfg_input_pins(cfg);
5091
Takashi Iwai82bc9552006-03-21 11:24:42 +01005092 /*
5093 * debug prints of the parsed results
5094 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02005095 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 +01005096 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5097 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02005098 cfg->line_out_pins[4],
5099 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5100 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5101 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01005102 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5103 cfg->speaker_outs, cfg->speaker_pins[0],
5104 cfg->speaker_pins[1], cfg->speaker_pins[2],
5105 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005106 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5107 cfg->hp_outs, cfg->hp_pins[0],
5108 cfg->hp_pins[1], cfg->hp_pins[2],
5109 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005110 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005111 if (cfg->dig_outs)
5112 snd_printd(" dig-out=0x%x/0x%x\n",
5113 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005114 snd_printd(" inputs:");
5115 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02005116 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02005117 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005118 cfg->inputs[i].pin);
5119 }
5120 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01005121 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01005122 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005123
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005124 return 0;
5125}
Takashi Iwaid025feb2011-08-23 15:24:39 +02005126EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005127
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005128int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005129{
5130 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005131 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005132 if (conn == AC_JACK_PORT_NONE)
5133 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005134 /* Windows may claim the internal mic to be BOTH, too */
5135 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005136 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005137 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005138 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005139 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005140 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005141 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005142 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005143 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005144 return INPUT_PIN_ATTR_FRONT;
5145 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005146}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005147EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005148
Takashi Iwai990061c2010-09-09 22:08:44 +02005149/**
5150 * hda_get_input_pin_label - Give a label for the given input pin
5151 *
5152 * When check_location is true, the function checks the pin location
5153 * for mic and line-in pins, and set an appropriate prefix like "Front",
5154 * "Rear", "Internal".
5155 */
5156
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005157static const char *hda_get_input_pin_label(struct hda_codec *codec,
5158 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02005159{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005160 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005161 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005162 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5163 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005164 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005165
Takashi Iwai10a20af2010-09-09 16:28:02 +02005166 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005167
5168 switch (get_defcfg_device(def_conf)) {
5169 case AC_JACK_MIC_IN:
5170 if (!check_location)
5171 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005172 attr = snd_hda_get_input_pin_attr(def_conf);
5173 if (!attr)
5174 return "None";
5175 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005176 case AC_JACK_LINE_IN:
5177 if (!check_location)
5178 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005179 attr = snd_hda_get_input_pin_attr(def_conf);
5180 if (!attr)
5181 return "None";
5182 if (attr == INPUT_PIN_ATTR_DOCK)
5183 return "Dock Line";
5184 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005185 case AC_JACK_AUX:
5186 return "Aux";
5187 case AC_JACK_CD:
5188 return "CD";
5189 case AC_JACK_SPDIF_IN:
5190 return "SPDIF In";
5191 case AC_JACK_DIG_OTHER_IN:
5192 return "Digital In";
5193 default:
5194 return "Misc";
5195 }
5196}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005197
Takashi Iwaia1c98512010-09-09 21:36:27 +02005198/* Check whether the location prefix needs to be added to the label.
5199 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5200 * have to put "Front" prefix to each label. In such a case, returns false.
5201 */
5202static int check_mic_location_need(struct hda_codec *codec,
5203 const struct auto_pin_cfg *cfg,
5204 int input)
5205{
5206 unsigned int defc;
5207 int i, attr, attr2;
5208
5209 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005210 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005211 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005212 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005213 return 1;
5214
5215 attr = 0;
5216 for (i = 0; i < cfg->num_inputs; i++) {
5217 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005218 attr2 = snd_hda_get_input_pin_attr(defc);
5219 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005220 if (attr && attr != attr2)
5221 return 1; /* different locations found */
5222 attr = attr2;
5223 }
5224 }
5225 return 0;
5226}
5227
Takashi Iwai990061c2010-09-09 22:08:44 +02005228/**
5229 * hda_get_autocfg_input_label - Get a label for the given input
5230 *
5231 * Get a label for the given input pin defined by the autocfg item.
5232 * Unlike hda_get_input_pin_label(), this function checks all inputs
5233 * defined in autocfg and avoids the redundant mic/line prefix as much as
5234 * possible.
5235 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005236const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5237 const struct auto_pin_cfg *cfg,
5238 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005239{
5240 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005241 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005242
Takashi Iwai10a20af2010-09-09 16:28:02 +02005243 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5244 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5245 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005246 if (has_multiple_pins && type == AUTO_PIN_MIC)
5247 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005248 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5249 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005250}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005251EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5252
Takashi Iwai358b6e62011-11-21 14:34:20 +01005253/* return the position of NID in the list, or -1 if not found */
5254static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5255{
5256 int i;
5257 for (i = 0; i < nums; i++)
5258 if (list[i] == nid)
5259 return i;
5260 return -1;
5261}
5262
Takashi Iwai201e06f2011-11-16 15:33:26 +01005263/* get a unique suffix or an index number */
5264static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5265 int num_pins, int *indexp)
5266{
5267 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005268 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005269 };
5270 int i;
5271
Takashi Iwai358b6e62011-11-21 14:34:20 +01005272 i = find_idx_in_nid_list(nid, pins, num_pins);
5273 if (i < 0)
5274 return NULL;
5275 if (num_pins == 1)
5276 return "";
5277 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5278 if (indexp)
5279 *indexp = i;
5280 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005281 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005282 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005283}
5284
5285static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5286 const struct auto_pin_cfg *cfg,
5287 const char *name, char *label, int maxlen,
5288 int *indexp)
5289{
5290 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5291 int attr = snd_hda_get_input_pin_attr(def_conf);
5292 const char *pfx = "", *sfx = "";
5293
5294 /* handle as a speaker if it's a fixed line-out */
Takashi Iwaie49a3432012-03-01 18:14:41 +01005295 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005296 name = "Speaker";
5297 /* check the location */
5298 switch (attr) {
5299 case INPUT_PIN_ATTR_DOCK:
5300 pfx = "Dock ";
5301 break;
5302 case INPUT_PIN_ATTR_FRONT:
5303 pfx = "Front ";
5304 break;
5305 }
5306 if (cfg) {
5307 /* try to give a unique suffix if needed */
5308 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5309 indexp);
5310 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005311 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5312 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005313 if (!sfx) {
5314 /* don't add channel suffix for Headphone controls */
5315 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5316 cfg->hp_outs);
5317 if (idx >= 0)
5318 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005319 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005320 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005321 }
5322 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5323 return 1;
5324}
5325
Takashi Iwai990061c2010-09-09 22:08:44 +02005326/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005327 * snd_hda_get_pin_label - Get a label for the given I/O pin
5328 *
5329 * Get a label for the given pin. This function works for both input and
5330 * output pins. When @cfg is given as non-NULL, the function tries to get
5331 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005332 *
5333 * This function tries to give a unique label string for the pin as much as
5334 * possible. For example, when the multiple line-outs are present, it adds
5335 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5336 * If no unique name with a suffix is available and @indexp is non-NULL, the
5337 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005338 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005339int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5340 const struct auto_pin_cfg *cfg,
5341 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005342{
5343 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005344 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005345 int i;
5346
Takashi Iwai201e06f2011-11-16 15:33:26 +01005347 if (indexp)
5348 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005349 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005350 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005351
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005352 switch (get_defcfg_device(def_conf)) {
5353 case AC_JACK_LINE_OUT:
Takashi Iwaie49a3432012-03-01 18:14:41 +01005354 return fill_audio_out_name(codec, nid, cfg, "Line Out",
Takashi Iwai201e06f2011-11-16 15:33:26 +01005355 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005356 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005357 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5358 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005359 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005360 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5361 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005362 case AC_JACK_SPDIF_OUT:
5363 case AC_JACK_DIG_OTHER_OUT:
5364 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005365 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005366 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005367 name = "SPDIF";
5368 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005369 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5370 cfg->dig_outs);
5371 if (i >= 0)
5372 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005373 }
5374 break;
5375 default:
5376 if (cfg) {
5377 for (i = 0; i < cfg->num_inputs; i++) {
5378 if (cfg->inputs[i].pin != nid)
5379 continue;
5380 name = hda_get_autocfg_input_label(codec, cfg, i);
5381 if (name)
5382 break;
5383 }
5384 }
5385 if (!name)
5386 name = hda_get_input_pin_label(codec, nid, true);
5387 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005388 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005389 if (!name)
5390 return 0;
5391 strlcpy(label, name, maxlen);
5392 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005393}
5394EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5395
Takashi Iwai990061c2010-09-09 22:08:44 +02005396/**
5397 * snd_hda_add_imux_item - Add an item to input_mux
5398 *
5399 * When the same label is used already in the existing items, the number
5400 * suffix is appended to the label. This label index number is stored
5401 * to type_idx when non-NULL pointer is given.
5402 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005403int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5404 int index, int *type_idx)
5405{
5406 int i, label_idx = 0;
5407 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5408 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5409 return -EINVAL;
5410 }
5411 for (i = 0; i < imux->num_items; i++) {
5412 if (!strncmp(label, imux->items[i].label, strlen(label)))
5413 label_idx++;
5414 }
5415 if (type_idx)
5416 *type_idx = label_idx;
5417 if (label_idx > 0)
5418 snprintf(imux->items[imux->num_items].label,
5419 sizeof(imux->items[imux->num_items].label),
5420 "%s %d", label, label_idx);
5421 else
5422 strlcpy(imux->items[imux->num_items].label, label,
5423 sizeof(imux->items[imux->num_items].label));
5424 imux->items[imux->num_items].index = index;
5425 imux->num_items++;
5426 return 0;
5427}
5428EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005429
Takashi Iwai4a471b72005-12-07 13:56:29 +01005430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431#ifdef CONFIG_PM
5432/*
5433 * power management
5434 */
5435
5436/**
5437 * snd_hda_suspend - suspend the codecs
5438 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 *
5440 * Returns 0 if successful.
5441 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005442int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005444 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Takashi Iwai0ba21762007-04-16 11:29:14 +02005446 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005447 if (hda_codec_is_power_on(codec))
5448 hda_call_codec_suspend(codec);
5449 if (codec->patch_ops.post_suspend)
5450 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452 return 0;
5453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005454EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456/**
5457 * snd_hda_resume - resume the codecs
5458 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 *
5460 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005461 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005462 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005463 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 */
5465int snd_hda_resume(struct hda_bus *bus)
5466{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005467 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Takashi Iwai0ba21762007-04-16 11:29:14 +02005469 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005470 if (codec->patch_ops.pre_resume)
5471 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005472 if (snd_hda_codec_needs_resume(codec))
5473 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 return 0;
5476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005477EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005478#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005479
5480/*
5481 * generic arrays
5482 */
5483
Takashi Iwaid5191e52009-11-16 14:58:17 +01005484/**
5485 * snd_array_new - get a new element from the given array
5486 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005487 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005488 * Get a new element from the given array. If it exceeds the
5489 * pre-allocated array size, re-allocate the array.
5490 *
5491 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005492 */
5493void *snd_array_new(struct snd_array *array)
5494{
5495 if (array->used >= array->alloced) {
5496 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005497 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005498 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005499 void *nlist;
5500 if (snd_BUG_ON(num >= 4096))
5501 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005502 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005503 if (!nlist)
5504 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005505 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005506 array->list = nlist;
5507 array->alloced = num;
5508 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005509 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005511EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005512
Takashi Iwaid5191e52009-11-16 14:58:17 +01005513/**
5514 * snd_array_free - free the given array elements
5515 * @array: the array object
5516 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005517void snd_array_free(struct snd_array *array)
5518{
5519 kfree(array->list);
5520 array->used = 0;
5521 array->alloced = 0;
5522 array->list = NULL;
5523}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005524EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005525
Takashi Iwaid5191e52009-11-16 14:58:17 +01005526/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005527 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5528 * @pcm: PCM caps bits
5529 * @buf: the string buffer to write
5530 * @buflen: the max buffer length
5531 *
5532 * used by hda_proc.c and hda_eld.c
5533 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005534void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5535{
5536 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5537 int i, j;
5538
5539 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5540 if (pcm & (AC_SUPPCM_BITS_8 << i))
5541 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5542
5543 buf[j] = '\0'; /* necessary when j == 0 */
5544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005545EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005546
5547MODULE_DESCRIPTION("HDA codec core");
5548MODULE_LICENSE("GPL");