blob: b981ea9c644cfa58d89991c35fff3b4cbc2a8213 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwaicb53c622007-08-10 17:21:45 +020097#ifdef CONFIG_SND_HDA_POWER_SAVE
98static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#else
102static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200103#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200104#endif
105
Takashi Iwaid5191e52009-11-16 14:58:17 +0100106/**
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
109 *
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
112 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400113const char *snd_hda_get_jack_location(u32 cfg)
114{
115 static char *bases[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 };
118 static unsigned char specials_idx[] = {
119 0x07, 0x08,
120 0x17, 0x18, 0x19,
121 0x37, 0x38
122 };
123 static char *specials[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
127 };
128 int i;
129 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 if ((cfg & 0x0f) < 7)
131 return bases[cfg & 0x0f];
132 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 if (cfg == specials_idx[i])
134 return specials[i];
135 }
136 return "UNKNOWN";
137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139
Takashi Iwaid5191e52009-11-16 14:58:17 +0100140/**
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
143 *
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
146 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147const char *snd_hda_get_jack_connectivity(u32 cfg)
148{
149 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150
151 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100153EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154
Takashi Iwaid5191e52009-11-16 14:58:17 +0100155/**
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
158 *
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162const char *snd_hda_get_jack_type(u32 cfg)
163{
164 static char *jack_types[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
169 };
170
171 return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 >> AC_DEFCFG_DEVICE_SHIFT];
173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100174EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400175
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176/*
177 * Compose a 32bit command word to be sent to the HD-audio controller
178 */
179static inline unsigned int
180make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 unsigned int verb, unsigned int parm)
182{
183 u32 val;
184
Takashi Iwai82e1b802009-07-17 12:47:34 +0200185 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800187 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec->addr, direct, nid, verb, parm);
189 return ~0;
190 }
191
192 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193 val |= (u32)direct << 27;
194 val |= (u32)nid << 20;
195 val |= verb << 8;
196 val |= parm;
197 return val;
198}
199
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200/*
201 * Send and receive a verb
202 */
203static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
204 unsigned int *res)
205{
206 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200207 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208
Wu Fengguang6430aee2009-07-17 16:49:19 +0800209 if (cmd == ~0)
210 return -1;
211
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 if (res)
213 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 snd_hda_power_up(codec);
216 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200217 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200219 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800220 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200221 trace_hda_get_response(codec, *res);
222 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 mutex_unlock(&bus->cmd_mutex);
224 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200225 if (res && *res == -1 && bus->rirb_error) {
226 if (bus->response_reset) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200230 bus->ops.bus_reset(bus);
231 }
232 goto again;
233 }
234 /* clear reset-flag when the communication gets recovered */
235 if (!err)
236 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 return err;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
247 *
248 * Send a single command and read the corresponding response.
249 *
250 * Returns the obtained response value, or -1 for an error.
251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200252unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int verb, unsigned int parm)
255{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700258 if (codec_exec_verb(codec, cmd, &res))
259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return res;
261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100262EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
271 *
272 * Send a single command without waiting for response.
273 *
274 * Returns 0 if successful, or a negative error code.
275 */
276int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 unsigned int verb, unsigned int parm)
278{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200279 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100280 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200281 return codec_exec_verb(codec, cmd,
282 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100284EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/**
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
290 *
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
293 */
294void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295{
296 for (; seq->nid; seq++)
297 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100299EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
304 * @nid: NID to parse
305 * @start_id: the pointer to store the start NID
306 *
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
309 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200310int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned int parm;
314
315 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200316 if (parm == -1)
317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100321EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200323/* look up the cached results */
324static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
325{
326 int i, len;
327 for (i = 0; i < array->used; ) {
328 hda_nid_t *p = snd_array_elem(array, i);
329 if (nid == *p)
330 return p;
331 len = p[1];
332 i += len + 2;
333 }
334 return NULL;
335}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200338 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @codec: the HDA codec
340 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200341 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * Parses the connection list of the given widget and stores the list
344 * of NIDs.
345 *
346 * Returns the number of connections, or a negative error code.
347 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200348int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350{
351 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200354 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200357 again:
358 /* if the connection-list is already cached, read it */
359 p = lookup_conn_list(array, nid);
360 if (p) {
361 if (listp)
362 *listp = p + 2;
363 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 if (snd_BUG_ON(added))
366 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200368 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200369 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200370 if (len < 0)
371 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 err = snd_hda_override_conn_list(codec, nid, len, list);
373 if (err < 0)
374 return err;
375 added = true;
376 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377}
Takashi Iwaidce20792011-06-24 14:10:28 +0200378EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
379
380/**
381 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * @codec: the HDA codec
383 * @nid: NID to parse
384 * @conn_list: connection list array
385 * @max_conns: max. number of connections to store
386 *
387 * Parses the connection list of the given widget and stores the list
388 * of NIDs.
389 *
390 * Returns the number of connections, or a negative error code.
391 */
392int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200393 hda_nid_t *conn_list, int max_conns)
394{
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 const hda_nid_t *list;
396 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200399 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200400 if (len > max_conns) {
401 snd_printk(KERN_ERR "hda_codec: "
402 "Too many connections %d for NID 0x%x\n",
403 len, nid);
404 return -EINVAL;
405 }
406 memcpy(conn_list, list, len * sizeof(hda_nid_t));
407 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200408}
409EXPORT_SYMBOL_HDA(snd_hda_get_connections);
410
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200411/**
412 * snd_hda_get_raw_connections - copy connection list without cache
413 * @codec: the HDA codec
414 * @nid: NID to parse
415 * @conn_list: connection list array
416 * @max_conns: max. number of connections to store
417 *
418 * Like snd_hda_get_connections(), copy the connection list but without
419 * checking through the connection-list cache.
420 * Currently called only from hda_proc.c, so not exported.
421 */
422int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200428 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100429 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwaida3cec32008-08-08 17:12:14 +0200431 if (snd_BUG_ON(!conn_list || max_conns <= 0))
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200434 wcaps = get_wcaps(codec, nid);
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm & AC_CLIST_LONG) {
441 /* long form */
442 shift = 16;
443 num_elems = 2;
444 } else {
445 /* short form */
446 shift = 8;
447 num_elems = 4;
448 }
449 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 mask = (1 << (shift-1)) - 1;
451
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0; /* no connection */
454
455 if (conn_len == 1) {
456 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200457 parm = snd_hda_codec_read(codec, nid, 0,
458 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200459 if (parm == -1 && codec->bus->rirb_error)
460 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 conn_list[0] = parm & mask;
462 return 1;
463 }
464
465 /* multi connection */
466 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 prev_nid = 0;
468 for (i = 0; i < conn_len; i++) {
469 int range_val;
470 hda_nid_t val, n;
471
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 parm = snd_hda_codec_read(codec, nid, 0,
474 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200475 if (parm == -1 && codec->bus->rirb_error)
476 return -EIO;
477 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100479 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200480 if (val == 0) {
481 snd_printk(KERN_WARNING "hda_codec: "
482 "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 nid, i, parm);
484 return 0;
485 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100486 parm >>= shift;
487 if (range_val) {
488 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!prev_nid || prev_nid >= val) {
490 snd_printk(KERN_WARNING "hda_codec: "
491 "invalid dep_range_val %x:%x\n",
492 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 for (n = prev_nid + 1; n <= val; n++) {
496 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200497 snd_printk(KERN_ERR "hda_codec: "
498 "Too many connections %d for NID 0x%x\n",
499 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 return -EINVAL;
501 }
502 conn_list[conns++] = n;
503 }
504 } else {
505 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200506 snd_printk(KERN_ERR "hda_codec: "
507 "Too many connections %d for NID 0x%x\n",
508 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100509 return -EINVAL;
510 }
511 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return conns;
516}
517
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200518static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519{
520 hda_nid_t *p = snd_array_new(array);
521 if (!p)
522 return false;
523 *p = nid;
524 return true;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200528 * snd_hda_override_conn_list - add/modify the connection-list to cache
529 * @codec: the HDA codec
530 * @nid: NID to parse
531 * @len: number of connection list entries
532 * @list: the list of connection entries
533 *
534 * Add or modify the given connection-list to the cache. If the corresponding
535 * cache already exists, invalidate it and append a new one.
536 *
537 * Returns zero or a negative error code.
538 */
539int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 struct snd_array *array = &codec->conn_lists;
543 hda_nid_t *p;
544 int i, old_used;
545
546 p = lookup_conn_list(array, nid);
547 if (p)
548 *p = -1; /* invalidate the old entry */
549
550 old_used = array->used;
551 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 goto error_add;
553 for (i = 0; i < len; i++)
554 if (!add_conn_list(array, list[i]))
555 goto error_add;
556 return 0;
557
558 error_add:
559 array->used = old_used;
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563
564/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
570 *
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
574 */
575int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
577{
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 int i, nums;
580
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
583 if (conn[i] == nid)
584 return i;
585 if (!recursive)
586 return -1;
587 if (recursive > 5) {
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 return -1;
590 }
591 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 return -1;
600}
601EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @bus: the BUS
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 *
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616{
617 struct hda_bus_unsolicited *unsol;
618 unsigned int wp;
619
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200620 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 unsol = bus->unsol;
622 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->wp = wp;
627
628 wp <<= 1;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
631
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100636EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800639 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Howellsc4028952006-11-22 14:57:56 +0000641static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howellsc4028952006-11-22 14:57:56 +0000643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
648
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
651 unsol->rp = rp;
652 rp <<= 1;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
660 }
661}
662
663/*
664 * initialize unsolicited queue
665 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200666static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct hda_bus_unsolicited *unsol;
669
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100670 if (bus->unsol) /* already initialized */
671 return 0;
672
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200674 if (!unsol) {
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&unsol->work, process_unsol_events);
680 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus->unsol = unsol;
682 return 0;
683}
684
685/*
686 * destructor
687 */
688static void snd_hda_codec_free(struct hda_codec *codec);
689
690static int snd_hda_bus_free(struct hda_bus *bus)
691{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Takashi Iwai0ba21762007-04-16 11:29:14 +0200694 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 if (bus->workq)
697 flush_workqueue(bus->workq);
698 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_hda_codec_free(codec);
702 }
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100705 if (bus->workq)
706 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(bus);
708 return 0;
709}
710
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100711static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100714 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return snd_hda_bus_free(bus);
716}
717
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718#ifdef CONFIG_SND_HDA_HWDEP
719static int snd_hda_bus_dev_register(struct snd_device *device)
720{
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100725 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200726 }
727 return 0;
728}
729#else
730#define snd_hda_bus_dev_register NULL
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
738 *
739 * Returns 0 if successful, or a negative error code.
740 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100741int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct hda_bus *bus;
746 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100747 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200748 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .dev_free = snd_hda_bus_dev_free,
750 };
751
Takashi Iwaida3cec32008-08-08 17:12:14 +0200752 if (snd_BUG_ON(!temp))
753 return -EINVAL;
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (busp)
758 *busp = NULL;
759
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (bus == NULL) {
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
763 return -ENOMEM;
764 }
765
766 bus->card = card;
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100770 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 bus->ops = temp->ops;
772
Ingo Molnar62932df2006-01-16 16:34:20 +0100773 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200774 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&bus->codec_list);
776
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
782 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 kfree(bus);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 snd_hda_bus_free(bus);
790 return err;
791 }
792 if (busp)
793 *busp = bus;
794 return 0;
795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100796EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798#ifdef CONFIG_SND_HDA_GENERIC
799#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200800 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200801#else
802#define is_generic_config(codec) 0
803#endif
804
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * find a matching codec preset
813 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200815find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
819 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai82467612007-07-27 19:15:54 +0200821 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 return NULL; /* use the generic parser */
823
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 again:
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
829 continue;
830 }
831 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100833 if (preset->afg && preset->afg != codec->afg)
834 continue;
835 if (preset->mfg && preset->mfg != codec->mfg)
836 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200839 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847 module_put(tbl->owner);
848 }
849 mutex_unlock(&preset_mutex);
850
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
852 char name[32];
853 if (!mod_requested)
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
855 codec->vendor_id);
856 else
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
860 mod_requested++;
861 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return NULL;
864}
865
866/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 char tmp[16];
875
876 if (codec->vendor_name)
877 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
881 vendor = c->name;
882 break;
883 }
884 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200885 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sprintf(tmp, "Generic %04x", vendor_id);
887 vendor = tmp;
888 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
891 return -ENOMEM;
892
893 get_chip_name:
894 if (codec->chip_name)
895 return 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 else {
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 }
903 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200904 return -ENOMEM;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200909 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200913 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hda_nid_t nid;
915
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200918 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200919 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200920 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200921 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200925 break;
926 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200930 break;
931 default:
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100938 * read widget caps for each widget and store in cache
939 */
940static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
941{
942 int i;
943 hda_nid_t nid;
944
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 &codec->start_nid);
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100949 return -ENOMEM;
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
954 return 0;
955}
956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* read all pin default configurations and save codec->init_pins */
958static int read_pin_defaults(struct hda_codec *codec)
959{
960 int i;
961 hda_nid_t nid = codec->start_nid;
962
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200966 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967 if (wid_type != AC_WID_PIN)
968 continue;
969 pin = snd_array_new(&codec->init_pins);
970 if (!pin)
971 return -ENOMEM;
972 pin->nid = nid;
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
977 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 }
979 return 0;
980}
981
982/* look up the given pin config list and return the item matching with NID */
983static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
985 hda_nid_t nid)
986{
987 int i;
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
990 if (pin->nid == nid)
991 return pin;
992 }
993 return NULL;
994}
995
996/* write a config value for the given NID */
997static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
998 unsigned int cfg)
999{
1000 int i;
1001 for (i = 0; i < 4; i++) {
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1004 cfg & 0xff);
1005 cfg >>= 8;
1006 }
1007}
1008
1009/* set the current pin config value for the given NID.
1010 * the value is cached, and read via snd_hda_codec_get_pincfg()
1011 */
1012int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1013 hda_nid_t nid, unsigned int cfg)
1014{
1015 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001016 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001017
Takashi Iwaib82855a2009-12-27 11:24:56 +01001018 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1019 return -EINVAL;
1020
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001021 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 pin = look_up_pincfg(codec, list, nid);
1023 if (!pin) {
1024 pin = snd_array_new(list);
1025 if (!pin)
1026 return -ENOMEM;
1027 pin->nid = nid;
1028 }
1029 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001030
1031 /* change only when needed; e.g. if the pincfg is already present
1032 * in user_pins[], don't write it
1033 */
1034 cfg = snd_hda_codec_get_pincfg(codec, nid);
1035 if (oldcfg != cfg)
1036 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001037 return 0;
1038}
1039
Takashi Iwaid5191e52009-11-16 14:58:17 +01001040/**
1041 * snd_hda_codec_set_pincfg - Override a pin default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to set the pin config
1044 * @cfg: the pin default config value
1045 *
1046 * Override a pin default configuration value in the cache.
1047 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1048 * priority than the real hardware value.
1049 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001050int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1051 hda_nid_t nid, unsigned int cfg)
1052{
Takashi Iwai346ff702009-02-23 09:42:57 +01001053 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001054}
1055EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1056
Takashi Iwaid5191e52009-11-16 14:58:17 +01001057/**
1058 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1059 * @codec: the HDA codec
1060 * @nid: NID to get the pin config
1061 *
1062 * Get the current pin config value of the given pin NID.
1063 * If the pincfg value is cached or overridden via sysfs or driver,
1064 * returns the cached value.
1065 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001066unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1067{
1068 struct hda_pincfg *pin;
1069
Takashi Iwai3be14142009-02-20 14:11:16 +01001070#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 if (pin)
1073 return pin->cfg;
1074#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001075 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1076 if (pin)
1077 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1079 if (pin)
1080 return pin->cfg;
1081 return 0;
1082}
1083EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1084
1085/* restore all current pin configs */
1086static void restore_pincfgs(struct hda_codec *codec)
1087{
1088 int i;
1089 for (i = 0; i < codec->init_pins.used; i++) {
1090 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1091 set_pincfg(codec, pin->nid,
1092 snd_hda_codec_get_pincfg(codec, pin->nid));
1093 }
1094}
Takashi Iwai54d17402005-11-21 16:33:22 +01001095
Takashi Iwai92ee6162009-12-27 11:18:59 +01001096/**
1097 * snd_hda_shutup_pins - Shut up all pins
1098 * @codec: the HDA codec
1099 *
1100 * Clear all pin controls to shup up before suspend for avoiding click noise.
1101 * The controls aren't cached so that they can be resumed properly.
1102 */
1103void snd_hda_shutup_pins(struct hda_codec *codec)
1104{
1105 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106 /* don't shut up pins when unloading the driver; otherwise it breaks
1107 * the default pin setup at the next load of the driver
1108 */
1109 if (codec->bus->shutdown)
1110 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001111 for (i = 0; i < codec->init_pins.used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1113 /* use read here for syncing after issuing each verb */
1114 snd_hda_codec_read(codec, pin->nid, 0,
1115 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1116 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001117 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001118}
1119EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1120
Takashi Iwai2a439522011-07-26 09:52:50 +02001121#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001122/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1123static void restore_shutup_pins(struct hda_codec *codec)
1124{
1125 int i;
1126 if (!codec->pins_shutup)
1127 return;
1128 if (codec->bus->shutdown)
1129 return;
1130 for (i = 0; i < codec->init_pins.used; i++) {
1131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1132 snd_hda_codec_write(codec, pin->nid, 0,
1133 AC_VERB_SET_PIN_WIDGET_CONTROL,
1134 pin->ctrl);
1135 }
1136 codec->pins_shutup = 0;
1137}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001138#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001139
Takashi Iwai01751f52007-08-10 16:59:39 +02001140static void init_hda_cache(struct hda_cache_rec *cache,
1141 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001142static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143
Takashi Iwai3be14142009-02-20 14:11:16 +01001144/* restore the initial pin cfgs and release all pincfg lists */
1145static void restore_init_pincfgs(struct hda_codec *codec)
1146{
Takashi Iwai346ff702009-02-23 09:42:57 +01001147 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001148 * so that only the values in init_pins are restored
1149 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001152 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001153#endif
1154 restore_pincfgs(codec);
1155 snd_array_free(&codec->init_pins);
1156}
1157
Takashi Iwai54d17402005-11-21 16:33:22 +01001158/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001159 * audio-converter setup caches
1160 */
1161struct hda_cvt_setup {
1162 hda_nid_t nid;
1163 u8 stream_tag;
1164 u8 channel_id;
1165 u16 format_id;
1166 unsigned char active; /* cvt is currently used */
1167 unsigned char dirty; /* setups should be cleared */
1168};
1169
1170/* get or create a cache entry for the given audio converter NID */
1171static struct hda_cvt_setup *
1172get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1173{
1174 struct hda_cvt_setup *p;
1175 int i;
1176
1177 for (i = 0; i < codec->cvt_setups.used; i++) {
1178 p = snd_array_elem(&codec->cvt_setups, i);
1179 if (p->nid == nid)
1180 return p;
1181 }
1182 p = snd_array_new(&codec->cvt_setups);
1183 if (p)
1184 p->nid = nid;
1185 return p;
1186}
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * codec destructor
1190 */
1191static void snd_hda_codec_free(struct hda_codec *codec)
1192{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001193 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001195 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#ifdef CONFIG_SND_HDA_POWER_SAVE
1197 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001198 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001201 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001202 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001208 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001209 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001210 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001211 kfree(codec->vendor_name);
1212 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001213 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001214 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(codec);
1216}
1217
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001218static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1219 unsigned int power_state);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
1222 * snd_hda_codec_new - create a HDA codec
1223 * @bus: the bus to assign
1224 * @codec_addr: the codec address
1225 * @codecp: the pointer to store the generated codec
1226 *
1227 * Returns 0 if successful, or a negative error code.
1228 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001229int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1230 unsigned int codec_addr,
1231 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001234 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
1236
Takashi Iwaida3cec32008-08-08 17:12:14 +02001237 if (snd_BUG_ON(!bus))
1238 return -EINVAL;
1239 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243 snd_printk(KERN_ERR "hda_codec: "
1244 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EBUSY;
1246 }
1247
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001248 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (codec == NULL) {
1250 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1251 return -ENOMEM;
1252 }
1253
1254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001258 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001259 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001260 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001262 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001263 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001264 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001265 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001267 if (codec->bus->modelname) {
1268 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1269 if (!codec->modelname) {
1270 snd_hda_codec_free(codec);
1271 return -ENODEV;
1272 }
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Takashi Iwaicb53c622007-08-10 17:21:45 +02001275#ifdef CONFIG_SND_HDA_POWER_SAVE
1276 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1277 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1278 * the caller has to power down appropriatley after initialization
1279 * phase.
1280 */
1281 hda_keep_power_on(codec);
1282#endif
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 list_add_tail(&codec->list, &bus->codec_list);
1285 bus->caddr_tbl[codec_addr] = codec;
1286
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001289 if (codec->vendor_id == -1)
1290 /* read again, hopefully the access method was corrected
1291 * in the last read...
1292 */
1293 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1294 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_SUBSYSTEM_ID);
1297 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001300 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001302 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 err = -ENODEV;
1304 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1308 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001309 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 err = read_pin_defaults(codec);
1313 if (err < 0)
1314 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001315
Takashi Iwai0ba21762007-04-16 11:29:14 +02001316 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001317 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 codec->subsystem_id =
1319 snd_hda_codec_read(codec, nid, 0,
1320 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001321 }
1322
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001323 /* power-up all before initialization */
1324 hda_set_power_state(codec,
1325 codec->afg ? codec->afg : codec->mfg,
1326 AC_PWRST_D0);
1327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_codec_proc_new(codec);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331
1332 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1333 codec->subsystem_id, codec->revision_id);
1334 snd_component_add(codec->bus->card, component);
1335
1336 if (codecp)
1337 *codecp = codec;
1338 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001339
1340 error:
1341 snd_hda_codec_free(codec);
1342 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001344EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345
Takashi Iwaid5191e52009-11-16 14:58:17 +01001346/**
1347 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1348 * @codec: the HDA codec
1349 *
1350 * Start parsing of the given codec tree and (re-)initialize the whole
1351 * patch instance.
1352 *
1353 * Returns 0 if successful or a negative error code.
1354 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001355int snd_hda_codec_configure(struct hda_codec *codec)
1356{
1357 int err;
1358
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001359 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001360 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001361 err = get_codec_name(codec);
1362 if (err < 0)
1363 return err;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Takashi Iwai82467612007-07-27 19:15:54 +02001366 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001368 goto patched;
1369 }
Takashi Iwai82467612007-07-27 19:15:54 +02001370 if (codec->preset && codec->preset->patch) {
1371 err = codec->preset->patch(codec);
1372 goto patched;
1373 }
1374
1375 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001376 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001377 if (err < 0)
1378 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001379
1380 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001381 if (!err && codec->patch_ops.unsol_event)
1382 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001383 /* audio codec should override the mixer name */
1384 if (!err && (codec->afg || !*codec->bus->card->mixername))
1385 snprintf(codec->bus->card->mixername,
1386 sizeof(codec->bus->card->mixername),
1387 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001390EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392/**
1393 * snd_hda_codec_setup_stream - set up the codec for streaming
1394 * @codec: the CODEC to set up
1395 * @nid: the NID to set up
1396 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1397 * @channel_id: channel id to pass, zero based.
1398 * @format: stream format.
1399 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001400void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1401 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int channel_id, int format)
1403{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001404 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001405 struct hda_cvt_setup *p;
1406 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001407 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001408 int i;
1409
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001411 return;
1412
Takashi Iwai0ba21762007-04-16 11:29:14 +02001413 snd_printdd("hda_codec_setup_stream: "
1414 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001416 p = get_hda_cvt_setup(codec, nid);
1417 if (!p)
1418 return;
1419 /* update the stream-id if changed */
1420 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1421 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1422 newval = (stream_tag << 4) | channel_id;
1423 if (oldval != newval)
1424 snd_hda_codec_write(codec, nid, 0,
1425 AC_VERB_SET_CHANNEL_STREAMID,
1426 newval);
1427 p->stream_tag = stream_tag;
1428 p->channel_id = channel_id;
1429 }
1430 /* update the format-id if changed */
1431 if (p->format_id != format) {
1432 oldval = snd_hda_codec_read(codec, nid, 0,
1433 AC_VERB_GET_STREAM_FORMAT, 0);
1434 if (oldval != format) {
1435 msleep(1);
1436 snd_hda_codec_write(codec, nid, 0,
1437 AC_VERB_SET_STREAM_FORMAT,
1438 format);
1439 }
1440 p->format_id = format;
1441 }
1442 p->active = 1;
1443 p->dirty = 0;
1444
1445 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001446 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001447 list_for_each_entry(c, &codec->bus->codec_list, list) {
1448 for (i = 0; i < c->cvt_setups.used; i++) {
1449 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001450 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001451 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001452 p->dirty = 1;
1453 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001456EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Takashi Iwaif0cea792010-08-13 11:56:53 +02001458static void really_cleanup_stream(struct hda_codec *codec,
1459 struct hda_cvt_setup *q);
1460
Takashi Iwaid5191e52009-11-16 14:58:17 +01001461/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001462 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463 * @codec: the CODEC to clean up
1464 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001465 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001466 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1468 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001469{
Takashi Iwaieb541332010-08-06 13:48:11 +02001470 struct hda_cvt_setup *p;
1471
Takashi Iwai888afa12008-03-18 09:57:50 +01001472 if (!nid)
1473 return;
1474
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001475 if (codec->no_sticky_stream)
1476 do_now = 1;
1477
Takashi Iwai888afa12008-03-18 09:57:50 +01001478 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001479 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001480 if (p) {
1481 /* here we just clear the active flag when do_now isn't set;
1482 * actual clean-ups will be done later in
1483 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1484 */
1485 if (do_now)
1486 really_cleanup_stream(codec, p);
1487 else
1488 p->active = 0;
1489 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001490}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001491EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001492
Takashi Iwaieb541332010-08-06 13:48:11 +02001493static void really_cleanup_stream(struct hda_codec *codec,
1494 struct hda_cvt_setup *q)
1495{
1496 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001497 if (q->stream_tag || q->channel_id)
1498 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1499 if (q->format_id)
1500 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1501);
Takashi Iwaieb541332010-08-06 13:48:11 +02001502 memset(q, 0, sizeof(*q));
1503 q->nid = nid;
1504}
1505
1506/* clean up the all conflicting obsolete streams */
1507static void purify_inactive_streams(struct hda_codec *codec)
1508{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001509 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001510 int i;
1511
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001512 list_for_each_entry(c, &codec->bus->codec_list, list) {
1513 for (i = 0; i < c->cvt_setups.used; i++) {
1514 struct hda_cvt_setup *p;
1515 p = snd_array_elem(&c->cvt_setups, i);
1516 if (p->dirty)
1517 really_cleanup_stream(c, p);
1518 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001519 }
1520}
1521
Takashi Iwai2a439522011-07-26 09:52:50 +02001522#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001523/* clean up all streams; called from suspend */
1524static void hda_cleanup_all_streams(struct hda_codec *codec)
1525{
1526 int i;
1527
1528 for (i = 0; i < codec->cvt_setups.used; i++) {
1529 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1530 if (p->stream_tag)
1531 really_cleanup_stream(codec, p);
1532 }
1533}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001534#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/*
1537 * amp access functions
1538 */
1539
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001540/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001541#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001542#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001543#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1544#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001546#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001549static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001550 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Takashi Iwai01751f52007-08-10 16:59:39 +02001552 memset(cache, 0, sizeof(*cache));
1553 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001554 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001555}
1556
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001557static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001558{
Takashi Iwai603c4012008-07-30 15:01:44 +02001559 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001563static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Takashi Iwai01751f52007-08-10 16:59:39 +02001565 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1566 u16 cur = cache->hash[idx];
1567 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001570 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (info->key == key)
1572 return info;
1573 cur = info->next;
1574 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001575 return NULL;
1576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001578/* query the hash. allocate an entry if not found. */
1579static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1580 u32 key)
1581{
1582 struct hda_cache_head *info = get_hash(cache, key);
1583 if (!info) {
1584 u16 idx, cur;
1585 /* add a new hash entry */
1586 info = snd_array_new(&cache->buf);
1587 if (!info)
1588 return NULL;
1589 cur = snd_array_index(&cache->buf, info);
1590 info->key = key;
1591 info->val = 0;
1592 idx = key % (u16)ARRAY_SIZE(cache->hash);
1593 info->next = cache->hash[idx];
1594 cache->hash[idx] = cur;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return info;
1597}
1598
Takashi Iwai01751f52007-08-10 16:59:39 +02001599/* query and allocate an amp hash entry */
1600static inline struct hda_amp_info *
1601get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1602{
1603 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1604}
1605
Takashi Iwaid5191e52009-11-16 14:58:17 +01001606/**
1607 * query_amp_caps - query AMP capabilities
1608 * @codec: the HD-auio codec
1609 * @nid: the NID to query
1610 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1611 *
1612 * Query AMP capabilities for the given widget and direction.
1613 * Returns the obtained capability bits.
1614 *
1615 * When cap bits have been already read, this doesn't read again but
1616 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001618u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001620 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1623 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001625 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 info->amp_caps = snd_hda_param_read(codec, nid,
1629 direction == HDA_OUTPUT ?
1630 AC_PAR_AMP_OUT_CAP :
1631 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001632 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001633 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 return info->amp_caps;
1636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001637EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Takashi Iwaid5191e52009-11-16 14:58:17 +01001639/**
1640 * snd_hda_override_amp_caps - Override the AMP capabilities
1641 * @codec: the CODEC to clean up
1642 * @nid: the NID to clean up
1643 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1644 * @caps: the capability bits to set
1645 *
1646 * Override the cached AMP caps bits value by the given one.
1647 * This function is useful if the driver needs to adjust the AMP ranges,
1648 * e.g. limit to 0dB, etc.
1649 *
1650 * Returns zero if successful or a negative error code.
1651 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001652int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1653 unsigned int caps)
1654{
1655 struct hda_amp_info *info;
1656
1657 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1658 if (!info)
1659 return -EINVAL;
1660 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001661 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001662 return 0;
1663}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001664EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001665
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001666static unsigned int
1667query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1668 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001669{
1670 struct hda_amp_info *info;
1671
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001672 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001673 if (!info)
1674 return 0;
1675 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001676 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001677 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001678 }
1679 return info->amp_caps;
1680}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001681
1682static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1683{
1684 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1685}
1686
Takashi Iwaid5191e52009-11-16 14:58:17 +01001687/**
1688 * snd_hda_query_pin_caps - Query PIN capabilities
1689 * @codec: the HD-auio codec
1690 * @nid: the NID to query
1691 *
1692 * Query PIN capabilities for the given widget.
1693 * Returns the obtained capability bits.
1694 *
1695 * When cap bits have been already read, this doesn't read again but
1696 * returns the cached value.
1697 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001698u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1699{
1700 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1701 read_pin_cap);
1702}
Takashi Iwai1327a322009-03-23 13:07:47 +01001703EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1704
Wu Fengguang864f92b2009-11-18 12:38:02 +08001705/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001706 * snd_hda_override_pin_caps - Override the pin capabilities
1707 * @codec: the CODEC
1708 * @nid: the NID to override
1709 * @caps: the capability bits to set
1710 *
1711 * Override the cached PIN capabilitiy bits value by the given one.
1712 *
1713 * Returns zero if successful or a negative error code.
1714 */
1715int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1716 unsigned int caps)
1717{
1718 struct hda_amp_info *info;
1719 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1720 if (!info)
1721 return -ENOMEM;
1722 info->amp_caps = caps;
1723 info->head.val |= INFO_AMP_CAPS;
1724 return 0;
1725}
1726EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728/*
1729 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001730 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001732static unsigned int get_vol_mute(struct hda_codec *codec,
1733 struct hda_amp_info *info, hda_nid_t nid,
1734 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 u32 val, parm;
1737
Takashi Iwai01751f52007-08-10 16:59:39 +02001738 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001739 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1742 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1743 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001744 val = snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001747 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001748 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
1751/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001752 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001754static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001755 hda_nid_t nid, int ch, int direction, int index,
1756 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 u32 parm;
1759
1760 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1761 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1762 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001763 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1764 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1765 ; /* set the zero value as a fake mute */
1766 else
1767 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001769 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772/**
1773 * snd_hda_codec_amp_read - Read AMP value
1774 * @codec: HD-audio codec
1775 * @nid: NID to read the AMP value
1776 * @ch: channel (left=0 or right=1)
1777 * @direction: #HDA_INPUT or #HDA_OUTPUT
1778 * @index: the index value (only for input direction)
1779 *
1780 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Takashi Iwai834be882006-03-01 14:16:17 +01001782int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1783 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001785 struct hda_amp_info *info;
1786 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1787 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001789 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001791EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Takashi Iwaid5191e52009-11-16 14:58:17 +01001793/**
1794 * snd_hda_codec_amp_update - update the AMP value
1795 * @codec: HD-audio codec
1796 * @nid: NID to read the AMP value
1797 * @ch: channel (left=0 or right=1)
1798 * @direction: #HDA_INPUT or #HDA_OUTPUT
1799 * @idx: the index value (only for input direction)
1800 * @mask: bit mask to set
1801 * @val: the bits value to set
1802 *
1803 * Update the AMP value with a bit mask.
1804 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001805 */
Takashi Iwai834be882006-03-01 14:16:17 +01001806int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1807 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001809 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001810
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1812 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001814 if (snd_BUG_ON(mask & ~0xff))
1815 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001816 val &= mask;
1817 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001818 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001820 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 1;
1822}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001823EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Takashi Iwaid5191e52009-11-16 14:58:17 +01001825/**
1826 * snd_hda_codec_amp_stereo - update the AMP stereo values
1827 * @codec: HD-audio codec
1828 * @nid: NID to read the AMP value
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1833 *
1834 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1835 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001836 */
1837int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1838 int direction, int idx, int mask, int val)
1839{
1840 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001841
1842 if (snd_BUG_ON(mask & ~0xff))
1843 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001844 for (ch = 0; ch < 2; ch++)
1845 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1846 idx, mask, val);
1847 return ret;
1848}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001849EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001850
Takashi Iwai2a439522011-07-26 09:52:50 +02001851#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001852/**
1853 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1854 * @codec: HD-audio codec
1855 *
1856 * Resume the all amp commands from the cache.
1857 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001858void snd_hda_codec_resume_amp(struct hda_codec *codec)
1859{
Takashi Iwai603c4012008-07-30 15:01:44 +02001860 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001861 int i;
1862
Takashi Iwai603c4012008-07-30 15:01:44 +02001863 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001864 u32 key = buffer->head.key;
1865 hda_nid_t nid;
1866 unsigned int idx, dir, ch;
1867 if (!key)
1868 continue;
1869 nid = key & 0xff;
1870 idx = (key >> 16) & 0xff;
1871 dir = (key >> 24) & 0xff;
1872 for (ch = 0; ch < 2; ch++) {
1873 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1874 continue;
1875 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1876 buffer->vol[ch]);
1877 }
1878 }
1879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001880EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001881#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001883static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1884 unsigned int ofs)
1885{
1886 u32 caps = query_amp_caps(codec, nid, dir);
1887 /* get num steps */
1888 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1889 if (ofs < caps)
1890 caps -= ofs;
1891 return caps;
1892}
1893
Takashi Iwaid5191e52009-11-16 14:58:17 +01001894/**
1895 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1896 *
1897 * The control element is supposed to have the private_value field
1898 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1899 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001900int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 u16 nid = get_amp_nid(kcontrol);
1905 u8 chs = get_amp_channels(kcontrol);
1906 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001907 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1910 uinfo->count = chs == 3 ? 2 : 1;
1911 uinfo->value.integer.min = 0;
1912 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1913 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001914 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001915 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1916 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return -EINVAL;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001921EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001923
1924static inline unsigned int
1925read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1926 int ch, int dir, int idx, unsigned int ofs)
1927{
1928 unsigned int val;
1929 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1930 val &= HDA_AMP_VOLMASK;
1931 if (val >= ofs)
1932 val -= ofs;
1933 else
1934 val = 0;
1935 return val;
1936}
1937
1938static inline int
1939update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1940 int ch, int dir, int idx, unsigned int ofs,
1941 unsigned int val)
1942{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001943 unsigned int maxval;
1944
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001945 if (val > 0)
1946 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001947 /* ofs = 0: raw max value */
1948 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001949 if (val > maxval)
1950 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001951 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1952 HDA_AMP_VOLMASK, val);
1953}
1954
Takashi Iwaid5191e52009-11-16 14:58:17 +01001955/**
1956 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1957 *
1958 * The control element is supposed to have the private_value field
1959 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1960 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001961int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1962 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1965 hda_nid_t nid = get_amp_nid(kcontrol);
1966 int chs = get_amp_channels(kcontrol);
1967 int dir = get_amp_direction(kcontrol);
1968 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001969 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 long *valp = ucontrol->value.integer.value;
1971
1972 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001973 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001978EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980/**
1981 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1982 *
1983 * The control element is supposed to have the private_value field
1984 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1985 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001986int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990 hda_nid_t nid = get_amp_nid(kcontrol);
1991 int chs = get_amp_channels(kcontrol);
1992 int dir = get_amp_direction(kcontrol);
1993 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001994 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 long *valp = ucontrol->value.integer.value;
1996 int change = 0;
1997
Takashi Iwaicb53c622007-08-10 17:21:45 +02001998 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001999 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002001 valp++;
2002 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002003 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002004 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002005 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return change;
2007}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002008EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2012 *
2013 * The control element is supposed to have the private_value field
2014 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2015 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002016int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2017 unsigned int size, unsigned int __user *_tlv)
2018{
2019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2020 hda_nid_t nid = get_amp_nid(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002022 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002023 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002024 u32 caps, val1, val2;
2025
2026 if (size < 4 * sizeof(unsigned int))
2027 return -ENOMEM;
2028 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002029 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2030 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002031 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002032 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002033 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002034 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002035 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002036 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2037 return -EFAULT;
2038 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2039 return -EFAULT;
2040 if (put_user(val1, _tlv + 2))
2041 return -EFAULT;
2042 if (put_user(val2, _tlv + 3))
2043 return -EFAULT;
2044 return 0;
2045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002046EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002047
Takashi Iwaid5191e52009-11-16 14:58:17 +01002048/**
2049 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2050 * @codec: HD-audio codec
2051 * @nid: NID of a reference widget
2052 * @dir: #HDA_INPUT or #HDA_OUTPUT
2053 * @tlv: TLV data to be stored, at least 4 elements
2054 *
2055 * Set (static) TLV data for a virtual master volume using the AMP caps
2056 * obtained from the reference NID.
2057 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002058 */
2059void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2060 unsigned int *tlv)
2061{
2062 u32 caps;
2063 int nums, step;
2064
2065 caps = query_amp_caps(codec, nid, dir);
2066 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2067 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2068 step = (step + 1) * 25;
2069 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2070 tlv[1] = 2 * sizeof(unsigned int);
2071 tlv[2] = -nums * step;
2072 tlv[3] = step;
2073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002074EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002075
2076/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002077static struct snd_kcontrol *
2078_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2079 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002080{
2081 struct snd_ctl_elem_id id;
2082 memset(&id, 0, sizeof(id));
2083 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002084 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002085 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2086 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002087 strcpy(id.name, name);
2088 return snd_ctl_find_id(codec->bus->card, &id);
2089}
2090
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091/**
2092 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2093 * @codec: HD-audio codec
2094 * @name: ctl id name string
2095 *
2096 * Get the control element with the given id string and IFACE_MIXER.
2097 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002098struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2099 const char *name)
2100{
2101 return _snd_hda_find_mixer_ctl(codec, name, 0);
2102}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002103EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002104
Takashi Iwai1afe2062010-12-23 10:17:52 +01002105static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2106{
2107 int idx;
2108 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2109 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2110 return idx;
2111 }
2112 return -EBUSY;
2113}
2114
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002116 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117 * @codec: HD-audio codec
2118 * @nid: corresponding NID (optional)
2119 * @kctl: the control element to assign
2120 *
2121 * Add the given control element to an array inside the codec instance.
2122 * All control elements belonging to a codec are supposed to be added
2123 * by this function so that a proper clean-up works at the free or
2124 * reconfiguration time.
2125 *
2126 * If non-zero @nid is passed, the NID is assigned to the control element.
2127 * The assignment is shown in the codec proc file.
2128 *
2129 * snd_hda_ctl_add() checks the control subdev id field whether
2130 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002131 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2132 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002133 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002134int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2135 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002136{
2137 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002138 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002139 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002140
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002141 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002142 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (nid == 0)
2144 nid = get_amp_nid_(kctl->private_value);
2145 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002146 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2147 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002148 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002149 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002150 err = snd_ctl_add(codec->bus->card, kctl);
2151 if (err < 0)
2152 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002153 item = snd_array_new(&codec->mixers);
2154 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002155 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002156 item->kctl = kctl;
2157 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002158 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002159 return 0;
2160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002161EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002162
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002164 * snd_hda_add_nid - Assign a NID to a control element
2165 * @codec: HD-audio codec
2166 * @nid: corresponding NID (optional)
2167 * @kctl: the control element to assign
2168 * @index: index to kctl
2169 *
2170 * Add the given control element to an array inside the codec instance.
2171 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2172 * NID:KCTL mapping - for example "Capture Source" selector.
2173 */
2174int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2175 unsigned int index, hda_nid_t nid)
2176{
2177 struct hda_nid_item *item;
2178
2179 if (nid > 0) {
2180 item = snd_array_new(&codec->nids);
2181 if (!item)
2182 return -ENOMEM;
2183 item->kctl = kctl;
2184 item->index = index;
2185 item->nid = nid;
2186 return 0;
2187 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002188 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2189 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002190 return -EINVAL;
2191}
2192EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2193
2194/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2196 * @codec: HD-audio codec
2197 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002198void snd_hda_ctls_clear(struct hda_codec *codec)
2199{
2200 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002201 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002202 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002203 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002205 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206}
2207
Takashi Iwaia65d6292009-02-23 16:57:04 +01002208/* pseudo device locking
2209 * toggle card->shutdown to allow/disallow the device access (as a hack)
2210 */
2211static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002212{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002213 spin_lock(&card->files_lock);
2214 if (card->shutdown) {
2215 spin_unlock(&card->files_lock);
2216 return -EINVAL;
2217 }
2218 card->shutdown = 1;
2219 spin_unlock(&card->files_lock);
2220 return 0;
2221}
2222
2223static void hda_unlock_devices(struct snd_card *card)
2224{
2225 spin_lock(&card->files_lock);
2226 card->shutdown = 0;
2227 spin_unlock(&card->files_lock);
2228}
2229
Takashi Iwaid5191e52009-11-16 14:58:17 +01002230/**
2231 * snd_hda_codec_reset - Clear all objects assigned to the codec
2232 * @codec: HD-audio codec
2233 *
2234 * This frees the all PCM and control elements assigned to the codec, and
2235 * clears the caches and restores the pin default configurations.
2236 *
2237 * When a device is being used, it returns -EBSY. If successfully freed,
2238 * returns zero.
2239 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002240int snd_hda_codec_reset(struct hda_codec *codec)
2241{
2242 struct snd_card *card = codec->bus->card;
2243 int i, pcm;
2244
2245 if (hda_lock_devices(card) < 0)
2246 return -EBUSY;
2247 /* check whether the codec isn't used by any mixer or PCM streams */
2248 if (!list_empty(&card->ctl_files)) {
2249 hda_unlock_devices(card);
2250 return -EBUSY;
2251 }
2252 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2253 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2254 if (!cpcm->pcm)
2255 continue;
2256 if (cpcm->pcm->streams[0].substream_opened ||
2257 cpcm->pcm->streams[1].substream_opened) {
2258 hda_unlock_devices(card);
2259 return -EBUSY;
2260 }
2261 }
2262
2263 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002264
2265#ifdef CONFIG_SND_HDA_POWER_SAVE
2266 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002267 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002268#endif
2269 snd_hda_ctls_clear(codec);
2270 /* relase PCMs */
2271 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002272 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002273 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002274 clear_bit(codec->pcm_info[i].device,
2275 codec->bus->pcm_dev_bits);
2276 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002277 }
2278 if (codec->patch_ops.free)
2279 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002280 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002281 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002282 codec->spec = NULL;
2283 free_hda_cache(&codec->amp_cache);
2284 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002285 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2286 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002287 /* free only driver_pins so that init_pins + user_pins are restored */
2288 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002289 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002290 codec->num_pcms = 0;
2291 codec->pcm_info = NULL;
2292 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002293 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2294 codec->slave_dig_outs = NULL;
2295 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002296 module_put(codec->owner);
2297 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002298
2299 /* allow device access again */
2300 hda_unlock_devices(card);
2301 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002302}
2303
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002304typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2305
2306/* apply the function to all matching slave ctls in the mixer list */
2307static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002308 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002309{
2310 struct hda_nid_item *items;
2311 const char * const *s;
2312 int i, err;
2313
2314 items = codec->mixers.list;
2315 for (i = 0; i < codec->mixers.used; i++) {
2316 struct snd_kcontrol *sctl = items[i].kctl;
2317 if (!sctl || !sctl->id.name ||
2318 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2319 continue;
2320 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002321 char tmpname[sizeof(sctl->id.name)];
2322 const char *name = *s;
2323 if (suffix) {
2324 snprintf(tmpname, sizeof(tmpname), "%s %s",
2325 name, suffix);
2326 name = tmpname;
2327 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002328 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002329 err = func(data, sctl);
2330 if (err)
2331 return err;
2332 break;
2333 }
2334 }
2335 }
2336 return 0;
2337}
2338
2339static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2340{
2341 return 1;
2342}
2343
Takashi Iwai18478e82012-03-09 17:51:10 +01002344/* guess the value corresponding to 0dB */
2345static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2346{
2347 int _tlv[4];
2348 const int *tlv = NULL;
2349 int val = -1;
2350
2351 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2352 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2353 mm_segment_t fs = get_fs();
2354 set_fs(get_ds());
2355 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2356 tlv = _tlv;
2357 set_fs(fs);
2358 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2359 tlv = kctl->tlv.p;
2360 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2361 val = -tlv[2] / tlv[3];
2362 return val;
2363}
2364
2365/* call kctl->put with the given value(s) */
2366static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2367{
2368 struct snd_ctl_elem_value *ucontrol;
2369 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2370 if (!ucontrol)
2371 return -ENOMEM;
2372 ucontrol->value.integer.value[0] = val;
2373 ucontrol->value.integer.value[1] = val;
2374 kctl->put(kctl, ucontrol);
2375 kfree(ucontrol);
2376 return 0;
2377}
2378
2379/* initialize the slave volume with 0dB */
2380static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2381{
2382 int offset = get_kctl_0dB_offset(slave);
2383 if (offset > 0)
2384 put_kctl_with_value(slave, offset);
2385 return 0;
2386}
2387
2388/* unmute the slave */
2389static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2390{
2391 return put_kctl_with_value(slave, 1);
2392}
2393
Takashi Iwaid5191e52009-11-16 14:58:17 +01002394/**
2395 * snd_hda_add_vmaster - create a virtual master control and add slaves
2396 * @codec: HD-audio codec
2397 * @name: vmaster control name
2398 * @tlv: TLV data (optional)
2399 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002400 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002401 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002402 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002403 *
2404 * Create a virtual master control with the given name. The TLV data
2405 * must be either NULL or a valid data.
2406 *
2407 * @slaves is a NULL-terminated array of strings, each of which is a
2408 * slave control name. All controls with these names are assigned to
2409 * the new virtual master control.
2410 *
2411 * This function returns zero if successful or a negative error code.
2412 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002413int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002414 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002415 const char *suffix, bool init_slave_vol,
2416 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002417{
2418 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002419 int err;
2420
Takashi Iwai29e58532012-03-12 12:25:03 +01002421 if (ctl_ret)
2422 *ctl_ret = NULL;
2423
Takashi Iwai9322ca52012-02-03 14:28:01 +01002424 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002425 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002426 snd_printdd("No slave found for %s\n", name);
2427 return 0;
2428 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002429 kctl = snd_ctl_make_virtual_master(name, tlv);
2430 if (!kctl)
2431 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002432 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002433 if (err < 0)
2434 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002435
Takashi Iwai9322ca52012-02-03 14:28:01 +01002436 err = map_slaves(codec, slaves, suffix,
2437 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002438 if (err < 0)
2439 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002440
2441 /* init with master mute & zero volume */
2442 put_kctl_with_value(kctl, 0);
2443 if (init_slave_vol)
2444 map_slaves(codec, slaves, suffix,
2445 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2446
Takashi Iwai29e58532012-03-12 12:25:03 +01002447 if (ctl_ret)
2448 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002449 return 0;
2450}
Takashi Iwai18478e82012-03-09 17:51:10 +01002451EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002452
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002453/*
2454 * mute-LED control using vmaster
2455 */
2456static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
2459 static const char * const texts[] = {
2460 "Off", "On", "Follow Master"
2461 };
2462 unsigned int index;
2463
2464 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2465 uinfo->count = 1;
2466 uinfo->value.enumerated.items = 3;
2467 index = uinfo->value.enumerated.item;
2468 if (index >= 3)
2469 index = 2;
2470 strcpy(uinfo->value.enumerated.name, texts[index]);
2471 return 0;
2472}
2473
2474static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_value *ucontrol)
2476{
2477 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2478 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2479 return 0;
2480}
2481
2482static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2483 struct snd_ctl_elem_value *ucontrol)
2484{
2485 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2486 unsigned int old_mode = hook->mute_mode;
2487
2488 hook->mute_mode = ucontrol->value.enumerated.item[0];
2489 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2490 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2491 if (old_mode == hook->mute_mode)
2492 return 0;
2493 snd_hda_sync_vmaster_hook(hook);
2494 return 1;
2495}
2496
2497static struct snd_kcontrol_new vmaster_mute_mode = {
2498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2499 .name = "Mute-LED Mode",
2500 .info = vmaster_mute_mode_info,
2501 .get = vmaster_mute_mode_get,
2502 .put = vmaster_mute_mode_put,
2503};
2504
2505/*
2506 * Add a mute-LED hook with the given vmaster switch kctl
2507 * "Mute-LED Mode" control is automatically created and associated with
2508 * the given hook.
2509 */
2510int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2511 struct hda_vmaster_mute_hook *hook)
2512{
2513 struct snd_kcontrol *kctl;
2514
2515 if (!hook->hook || !hook->sw_kctl)
2516 return 0;
2517 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2518 hook->codec = codec;
2519 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2520 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2521 if (!kctl)
2522 return -ENOMEM;
2523 return snd_hda_ctl_add(codec, 0, kctl);
2524}
2525EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2526
2527/*
2528 * Call the hook with the current value for synchronization
2529 * Should be called in init callback
2530 */
2531void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2532{
2533 if (!hook->hook || !hook->codec)
2534 return;
2535 switch (hook->mute_mode) {
2536 case HDA_VMUTE_FOLLOW_MASTER:
2537 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2538 break;
2539 default:
2540 hook->hook(hook->codec, hook->mute_mode);
2541 break;
2542 }
2543}
2544EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2545
2546
Takashi Iwaid5191e52009-11-16 14:58:17 +01002547/**
2548 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2549 *
2550 * The control element is supposed to have the private_value field
2551 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2552 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002553int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 int chs = get_amp_channels(kcontrol);
2557
2558 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2559 uinfo->count = chs == 3 ? 2 : 1;
2560 uinfo->value.integer.min = 0;
2561 uinfo->value.integer.max = 1;
2562 return 0;
2563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002564EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Takashi Iwaid5191e52009-11-16 14:58:17 +01002566/**
2567 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2568 *
2569 * The control element is supposed to have the private_value field
2570 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2571 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002572int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 hda_nid_t nid = get_amp_nid(kcontrol);
2577 int chs = get_amp_channels(kcontrol);
2578 int dir = get_amp_direction(kcontrol);
2579 int idx = get_amp_index(kcontrol);
2580 long *valp = ucontrol->value.integer.value;
2581
2582 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002584 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002586 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002587 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 0;
2589}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002590EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Takashi Iwaid5191e52009-11-16 14:58:17 +01002592/**
2593 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2594 *
2595 * The control element is supposed to have the private_value field
2596 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2597 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002598int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2602 hda_nid_t nid = get_amp_nid(kcontrol);
2603 int chs = get_amp_channels(kcontrol);
2604 int dir = get_amp_direction(kcontrol);
2605 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 long *valp = ucontrol->value.integer.value;
2607 int change = 0;
2608
Takashi Iwaicb53c622007-08-10 17:21:45 +02002609 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002610 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002611 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002612 HDA_AMP_MUTE,
2613 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002614 valp++;
2615 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002616 if (chs & 2)
2617 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002618 HDA_AMP_MUTE,
2619 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002620 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002621 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return change;
2623}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002624EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Takashi Iwai67d634c2009-11-16 15:35:59 +01002626#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002627/**
2628 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2629 *
2630 * This function calls snd_hda_enable_beep_device(), which behaves differently
2631 * depending on beep_mode option.
2632 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002633int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2635{
2636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2637 long *valp = ucontrol->value.integer.value;
2638
2639 snd_hda_enable_beep_device(codec, *valp);
2640 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2641}
2642EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002643#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645/*
Takashi Iwai985be542005-11-02 18:26:49 +01002646 * bound volume controls
2647 *
2648 * bind multiple volumes (# indices, from 0)
2649 */
2650
2651#define AMP_VAL_IDX_SHIFT 19
2652#define AMP_VAL_IDX_MASK (0x0f<<19)
2653
Takashi Iwaid5191e52009-11-16 14:58:17 +01002654/**
2655 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2656 *
2657 * The control element is supposed to have the private_value field
2658 * set up via HDA_BIND_MUTE*() macros.
2659 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002660int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2661 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002662{
2663 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2664 unsigned long pval;
2665 int err;
2666
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002667 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002668 pval = kcontrol->private_value;
2669 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2670 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2671 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002672 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002673 return err;
2674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002675EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002676
Takashi Iwaid5191e52009-11-16 14:58:17 +01002677/**
2678 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2679 *
2680 * The control element is supposed to have the private_value field
2681 * set up via HDA_BIND_MUTE*() macros.
2682 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002683int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002685{
2686 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2687 unsigned long pval;
2688 int i, indices, err = 0, change = 0;
2689
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002690 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002691 pval = kcontrol->private_value;
2692 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2693 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2695 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002696 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2697 if (err < 0)
2698 break;
2699 change |= err;
2700 }
2701 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002702 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002703 return err < 0 ? err : change;
2704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002705EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002706
Takashi Iwaid5191e52009-11-16 14:58:17 +01002707/**
2708 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2709 *
2710 * The control element is supposed to have the private_value field
2711 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002712 */
2713int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_info *uinfo)
2715{
2716 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2717 struct hda_bind_ctls *c;
2718 int err;
2719
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002720 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002721 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002722 kcontrol->private_value = *c->values;
2723 err = c->ops->info(kcontrol, uinfo);
2724 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002725 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002726 return err;
2727}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002728EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002729
Takashi Iwaid5191e52009-11-16 14:58:17 +01002730/**
2731 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2732 *
2733 * The control element is supposed to have the private_value field
2734 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2735 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002736int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_value *ucontrol)
2738{
2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2740 struct hda_bind_ctls *c;
2741 int err;
2742
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002743 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002744 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002745 kcontrol->private_value = *c->values;
2746 err = c->ops->get(kcontrol, ucontrol);
2747 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002748 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002749 return err;
2750}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002751EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002752
Takashi Iwaid5191e52009-11-16 14:58:17 +01002753/**
2754 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2755 *
2756 * The control element is supposed to have the private_value field
2757 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2758 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002759int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_value *ucontrol)
2761{
2762 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2763 struct hda_bind_ctls *c;
2764 unsigned long *vals;
2765 int err = 0, change = 0;
2766
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002767 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002768 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002769 for (vals = c->values; *vals; vals++) {
2770 kcontrol->private_value = *vals;
2771 err = c->ops->put(kcontrol, ucontrol);
2772 if (err < 0)
2773 break;
2774 change |= err;
2775 }
2776 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002777 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002778 return err < 0 ? err : change;
2779}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002780EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002781
Takashi Iwaid5191e52009-11-16 14:58:17 +01002782/**
2783 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2784 *
2785 * The control element is supposed to have the private_value field
2786 * set up via HDA_BIND_VOL() macro.
2787 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002788int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2789 unsigned int size, unsigned int __user *tlv)
2790{
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2792 struct hda_bind_ctls *c;
2793 int err;
2794
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002795 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002796 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002797 kcontrol->private_value = *c->values;
2798 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2799 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002800 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002801 return err;
2802}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002803EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002804
2805struct hda_ctl_ops snd_hda_bind_vol = {
2806 .info = snd_hda_mixer_amp_volume_info,
2807 .get = snd_hda_mixer_amp_volume_get,
2808 .put = snd_hda_mixer_amp_volume_put,
2809 .tlv = snd_hda_mixer_amp_tlv
2810};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002812
2813struct hda_ctl_ops snd_hda_bind_sw = {
2814 .info = snd_hda_mixer_amp_switch_info,
2815 .get = snd_hda_mixer_amp_switch_get,
2816 .put = snd_hda_mixer_amp_switch_put,
2817 .tlv = snd_hda_mixer_amp_tlv
2818};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002820
2821/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * SPDIF out controls
2823 */
2824
Takashi Iwai0ba21762007-04-16 11:29:14 +02002825static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2829 uinfo->count = 1;
2830 return 0;
2831}
2832
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2837 IEC958_AES0_NONAUDIO |
2838 IEC958_AES0_CON_EMPHASIS_5015 |
2839 IEC958_AES0_CON_NOT_COPYRIGHT;
2840 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2841 IEC958_AES1_CON_ORIGINAL;
2842 return 0;
2843}
2844
Takashi Iwai0ba21762007-04-16 11:29:14 +02002845static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2849 IEC958_AES0_NONAUDIO |
2850 IEC958_AES0_PRO_EMPHASIS_5015;
2851 return 0;
2852}
2853
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002858 int idx = kcontrol->private_value;
2859 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Stephen Warren7c9359762011-06-01 11:14:17 -06002861 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2862 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2863 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2864 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 return 0;
2867}
2868
2869/* convert from SPDIF status bits to HDA SPDIF bits
2870 * bit 0 (DigEn) is always set zero (to be filled later)
2871 */
2872static unsigned short convert_from_spdif_status(unsigned int sbits)
2873{
2874 unsigned short val = 0;
2875
2876 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002877 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002879 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002881 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2882 IEC958_AES0_PRO_EMPHASIS_5015)
2883 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002885 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2886 IEC958_AES0_CON_EMPHASIS_5015)
2887 val |= AC_DIG1_EMPHASIS;
2888 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2889 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002891 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2893 }
2894 return val;
2895}
2896
2897/* convert to SPDIF status bits from HDA SPDIF bits
2898 */
2899static unsigned int convert_to_spdif_status(unsigned short val)
2900{
2901 unsigned int sbits = 0;
2902
Takashi Iwai0ba21762007-04-16 11:29:14 +02002903 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002905 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 sbits |= IEC958_AES0_PROFESSIONAL;
2907 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002908 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2910 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002915 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2917 sbits |= val & (0x7f << 8);
2918 }
2919 return sbits;
2920}
2921
Takashi Iwai2f728532008-09-25 16:32:41 +02002922/* set digital convert verbs both for the given NID and its slaves */
2923static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2924 int verb, int val)
2925{
Takashi Iwaidda14412011-05-02 11:29:30 +02002926 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002927
Takashi Iwai9e976972008-11-25 08:17:20 +01002928 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002929 d = codec->slave_dig_outs;
2930 if (!d)
2931 return;
2932 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002933 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002934}
2935
2936static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2937 int dig1, int dig2)
2938{
2939 if (dig1 != -1)
2940 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2941 if (dig2 != -1)
2942 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2943}
2944
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002949 int idx = kcontrol->private_value;
2950 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2951 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 unsigned short val;
2953 int change;
2954
Ingo Molnar62932df2006-01-16 16:34:20 +01002955 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002956 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2958 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2959 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002960 val = convert_from_spdif_status(spdif->status);
2961 val |= spdif->ctls & 1;
2962 change = spdif->ctls != val;
2963 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002964 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002965 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002966 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return change;
2968}
2969
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002970#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Takashi Iwai0ba21762007-04-16 11:29:14 +02002972static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002976 int idx = kcontrol->private_value;
2977 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Stephen Warren7c9359762011-06-01 11:14:17 -06002979 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return 0;
2981}
2982
Stephen Warren74b654c2011-06-01 11:14:18 -06002983static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2984 int dig1, int dig2)
2985{
2986 set_dig_out_convert(codec, nid, dig1, dig2);
2987 /* unmute amp switch (if any) */
2988 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2989 (dig1 & AC_DIG1_ENABLE))
2990 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2991 HDA_AMP_MUTE, 0);
2992}
2993
Takashi Iwai0ba21762007-04-16 11:29:14 +02002994static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2995 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002998 int idx = kcontrol->private_value;
2999 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3000 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 unsigned short val;
3002 int change;
3003
Ingo Molnar62932df2006-01-16 16:34:20 +01003004 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06003005 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003007 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003008 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003009 spdif->ctls = val;
3010 if (change && nid != (u16)-1)
3011 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003012 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return change;
3014}
3015
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003016static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 {
3018 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3019 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003020 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 .info = snd_hda_spdif_mask_info,
3022 .get = snd_hda_spdif_cmask_get,
3023 },
3024 {
3025 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3026 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003027 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 .info = snd_hda_spdif_mask_info,
3029 .get = snd_hda_spdif_pmask_get,
3030 },
3031 {
3032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003033 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .info = snd_hda_spdif_mask_info,
3035 .get = snd_hda_spdif_default_get,
3036 .put = snd_hda_spdif_default_put,
3037 },
3038 {
3039 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003040 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .info = snd_hda_spdif_out_switch_info,
3042 .get = snd_hda_spdif_out_switch_get,
3043 .put = snd_hda_spdif_out_switch_put,
3044 },
3045 { } /* end */
3046};
3047
3048/**
3049 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3050 * @codec: the HDA codec
3051 * @nid: audio out widget NID
3052 *
3053 * Creates controls related with the SPDIF output.
3054 * Called from each patch supporting the SPDIF out.
3055 *
3056 * Returns 0 if successful, or a negative error code.
3057 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003058int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3059 hda_nid_t associated_nid,
3060 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003063 struct snd_kcontrol *kctl;
3064 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003065 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003066 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Takashi Iwai1afe2062010-12-23 10:17:52 +01003068 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3069 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003070 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3071 return -EBUSY;
3072 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003073 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3075 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003076 if (!kctl)
3077 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003078 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003079 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003080 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return err;
3083 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003084 spdif->nid = cvt_nid;
3085 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003086 AC_VERB_GET_DIGI_CONVERT_1, 0);
3087 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return 0;
3089}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003090EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Stephen Warren7c9359762011-06-01 11:14:17 -06003092struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3093 hda_nid_t nid)
3094{
3095 int i;
3096 for (i = 0; i < codec->spdif_out.used; i++) {
3097 struct hda_spdif_out *spdif =
3098 snd_array_elem(&codec->spdif_out, i);
3099 if (spdif->nid == nid)
3100 return spdif;
3101 }
3102 return NULL;
3103}
3104EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3105
Stephen Warren74b654c2011-06-01 11:14:18 -06003106void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3107{
3108 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3109
3110 mutex_lock(&codec->spdif_mutex);
3111 spdif->nid = (u16)-1;
3112 mutex_unlock(&codec->spdif_mutex);
3113}
3114EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3115
3116void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3117{
3118 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3119 unsigned short val;
3120
3121 mutex_lock(&codec->spdif_mutex);
3122 if (spdif->nid != nid) {
3123 spdif->nid = nid;
3124 val = spdif->ctls;
3125 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3126 }
3127 mutex_unlock(&codec->spdif_mutex);
3128}
3129EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003132 * SPDIF sharing with analog output
3133 */
3134static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3135 struct snd_ctl_elem_value *ucontrol)
3136{
3137 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3138 ucontrol->value.integer.value[0] = mout->share_spdif;
3139 return 0;
3140}
3141
3142static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
3145 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3146 mout->share_spdif = !!ucontrol->value.integer.value[0];
3147 return 0;
3148}
3149
3150static struct snd_kcontrol_new spdif_share_sw = {
3151 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3152 .name = "IEC958 Default PCM Playback Switch",
3153 .info = snd_ctl_boolean_mono_info,
3154 .get = spdif_share_sw_get,
3155 .put = spdif_share_sw_put,
3156};
3157
Takashi Iwaid5191e52009-11-16 14:58:17 +01003158/**
3159 * snd_hda_create_spdif_share_sw - create Default PCM switch
3160 * @codec: the HDA codec
3161 * @mout: multi-out instance
3162 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003163int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3164 struct hda_multi_out *mout)
3165{
3166 if (!mout->dig_out_nid)
3167 return 0;
3168 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003169 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3170 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003172EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003173
3174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 * SPDIF input
3176 */
3177
3178#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3179
Takashi Iwai0ba21762007-04-16 11:29:14 +02003180static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3181 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3184
3185 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3186 return 0;
3187}
3188
Takashi Iwai0ba21762007-04-16 11:29:14 +02003189static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3190 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3193 hda_nid_t nid = kcontrol->private_value;
3194 unsigned int val = !!ucontrol->value.integer.value[0];
3195 int change;
3196
Ingo Molnar62932df2006-01-16 16:34:20 +01003197 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003199 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003201 snd_hda_codec_write_cache(codec, nid, 0,
3202 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003204 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return change;
3206}
3207
Takashi Iwai0ba21762007-04-16 11:29:14 +02003208static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3212 hda_nid_t nid = kcontrol->private_value;
3213 unsigned short val;
3214 unsigned int sbits;
3215
Andrew Paprocki3982d172007-12-19 12:13:44 +01003216 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 sbits = convert_to_spdif_status(val);
3218 ucontrol->value.iec958.status[0] = sbits;
3219 ucontrol->value.iec958.status[1] = sbits >> 8;
3220 ucontrol->value.iec958.status[2] = sbits >> 16;
3221 ucontrol->value.iec958.status[3] = sbits >> 24;
3222 return 0;
3223}
3224
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003225static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 {
3227 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003228 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 .info = snd_hda_spdif_in_switch_info,
3230 .get = snd_hda_spdif_in_switch_get,
3231 .put = snd_hda_spdif_in_switch_put,
3232 },
3233 {
3234 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003236 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 .info = snd_hda_spdif_mask_info,
3238 .get = snd_hda_spdif_in_status_get,
3239 },
3240 { } /* end */
3241};
3242
3243/**
3244 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3245 * @codec: the HDA codec
3246 * @nid: audio in widget NID
3247 *
3248 * Creates controls related with the SPDIF input.
3249 * Called from each patch supporting the SPDIF in.
3250 *
3251 * Returns 0 if successful, or a negative error code.
3252 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003253int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003256 struct snd_kcontrol *kctl;
3257 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003258 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Takashi Iwai1afe2062010-12-23 10:17:52 +01003260 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3261 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003262 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3263 return -EBUSY;
3264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3266 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003267 if (!kctl)
3268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003270 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003271 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return err;
3273 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003274 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003275 snd_hda_codec_read(codec, nid, 0,
3276 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003277 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003280EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Takashi Iwai2a439522011-07-26 09:52:50 +02003282#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003283/*
3284 * command cache
3285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003287/* build a 32bit cache key with the widget id and the command parameter */
3288#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3289#define get_cmd_cache_nid(key) ((key) & 0xff)
3290#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3291
3292/**
3293 * snd_hda_codec_write_cache - send a single command with caching
3294 * @codec: the HDA codec
3295 * @nid: NID to send the command
3296 * @direct: direct flag
3297 * @verb: the verb to send
3298 * @parm: the parameter for the verb
3299 *
3300 * Send a single command without waiting for response.
3301 *
3302 * Returns 0 if successful, or a negative error code.
3303 */
3304int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3305 int direct, unsigned int verb, unsigned int parm)
3306{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003307 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3308 struct hda_cache_head *c;
3309 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003310
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003311 if (err < 0)
3312 return err;
3313 /* parm may contain the verb stuff for get/set amp */
3314 verb = verb | (parm >> 8);
3315 parm &= 0xff;
3316 key = build_cmd_cache_key(nid, verb);
3317 mutex_lock(&codec->bus->cmd_mutex);
3318 c = get_alloc_hash(&codec->cmd_cache, key);
3319 if (c)
3320 c->val = parm;
3321 mutex_unlock(&codec->bus->cmd_mutex);
3322 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003324EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003325
Takashi Iwaid5191e52009-11-16 14:58:17 +01003326/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003327 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3328 * @codec: the HDA codec
3329 * @nid: NID to send the command
3330 * @direct: direct flag
3331 * @verb: the verb to send
3332 * @parm: the parameter for the verb
3333 *
3334 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3335 * command if the parameter is already identical with the cached value.
3336 * If not, it sends the command and refreshes the cache.
3337 *
3338 * Returns 0 if successful, or a negative error code.
3339 */
3340int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3341 int direct, unsigned int verb, unsigned int parm)
3342{
3343 struct hda_cache_head *c;
3344 u32 key;
3345
3346 /* parm may contain the verb stuff for get/set amp */
3347 verb = verb | (parm >> 8);
3348 parm &= 0xff;
3349 key = build_cmd_cache_key(nid, verb);
3350 mutex_lock(&codec->bus->cmd_mutex);
3351 c = get_hash(&codec->cmd_cache, key);
3352 if (c && c->val == parm) {
3353 mutex_unlock(&codec->bus->cmd_mutex);
3354 return 0;
3355 }
3356 mutex_unlock(&codec->bus->cmd_mutex);
3357 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3358}
3359EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3360
3361/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003362 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3363 * @codec: HD-audio codec
3364 *
3365 * Execute all verbs recorded in the command caches to resume.
3366 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003367void snd_hda_codec_resume_cache(struct hda_codec *codec)
3368{
Takashi Iwai603c4012008-07-30 15:01:44 +02003369 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003370 int i;
3371
Takashi Iwai603c4012008-07-30 15:01:44 +02003372 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003373 u32 key = buffer->key;
3374 if (!key)
3375 continue;
3376 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3377 get_cmd_cache_cmd(key), buffer->val);
3378 }
3379}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003380EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003381
3382/**
3383 * snd_hda_sequence_write_cache - sequence writes with caching
3384 * @codec: the HDA codec
3385 * @seq: VERB array to send
3386 *
3387 * Send the commands sequentially from the given array.
3388 * Thte commands are recorded on cache for power-save and resume.
3389 * The array must be terminated with NID=0.
3390 */
3391void snd_hda_sequence_write_cache(struct hda_codec *codec,
3392 const struct hda_verb *seq)
3393{
3394 for (; seq->nid; seq++)
3395 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3396 seq->param);
3397}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003398EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003399#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003400
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003401void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3402 unsigned int power_state,
3403 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003404{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003405 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003406 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003407
Takashi Iwaicb53c622007-08-10 17:21:45 +02003408 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003409 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003410 if (!(wcaps & AC_WCAP_POWER))
3411 continue;
3412 /* don't power down the widget if it controls eapd and
3413 * EAPD_BTLENABLE is set.
3414 */
3415 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3416 get_wcaps_type(wcaps) == AC_WID_PIN &&
3417 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3418 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003419 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003420 if (eapd & 0x02)
3421 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003422 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003423 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3424 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003425 }
3426
Takashi Iwaicb53c622007-08-10 17:21:45 +02003427 if (power_state == AC_PWRST_D0) {
3428 unsigned long end_time;
3429 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003430 /* wait until the codec reachs to D0 */
3431 end_time = jiffies + msecs_to_jiffies(500);
3432 do {
3433 state = snd_hda_codec_read(codec, fg, 0,
3434 AC_VERB_GET_POWER_STATE, 0);
3435 if (state == power_state)
3436 break;
3437 msleep(1);
3438 } while (time_after_eq(end_time, jiffies));
3439 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003440}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003441EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3442
3443/*
3444 * set power state of the codec
3445 */
3446static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3447 unsigned int power_state)
3448{
3449 if (codec->patch_ops.set_power_state) {
3450 codec->patch_ops.set_power_state(codec, fg, power_state);
3451 return;
3452 }
3453
3454 /* this delay seems necessary to avoid click noise at power-down */
3455 if (power_state == AC_PWRST_D3)
3456 msleep(100);
3457 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3458 power_state);
3459 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3460}
Takashi Iwai54d17402005-11-21 16:33:22 +01003461
Takashi Iwai11aeff02008-07-30 15:01:46 +02003462#ifdef CONFIG_SND_HDA_HWDEP
3463/* execute additional init verbs */
3464static void hda_exec_init_verbs(struct hda_codec *codec)
3465{
3466 if (codec->init_verbs.list)
3467 snd_hda_sequence_write(codec, codec->init_verbs.list);
3468}
3469#else
3470static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3471#endif
3472
Takashi Iwai2a439522011-07-26 09:52:50 +02003473#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003474/*
3475 * call suspend and power-down; used both from PM and power-save
3476 */
3477static void hda_call_codec_suspend(struct hda_codec *codec)
3478{
3479 if (codec->patch_ops.suspend)
3480 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003481 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003482 hda_set_power_state(codec,
3483 codec->afg ? codec->afg : codec->mfg,
3484 AC_PWRST_D3);
3485#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003486 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003487 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003488 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003489 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003490 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003491#endif
3492}
3493
3494/*
3495 * kick up codec; used both from PM and power-save
3496 */
3497static void hda_call_codec_resume(struct hda_codec *codec)
3498{
3499 hda_set_power_state(codec,
3500 codec->afg ? codec->afg : codec->mfg,
3501 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003502 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003503 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003504 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003505 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003506 if (codec->patch_ops.resume)
3507 codec->patch_ops.resume(codec);
3508 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003509 if (codec->patch_ops.init)
3510 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003511 snd_hda_codec_resume_amp(codec);
3512 snd_hda_codec_resume_cache(codec);
3513 }
3514}
Takashi Iwai2a439522011-07-26 09:52:50 +02003515#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003516
Takashi Iwai54d17402005-11-21 16:33:22 +01003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/**
3519 * snd_hda_build_controls - build mixer controls
3520 * @bus: the BUS
3521 *
3522 * Creates mixer controls for each codec included in the bus.
3523 *
3524 * Returns 0 if successful, otherwise a negative error code.
3525 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003526int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003528 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Takashi Iwai0ba21762007-04-16 11:29:14 +02003530 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003531 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003532 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003533 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003534 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003535 err = snd_hda_codec_reset(codec);
3536 if (err < 0) {
3537 printk(KERN_ERR
3538 "hda_codec: cannot revert codec\n");
3539 return err;
3540 }
3541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003543 return 0;
3544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003545EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003547int snd_hda_codec_build_controls(struct hda_codec *codec)
3548{
3549 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003550 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003551 /* continue to initialize... */
3552 if (codec->patch_ops.init)
3553 err = codec->patch_ops.init(codec);
3554 if (!err && codec->patch_ops.build_controls)
3555 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003556 if (err < 0)
3557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return 0;
3559}
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561/*
3562 * stream formats
3563 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003564struct hda_rate_tbl {
3565 unsigned int hz;
3566 unsigned int alsa_bits;
3567 unsigned int hda_fmt;
3568};
3569
Takashi Iwai92f10b32010-08-03 14:21:00 +02003570/* rate = base * mult / div */
3571#define HDA_RATE(base, mult, div) \
3572 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3573 (((div) - 1) << AC_FMT_DIV_SHIFT))
3574
Takashi Iwaibefdf312005-08-22 13:57:55 +02003575static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003577
3578 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003579 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3580 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3581 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3582 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3583 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3584 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3585 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3586 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3587 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3588 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3589 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003590#define AC_PAR_PCM_RATE_BITS 11
3591 /* up to bits 10, 384kHZ isn't supported properly */
3592
3593 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003594 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003595
Takashi Iwaibefdf312005-08-22 13:57:55 +02003596 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597};
3598
3599/**
3600 * snd_hda_calc_stream_format - calculate format bitset
3601 * @rate: the sample rate
3602 * @channels: the number of channels
3603 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3604 * @maxbps: the max. bps
3605 *
3606 * Calculate the format bitset from the given rate, channels and th PCM format.
3607 *
3608 * Return zero if invalid.
3609 */
3610unsigned int snd_hda_calc_stream_format(unsigned int rate,
3611 unsigned int channels,
3612 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003613 unsigned int maxbps,
3614 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 int i;
3617 unsigned int val = 0;
3618
Takashi Iwaibefdf312005-08-22 13:57:55 +02003619 for (i = 0; rate_bits[i].hz; i++)
3620 if (rate_bits[i].hz == rate) {
3621 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 break;
3623 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003624 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 snd_printdd("invalid rate %d\n", rate);
3626 return 0;
3627 }
3628
3629 if (channels == 0 || channels > 8) {
3630 snd_printdd("invalid channels %d\n", channels);
3631 return 0;
3632 }
3633 val |= channels - 1;
3634
3635 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003636 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003637 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003638 break;
3639 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003640 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 case 20:
3643 case 24:
3644 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003645 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003646 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003648 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003650 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 break;
3652 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003653 snd_printdd("invalid format width %d\n",
3654 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return 0;
3656 }
3657
Anssi Hannula32c168c2010-08-03 13:28:57 +03003658 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003659 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003660
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return val;
3662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003663EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003665static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3666{
3667 unsigned int val = 0;
3668 if (nid != codec->afg &&
3669 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3670 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3671 if (!val || val == -1)
3672 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3673 if (!val || val == -1)
3674 return 0;
3675 return val;
3676}
3677
3678static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3679{
3680 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3681 get_pcm_param);
3682}
3683
3684static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3685{
3686 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3687 if (!streams || streams == -1)
3688 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3689 if (!streams || streams == -1)
3690 return 0;
3691 return streams;
3692}
3693
3694static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3695{
3696 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3697 get_stream_param);
3698}
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700/**
3701 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3702 * @codec: the HDA codec
3703 * @nid: NID to query
3704 * @ratesp: the pointer to store the detected rate bitflags
3705 * @formatsp: the pointer to store the detected formats
3706 * @bpsp: the pointer to store the detected format widths
3707 *
3708 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3709 * or @bsps argument is ignored.
3710 *
3711 * Returns 0 if successful, otherwise a negative error code.
3712 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003713int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3715{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003716 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003718 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003719 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 if (ratesp) {
3722 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003723 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003725 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003727 if (rates == 0) {
3728 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3729 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3730 nid, val,
3731 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3732 return -EIO;
3733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 *ratesp = rates;
3735 }
3736
3737 if (formatsp || bpsp) {
3738 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003739 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003741 streams = query_stream_param(codec, nid);
3742 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 bps = 0;
3746 if (streams & AC_SUPFMT_PCM) {
3747 if (val & AC_SUPPCM_BITS_8) {
3748 formats |= SNDRV_PCM_FMTBIT_U8;
3749 bps = 8;
3750 }
3751 if (val & AC_SUPPCM_BITS_16) {
3752 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3753 bps = 16;
3754 }
3755 if (wcaps & AC_WCAP_DIGITAL) {
3756 if (val & AC_SUPPCM_BITS_32)
3757 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3758 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3759 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3760 if (val & AC_SUPPCM_BITS_24)
3761 bps = 24;
3762 else if (val & AC_SUPPCM_BITS_20)
3763 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003764 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3765 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3767 if (val & AC_SUPPCM_BITS_32)
3768 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 else if (val & AC_SUPPCM_BITS_24)
3770 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003771 else if (val & AC_SUPPCM_BITS_20)
3772 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
3774 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003775 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003777 if (!bps)
3778 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003779 }
3780 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003781 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /* temporary hack: we have still no proper support
3783 * for the direct AC3 stream...
3784 */
3785 formats |= SNDRV_PCM_FMTBIT_U8;
3786 bps = 8;
3787 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003788 if (formats == 0) {
3789 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3790 "(nid=0x%x, val=0x%x, ovrd=%i, "
3791 "streams=0x%x)\n",
3792 nid, val,
3793 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3794 streams);
3795 return -EIO;
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 if (formatsp)
3798 *formatsp = formats;
3799 if (bpsp)
3800 *bpsp = bps;
3801 }
3802
3803 return 0;
3804}
Stephen Warren384a48d2011-06-01 11:14:21 -06003805EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003808 * snd_hda_is_supported_format - Check the validity of the format
3809 * @codec: HD-audio codec
3810 * @nid: NID to check
3811 * @format: the HD-audio format value to check
3812 *
3813 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 *
3815 * Returns 1 if supported, 0 if not.
3816 */
3817int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3818 unsigned int format)
3819{
3820 int i;
3821 unsigned int val = 0, rate, stream;
3822
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003823 val = query_pcm_param(codec, nid);
3824 if (!val)
3825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003828 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003829 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (val & (1 << i))
3831 break;
3832 return 0;
3833 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003834 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return 0;
3836
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003837 stream = query_stream_param(codec, nid);
3838 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return 0;
3840
3841 if (stream & AC_SUPFMT_PCM) {
3842 switch (format & 0xf0) {
3843 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003844 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return 0;
3846 break;
3847 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003848 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 return 0;
3850 break;
3851 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003852 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return 0;
3854 break;
3855 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003856 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return 0;
3858 break;
3859 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return 0;
3862 break;
3863 default:
3864 return 0;
3865 }
3866 } else {
3867 /* FIXME: check for float32 and AC3? */
3868 }
3869
3870 return 1;
3871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003872EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874/*
3875 * PCM stuff
3876 */
3877static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3878 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003879 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 return 0;
3882}
3883
3884static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3885 struct hda_codec *codec,
3886 unsigned int stream_tag,
3887 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003888 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
3890 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3891 return 0;
3892}
3893
3894static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3895 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003896 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
Takashi Iwai888afa12008-03-18 09:57:50 +01003898 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return 0;
3900}
3901
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003902static int set_pcm_default_values(struct hda_codec *codec,
3903 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003905 int err;
3906
Takashi Iwai0ba21762007-04-16 11:29:14 +02003907 /* query support PCM information from the given NID */
3908 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003909 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003910 info->rates ? NULL : &info->rates,
3911 info->formats ? NULL : &info->formats,
3912 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003913 if (err < 0)
3914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
3916 if (info->ops.open == NULL)
3917 info->ops.open = hda_pcm_default_open_close;
3918 if (info->ops.close == NULL)
3919 info->ops.close = hda_pcm_default_open_close;
3920 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003921 if (snd_BUG_ON(!info->nid))
3922 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 info->ops.prepare = hda_pcm_default_prepare;
3924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 if (info->ops.cleanup == 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.cleanup = hda_pcm_default_cleanup;
3929 }
3930 return 0;
3931}
3932
Takashi Iwaieb541332010-08-06 13:48:11 +02003933/*
3934 * codec prepare/cleanup entries
3935 */
3936int snd_hda_codec_prepare(struct hda_codec *codec,
3937 struct hda_pcm_stream *hinfo,
3938 unsigned int stream,
3939 unsigned int format,
3940 struct snd_pcm_substream *substream)
3941{
3942 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003943 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003944 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3945 if (ret >= 0)
3946 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003947 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003948 return ret;
3949}
3950EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3951
3952void snd_hda_codec_cleanup(struct hda_codec *codec,
3953 struct hda_pcm_stream *hinfo,
3954 struct snd_pcm_substream *substream)
3955{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003956 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003957 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003958 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003959}
3960EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3961
Takashi Iwaid5191e52009-11-16 14:58:17 +01003962/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003963const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3964 "Audio", "SPDIF", "HDMI", "Modem"
3965};
3966
Takashi Iwai176d5332008-07-30 15:01:44 +02003967/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003968 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003969 *
3970 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003971 */
3972static int get_empty_pcm_device(struct hda_bus *bus, int type)
3973{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003974 /* audio device indices; not linear to keep compatibility */
3975 static int audio_idx[HDA_PCM_NTYPES][5] = {
3976 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3977 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003978 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003979 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003980 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003981 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003982
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003983 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003984 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3985 return -EINVAL;
3986 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003987
3988 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3989 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3990 return audio_idx[type][i];
3991
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003992 /* non-fixed slots starting from 10 */
3993 for (i = 10; i < 32; i++) {
3994 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3995 return i;
3996 }
3997
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003998 snd_printk(KERN_WARNING "Too many %s devices\n",
3999 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004000 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004001}
4002
4003/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004004 * attach a new PCM stream
4005 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004006static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004007{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004008 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004009 struct hda_pcm_stream *info;
4010 int stream, err;
4011
Takashi Iwaib91f0802008-11-04 08:43:08 +01004012 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004013 return -EINVAL;
4014 for (stream = 0; stream < 2; stream++) {
4015 info = &pcm->stream[stream];
4016 if (info->substreams) {
4017 err = set_pcm_default_values(codec, info);
4018 if (err < 0)
4019 return err;
4020 }
4021 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004022 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004023}
4024
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004025/* assign all PCMs of the given codec */
4026int snd_hda_codec_build_pcms(struct hda_codec *codec)
4027{
4028 unsigned int pcm;
4029 int err;
4030
4031 if (!codec->num_pcms) {
4032 if (!codec->patch_ops.build_pcms)
4033 return 0;
4034 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004035 if (err < 0) {
4036 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004037 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004038 err = snd_hda_codec_reset(codec);
4039 if (err < 0) {
4040 printk(KERN_ERR
4041 "hda_codec: cannot revert codec\n");
4042 return err;
4043 }
4044 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004045 }
4046 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4047 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4048 int dev;
4049
4050 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004051 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004052
4053 if (!cpcm->pcm) {
4054 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4055 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004056 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004057 cpcm->device = dev;
4058 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004059 if (err < 0) {
4060 printk(KERN_ERR "hda_codec: cannot attach "
4061 "PCM stream %d for codec #%d\n",
4062 dev, codec->addr);
4063 continue; /* no fatal error */
4064 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004065 }
4066 }
4067 return 0;
4068}
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070/**
4071 * snd_hda_build_pcms - build PCM information
4072 * @bus: the BUS
4073 *
4074 * Create PCM information for each codec included in the bus.
4075 *
4076 * The build_pcms codec patch is requested to set up codec->num_pcms and
4077 * codec->pcm_info properly. The array is referred by the top-level driver
4078 * to create its PCM instances.
4079 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4080 * callback.
4081 *
4082 * At least, substreams, channels_min and channels_max must be filled for
4083 * each stream. substreams = 0 indicates that the stream doesn't exist.
4084 * When rates and/or formats are zero, the supported values are queried
4085 * from the given nid. The nid is used also by the default ops.prepare
4086 * and ops.cleanup callbacks.
4087 *
4088 * The driver needs to call ops.open in its open callback. Similarly,
4089 * ops.close is supposed to be called in the close callback.
4090 * ops.prepare should be called in the prepare or hw_params callback
4091 * with the proper parameters for set up.
4092 * ops.cleanup should be called in hw_free for clean up of streams.
4093 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004094 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004096int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004098 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Takashi Iwai0ba21762007-04-16 11:29:14 +02004100 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004101 int err = snd_hda_codec_build_pcms(codec);
4102 if (err < 0)
4103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
4105 return 0;
4106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004107EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109/**
4110 * snd_hda_check_board_config - compare the current codec with the config table
4111 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004112 * @num_configs: number of config enums
4113 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 * @tbl: configuration table, terminated by null entries
4115 *
4116 * Compares the modelname or PCI subsystem id of the current codec with the
4117 * given configuration table. If a matching entry is found, returns its
4118 * config value (supposed to be 0 or positive).
4119 *
4120 * If no entries are matching, the function returns a negative value.
4121 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004122int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004123 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004124 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004126 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004127 int i;
4128 for (i = 0; i < num_configs; i++) {
4129 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004130 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004131 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4132 "selected\n", models[i]);
4133 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135 }
4136 }
4137
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004138 if (!codec->bus->pci || !tbl)
4139 return -1;
4140
4141 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4142 if (!tbl)
4143 return -1;
4144 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004145#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004146 char tmp[10];
4147 const char *model = NULL;
4148 if (models)
4149 model = models[tbl->value];
4150 if (!model) {
4151 sprintf(tmp, "#%d", tbl->value);
4152 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004154 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4155 "for config %x:%x (%s)\n",
4156 model, tbl->subvendor, tbl->subdevice,
4157 (tbl->name ? tbl->name : "Unknown device"));
4158#endif
4159 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
4161 return -1;
4162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004163EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004166 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004167 subsystem ID with the
4168 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004169
4170 This is important for Gateway notebooks with SB450 HDA Audio
4171 where the vendor ID of the PCI device is:
4172 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4173 and the vendor/subvendor are found only at the codec.
4174
4175 * @codec: the HDA codec
4176 * @num_configs: number of config enums
4177 * @models: array of model name strings
4178 * @tbl: configuration table, terminated by null entries
4179 *
4180 * Compares the modelname or PCI subsystem id of the current codec with the
4181 * given configuration table. If a matching entry is found, returns its
4182 * config value (supposed to be 0 or positive).
4183 *
4184 * If no entries are matching, the function returns a negative value.
4185 */
4186int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004187 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004188 const struct snd_pci_quirk *tbl)
4189{
4190 const struct snd_pci_quirk *q;
4191
4192 /* Search for codec ID */
4193 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004194 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4195 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4196 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004197 break;
4198 }
4199
4200 if (!q->subvendor)
4201 return -1;
4202
4203 tbl = q;
4204
4205 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004206#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004207 char tmp[10];
4208 const char *model = NULL;
4209 if (models)
4210 model = models[tbl->value];
4211 if (!model) {
4212 sprintf(tmp, "#%d", tbl->value);
4213 model = tmp;
4214 }
4215 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4216 "for config %x:%x (%s)\n",
4217 model, tbl->subvendor, tbl->subdevice,
4218 (tbl->name ? tbl->name : "Unknown device"));
4219#endif
4220 return tbl->value;
4221 }
4222 return -1;
4223}
4224EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4225
4226/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 * snd_hda_add_new_ctls - create controls from the array
4228 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004229 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 *
4231 * This helper function creates and add new controls in the given array.
4232 * The array must be terminated with an empty entry as terminator.
4233 *
4234 * Returns 0 if successful, or a negative error code.
4235 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004236int snd_hda_add_new_ctls(struct hda_codec *codec,
4237 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004242 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004243 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004244 if (knew->iface == -1) /* skip this codec private value */
4245 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004246 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004247 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004248 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004249 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004250 if (addr > 0)
4251 kctl->id.device = addr;
4252 if (idx > 0)
4253 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004254 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004255 if (!err)
4256 break;
4257 /* try first with another device index corresponding to
4258 * the codec addr; if it still fails (or it's the
4259 * primary codec), then try another control index
4260 */
4261 if (!addr && codec->addr)
4262 addr = codec->addr;
4263 else if (!idx && !knew->index) {
4264 idx = find_empty_mixer_ctl_idx(codec,
4265 knew->name);
4266 if (idx <= 0)
4267 return err;
4268 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004269 return err;
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
4272 return 0;
4273}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004274EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Takashi Iwaicb53c622007-08-10 17:21:45 +02004276#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004277static void hda_power_work(struct work_struct *work)
4278{
4279 struct hda_codec *codec =
4280 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004281 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004282
Maxim Levitsky2e492462007-09-03 15:26:57 +02004283 if (!codec->power_on || codec->power_count) {
4284 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004285 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004286 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004287
Takashi Iwaid66fee52011-08-02 15:39:31 +02004288 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004289 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004290 if (bus->ops.pm_notify)
4291 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004292}
4293
4294static void hda_keep_power_on(struct hda_codec *codec)
4295{
4296 codec->power_count++;
4297 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004298 codec->power_jiffies = jiffies;
4299}
4300
Takashi Iwaid5191e52009-11-16 14:58:17 +01004301/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004302void snd_hda_update_power_acct(struct hda_codec *codec)
4303{
4304 unsigned long delta = jiffies - codec->power_jiffies;
4305 if (codec->power_on)
4306 codec->power_on_acct += delta;
4307 else
4308 codec->power_off_acct += delta;
4309 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004310}
4311
Takashi Iwaid5191e52009-11-16 14:58:17 +01004312/**
4313 * snd_hda_power_up - Power-up the codec
4314 * @codec: HD-audio codec
4315 *
4316 * Increment the power-up counter and power up the hardware really when
4317 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004318 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004319void snd_hda_power_up(struct hda_codec *codec)
4320{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004321 struct hda_bus *bus = codec->bus;
4322
Takashi Iwaicb53c622007-08-10 17:21:45 +02004323 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004324 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004325 return;
4326
Takashi Iwaid66fee52011-08-02 15:39:31 +02004327 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004328 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004329 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004330 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004331 if (bus->ops.pm_notify)
4332 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004333 hda_call_codec_resume(codec);
4334 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004335 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004337EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004338
4339#define power_save(codec) \
4340 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004341
Takashi Iwaid5191e52009-11-16 14:58:17 +01004342/**
4343 * snd_hda_power_down - Power-down the codec
4344 * @codec: HD-audio codec
4345 *
4346 * Decrement the power-up counter and schedules the power-off work if
4347 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004348 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004349void snd_hda_power_down(struct hda_codec *codec)
4350{
4351 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004352 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004353 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004354 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004355 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004356 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004357 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004358 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004359}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004360EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004361
Takashi Iwaid5191e52009-11-16 14:58:17 +01004362/**
4363 * snd_hda_check_amp_list_power - Check the amp list and update the power
4364 * @codec: HD-audio codec
4365 * @check: the object containing an AMP list and the status
4366 * @nid: NID to check / update
4367 *
4368 * Check whether the given NID is in the amp list. If it's in the list,
4369 * check the current AMP status, and update the the power-status according
4370 * to the mute status.
4371 *
4372 * This function is supposed to be set or called from the check_power_status
4373 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004374 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004375int snd_hda_check_amp_list_power(struct hda_codec *codec,
4376 struct hda_loopback_check *check,
4377 hda_nid_t nid)
4378{
Takashi Iwai031024e2011-05-02 11:29:30 +02004379 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004380 int ch, v;
4381
4382 if (!check->amplist)
4383 return 0;
4384 for (p = check->amplist; p->nid; p++) {
4385 if (p->nid == nid)
4386 break;
4387 }
4388 if (!p->nid)
4389 return 0; /* nothing changed */
4390
4391 for (p = check->amplist; p->nid; p++) {
4392 for (ch = 0; ch < 2; ch++) {
4393 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4394 p->idx);
4395 if (!(v & HDA_AMP_MUTE) && v > 0) {
4396 if (!check->power_on) {
4397 check->power_on = 1;
4398 snd_hda_power_up(codec);
4399 }
4400 return 1;
4401 }
4402 }
4403 }
4404 if (check->power_on) {
4405 check->power_on = 0;
4406 snd_hda_power_down(codec);
4407 }
4408 return 0;
4409}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004410EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004413/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004414 * Channel mode helper
4415 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004416
4417/**
4418 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4419 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004420int snd_hda_ch_mode_info(struct hda_codec *codec,
4421 struct snd_ctl_elem_info *uinfo,
4422 const struct hda_channel_mode *chmode,
4423 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004424{
4425 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4426 uinfo->count = 1;
4427 uinfo->value.enumerated.items = num_chmodes;
4428 if (uinfo->value.enumerated.item >= num_chmodes)
4429 uinfo->value.enumerated.item = num_chmodes - 1;
4430 sprintf(uinfo->value.enumerated.name, "%dch",
4431 chmode[uinfo->value.enumerated.item].channels);
4432 return 0;
4433}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004434EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004435
Takashi Iwaid5191e52009-11-16 14:58:17 +01004436/**
4437 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4438 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004439int snd_hda_ch_mode_get(struct hda_codec *codec,
4440 struct snd_ctl_elem_value *ucontrol,
4441 const struct hda_channel_mode *chmode,
4442 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004443 int max_channels)
4444{
4445 int i;
4446
4447 for (i = 0; i < num_chmodes; i++) {
4448 if (max_channels == chmode[i].channels) {
4449 ucontrol->value.enumerated.item[0] = i;
4450 break;
4451 }
4452 }
4453 return 0;
4454}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004455EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004456
Takashi Iwaid5191e52009-11-16 14:58:17 +01004457/**
4458 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4459 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004460int snd_hda_ch_mode_put(struct hda_codec *codec,
4461 struct snd_ctl_elem_value *ucontrol,
4462 const struct hda_channel_mode *chmode,
4463 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004464 int *max_channelsp)
4465{
4466 unsigned int mode;
4467
4468 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004469 if (mode >= num_chmodes)
4470 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004471 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004472 return 0;
4473 /* change the current channel setting */
4474 *max_channelsp = chmode[mode].channels;
4475 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004476 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004477 return 1;
4478}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004479EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481/*
4482 * input MUX helper
4483 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004484
4485/**
4486 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4487 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004488int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4489 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
4491 unsigned int index;
4492
4493 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4494 uinfo->count = 1;
4495 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004496 if (!imux->num_items)
4497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 index = uinfo->value.enumerated.item;
4499 if (index >= imux->num_items)
4500 index = imux->num_items - 1;
4501 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4502 return 0;
4503}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004504EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Takashi Iwaid5191e52009-11-16 14:58:17 +01004506/**
4507 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4508 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004509int snd_hda_input_mux_put(struct hda_codec *codec,
4510 const struct hda_input_mux *imux,
4511 struct snd_ctl_elem_value *ucontrol,
4512 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 unsigned int *cur_val)
4514{
4515 unsigned int idx;
4516
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004517 if (!imux->num_items)
4518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 idx = ucontrol->value.enumerated.item[0];
4520 if (idx >= imux->num_items)
4521 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004522 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004524 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4525 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 *cur_val = idx;
4527 return 1;
4528}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004529EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531
4532/*
4533 * Multi-channel / digital-out PCM helper functions
4534 */
4535
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004536/* setup SPDIF output stream */
4537static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4538 unsigned int stream_tag, unsigned int format)
4539{
Stephen Warren7c9359762011-06-01 11:14:17 -06004540 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4541
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004542 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004543 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004544 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004545 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004546 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004547 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004548 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004549 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004550 for (d = codec->slave_dig_outs; *d; d++)
4551 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4552 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004553 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004554 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004555 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004556 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004557 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004558}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004559
Takashi Iwai2f728532008-09-25 16:32:41 +02004560static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4561{
4562 snd_hda_codec_cleanup_stream(codec, nid);
4563 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004564 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004565 for (d = codec->slave_dig_outs; *d; d++)
4566 snd_hda_codec_cleanup_stream(codec, *d);
4567 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004568}
4569
Takashi Iwaid5191e52009-11-16 14:58:17 +01004570/**
4571 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4572 * @bus: HD-audio bus
4573 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004574void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4575{
4576 struct hda_codec *codec;
4577
4578 if (!bus)
4579 return;
4580 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004581 if (hda_codec_is_power_on(codec) &&
4582 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004583 codec->patch_ops.reboot_notify(codec);
4584 }
4585}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004586EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004587
Takashi Iwaid5191e52009-11-16 14:58:17 +01004588/**
4589 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004591int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4592 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Ingo Molnar62932df2006-01-16 16:34:20 +01004594 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004595 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4596 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004597 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004599 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 return 0;
4601}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004602EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Takashi Iwaid5191e52009-11-16 14:58:17 +01004604/**
4605 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4606 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004607int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4608 struct hda_multi_out *mout,
4609 unsigned int stream_tag,
4610 unsigned int format,
4611 struct snd_pcm_substream *substream)
4612{
4613 mutex_lock(&codec->spdif_mutex);
4614 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4615 mutex_unlock(&codec->spdif_mutex);
4616 return 0;
4617}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004618EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004619
Takashi Iwaid5191e52009-11-16 14:58:17 +01004620/**
4621 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4622 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004623int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4624 struct hda_multi_out *mout)
4625{
4626 mutex_lock(&codec->spdif_mutex);
4627 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4628 mutex_unlock(&codec->spdif_mutex);
4629 return 0;
4630}
4631EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4632
Takashi Iwaid5191e52009-11-16 14:58:17 +01004633/**
4634 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004636int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4637 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
Ingo Molnar62932df2006-01-16 16:34:20 +01004639 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004641 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return 0;
4643}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004644EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Takashi Iwaid5191e52009-11-16 14:58:17 +01004646/**
4647 * snd_hda_multi_out_analog_open - open analog outputs
4648 *
4649 * Open analog outputs and set up the hw-constraints.
4650 * If the digital outputs can be opened as slave, open the digital
4651 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004653int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4654 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004655 struct snd_pcm_substream *substream,
4656 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Takashi Iwai9a081602008-02-12 18:37:26 +01004658 struct snd_pcm_runtime *runtime = substream->runtime;
4659 runtime->hw.channels_max = mout->max_channels;
4660 if (mout->dig_out_nid) {
4661 if (!mout->analog_rates) {
4662 mout->analog_rates = hinfo->rates;
4663 mout->analog_formats = hinfo->formats;
4664 mout->analog_maxbps = hinfo->maxbps;
4665 } else {
4666 runtime->hw.rates = mout->analog_rates;
4667 runtime->hw.formats = mout->analog_formats;
4668 hinfo->maxbps = mout->analog_maxbps;
4669 }
4670 if (!mout->spdif_rates) {
4671 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4672 &mout->spdif_rates,
4673 &mout->spdif_formats,
4674 &mout->spdif_maxbps);
4675 }
4676 mutex_lock(&codec->spdif_mutex);
4677 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004678 if ((runtime->hw.rates & mout->spdif_rates) &&
4679 (runtime->hw.formats & mout->spdif_formats)) {
4680 runtime->hw.rates &= mout->spdif_rates;
4681 runtime->hw.formats &= mout->spdif_formats;
4682 if (mout->spdif_maxbps < hinfo->maxbps)
4683 hinfo->maxbps = mout->spdif_maxbps;
4684 } else {
4685 mout->share_spdif = 0;
4686 /* FIXME: need notify? */
4687 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004688 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004689 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4692 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4693}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004694EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Takashi Iwaid5191e52009-11-16 14:58:17 +01004696/**
4697 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4698 *
4699 * Set up the i/o for analog out.
4700 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004702int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4703 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 unsigned int stream_tag,
4705 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004706 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Takashi Iwaidda14412011-05-02 11:29:30 +02004708 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004710 struct hda_spdif_out *spdif =
4711 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 int i;
4713
Ingo Molnar62932df2006-01-16 16:34:20 +01004714 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004715 if (mout->dig_out_nid && mout->share_spdif &&
4716 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004718 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4719 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004720 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004722 setup_dig_out_stream(codec, mout->dig_out_nid,
4723 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 } else {
4725 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004726 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
4728 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004729 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004732 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4733 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004734 if (!mout->no_share_stream &&
4735 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004737 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4738 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004739 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004740 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4741 if (!mout->no_share_stream && mout->hp_out_nid[i])
4742 snd_hda_codec_setup_stream(codec,
4743 mout->hp_out_nid[i],
4744 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004745 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004746 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004747 snd_hda_codec_setup_stream(codec,
4748 mout->extra_out_nid[i],
4749 stream_tag, 0, format);
4750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 /* surrounds */
4752 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004753 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004754 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4755 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004756 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004757 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4758 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 }
4760 return 0;
4761}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004762EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Takashi Iwaid5191e52009-11-16 14:58:17 +01004764/**
4765 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004767int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4768 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Takashi Iwaidda14412011-05-02 11:29:30 +02004770 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 int i;
4772
4773 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004774 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004776 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004777 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4778 if (mout->hp_out_nid[i])
4779 snd_hda_codec_cleanup_stream(codec,
4780 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004781 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4782 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004783 snd_hda_codec_cleanup_stream(codec,
4784 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004785 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004787 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 mout->dig_out_used = 0;
4789 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004790 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return 0;
4792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004793EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004795/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004796 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004797 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004798
Takashi Iwaidda14412011-05-02 11:29:30 +02004799static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004800{
4801 for (; *list; list++)
4802 if (*list == nid)
4803 return 1;
4804 return 0;
4805}
4806
Steve Longerbeam81937d32007-05-08 15:33:03 +02004807
4808/*
4809 * Sort an associated group of pins according to their sequence numbers.
4810 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004811static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004812 int num_pins)
4813{
4814 int i, j;
4815 short seq;
4816 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004817
Steve Longerbeam81937d32007-05-08 15:33:03 +02004818 for (i = 0; i < num_pins; i++) {
4819 for (j = i + 1; j < num_pins; j++) {
4820 if (sequences[i] > sequences[j]) {
4821 seq = sequences[i];
4822 sequences[i] = sequences[j];
4823 sequences[j] = seq;
4824 nid = pins[i];
4825 pins[i] = pins[j];
4826 pins[j] = nid;
4827 }
4828 }
4829 }
4830}
4831
4832
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004833/* add the found input-pin to the cfg->inputs[] table */
4834static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4835 int type)
4836{
4837 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4838 cfg->inputs[cfg->num_inputs].pin = nid;
4839 cfg->inputs[cfg->num_inputs].type = type;
4840 cfg->num_inputs++;
4841 }
4842}
4843
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004844/* sort inputs in the order of AUTO_PIN_* type */
4845static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4846{
4847 int i, j;
4848
4849 for (i = 0; i < cfg->num_inputs; i++) {
4850 for (j = i + 1; j < cfg->num_inputs; j++) {
4851 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4852 struct auto_pin_cfg_item tmp;
4853 tmp = cfg->inputs[i];
4854 cfg->inputs[i] = cfg->inputs[j];
4855 cfg->inputs[j] = tmp;
4856 }
4857 }
4858 }
4859}
4860
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004861/* Reorder the surround channels
4862 * ALSA sequence is front/surr/clfe/side
4863 * HDA sequence is:
4864 * 4-ch: front/surr => OK as it is
4865 * 6-ch: front/clfe/surr
4866 * 8-ch: front/clfe/rear/side|fc
4867 */
4868static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4869{
4870 hda_nid_t nid;
4871
4872 switch (nums) {
4873 case 3:
4874 case 4:
4875 nid = pins[1];
4876 pins[1] = pins[2];
4877 pins[2] = nid;
4878 break;
4879 }
4880}
4881
Takashi Iwai82bc9552006-03-21 11:24:42 +01004882/*
4883 * Parse all pin widgets and store the useful pin nids to cfg
4884 *
4885 * The number of line-outs or any primary output is stored in line_outs,
4886 * and the corresponding output pins are assigned to line_out_pins[],
4887 * in the order of front, rear, CLFE, side, ...
4888 *
4889 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004890 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004891 * is detected, one of speaker of HP pins is assigned as the primary
4892 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4893 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004894 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004895 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004896 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4897 * respectively.
4898 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004899int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4900 struct auto_pin_cfg *cfg,
4901 const hda_nid_t *ignore_nids,
4902 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004903{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004904 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004905 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004906 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4907 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004908 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004909 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004910
4911 memset(cfg, 0, sizeof(*cfg));
4912
Steve Longerbeam81937d32007-05-08 15:33:03 +02004913 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4914 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004915 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004916 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004917
Takashi Iwai2f451d22011-11-10 12:36:46 +01004918 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004919 end_nid = codec->start_nid + codec->num_nodes;
4920 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004921 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004922 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004923 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004924 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004925
4926 /* read all default configuration for pin complex */
4927 if (wid_type != AC_WID_PIN)
4928 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004929 /* ignore the given nids (e.g. pc-beep returns error) */
4930 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4931 continue;
4932
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004933 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01004934 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4935 AC_DEFCFG_MISC_NO_PRESENCE))
4936 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004937 conn = get_defcfg_connect(def_conf);
4938 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004939 continue;
4940 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004941 dev = get_defcfg_device(def_conf);
4942
4943 /* workaround for buggy BIOS setups */
4944 if (dev == AC_JACK_LINE_OUT) {
4945 if (conn == AC_JACK_PORT_FIXED)
4946 dev = AC_JACK_SPEAKER;
4947 }
4948
4949 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004950 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004951 seq = get_defcfg_sequence(def_conf);
4952 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004953
4954 if (!(wid_caps & AC_WCAP_STEREO))
4955 if (!cfg->mono_out_pin)
4956 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004957 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004958 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004959 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004960 assoc_line_out = assoc;
4961 else if (assoc_line_out != assoc)
4962 continue;
4963 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4964 continue;
4965 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004966 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004967 cfg->line_outs++;
4968 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004969 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004970 seq = get_defcfg_sequence(def_conf);
4971 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004972 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4973 continue;
4974 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004975 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004976 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004977 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004978 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004979 seq = get_defcfg_sequence(def_conf);
4980 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004981 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4982 continue;
4983 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004984 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004985 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004986 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004987 case AC_JACK_MIC_IN:
4988 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004989 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004990 case AC_JACK_LINE_IN:
4991 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004992 break;
4993 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004994 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004995 break;
4996 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004997 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004998 break;
4999 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005000 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005001 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5002 continue;
5003 cfg->dig_out_pins[cfg->dig_outs] = nid;
5004 cfg->dig_out_type[cfg->dig_outs] =
5005 (loc == AC_JACK_LOC_HDMI) ?
5006 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5007 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005008 break;
5009 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005010 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005011 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01005012 if (loc == AC_JACK_LOC_HDMI)
5013 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5014 else
5015 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005016 break;
5017 }
5018 }
5019
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005020 /* FIX-UP:
5021 * If no line-out is defined but multiple HPs are found,
5022 * some of them might be the real line-outs.
5023 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005024 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5025 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005026 int i = 0;
5027 while (i < cfg->hp_outs) {
5028 /* The real HPs should have the sequence 0x0f */
5029 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5030 i++;
5031 continue;
5032 }
5033 /* Move it to the line-out table */
5034 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5035 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5036 cfg->line_outs++;
5037 cfg->hp_outs--;
5038 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5039 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02005040 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005041 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5042 }
Takashi Iwai03642c92010-09-08 15:28:19 +02005043 memset(cfg->hp_pins + cfg->hp_outs, 0,
5044 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01005045 if (!cfg->hp_outs)
5046 cfg->line_out_type = AUTO_PIN_HP_OUT;
5047
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005048 }
5049
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005050 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02005051 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5052 cfg->line_outs);
5053 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5054 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01005055 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5056 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005057
Steve Longerbeam81937d32007-05-08 15:33:03 +02005058 /*
5059 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5060 * as a primary output
5061 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005062 if (!cfg->line_outs &&
5063 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02005064 if (cfg->speaker_outs) {
5065 cfg->line_outs = cfg->speaker_outs;
5066 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5067 sizeof(cfg->speaker_pins));
5068 cfg->speaker_outs = 0;
5069 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5070 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5071 } else if (cfg->hp_outs) {
5072 cfg->line_outs = cfg->hp_outs;
5073 memcpy(cfg->line_out_pins, cfg->hp_pins,
5074 sizeof(cfg->hp_pins));
5075 cfg->hp_outs = 0;
5076 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5077 cfg->line_out_type = AUTO_PIN_HP_OUT;
5078 }
5079 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005080
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02005081 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5082 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5083 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005084
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02005085 sort_autocfg_input_pins(cfg);
5086
Takashi Iwai82bc9552006-03-21 11:24:42 +01005087 /*
5088 * debug prints of the parsed results
5089 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02005090 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 +01005091 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5092 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02005093 cfg->line_out_pins[4],
5094 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5095 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5096 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01005097 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5098 cfg->speaker_outs, cfg->speaker_pins[0],
5099 cfg->speaker_pins[1], cfg->speaker_pins[2],
5100 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005101 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5102 cfg->hp_outs, cfg->hp_pins[0],
5103 cfg->hp_pins[1], cfg->hp_pins[2],
5104 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005105 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005106 if (cfg->dig_outs)
5107 snd_printd(" dig-out=0x%x/0x%x\n",
5108 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005109 snd_printd(" inputs:");
5110 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02005111 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02005112 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005113 cfg->inputs[i].pin);
5114 }
5115 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01005116 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01005117 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005118
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005119 return 0;
5120}
Takashi Iwaid025feb2011-08-23 15:24:39 +02005121EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005122
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005123int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005124{
5125 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005126 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005127 if (conn == AC_JACK_PORT_NONE)
5128 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005129 /* Windows may claim the internal mic to be BOTH, too */
5130 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005131 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005132 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005133 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005134 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005135 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005136 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005137 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005138 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005139 return INPUT_PIN_ATTR_FRONT;
5140 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005141}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005142EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005143
Takashi Iwai990061c2010-09-09 22:08:44 +02005144/**
5145 * hda_get_input_pin_label - Give a label for the given input pin
5146 *
5147 * When check_location is true, the function checks the pin location
5148 * for mic and line-in pins, and set an appropriate prefix like "Front",
5149 * "Rear", "Internal".
5150 */
5151
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005152static const char *hda_get_input_pin_label(struct hda_codec *codec,
5153 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02005154{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005155 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005156 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005157 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5158 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005159 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005160
Takashi Iwai10a20af2010-09-09 16:28:02 +02005161 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005162
5163 switch (get_defcfg_device(def_conf)) {
5164 case AC_JACK_MIC_IN:
5165 if (!check_location)
5166 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005167 attr = snd_hda_get_input_pin_attr(def_conf);
5168 if (!attr)
5169 return "None";
5170 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005171 case AC_JACK_LINE_IN:
5172 if (!check_location)
5173 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005174 attr = snd_hda_get_input_pin_attr(def_conf);
5175 if (!attr)
5176 return "None";
5177 if (attr == INPUT_PIN_ATTR_DOCK)
5178 return "Dock Line";
5179 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005180 case AC_JACK_AUX:
5181 return "Aux";
5182 case AC_JACK_CD:
5183 return "CD";
5184 case AC_JACK_SPDIF_IN:
5185 return "SPDIF In";
5186 case AC_JACK_DIG_OTHER_IN:
5187 return "Digital In";
5188 default:
5189 return "Misc";
5190 }
5191}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005192
Takashi Iwaia1c98512010-09-09 21:36:27 +02005193/* Check whether the location prefix needs to be added to the label.
5194 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5195 * have to put "Front" prefix to each label. In such a case, returns false.
5196 */
5197static int check_mic_location_need(struct hda_codec *codec,
5198 const struct auto_pin_cfg *cfg,
5199 int input)
5200{
5201 unsigned int defc;
5202 int i, attr, attr2;
5203
5204 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005205 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005206 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005207 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005208 return 1;
5209
5210 attr = 0;
5211 for (i = 0; i < cfg->num_inputs; i++) {
5212 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005213 attr2 = snd_hda_get_input_pin_attr(defc);
5214 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005215 if (attr && attr != attr2)
5216 return 1; /* different locations found */
5217 attr = attr2;
5218 }
5219 }
5220 return 0;
5221}
5222
Takashi Iwai990061c2010-09-09 22:08:44 +02005223/**
5224 * hda_get_autocfg_input_label - Get a label for the given input
5225 *
5226 * Get a label for the given input pin defined by the autocfg item.
5227 * Unlike hda_get_input_pin_label(), this function checks all inputs
5228 * defined in autocfg and avoids the redundant mic/line prefix as much as
5229 * possible.
5230 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005231const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5232 const struct auto_pin_cfg *cfg,
5233 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005234{
5235 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005236 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005237
Takashi Iwai10a20af2010-09-09 16:28:02 +02005238 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5239 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5240 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005241 if (has_multiple_pins && type == AUTO_PIN_MIC)
5242 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005243 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5244 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005245}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005246EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5247
Takashi Iwai358b6e62011-11-21 14:34:20 +01005248/* return the position of NID in the list, or -1 if not found */
5249static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5250{
5251 int i;
5252 for (i = 0; i < nums; i++)
5253 if (list[i] == nid)
5254 return i;
5255 return -1;
5256}
5257
Takashi Iwai201e06f2011-11-16 15:33:26 +01005258/* get a unique suffix or an index number */
5259static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5260 int num_pins, int *indexp)
5261{
5262 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005263 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005264 };
5265 int i;
5266
Takashi Iwai358b6e62011-11-21 14:34:20 +01005267 i = find_idx_in_nid_list(nid, pins, num_pins);
5268 if (i < 0)
5269 return NULL;
5270 if (num_pins == 1)
5271 return "";
5272 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5273 if (indexp)
5274 *indexp = i;
5275 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005276 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005277 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005278}
5279
5280static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5281 const struct auto_pin_cfg *cfg,
5282 const char *name, char *label, int maxlen,
5283 int *indexp)
5284{
5285 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5286 int attr = snd_hda_get_input_pin_attr(def_conf);
5287 const char *pfx = "", *sfx = "";
5288
5289 /* handle as a speaker if it's a fixed line-out */
Takashi Iwaie49a3432012-03-01 18:14:41 +01005290 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005291 name = "Speaker";
5292 /* check the location */
5293 switch (attr) {
5294 case INPUT_PIN_ATTR_DOCK:
5295 pfx = "Dock ";
5296 break;
5297 case INPUT_PIN_ATTR_FRONT:
5298 pfx = "Front ";
5299 break;
5300 }
5301 if (cfg) {
5302 /* try to give a unique suffix if needed */
5303 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5304 indexp);
5305 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005306 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5307 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005308 if (!sfx) {
5309 /* don't add channel suffix for Headphone controls */
5310 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5311 cfg->hp_outs);
5312 if (idx >= 0)
5313 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005314 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005315 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005316 }
5317 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5318 return 1;
5319}
5320
Takashi Iwai990061c2010-09-09 22:08:44 +02005321/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005322 * snd_hda_get_pin_label - Get a label for the given I/O pin
5323 *
5324 * Get a label for the given pin. This function works for both input and
5325 * output pins. When @cfg is given as non-NULL, the function tries to get
5326 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005327 *
5328 * This function tries to give a unique label string for the pin as much as
5329 * possible. For example, when the multiple line-outs are present, it adds
5330 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5331 * If no unique name with a suffix is available and @indexp is non-NULL, the
5332 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005333 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005334int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5335 const struct auto_pin_cfg *cfg,
5336 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005337{
5338 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005339 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005340 int i;
5341
Takashi Iwai201e06f2011-11-16 15:33:26 +01005342 if (indexp)
5343 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005344 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005345 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005346
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005347 switch (get_defcfg_device(def_conf)) {
5348 case AC_JACK_LINE_OUT:
Takashi Iwaie49a3432012-03-01 18:14:41 +01005349 return fill_audio_out_name(codec, nid, cfg, "Line Out",
Takashi Iwai201e06f2011-11-16 15:33:26 +01005350 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005351 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005352 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5353 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005354 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005355 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5356 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005357 case AC_JACK_SPDIF_OUT:
5358 case AC_JACK_DIG_OTHER_OUT:
5359 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005360 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005361 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005362 name = "SPDIF";
5363 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005364 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5365 cfg->dig_outs);
5366 if (i >= 0)
5367 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005368 }
5369 break;
5370 default:
5371 if (cfg) {
5372 for (i = 0; i < cfg->num_inputs; i++) {
5373 if (cfg->inputs[i].pin != nid)
5374 continue;
5375 name = hda_get_autocfg_input_label(codec, cfg, i);
5376 if (name)
5377 break;
5378 }
5379 }
5380 if (!name)
5381 name = hda_get_input_pin_label(codec, nid, true);
5382 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005383 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005384 if (!name)
5385 return 0;
5386 strlcpy(label, name, maxlen);
5387 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005388}
5389EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5390
Takashi Iwai990061c2010-09-09 22:08:44 +02005391/**
5392 * snd_hda_add_imux_item - Add an item to input_mux
5393 *
5394 * When the same label is used already in the existing items, the number
5395 * suffix is appended to the label. This label index number is stored
5396 * to type_idx when non-NULL pointer is given.
5397 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005398int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5399 int index, int *type_idx)
5400{
5401 int i, label_idx = 0;
5402 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5403 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5404 return -EINVAL;
5405 }
5406 for (i = 0; i < imux->num_items; i++) {
5407 if (!strncmp(label, imux->items[i].label, strlen(label)))
5408 label_idx++;
5409 }
5410 if (type_idx)
5411 *type_idx = label_idx;
5412 if (label_idx > 0)
5413 snprintf(imux->items[imux->num_items].label,
5414 sizeof(imux->items[imux->num_items].label),
5415 "%s %d", label, label_idx);
5416 else
5417 strlcpy(imux->items[imux->num_items].label, label,
5418 sizeof(imux->items[imux->num_items].label));
5419 imux->items[imux->num_items].index = index;
5420 imux->num_items++;
5421 return 0;
5422}
5423EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005424
Takashi Iwai4a471b72005-12-07 13:56:29 +01005425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426#ifdef CONFIG_PM
5427/*
5428 * power management
5429 */
5430
5431/**
5432 * snd_hda_suspend - suspend the codecs
5433 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 *
5435 * Returns 0 if successful.
5436 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005437int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005439 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Takashi Iwai0ba21762007-04-16 11:29:14 +02005441 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005442 if (hda_codec_is_power_on(codec))
5443 hda_call_codec_suspend(codec);
Takashi Iwai785f8572012-03-07 10:58:39 +01005444 else /* forcibly change the power to D3 even if not used */
5445 hda_set_power_state(codec,
5446 codec->afg ? codec->afg : codec->mfg,
5447 AC_PWRST_D3);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005448 if (codec->patch_ops.post_suspend)
5449 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451 return 0;
5452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005453EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455/**
5456 * snd_hda_resume - resume the codecs
5457 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 *
5459 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005460 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005461 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005462 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 */
5464int snd_hda_resume(struct hda_bus *bus)
5465{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005466 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
Takashi Iwai0ba21762007-04-16 11:29:14 +02005468 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005469 if (codec->patch_ops.pre_resume)
5470 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005471 if (snd_hda_codec_needs_resume(codec))
5472 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return 0;
5475}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005476EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005477#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005478
5479/*
5480 * generic arrays
5481 */
5482
Takashi Iwaid5191e52009-11-16 14:58:17 +01005483/**
5484 * snd_array_new - get a new element from the given array
5485 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005486 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005487 * Get a new element from the given array. If it exceeds the
5488 * pre-allocated array size, re-allocate the array.
5489 *
5490 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005491 */
5492void *snd_array_new(struct snd_array *array)
5493{
5494 if (array->used >= array->alloced) {
5495 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005496 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005497 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005498 void *nlist;
5499 if (snd_BUG_ON(num >= 4096))
5500 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005501 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005502 if (!nlist)
5503 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005504 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005505 array->list = nlist;
5506 array->alloced = num;
5507 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005508 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005510EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005511
Takashi Iwaid5191e52009-11-16 14:58:17 +01005512/**
5513 * snd_array_free - free the given array elements
5514 * @array: the array object
5515 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005516void snd_array_free(struct snd_array *array)
5517{
5518 kfree(array->list);
5519 array->used = 0;
5520 array->alloced = 0;
5521 array->list = NULL;
5522}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005523EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005524
Takashi Iwaid5191e52009-11-16 14:58:17 +01005525/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005526 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5527 * @pcm: PCM caps bits
5528 * @buf: the string buffer to write
5529 * @buflen: the max buffer length
5530 *
5531 * used by hda_proc.c and hda_eld.c
5532 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005533void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5534{
5535 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5536 int i, j;
5537
5538 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5539 if (pcm & (AC_SUPPCM_BITS_8 << i))
5540 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5541
5542 buf[j] = '\0'; /* necessary when j == 0 */
5543}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005544EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005545
5546MODULE_DESCRIPTION("HDA codec core");
5547MODULE_LICENSE("GPL");