blob: 30a3d8ff978b4f22a5c445795f72e41fa4535b84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwaicb53c622007-08-10 17:21:45 +020097#ifdef CONFIG_SND_HDA_POWER_SAVE
98static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#else
102static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200103#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200104#endif
105
Takashi Iwaid5191e52009-11-16 14:58:17 +0100106/**
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
109 *
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
112 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400113const char *snd_hda_get_jack_location(u32 cfg)
114{
115 static char *bases[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 };
118 static unsigned char specials_idx[] = {
119 0x07, 0x08,
120 0x17, 0x18, 0x19,
121 0x37, 0x38
122 };
123 static char *specials[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
127 };
128 int i;
129 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 if ((cfg & 0x0f) < 7)
131 return bases[cfg & 0x0f];
132 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 if (cfg == specials_idx[i])
134 return specials[i];
135 }
136 return "UNKNOWN";
137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139
Takashi Iwaid5191e52009-11-16 14:58:17 +0100140/**
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
143 *
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
146 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147const char *snd_hda_get_jack_connectivity(u32 cfg)
148{
149 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150
151 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100153EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154
Takashi Iwaid5191e52009-11-16 14:58:17 +0100155/**
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
158 *
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162const char *snd_hda_get_jack_type(u32 cfg)
163{
164 static char *jack_types[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
169 };
170
171 return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 >> AC_DEFCFG_DEVICE_SHIFT];
173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100174EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400175
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176/*
177 * Compose a 32bit command word to be sent to the HD-audio controller
178 */
179static inline unsigned int
180make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 unsigned int verb, unsigned int parm)
182{
183 u32 val;
184
Takashi Iwai82e1b802009-07-17 12:47:34 +0200185 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800187 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec->addr, direct, nid, verb, parm);
189 return ~0;
190 }
191
192 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193 val |= (u32)direct << 27;
194 val |= (u32)nid << 20;
195 val |= verb << 8;
196 val |= parm;
197 return val;
198}
199
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200/*
201 * Send and receive a verb
202 */
203static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
204 unsigned int *res)
205{
206 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200207 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208
Wu Fengguang6430aee2009-07-17 16:49:19 +0800209 if (cmd == ~0)
210 return -1;
211
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 if (res)
213 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 snd_hda_power_up(codec);
216 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200217 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200219 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800220 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200221 trace_hda_get_response(codec, *res);
222 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 mutex_unlock(&bus->cmd_mutex);
224 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200225 if (res && *res == -1 && bus->rirb_error) {
226 if (bus->response_reset) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200230 bus->ops.bus_reset(bus);
231 }
232 goto again;
233 }
234 /* clear reset-flag when the communication gets recovered */
235 if (!err)
236 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 return err;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
247 *
248 * Send a single command and read the corresponding response.
249 *
250 * Returns the obtained response value, or -1 for an error.
251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200252unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int verb, unsigned int parm)
255{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700258 if (codec_exec_verb(codec, cmd, &res))
259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return res;
261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100262EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
271 *
272 * Send a single command without waiting for response.
273 *
274 * Returns 0 if successful, or a negative error code.
275 */
276int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 unsigned int verb, unsigned int parm)
278{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200279 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100280 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200281 return codec_exec_verb(codec, cmd,
282 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100284EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/**
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
290 *
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
293 */
294void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295{
296 for (; seq->nid; seq++)
297 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100299EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
304 * @nid: NID to parse
305 * @start_id: the pointer to store the start NID
306 *
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
309 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200310int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned int parm;
314
315 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200316 if (parm == -1)
317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100321EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200323/* look up the cached results */
324static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
325{
326 int i, len;
327 for (i = 0; i < array->used; ) {
328 hda_nid_t *p = snd_array_elem(array, i);
329 if (nid == *p)
330 return p;
331 len = p[1];
332 i += len + 2;
333 }
334 return NULL;
335}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200338 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @codec: the HDA codec
340 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200341 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * Parses the connection list of the given widget and stores the list
344 * of NIDs.
345 *
346 * Returns the number of connections, or a negative error code.
347 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200348int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350{
351 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200354 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200357 again:
358 /* if the connection-list is already cached, read it */
359 p = lookup_conn_list(array, nid);
360 if (p) {
361 if (listp)
362 *listp = p + 2;
363 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 if (snd_BUG_ON(added))
366 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200368 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200369 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200370 if (len < 0)
371 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 err = snd_hda_override_conn_list(codec, nid, len, list);
373 if (err < 0)
374 return err;
375 added = true;
376 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377}
Takashi Iwaidce20792011-06-24 14:10:28 +0200378EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
379
380/**
381 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * @codec: the HDA codec
383 * @nid: NID to parse
384 * @conn_list: connection list array
385 * @max_conns: max. number of connections to store
386 *
387 * Parses the connection list of the given widget and stores the list
388 * of NIDs.
389 *
390 * Returns the number of connections, or a negative error code.
391 */
392int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200393 hda_nid_t *conn_list, int max_conns)
394{
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 const hda_nid_t *list;
396 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200399 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200400 if (len > max_conns) {
401 snd_printk(KERN_ERR "hda_codec: "
402 "Too many connections %d for NID 0x%x\n",
403 len, nid);
404 return -EINVAL;
405 }
406 memcpy(conn_list, list, len * sizeof(hda_nid_t));
407 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200408}
409EXPORT_SYMBOL_HDA(snd_hda_get_connections);
410
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200411/**
412 * snd_hda_get_raw_connections - copy connection list without cache
413 * @codec: the HDA codec
414 * @nid: NID to parse
415 * @conn_list: connection list array
416 * @max_conns: max. number of connections to store
417 *
418 * Like snd_hda_get_connections(), copy the connection list but without
419 * checking through the connection-list cache.
420 * Currently called only from hda_proc.c, so not exported.
421 */
422int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200428 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100429 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwaida3cec32008-08-08 17:12:14 +0200431 if (snd_BUG_ON(!conn_list || max_conns <= 0))
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200434 wcaps = get_wcaps(codec, nid);
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm & AC_CLIST_LONG) {
441 /* long form */
442 shift = 16;
443 num_elems = 2;
444 } else {
445 /* short form */
446 shift = 8;
447 num_elems = 4;
448 }
449 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 mask = (1 << (shift-1)) - 1;
451
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0; /* no connection */
454
455 if (conn_len == 1) {
456 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200457 parm = snd_hda_codec_read(codec, nid, 0,
458 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200459 if (parm == -1 && codec->bus->rirb_error)
460 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 conn_list[0] = parm & mask;
462 return 1;
463 }
464
465 /* multi connection */
466 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 prev_nid = 0;
468 for (i = 0; i < conn_len; i++) {
469 int range_val;
470 hda_nid_t val, n;
471
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 parm = snd_hda_codec_read(codec, nid, 0,
474 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200475 if (parm == -1 && codec->bus->rirb_error)
476 return -EIO;
477 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100479 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200480 if (val == 0) {
481 snd_printk(KERN_WARNING "hda_codec: "
482 "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 nid, i, parm);
484 return 0;
485 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100486 parm >>= shift;
487 if (range_val) {
488 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!prev_nid || prev_nid >= val) {
490 snd_printk(KERN_WARNING "hda_codec: "
491 "invalid dep_range_val %x:%x\n",
492 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 for (n = prev_nid + 1; n <= val; n++) {
496 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200497 snd_printk(KERN_ERR "hda_codec: "
498 "Too many connections %d for NID 0x%x\n",
499 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 return -EINVAL;
501 }
502 conn_list[conns++] = n;
503 }
504 } else {
505 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200506 snd_printk(KERN_ERR "hda_codec: "
507 "Too many connections %d for NID 0x%x\n",
508 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100509 return -EINVAL;
510 }
511 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return conns;
516}
517
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200518static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519{
520 hda_nid_t *p = snd_array_new(array);
521 if (!p)
522 return false;
523 *p = nid;
524 return true;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200528 * snd_hda_override_conn_list - add/modify the connection-list to cache
529 * @codec: the HDA codec
530 * @nid: NID to parse
531 * @len: number of connection list entries
532 * @list: the list of connection entries
533 *
534 * Add or modify the given connection-list to the cache. If the corresponding
535 * cache already exists, invalidate it and append a new one.
536 *
537 * Returns zero or a negative error code.
538 */
539int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 struct snd_array *array = &codec->conn_lists;
543 hda_nid_t *p;
544 int i, old_used;
545
546 p = lookup_conn_list(array, nid);
547 if (p)
548 *p = -1; /* invalidate the old entry */
549
550 old_used = array->used;
551 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 goto error_add;
553 for (i = 0; i < len; i++)
554 if (!add_conn_list(array, list[i]))
555 goto error_add;
556 return 0;
557
558 error_add:
559 array->used = old_used;
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563
564/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
570 *
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
574 */
575int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
577{
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 int i, nums;
580
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
583 if (conn[i] == nid)
584 return i;
585 if (!recursive)
586 return -1;
587 if (recursive > 5) {
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 return -1;
590 }
591 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 return -1;
600}
601EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @bus: the BUS
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 *
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616{
617 struct hda_bus_unsolicited *unsol;
618 unsigned int wp;
619
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200620 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 unsol = bus->unsol;
622 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->wp = wp;
627
628 wp <<= 1;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
631
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100636EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800639 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Howellsc4028952006-11-22 14:57:56 +0000641static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howellsc4028952006-11-22 14:57:56 +0000643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
648
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
651 unsol->rp = rp;
652 rp <<= 1;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
660 }
661}
662
663/*
664 * initialize unsolicited queue
665 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200666static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct hda_bus_unsolicited *unsol;
669
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100670 if (bus->unsol) /* already initialized */
671 return 0;
672
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200674 if (!unsol) {
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&unsol->work, process_unsol_events);
680 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus->unsol = unsol;
682 return 0;
683}
684
685/*
686 * destructor
687 */
688static void snd_hda_codec_free(struct hda_codec *codec);
689
690static int snd_hda_bus_free(struct hda_bus *bus)
691{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Takashi Iwai0ba21762007-04-16 11:29:14 +0200694 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 if (bus->workq)
697 flush_workqueue(bus->workq);
698 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_hda_codec_free(codec);
702 }
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100705 if (bus->workq)
706 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(bus);
708 return 0;
709}
710
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100711static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100714 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return snd_hda_bus_free(bus);
716}
717
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718#ifdef CONFIG_SND_HDA_HWDEP
719static int snd_hda_bus_dev_register(struct snd_device *device)
720{
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100725 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200726 }
727 return 0;
728}
729#else
730#define snd_hda_bus_dev_register NULL
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
738 *
739 * Returns 0 if successful, or a negative error code.
740 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100741int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct hda_bus *bus;
746 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100747 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200748 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .dev_free = snd_hda_bus_dev_free,
750 };
751
Takashi Iwaida3cec32008-08-08 17:12:14 +0200752 if (snd_BUG_ON(!temp))
753 return -EINVAL;
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (busp)
758 *busp = NULL;
759
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (bus == NULL) {
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
763 return -ENOMEM;
764 }
765
766 bus->card = card;
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100770 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 bus->ops = temp->ops;
772
Ingo Molnar62932df2006-01-16 16:34:20 +0100773 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200774 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&bus->codec_list);
776
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
782 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 kfree(bus);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 snd_hda_bus_free(bus);
790 return err;
791 }
792 if (busp)
793 *busp = bus;
794 return 0;
795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100796EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798#ifdef CONFIG_SND_HDA_GENERIC
799#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200800 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200801#else
802#define is_generic_config(codec) 0
803#endif
804
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * find a matching codec preset
813 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200815find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
819 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai82467612007-07-27 19:15:54 +0200821 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 return NULL; /* use the generic parser */
823
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 again:
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
829 continue;
830 }
831 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100833 if (preset->afg && preset->afg != codec->afg)
834 continue;
835 if (preset->mfg && preset->mfg != codec->mfg)
836 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200839 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847 module_put(tbl->owner);
848 }
849 mutex_unlock(&preset_mutex);
850
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
852 char name[32];
853 if (!mod_requested)
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
855 codec->vendor_id);
856 else
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
860 mod_requested++;
861 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return NULL;
864}
865
866/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 char tmp[16];
875
876 if (codec->vendor_name)
877 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
881 vendor = c->name;
882 break;
883 }
884 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200885 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sprintf(tmp, "Generic %04x", vendor_id);
887 vendor = tmp;
888 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
891 return -ENOMEM;
892
893 get_chip_name:
894 if (codec->chip_name)
895 return 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 else {
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 }
903 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200904 return -ENOMEM;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200909 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200913 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hda_nid_t nid;
915
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200918 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200919 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200920 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200921 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200925 break;
926 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200930 break;
931 default:
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100938 * read widget caps for each widget and store in cache
939 */
940static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
941{
942 int i;
943 hda_nid_t nid;
944
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 &codec->start_nid);
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100949 return -ENOMEM;
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
954 return 0;
955}
956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* read all pin default configurations and save codec->init_pins */
958static int read_pin_defaults(struct hda_codec *codec)
959{
960 int i;
961 hda_nid_t nid = codec->start_nid;
962
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200966 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967 if (wid_type != AC_WID_PIN)
968 continue;
969 pin = snd_array_new(&codec->init_pins);
970 if (!pin)
971 return -ENOMEM;
972 pin->nid = nid;
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
977 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 }
979 return 0;
980}
981
982/* look up the given pin config list and return the item matching with NID */
983static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
985 hda_nid_t nid)
986{
987 int i;
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
990 if (pin->nid == nid)
991 return pin;
992 }
993 return NULL;
994}
995
996/* write a config value for the given NID */
997static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
998 unsigned int cfg)
999{
1000 int i;
1001 for (i = 0; i < 4; i++) {
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1004 cfg & 0xff);
1005 cfg >>= 8;
1006 }
1007}
1008
1009/* set the current pin config value for the given NID.
1010 * the value is cached, and read via snd_hda_codec_get_pincfg()
1011 */
1012int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1013 hda_nid_t nid, unsigned int cfg)
1014{
1015 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001016 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001017
Takashi Iwaib82855a2009-12-27 11:24:56 +01001018 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1019 return -EINVAL;
1020
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001021 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 pin = look_up_pincfg(codec, list, nid);
1023 if (!pin) {
1024 pin = snd_array_new(list);
1025 if (!pin)
1026 return -ENOMEM;
1027 pin->nid = nid;
1028 }
1029 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001030
1031 /* change only when needed; e.g. if the pincfg is already present
1032 * in user_pins[], don't write it
1033 */
1034 cfg = snd_hda_codec_get_pincfg(codec, nid);
1035 if (oldcfg != cfg)
1036 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001037 return 0;
1038}
1039
Takashi Iwaid5191e52009-11-16 14:58:17 +01001040/**
1041 * snd_hda_codec_set_pincfg - Override a pin default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to set the pin config
1044 * @cfg: the pin default config value
1045 *
1046 * Override a pin default configuration value in the cache.
1047 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1048 * priority than the real hardware value.
1049 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001050int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1051 hda_nid_t nid, unsigned int cfg)
1052{
Takashi Iwai346ff702009-02-23 09:42:57 +01001053 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001054}
1055EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1056
Takashi Iwaid5191e52009-11-16 14:58:17 +01001057/**
1058 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1059 * @codec: the HDA codec
1060 * @nid: NID to get the pin config
1061 *
1062 * Get the current pin config value of the given pin NID.
1063 * If the pincfg value is cached or overridden via sysfs or driver,
1064 * returns the cached value.
1065 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001066unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1067{
1068 struct hda_pincfg *pin;
1069
Takashi Iwai3be14142009-02-20 14:11:16 +01001070#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 if (pin)
1073 return pin->cfg;
1074#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001075 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1076 if (pin)
1077 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1079 if (pin)
1080 return pin->cfg;
1081 return 0;
1082}
1083EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1084
1085/* restore all current pin configs */
1086static void restore_pincfgs(struct hda_codec *codec)
1087{
1088 int i;
1089 for (i = 0; i < codec->init_pins.used; i++) {
1090 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1091 set_pincfg(codec, pin->nid,
1092 snd_hda_codec_get_pincfg(codec, pin->nid));
1093 }
1094}
Takashi Iwai54d17402005-11-21 16:33:22 +01001095
Takashi Iwai92ee6162009-12-27 11:18:59 +01001096/**
1097 * snd_hda_shutup_pins - Shut up all pins
1098 * @codec: the HDA codec
1099 *
1100 * Clear all pin controls to shup up before suspend for avoiding click noise.
1101 * The controls aren't cached so that they can be resumed properly.
1102 */
1103void snd_hda_shutup_pins(struct hda_codec *codec)
1104{
1105 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106 /* don't shut up pins when unloading the driver; otherwise it breaks
1107 * the default pin setup at the next load of the driver
1108 */
1109 if (codec->bus->shutdown)
1110 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001111 for (i = 0; i < codec->init_pins.used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1113 /* use read here for syncing after issuing each verb */
1114 snd_hda_codec_read(codec, pin->nid, 0,
1115 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1116 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001117 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001118}
1119EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1120
Takashi Iwai2a439522011-07-26 09:52:50 +02001121#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001122/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1123static void restore_shutup_pins(struct hda_codec *codec)
1124{
1125 int i;
1126 if (!codec->pins_shutup)
1127 return;
1128 if (codec->bus->shutdown)
1129 return;
1130 for (i = 0; i < codec->init_pins.used; i++) {
1131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1132 snd_hda_codec_write(codec, pin->nid, 0,
1133 AC_VERB_SET_PIN_WIDGET_CONTROL,
1134 pin->ctrl);
1135 }
1136 codec->pins_shutup = 0;
1137}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001138#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001139
Takashi Iwai01751f52007-08-10 16:59:39 +02001140static void init_hda_cache(struct hda_cache_rec *cache,
1141 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001142static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143
Takashi Iwai3be14142009-02-20 14:11:16 +01001144/* restore the initial pin cfgs and release all pincfg lists */
1145static void restore_init_pincfgs(struct hda_codec *codec)
1146{
Takashi Iwai346ff702009-02-23 09:42:57 +01001147 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001148 * so that only the values in init_pins are restored
1149 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001152 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001153#endif
1154 restore_pincfgs(codec);
1155 snd_array_free(&codec->init_pins);
1156}
1157
Takashi Iwai54d17402005-11-21 16:33:22 +01001158/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001159 * audio-converter setup caches
1160 */
1161struct hda_cvt_setup {
1162 hda_nid_t nid;
1163 u8 stream_tag;
1164 u8 channel_id;
1165 u16 format_id;
1166 unsigned char active; /* cvt is currently used */
1167 unsigned char dirty; /* setups should be cleared */
1168};
1169
1170/* get or create a cache entry for the given audio converter NID */
1171static struct hda_cvt_setup *
1172get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1173{
1174 struct hda_cvt_setup *p;
1175 int i;
1176
1177 for (i = 0; i < codec->cvt_setups.used; i++) {
1178 p = snd_array_elem(&codec->cvt_setups, i);
1179 if (p->nid == nid)
1180 return p;
1181 }
1182 p = snd_array_new(&codec->cvt_setups);
1183 if (p)
1184 p->nid = nid;
1185 return p;
1186}
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * codec destructor
1190 */
1191static void snd_hda_codec_free(struct hda_codec *codec)
1192{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001193 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001195 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#ifdef CONFIG_SND_HDA_POWER_SAVE
1197 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001198 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001201 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001202 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001208 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001209 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001210 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001211 kfree(codec->vendor_name);
1212 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001213 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001214 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(codec);
1216}
1217
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001218static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1219 unsigned int power_state);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
1222 * snd_hda_codec_new - create a HDA codec
1223 * @bus: the bus to assign
1224 * @codec_addr: the codec address
1225 * @codecp: the pointer to store the generated codec
1226 *
1227 * Returns 0 if successful, or a negative error code.
1228 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001229int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1230 unsigned int codec_addr,
1231 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001234 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
1236
Takashi Iwaida3cec32008-08-08 17:12:14 +02001237 if (snd_BUG_ON(!bus))
1238 return -EINVAL;
1239 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243 snd_printk(KERN_ERR "hda_codec: "
1244 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EBUSY;
1246 }
1247
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001248 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (codec == NULL) {
1250 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1251 return -ENOMEM;
1252 }
1253
1254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001258 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001259 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001260 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001262 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001263 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001264 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001265 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001269 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001270 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1271 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1272 * the caller has to power down appropriatley after initialization
1273 * phase.
1274 */
1275 hda_keep_power_on(codec);
1276#endif
1277
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001278 if (codec->bus->modelname) {
1279 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1280 if (!codec->modelname) {
1281 snd_hda_codec_free(codec);
1282 return -ENODEV;
1283 }
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_add_tail(&codec->list, &bus->codec_list);
1287 bus->caddr_tbl[codec_addr] = codec;
1288
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001291 if (codec->vendor_id == -1)
1292 /* read again, hopefully the access method was corrected
1293 * in the last read...
1294 */
1295 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001297 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_SUBSYSTEM_ID);
1299 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1300 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001302 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001304 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 err = -ENODEV;
1306 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1310 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = read_pin_defaults(codec);
1315 if (err < 0)
1316 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001319 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001320 codec->subsystem_id =
1321 snd_hda_codec_read(codec, nid, 0,
1322 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001323 }
1324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
1326 hda_set_power_state(codec,
1327 codec->afg ? codec->afg : codec->mfg,
1328 AC_PWRST_D0);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_codec_proc_new(codec);
1331
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333
1334 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1335 codec->subsystem_id, codec->revision_id);
1336 snd_component_add(codec->bus->card, component);
1337
1338 if (codecp)
1339 *codecp = codec;
1340 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001341
1342 error:
1343 snd_hda_codec_free(codec);
1344 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001346EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348/**
1349 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1350 * @codec: the HDA codec
1351 *
1352 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * patch instance.
1354 *
1355 * Returns 0 if successful or a negative error code.
1356 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001357int snd_hda_codec_configure(struct hda_codec *codec)
1358{
1359 int err;
1360
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001361 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001363 err = get_codec_name(codec);
1364 if (err < 0)
1365 return err;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001370 goto patched;
1371 }
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (codec->preset && codec->preset->patch) {
1373 err = codec->preset->patch(codec);
1374 goto patched;
1375 }
1376
1377 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001378 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001379 if (err < 0)
1380 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001381
1382 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 if (!err && codec->patch_ops.unsol_event)
1384 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001385 /* audio codec should override the mixer name */
1386 if (!err && (codec->afg || !*codec->bus->card->mixername))
1387 snprintf(codec->bus->card->mixername,
1388 sizeof(codec->bus->card->mixername),
1389 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001392EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/**
1395 * snd_hda_codec_setup_stream - set up the codec for streaming
1396 * @codec: the CODEC to set up
1397 * @nid: the NID to set up
1398 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1399 * @channel_id: channel id to pass, zero based.
1400 * @format: stream format.
1401 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1403 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int channel_id, int format)
1405{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001406 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001407 struct hda_cvt_setup *p;
1408 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001409 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001410 int i;
1411
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001413 return;
1414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 snd_printdd("hda_codec_setup_stream: "
1416 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001418 p = get_hda_cvt_setup(codec, nid);
1419 if (!p)
1420 return;
1421 /* update the stream-id if changed */
1422 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1423 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1424 newval = (stream_tag << 4) | channel_id;
1425 if (oldval != newval)
1426 snd_hda_codec_write(codec, nid, 0,
1427 AC_VERB_SET_CHANNEL_STREAMID,
1428 newval);
1429 p->stream_tag = stream_tag;
1430 p->channel_id = channel_id;
1431 }
1432 /* update the format-id if changed */
1433 if (p->format_id != format) {
1434 oldval = snd_hda_codec_read(codec, nid, 0,
1435 AC_VERB_GET_STREAM_FORMAT, 0);
1436 if (oldval != format) {
1437 msleep(1);
1438 snd_hda_codec_write(codec, nid, 0,
1439 AC_VERB_SET_STREAM_FORMAT,
1440 format);
1441 }
1442 p->format_id = format;
1443 }
1444 p->active = 1;
1445 p->dirty = 0;
1446
1447 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001448 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001449 list_for_each_entry(c, &codec->bus->codec_list, list) {
1450 for (i = 0; i < c->cvt_setups.used; i++) {
1451 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001452 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001453 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001454 p->dirty = 1;
1455 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001458EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460static void really_cleanup_stream(struct hda_codec *codec,
1461 struct hda_cvt_setup *q);
1462
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001465 * @codec: the CODEC to clean up
1466 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001468 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001469void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1470 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001471{
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 struct hda_cvt_setup *p;
1473
Takashi Iwai888afa12008-03-18 09:57:50 +01001474 if (!nid)
1475 return;
1476
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001477 if (codec->no_sticky_stream)
1478 do_now = 1;
1479
Takashi Iwai888afa12008-03-18 09:57:50 +01001480 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001481 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001482 if (p) {
1483 /* here we just clear the active flag when do_now isn't set;
1484 * actual clean-ups will be done later in
1485 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1486 */
1487 if (do_now)
1488 really_cleanup_stream(codec, p);
1489 else
1490 p->active = 0;
1491 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001492}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001493EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001494
Takashi Iwaieb541332010-08-06 13:48:11 +02001495static void really_cleanup_stream(struct hda_codec *codec,
1496 struct hda_cvt_setup *q)
1497{
1498 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001499 if (q->stream_tag || q->channel_id)
1500 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1501 if (q->format_id)
1502 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1503);
Takashi Iwaieb541332010-08-06 13:48:11 +02001504 memset(q, 0, sizeof(*q));
1505 q->nid = nid;
1506}
1507
1508/* clean up the all conflicting obsolete streams */
1509static void purify_inactive_streams(struct hda_codec *codec)
1510{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001511 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001512 int i;
1513
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001514 list_for_each_entry(c, &codec->bus->codec_list, list) {
1515 for (i = 0; i < c->cvt_setups.used; i++) {
1516 struct hda_cvt_setup *p;
1517 p = snd_array_elem(&c->cvt_setups, i);
1518 if (p->dirty)
1519 really_cleanup_stream(c, p);
1520 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 }
1522}
1523
Takashi Iwai2a439522011-07-26 09:52:50 +02001524#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001525/* clean up all streams; called from suspend */
1526static void hda_cleanup_all_streams(struct hda_codec *codec)
1527{
1528 int i;
1529
1530 for (i = 0; i < codec->cvt_setups.used; i++) {
1531 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1532 if (p->stream_tag)
1533 really_cleanup_stream(codec, p);
1534 }
1535}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001536#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
1539 * amp access functions
1540 */
1541
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001542/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001543#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001544#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001545#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1546#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001548#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001551static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001552 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai01751f52007-08-10 16:59:39 +02001554 memset(cache, 0, sizeof(*cache));
1555 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001556 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001557}
1558
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001559static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001560{
Takashi Iwai603c4012008-07-30 15:01:44 +02001561 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001565static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai01751f52007-08-10 16:59:39 +02001567 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1568 u16 cur = cache->hash[idx];
1569 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001572 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (info->key == key)
1574 return info;
1575 cur = info->next;
1576 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001577 return NULL;
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001580/* query the hash. allocate an entry if not found. */
1581static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1582 u32 key)
1583{
1584 struct hda_cache_head *info = get_hash(cache, key);
1585 if (!info) {
1586 u16 idx, cur;
1587 /* add a new hash entry */
1588 info = snd_array_new(&cache->buf);
1589 if (!info)
1590 return NULL;
1591 cur = snd_array_index(&cache->buf, info);
1592 info->key = key;
1593 info->val = 0;
1594 idx = key % (u16)ARRAY_SIZE(cache->hash);
1595 info->next = cache->hash[idx];
1596 cache->hash[idx] = cur;
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return info;
1599}
1600
Takashi Iwai01751f52007-08-10 16:59:39 +02001601/* query and allocate an amp hash entry */
1602static inline struct hda_amp_info *
1603get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1604{
1605 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1606}
1607
Takashi Iwaid5191e52009-11-16 14:58:17 +01001608/**
1609 * query_amp_caps - query AMP capabilities
1610 * @codec: the HD-auio codec
1611 * @nid: the NID to query
1612 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1613 *
1614 * Query AMP capabilities for the given widget and direction.
1615 * Returns the obtained capability bits.
1616 *
1617 * When cap bits have been already read, this doesn't read again but
1618 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001620u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1625 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001627 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 info->amp_caps = snd_hda_param_read(codec, nid,
1631 direction == HDA_OUTPUT ?
1632 AC_PAR_AMP_OUT_CAP :
1633 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001634 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001635 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 return info->amp_caps;
1638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001639EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Takashi Iwaid5191e52009-11-16 14:58:17 +01001641/**
1642 * snd_hda_override_amp_caps - Override the AMP capabilities
1643 * @codec: the CODEC to clean up
1644 * @nid: the NID to clean up
1645 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1646 * @caps: the capability bits to set
1647 *
1648 * Override the cached AMP caps bits value by the given one.
1649 * This function is useful if the driver needs to adjust the AMP ranges,
1650 * e.g. limit to 0dB, etc.
1651 *
1652 * Returns zero if successful or a negative error code.
1653 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001654int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1655 unsigned int caps)
1656{
1657 struct hda_amp_info *info;
1658
1659 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1660 if (!info)
1661 return -EINVAL;
1662 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001663 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001664 return 0;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001667
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001668static unsigned int
1669query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1670 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001671{
1672 struct hda_amp_info *info;
1673
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001674 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001675 if (!info)
1676 return 0;
1677 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001678 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001679 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001680 }
1681 return info->amp_caps;
1682}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001683
1684static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1685{
1686 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1687}
1688
Takashi Iwaid5191e52009-11-16 14:58:17 +01001689/**
1690 * snd_hda_query_pin_caps - Query PIN capabilities
1691 * @codec: the HD-auio codec
1692 * @nid: the NID to query
1693 *
1694 * Query PIN capabilities for the given widget.
1695 * Returns the obtained capability bits.
1696 *
1697 * When cap bits have been already read, this doesn't read again but
1698 * returns the cached value.
1699 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001700u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1701{
1702 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1703 read_pin_cap);
1704}
Takashi Iwai1327a322009-03-23 13:07:47 +01001705EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1706
Wu Fengguang864f92b2009-11-18 12:38:02 +08001707/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001708 * snd_hda_override_pin_caps - Override the pin capabilities
1709 * @codec: the CODEC
1710 * @nid: the NID to override
1711 * @caps: the capability bits to set
1712 *
1713 * Override the cached PIN capabilitiy bits value by the given one.
1714 *
1715 * Returns zero if successful or a negative error code.
1716 */
1717int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1718 unsigned int caps)
1719{
1720 struct hda_amp_info *info;
1721 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1722 if (!info)
1723 return -ENOMEM;
1724 info->amp_caps = caps;
1725 info->head.val |= INFO_AMP_CAPS;
1726 return 0;
1727}
1728EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001732 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734static unsigned int get_vol_mute(struct hda_codec *codec,
1735 struct hda_amp_info *info, hda_nid_t nid,
1736 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 u32 val, parm;
1739
Takashi Iwai01751f52007-08-10 16:59:39 +02001740 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001741 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1744 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1745 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001746 val = snd_hda_codec_read(codec, nid, 0,
1747 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001749 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001750 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001754 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001756static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001757 hda_nid_t nid, int ch, int direction, int index,
1758 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 u32 parm;
1761
1762 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1763 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1764 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001765 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1766 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1767 ; /* set the zero value as a fake mute */
1768 else
1769 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001771 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Takashi Iwaid5191e52009-11-16 14:58:17 +01001774/**
1775 * snd_hda_codec_amp_read - Read AMP value
1776 * @codec: HD-audio codec
1777 * @nid: NID to read the AMP value
1778 * @ch: channel (left=0 or right=1)
1779 * @direction: #HDA_INPUT or #HDA_OUTPUT
1780 * @index: the index value (only for input direction)
1781 *
1782 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Takashi Iwai834be882006-03-01 14:16:17 +01001784int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1785 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787 struct hda_amp_info *info;
1788 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1789 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001791 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001793EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Takashi Iwaid5191e52009-11-16 14:58:17 +01001795/**
1796 * snd_hda_codec_amp_update - update the AMP value
1797 * @codec: HD-audio codec
1798 * @nid: NID to read the AMP value
1799 * @ch: channel (left=0 or right=1)
1800 * @direction: #HDA_INPUT or #HDA_OUTPUT
1801 * @idx: the index value (only for input direction)
1802 * @mask: bit mask to set
1803 * @val: the bits value to set
1804 *
1805 * Update the AMP value with a bit mask.
1806 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001807 */
Takashi Iwai834be882006-03-01 14:16:17 +01001808int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1809 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001812
Takashi Iwai0ba21762007-04-16 11:29:14 +02001813 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1814 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001816 if (snd_BUG_ON(mask & ~0xff))
1817 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001818 val &= mask;
1819 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001820 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001822 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return 1;
1824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001825EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Takashi Iwaid5191e52009-11-16 14:58:17 +01001827/**
1828 * snd_hda_codec_amp_stereo - update the AMP stereo values
1829 * @codec: HD-audio codec
1830 * @nid: NID to read the AMP value
1831 * @direction: #HDA_INPUT or #HDA_OUTPUT
1832 * @idx: the index value (only for input direction)
1833 * @mask: bit mask to set
1834 * @val: the bits value to set
1835 *
1836 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1837 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001838 */
1839int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1840 int direction, int idx, int mask, int val)
1841{
1842 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001843
1844 if (snd_BUG_ON(mask & ~0xff))
1845 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001846 for (ch = 0; ch < 2; ch++)
1847 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1848 idx, mask, val);
1849 return ret;
1850}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001851EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001852
Takashi Iwai2a439522011-07-26 09:52:50 +02001853#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001854/**
1855 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1856 * @codec: HD-audio codec
1857 *
1858 * Resume the all amp commands from the cache.
1859 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001860void snd_hda_codec_resume_amp(struct hda_codec *codec)
1861{
Takashi Iwai603c4012008-07-30 15:01:44 +02001862 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001863 int i;
1864
Takashi Iwai603c4012008-07-30 15:01:44 +02001865 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001866 u32 key = buffer->head.key;
1867 hda_nid_t nid;
1868 unsigned int idx, dir, ch;
1869 if (!key)
1870 continue;
1871 nid = key & 0xff;
1872 idx = (key >> 16) & 0xff;
1873 dir = (key >> 24) & 0xff;
1874 for (ch = 0; ch < 2; ch++) {
1875 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1876 continue;
1877 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1878 buffer->vol[ch]);
1879 }
1880 }
1881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001882EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001883#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001885static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1886 unsigned int ofs)
1887{
1888 u32 caps = query_amp_caps(codec, nid, dir);
1889 /* get num steps */
1890 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1891 if (ofs < caps)
1892 caps -= ofs;
1893 return caps;
1894}
1895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1898 *
1899 * The control element is supposed to have the private_value field
1900 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1901 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001902int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 u16 nid = get_amp_nid(kcontrol);
1907 u8 chs = get_amp_channels(kcontrol);
1908 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001909 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1912 uinfo->count = chs == 3 ? 2 : 1;
1913 uinfo->value.integer.min = 0;
1914 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1915 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001916 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001917 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1918 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EINVAL;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001923EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001925
1926static inline unsigned int
1927read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1928 int ch, int dir, int idx, unsigned int ofs)
1929{
1930 unsigned int val;
1931 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1932 val &= HDA_AMP_VOLMASK;
1933 if (val >= ofs)
1934 val -= ofs;
1935 else
1936 val = 0;
1937 return val;
1938}
1939
1940static inline int
1941update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1942 int ch, int dir, int idx, unsigned int ofs,
1943 unsigned int val)
1944{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001945 unsigned int maxval;
1946
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001947 if (val > 0)
1948 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001949 /* ofs = 0: raw max value */
1950 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001951 if (val > maxval)
1952 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001953 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1954 HDA_AMP_VOLMASK, val);
1955}
1956
Takashi Iwaid5191e52009-11-16 14:58:17 +01001957/**
1958 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1959 *
1960 * The control element is supposed to have the private_value field
1961 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1962 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001963int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1967 hda_nid_t nid = get_amp_nid(kcontrol);
1968 int chs = get_amp_channels(kcontrol);
1969 int dir = get_amp_direction(kcontrol);
1970 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001971 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 long *valp = ucontrol->value.integer.value;
1973
1974 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001977 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001980EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Takashi Iwaid5191e52009-11-16 14:58:17 +01001982/**
1983 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1984 *
1985 * The control element is supposed to have the private_value field
1986 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001988int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 hda_nid_t nid = get_amp_nid(kcontrol);
1993 int chs = get_amp_channels(kcontrol);
1994 int dir = get_amp_direction(kcontrol);
1995 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001996 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 long *valp = ucontrol->value.integer.value;
1998 int change = 0;
1999
Takashi Iwaicb53c622007-08-10 17:21:45 +02002000 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002001 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002003 valp++;
2004 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002005 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002007 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return change;
2009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002010EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Takashi Iwaid5191e52009-11-16 14:58:17 +01002012/**
2013 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2014 *
2015 * The control element is supposed to have the private_value field
2016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2017 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002018int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2019 unsigned int size, unsigned int __user *_tlv)
2020{
2021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2022 hda_nid_t nid = get_amp_nid(kcontrol);
2023 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002025 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002026 u32 caps, val1, val2;
2027
2028 if (size < 4 * sizeof(unsigned int))
2029 return -ENOMEM;
2030 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002031 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2032 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002033 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002034 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002035 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002036 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002037 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002038 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2039 return -EFAULT;
2040 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2041 return -EFAULT;
2042 if (put_user(val1, _tlv + 2))
2043 return -EFAULT;
2044 if (put_user(val2, _tlv + 3))
2045 return -EFAULT;
2046 return 0;
2047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002048EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
2051 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2052 * @codec: HD-audio codec
2053 * @nid: NID of a reference widget
2054 * @dir: #HDA_INPUT or #HDA_OUTPUT
2055 * @tlv: TLV data to be stored, at least 4 elements
2056 *
2057 * Set (static) TLV data for a virtual master volume using the AMP caps
2058 * obtained from the reference NID.
2059 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002060 */
2061void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2062 unsigned int *tlv)
2063{
2064 u32 caps;
2065 int nums, step;
2066
2067 caps = query_amp_caps(codec, nid, dir);
2068 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2069 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2070 step = (step + 1) * 25;
2071 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2072 tlv[1] = 2 * sizeof(unsigned int);
2073 tlv[2] = -nums * step;
2074 tlv[3] = step;
2075}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002076EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002077
2078/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002079static struct snd_kcontrol *
2080_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2081 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002082{
2083 struct snd_ctl_elem_id id;
2084 memset(&id, 0, sizeof(id));
2085 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002086 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002087 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2088 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002089 strcpy(id.name, name);
2090 return snd_ctl_find_id(codec->bus->card, &id);
2091}
2092
Takashi Iwaid5191e52009-11-16 14:58:17 +01002093/**
2094 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2095 * @codec: HD-audio codec
2096 * @name: ctl id name string
2097 *
2098 * Get the control element with the given id string and IFACE_MIXER.
2099 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002100struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2101 const char *name)
2102{
2103 return _snd_hda_find_mixer_ctl(codec, name, 0);
2104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002105EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002106
Takashi Iwai1afe2062010-12-23 10:17:52 +01002107static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2108{
2109 int idx;
2110 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2111 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2112 return idx;
2113 }
2114 return -EBUSY;
2115}
2116
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002118 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002119 * @codec: HD-audio codec
2120 * @nid: corresponding NID (optional)
2121 * @kctl: the control element to assign
2122 *
2123 * Add the given control element to an array inside the codec instance.
2124 * All control elements belonging to a codec are supposed to be added
2125 * by this function so that a proper clean-up works at the free or
2126 * reconfiguration time.
2127 *
2128 * If non-zero @nid is passed, the NID is assigned to the control element.
2129 * The assignment is shown in the codec proc file.
2130 *
2131 * snd_hda_ctl_add() checks the control subdev id field whether
2132 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002133 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2134 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002135 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002136int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2137 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002138{
2139 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002140 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002141 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002142
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002144 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002145 if (nid == 0)
2146 nid = get_amp_nid_(kctl->private_value);
2147 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002148 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2149 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002150 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002151 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002152 err = snd_ctl_add(codec->bus->card, kctl);
2153 if (err < 0)
2154 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002155 item = snd_array_new(&codec->mixers);
2156 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002157 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002158 item->kctl = kctl;
2159 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002160 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002161 return 0;
2162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002163EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002164
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002166 * snd_hda_add_nid - Assign a NID to a control element
2167 * @codec: HD-audio codec
2168 * @nid: corresponding NID (optional)
2169 * @kctl: the control element to assign
2170 * @index: index to kctl
2171 *
2172 * Add the given control element to an array inside the codec instance.
2173 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2174 * NID:KCTL mapping - for example "Capture Source" selector.
2175 */
2176int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2177 unsigned int index, hda_nid_t nid)
2178{
2179 struct hda_nid_item *item;
2180
2181 if (nid > 0) {
2182 item = snd_array_new(&codec->nids);
2183 if (!item)
2184 return -ENOMEM;
2185 item->kctl = kctl;
2186 item->index = index;
2187 item->nid = nid;
2188 return 0;
2189 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002190 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2191 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002192 return -EINVAL;
2193}
2194EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2195
2196/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2198 * @codec: HD-audio codec
2199 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002200void snd_hda_ctls_clear(struct hda_codec *codec)
2201{
2202 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002203 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002205 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002207 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002208}
2209
Takashi Iwaia65d6292009-02-23 16:57:04 +01002210/* pseudo device locking
2211 * toggle card->shutdown to allow/disallow the device access (as a hack)
2212 */
2213static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002214{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002215 spin_lock(&card->files_lock);
2216 if (card->shutdown) {
2217 spin_unlock(&card->files_lock);
2218 return -EINVAL;
2219 }
2220 card->shutdown = 1;
2221 spin_unlock(&card->files_lock);
2222 return 0;
2223}
2224
2225static void hda_unlock_devices(struct snd_card *card)
2226{
2227 spin_lock(&card->files_lock);
2228 card->shutdown = 0;
2229 spin_unlock(&card->files_lock);
2230}
2231
Takashi Iwaid5191e52009-11-16 14:58:17 +01002232/**
2233 * snd_hda_codec_reset - Clear all objects assigned to the codec
2234 * @codec: HD-audio codec
2235 *
2236 * This frees the all PCM and control elements assigned to the codec, and
2237 * clears the caches and restores the pin default configurations.
2238 *
2239 * When a device is being used, it returns -EBSY. If successfully freed,
2240 * returns zero.
2241 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002242int snd_hda_codec_reset(struct hda_codec *codec)
2243{
2244 struct snd_card *card = codec->bus->card;
2245 int i, pcm;
2246
2247 if (hda_lock_devices(card) < 0)
2248 return -EBUSY;
2249 /* check whether the codec isn't used by any mixer or PCM streams */
2250 if (!list_empty(&card->ctl_files)) {
2251 hda_unlock_devices(card);
2252 return -EBUSY;
2253 }
2254 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2255 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2256 if (!cpcm->pcm)
2257 continue;
2258 if (cpcm->pcm->streams[0].substream_opened ||
2259 cpcm->pcm->streams[1].substream_opened) {
2260 hda_unlock_devices(card);
2261 return -EBUSY;
2262 }
2263 }
2264
2265 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002266
2267#ifdef CONFIG_SND_HDA_POWER_SAVE
2268 cancel_delayed_work(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002269 codec->power_on = 0;
2270 codec->power_transition = 0;
2271 codec->power_jiffies = jiffies;
Takashi Iwai6acaed32009-01-12 10:09:24 +01002272 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002273#endif
2274 snd_hda_ctls_clear(codec);
2275 /* relase PCMs */
2276 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002277 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002278 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002279 clear_bit(codec->pcm_info[i].device,
2280 codec->bus->pcm_dev_bits);
2281 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002282 }
2283 if (codec->patch_ops.free)
2284 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002285 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002286 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002287 codec->spec = NULL;
2288 free_hda_cache(&codec->amp_cache);
2289 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002290 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2291 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002292 /* free only driver_pins so that init_pins + user_pins are restored */
2293 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002294 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002295 codec->num_pcms = 0;
2296 codec->pcm_info = NULL;
2297 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002298 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2299 codec->slave_dig_outs = NULL;
2300 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002301 module_put(codec->owner);
2302 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002303
2304 /* allow device access again */
2305 hda_unlock_devices(card);
2306 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002307}
2308
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002309typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2310
2311/* apply the function to all matching slave ctls in the mixer list */
2312static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002313 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002314{
2315 struct hda_nid_item *items;
2316 const char * const *s;
2317 int i, err;
2318
2319 items = codec->mixers.list;
2320 for (i = 0; i < codec->mixers.used; i++) {
2321 struct snd_kcontrol *sctl = items[i].kctl;
2322 if (!sctl || !sctl->id.name ||
2323 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2324 continue;
2325 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002326 char tmpname[sizeof(sctl->id.name)];
2327 const char *name = *s;
2328 if (suffix) {
2329 snprintf(tmpname, sizeof(tmpname), "%s %s",
2330 name, suffix);
2331 name = tmpname;
2332 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002333 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002334 err = func(data, sctl);
2335 if (err)
2336 return err;
2337 break;
2338 }
2339 }
2340 }
2341 return 0;
2342}
2343
2344static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2345{
2346 return 1;
2347}
2348
Takashi Iwai18478e82012-03-09 17:51:10 +01002349/* guess the value corresponding to 0dB */
2350static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2351{
2352 int _tlv[4];
2353 const int *tlv = NULL;
2354 int val = -1;
2355
2356 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2357 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2358 mm_segment_t fs = get_fs();
2359 set_fs(get_ds());
2360 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2361 tlv = _tlv;
2362 set_fs(fs);
2363 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2364 tlv = kctl->tlv.p;
2365 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2366 val = -tlv[2] / tlv[3];
2367 return val;
2368}
2369
2370/* call kctl->put with the given value(s) */
2371static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2372{
2373 struct snd_ctl_elem_value *ucontrol;
2374 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2375 if (!ucontrol)
2376 return -ENOMEM;
2377 ucontrol->value.integer.value[0] = val;
2378 ucontrol->value.integer.value[1] = val;
2379 kctl->put(kctl, ucontrol);
2380 kfree(ucontrol);
2381 return 0;
2382}
2383
2384/* initialize the slave volume with 0dB */
2385static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2386{
2387 int offset = get_kctl_0dB_offset(slave);
2388 if (offset > 0)
2389 put_kctl_with_value(slave, offset);
2390 return 0;
2391}
2392
2393/* unmute the slave */
2394static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2395{
2396 return put_kctl_with_value(slave, 1);
2397}
2398
Takashi Iwaid5191e52009-11-16 14:58:17 +01002399/**
2400 * snd_hda_add_vmaster - create a virtual master control and add slaves
2401 * @codec: HD-audio codec
2402 * @name: vmaster control name
2403 * @tlv: TLV data (optional)
2404 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002405 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002406 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002407 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002408 *
2409 * Create a virtual master control with the given name. The TLV data
2410 * must be either NULL or a valid data.
2411 *
2412 * @slaves is a NULL-terminated array of strings, each of which is a
2413 * slave control name. All controls with these names are assigned to
2414 * the new virtual master control.
2415 *
2416 * This function returns zero if successful or a negative error code.
2417 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002418int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002419 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002420 const char *suffix, bool init_slave_vol,
2421 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002422{
2423 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002424 int err;
2425
Takashi Iwai29e58532012-03-12 12:25:03 +01002426 if (ctl_ret)
2427 *ctl_ret = NULL;
2428
Takashi Iwai9322ca52012-02-03 14:28:01 +01002429 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002430 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002431 snd_printdd("No slave found for %s\n", name);
2432 return 0;
2433 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002434 kctl = snd_ctl_make_virtual_master(name, tlv);
2435 if (!kctl)
2436 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002437 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002438 if (err < 0)
2439 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002440
Takashi Iwai9322ca52012-02-03 14:28:01 +01002441 err = map_slaves(codec, slaves, suffix,
2442 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002443 if (err < 0)
2444 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002445
2446 /* init with master mute & zero volume */
2447 put_kctl_with_value(kctl, 0);
2448 if (init_slave_vol)
2449 map_slaves(codec, slaves, suffix,
2450 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2451
Takashi Iwai29e58532012-03-12 12:25:03 +01002452 if (ctl_ret)
2453 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002454 return 0;
2455}
Takashi Iwai18478e82012-03-09 17:51:10 +01002456EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002457
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002458/*
2459 * mute-LED control using vmaster
2460 */
2461static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_info *uinfo)
2463{
2464 static const char * const texts[] = {
2465 "Off", "On", "Follow Master"
2466 };
2467 unsigned int index;
2468
2469 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2470 uinfo->count = 1;
2471 uinfo->value.enumerated.items = 3;
2472 index = uinfo->value.enumerated.item;
2473 if (index >= 3)
2474 index = 2;
2475 strcpy(uinfo->value.enumerated.name, texts[index]);
2476 return 0;
2477}
2478
2479static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2481{
2482 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2483 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2484 return 0;
2485}
2486
2487static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2491 unsigned int old_mode = hook->mute_mode;
2492
2493 hook->mute_mode = ucontrol->value.enumerated.item[0];
2494 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2495 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2496 if (old_mode == hook->mute_mode)
2497 return 0;
2498 snd_hda_sync_vmaster_hook(hook);
2499 return 1;
2500}
2501
2502static struct snd_kcontrol_new vmaster_mute_mode = {
2503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2504 .name = "Mute-LED Mode",
2505 .info = vmaster_mute_mode_info,
2506 .get = vmaster_mute_mode_get,
2507 .put = vmaster_mute_mode_put,
2508};
2509
2510/*
2511 * Add a mute-LED hook with the given vmaster switch kctl
2512 * "Mute-LED Mode" control is automatically created and associated with
2513 * the given hook.
2514 */
2515int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002516 struct hda_vmaster_mute_hook *hook,
2517 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002518{
2519 struct snd_kcontrol *kctl;
2520
2521 if (!hook->hook || !hook->sw_kctl)
2522 return 0;
2523 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2524 hook->codec = codec;
2525 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002526 if (!expose_enum_ctl)
2527 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002528 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2529 if (!kctl)
2530 return -ENOMEM;
2531 return snd_hda_ctl_add(codec, 0, kctl);
2532}
2533EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2534
2535/*
2536 * Call the hook with the current value for synchronization
2537 * Should be called in init callback
2538 */
2539void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2540{
2541 if (!hook->hook || !hook->codec)
2542 return;
2543 switch (hook->mute_mode) {
2544 case HDA_VMUTE_FOLLOW_MASTER:
2545 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2546 break;
2547 default:
2548 hook->hook(hook->codec, hook->mute_mode);
2549 break;
2550 }
2551}
2552EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2553
2554
Takashi Iwaid5191e52009-11-16 14:58:17 +01002555/**
2556 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2557 *
2558 * The control element is supposed to have the private_value field
2559 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2560 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002561int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 int chs = get_amp_channels(kcontrol);
2565
2566 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2567 uinfo->count = chs == 3 ? 2 : 1;
2568 uinfo->value.integer.min = 0;
2569 uinfo->value.integer.max = 1;
2570 return 0;
2571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002572EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Takashi Iwaid5191e52009-11-16 14:58:17 +01002574/**
2575 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2576 *
2577 * The control element is supposed to have the private_value field
2578 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2579 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002580int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 hda_nid_t nid = get_amp_nid(kcontrol);
2585 int chs = get_amp_channels(kcontrol);
2586 int dir = get_amp_direction(kcontrol);
2587 int idx = get_amp_index(kcontrol);
2588 long *valp = ucontrol->value.integer.value;
2589
2590 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002591 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002592 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002594 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002595 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002598EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Takashi Iwaid5191e52009-11-16 14:58:17 +01002600/**
2601 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2602 *
2603 * The control element is supposed to have the private_value field
2604 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2605 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = get_amp_nid(kcontrol);
2611 int chs = get_amp_channels(kcontrol);
2612 int dir = get_amp_direction(kcontrol);
2613 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 long *valp = ucontrol->value.integer.value;
2615 int change = 0;
2616
Takashi Iwaicb53c622007-08-10 17:21:45 +02002617 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002618 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002619 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002620 HDA_AMP_MUTE,
2621 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002622 valp++;
2623 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002624 if (chs & 2)
2625 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002626 HDA_AMP_MUTE,
2627 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002628 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002629 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return change;
2631}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002632EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Takashi Iwai67d634c2009-11-16 15:35:59 +01002634#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002635/**
2636 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2637 *
2638 * This function calls snd_hda_enable_beep_device(), which behaves differently
2639 * depending on beep_mode option.
2640 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002641int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
2644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2645 long *valp = ucontrol->value.integer.value;
2646
2647 snd_hda_enable_beep_device(codec, *valp);
2648 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2649}
2650EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002651#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
Takashi Iwai985be542005-11-02 18:26:49 +01002654 * bound volume controls
2655 *
2656 * bind multiple volumes (# indices, from 0)
2657 */
2658
2659#define AMP_VAL_IDX_SHIFT 19
2660#define AMP_VAL_IDX_MASK (0x0f<<19)
2661
Takashi Iwaid5191e52009-11-16 14:58:17 +01002662/**
2663 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2664 *
2665 * The control element is supposed to have the private_value field
2666 * set up via HDA_BIND_MUTE*() macros.
2667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002668int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002670{
2671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 unsigned long pval;
2673 int err;
2674
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002675 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002676 pval = kcontrol->private_value;
2677 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2678 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2679 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002680 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002681 return err;
2682}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002683EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002684
Takashi Iwaid5191e52009-11-16 14:58:17 +01002685/**
2686 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2687 *
2688 * The control element is supposed to have the private_value field
2689 * set up via HDA_BIND_MUTE*() macros.
2690 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002693{
2694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2695 unsigned long pval;
2696 int i, indices, err = 0, change = 0;
2697
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002698 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002699 pval = kcontrol->private_value;
2700 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2701 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002702 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2703 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002704 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2705 if (err < 0)
2706 break;
2707 change |= err;
2708 }
2709 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002710 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002711 return err < 0 ? err : change;
2712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002713EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002714
Takashi Iwaid5191e52009-11-16 14:58:17 +01002715/**
2716 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2717 *
2718 * The control element is supposed to have the private_value field
2719 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002720 */
2721int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_info *uinfo)
2723{
2724 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2725 struct hda_bind_ctls *c;
2726 int err;
2727
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002728 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002729 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002730 kcontrol->private_value = *c->values;
2731 err = c->ops->info(kcontrol, uinfo);
2732 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002733 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002734 return err;
2735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002736EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002737
Takashi Iwaid5191e52009-11-16 14:58:17 +01002738/**
2739 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2740 *
2741 * The control element is supposed to have the private_value field
2742 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2743 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002744int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
2747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 struct hda_bind_ctls *c;
2749 int err;
2750
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002751 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002752 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002753 kcontrol->private_value = *c->values;
2754 err = c->ops->get(kcontrol, ucontrol);
2755 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002756 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002757 return err;
2758}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002759EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002760
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761/**
2762 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2763 *
2764 * The control element is supposed to have the private_value field
2765 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2766 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002767int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct hda_bind_ctls *c;
2772 unsigned long *vals;
2773 int err = 0, change = 0;
2774
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002776 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 for (vals = c->values; *vals; vals++) {
2778 kcontrol->private_value = *vals;
2779 err = c->ops->put(kcontrol, ucontrol);
2780 if (err < 0)
2781 break;
2782 change |= err;
2783 }
2784 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002785 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002786 return err < 0 ? err : change;
2787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002788EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002789
Takashi Iwaid5191e52009-11-16 14:58:17 +01002790/**
2791 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2792 *
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_BIND_VOL() macro.
2795 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002796int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2797 unsigned int size, unsigned int __user *tlv)
2798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 struct hda_bind_ctls *c;
2801 int err;
2802
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002804 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002805 kcontrol->private_value = *c->values;
2806 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2807 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002808 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809 return err;
2810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002812
2813struct hda_ctl_ops snd_hda_bind_vol = {
2814 .info = snd_hda_mixer_amp_volume_info,
2815 .get = snd_hda_mixer_amp_volume_get,
2816 .put = snd_hda_mixer_amp_volume_put,
2817 .tlv = snd_hda_mixer_amp_tlv
2818};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002820
2821struct hda_ctl_ops snd_hda_bind_sw = {
2822 .info = snd_hda_mixer_amp_switch_info,
2823 .get = snd_hda_mixer_amp_switch_get,
2824 .put = snd_hda_mixer_amp_switch_put,
2825 .tlv = snd_hda_mixer_amp_tlv
2826};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002828
2829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * SPDIF out controls
2831 */
2832
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2837 uinfo->count = 1;
2838 return 0;
2839}
2840
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2845 IEC958_AES0_NONAUDIO |
2846 IEC958_AES0_CON_EMPHASIS_5015 |
2847 IEC958_AES0_CON_NOT_COPYRIGHT;
2848 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2849 IEC958_AES1_CON_ORIGINAL;
2850 return 0;
2851}
2852
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2857 IEC958_AES0_NONAUDIO |
2858 IEC958_AES0_PRO_EMPHASIS_5015;
2859 return 0;
2860}
2861
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002866 int idx = kcontrol->private_value;
2867 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Stephen Warren7c9359762011-06-01 11:14:17 -06002869 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2870 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2871 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2872 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 return 0;
2875}
2876
2877/* convert from SPDIF status bits to HDA SPDIF bits
2878 * bit 0 (DigEn) is always set zero (to be filled later)
2879 */
2880static unsigned short convert_from_spdif_status(unsigned int sbits)
2881{
2882 unsigned short val = 0;
2883
2884 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002885 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002887 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002889 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2890 IEC958_AES0_PRO_EMPHASIS_5015)
2891 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002893 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2894 IEC958_AES0_CON_EMPHASIS_5015)
2895 val |= AC_DIG1_EMPHASIS;
2896 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2897 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002899 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2901 }
2902 return val;
2903}
2904
2905/* convert to SPDIF status bits from HDA SPDIF bits
2906 */
2907static unsigned int convert_to_spdif_status(unsigned short val)
2908{
2909 unsigned int sbits = 0;
2910
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 sbits |= IEC958_AES0_PROFESSIONAL;
2915 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2918 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002923 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2925 sbits |= val & (0x7f << 8);
2926 }
2927 return sbits;
2928}
2929
Takashi Iwai2f728532008-09-25 16:32:41 +02002930/* set digital convert verbs both for the given NID and its slaves */
2931static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2932 int verb, int val)
2933{
Takashi Iwaidda14412011-05-02 11:29:30 +02002934 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002935
Takashi Iwai9e976972008-11-25 08:17:20 +01002936 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002937 d = codec->slave_dig_outs;
2938 if (!d)
2939 return;
2940 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002941 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002942}
2943
2944static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2945 int dig1, int dig2)
2946{
2947 if (dig1 != -1)
2948 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2949 if (dig2 != -1)
2950 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2951}
2952
Takashi Iwai0ba21762007-04-16 11:29:14 +02002953static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2954 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002957 int idx = kcontrol->private_value;
2958 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2959 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 unsigned short val;
2961 int change;
2962
Ingo Molnar62932df2006-01-16 16:34:20 +01002963 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002964 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2966 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2967 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002968 val = convert_from_spdif_status(spdif->status);
2969 val |= spdif->ctls & 1;
2970 change = spdif->ctls != val;
2971 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002972 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002973 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002974 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return change;
2976}
2977
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002978#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Takashi Iwai0ba21762007-04-16 11:29:14 +02002980static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002984 int idx = kcontrol->private_value;
2985 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Stephen Warren7c9359762011-06-01 11:14:17 -06002987 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return 0;
2989}
2990
Stephen Warren74b654c2011-06-01 11:14:18 -06002991static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2992 int dig1, int dig2)
2993{
2994 set_dig_out_convert(codec, nid, dig1, dig2);
2995 /* unmute amp switch (if any) */
2996 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2997 (dig1 & AC_DIG1_ENABLE))
2998 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2999 HDA_AMP_MUTE, 0);
3000}
3001
Takashi Iwai0ba21762007-04-16 11:29:14 +02003002static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003006 int idx = kcontrol->private_value;
3007 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3008 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 unsigned short val;
3010 int change;
3011
Ingo Molnar62932df2006-01-16 16:34:20 +01003012 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06003013 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003016 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003017 spdif->ctls = val;
3018 if (change && nid != (u16)-1)
3019 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003020 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return change;
3022}
3023
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003024static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 {
3026 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3027 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003028 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 .info = snd_hda_spdif_mask_info,
3030 .get = snd_hda_spdif_cmask_get,
3031 },
3032 {
3033 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3034 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003035 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .info = snd_hda_spdif_mask_info,
3037 .get = snd_hda_spdif_pmask_get,
3038 },
3039 {
3040 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003041 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .info = snd_hda_spdif_mask_info,
3043 .get = snd_hda_spdif_default_get,
3044 .put = snd_hda_spdif_default_put,
3045 },
3046 {
3047 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003048 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 .info = snd_hda_spdif_out_switch_info,
3050 .get = snd_hda_spdif_out_switch_get,
3051 .put = snd_hda_spdif_out_switch_put,
3052 },
3053 { } /* end */
3054};
3055
3056/**
3057 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3058 * @codec: the HDA codec
3059 * @nid: audio out widget NID
3060 *
3061 * Creates controls related with the SPDIF output.
3062 * Called from each patch supporting the SPDIF out.
3063 *
3064 * Returns 0 if successful, or a negative error code.
3065 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003066int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3067 hda_nid_t associated_nid,
3068 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003071 struct snd_kcontrol *kctl;
3072 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003073 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003074 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Takashi Iwai1afe2062010-12-23 10:17:52 +01003076 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3077 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003078 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3079 return -EBUSY;
3080 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003081 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3083 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003084 if (!kctl)
3085 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003086 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003087 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003088 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return err;
3091 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003092 spdif->nid = cvt_nid;
3093 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003094 AC_VERB_GET_DIGI_CONVERT_1, 0);
3095 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return 0;
3097}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003098EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Stephen Warren7c9359762011-06-01 11:14:17 -06003100struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3101 hda_nid_t nid)
3102{
3103 int i;
3104 for (i = 0; i < codec->spdif_out.used; i++) {
3105 struct hda_spdif_out *spdif =
3106 snd_array_elem(&codec->spdif_out, i);
3107 if (spdif->nid == nid)
3108 return spdif;
3109 }
3110 return NULL;
3111}
3112EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3113
Stephen Warren74b654c2011-06-01 11:14:18 -06003114void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3115{
3116 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3117
3118 mutex_lock(&codec->spdif_mutex);
3119 spdif->nid = (u16)-1;
3120 mutex_unlock(&codec->spdif_mutex);
3121}
3122EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3123
3124void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3125{
3126 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3127 unsigned short val;
3128
3129 mutex_lock(&codec->spdif_mutex);
3130 if (spdif->nid != nid) {
3131 spdif->nid = nid;
3132 val = spdif->ctls;
3133 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3134 }
3135 mutex_unlock(&codec->spdif_mutex);
3136}
3137EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003140 * SPDIF sharing with analog output
3141 */
3142static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
3145 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3146 ucontrol->value.integer.value[0] = mout->share_spdif;
3147 return 0;
3148}
3149
3150static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
3152{
3153 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3154 mout->share_spdif = !!ucontrol->value.integer.value[0];
3155 return 0;
3156}
3157
3158static struct snd_kcontrol_new spdif_share_sw = {
3159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3160 .name = "IEC958 Default PCM Playback Switch",
3161 .info = snd_ctl_boolean_mono_info,
3162 .get = spdif_share_sw_get,
3163 .put = spdif_share_sw_put,
3164};
3165
Takashi Iwaid5191e52009-11-16 14:58:17 +01003166/**
3167 * snd_hda_create_spdif_share_sw - create Default PCM switch
3168 * @codec: the HDA codec
3169 * @mout: multi-out instance
3170 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003171int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3172 struct hda_multi_out *mout)
3173{
3174 if (!mout->dig_out_nid)
3175 return 0;
3176 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003177 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3178 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003180EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003181
3182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 * SPDIF input
3184 */
3185
3186#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3187
Takashi Iwai0ba21762007-04-16 11:29:14 +02003188static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192
3193 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3194 return 0;
3195}
3196
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3201 hda_nid_t nid = kcontrol->private_value;
3202 unsigned int val = !!ucontrol->value.integer.value[0];
3203 int change;
3204
Ingo Molnar62932df2006-01-16 16:34:20 +01003205 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003207 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003209 snd_hda_codec_write_cache(codec, nid, 0,
3210 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003212 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return change;
3214}
3215
Takashi Iwai0ba21762007-04-16 11:29:14 +02003216static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3220 hda_nid_t nid = kcontrol->private_value;
3221 unsigned short val;
3222 unsigned int sbits;
3223
Andrew Paprocki3982d172007-12-19 12:13:44 +01003224 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 sbits = convert_to_spdif_status(val);
3226 ucontrol->value.iec958.status[0] = sbits;
3227 ucontrol->value.iec958.status[1] = sbits >> 8;
3228 ucontrol->value.iec958.status[2] = sbits >> 16;
3229 ucontrol->value.iec958.status[3] = sbits >> 24;
3230 return 0;
3231}
3232
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003233static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 {
3235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003236 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 .info = snd_hda_spdif_in_switch_info,
3238 .get = snd_hda_spdif_in_switch_get,
3239 .put = snd_hda_spdif_in_switch_put,
3240 },
3241 {
3242 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003244 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .info = snd_hda_spdif_mask_info,
3246 .get = snd_hda_spdif_in_status_get,
3247 },
3248 { } /* end */
3249};
3250
3251/**
3252 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3253 * @codec: the HDA codec
3254 * @nid: audio in widget NID
3255 *
3256 * Creates controls related with the SPDIF input.
3257 * Called from each patch supporting the SPDIF in.
3258 *
3259 * Returns 0 if successful, or a negative error code.
3260 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003261int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003264 struct snd_kcontrol *kctl;
3265 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003266 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Takashi Iwai1afe2062010-12-23 10:17:52 +01003268 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3269 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003270 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3271 return -EBUSY;
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3274 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003275 if (!kctl)
3276 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003278 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003279 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return err;
3281 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003283 snd_hda_codec_read(codec, nid, 0,
3284 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003285 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return 0;
3287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003288EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Takashi Iwai2a439522011-07-26 09:52:50 +02003290#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003291/*
3292 * command cache
3293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003295/* build a 32bit cache key with the widget id and the command parameter */
3296#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3297#define get_cmd_cache_nid(key) ((key) & 0xff)
3298#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3299
3300/**
3301 * snd_hda_codec_write_cache - send a single command with caching
3302 * @codec: the HDA codec
3303 * @nid: NID to send the command
3304 * @direct: direct flag
3305 * @verb: the verb to send
3306 * @parm: the parameter for the verb
3307 *
3308 * Send a single command without waiting for response.
3309 *
3310 * Returns 0 if successful, or a negative error code.
3311 */
3312int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3313 int direct, unsigned int verb, unsigned int parm)
3314{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003315 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3316 struct hda_cache_head *c;
3317 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003318
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003319 if (err < 0)
3320 return err;
3321 /* parm may contain the verb stuff for get/set amp */
3322 verb = verb | (parm >> 8);
3323 parm &= 0xff;
3324 key = build_cmd_cache_key(nid, verb);
3325 mutex_lock(&codec->bus->cmd_mutex);
3326 c = get_alloc_hash(&codec->cmd_cache, key);
3327 if (c)
3328 c->val = parm;
3329 mutex_unlock(&codec->bus->cmd_mutex);
3330 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003331}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003332EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003333
Takashi Iwaid5191e52009-11-16 14:58:17 +01003334/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003335 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3336 * @codec: the HDA codec
3337 * @nid: NID to send the command
3338 * @direct: direct flag
3339 * @verb: the verb to send
3340 * @parm: the parameter for the verb
3341 *
3342 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3343 * command if the parameter is already identical with the cached value.
3344 * If not, it sends the command and refreshes the cache.
3345 *
3346 * Returns 0 if successful, or a negative error code.
3347 */
3348int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3349 int direct, unsigned int verb, unsigned int parm)
3350{
3351 struct hda_cache_head *c;
3352 u32 key;
3353
3354 /* parm may contain the verb stuff for get/set amp */
3355 verb = verb | (parm >> 8);
3356 parm &= 0xff;
3357 key = build_cmd_cache_key(nid, verb);
3358 mutex_lock(&codec->bus->cmd_mutex);
3359 c = get_hash(&codec->cmd_cache, key);
3360 if (c && c->val == parm) {
3361 mutex_unlock(&codec->bus->cmd_mutex);
3362 return 0;
3363 }
3364 mutex_unlock(&codec->bus->cmd_mutex);
3365 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3366}
3367EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3368
3369/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003370 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3371 * @codec: HD-audio codec
3372 *
3373 * Execute all verbs recorded in the command caches to resume.
3374 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003375void snd_hda_codec_resume_cache(struct hda_codec *codec)
3376{
Takashi Iwai603c4012008-07-30 15:01:44 +02003377 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003378 int i;
3379
Takashi Iwai603c4012008-07-30 15:01:44 +02003380 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003381 u32 key = buffer->key;
3382 if (!key)
3383 continue;
3384 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3385 get_cmd_cache_cmd(key), buffer->val);
3386 }
3387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003388EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003389
3390/**
3391 * snd_hda_sequence_write_cache - sequence writes with caching
3392 * @codec: the HDA codec
3393 * @seq: VERB array to send
3394 *
3395 * Send the commands sequentially from the given array.
3396 * Thte commands are recorded on cache for power-save and resume.
3397 * The array must be terminated with NID=0.
3398 */
3399void snd_hda_sequence_write_cache(struct hda_codec *codec,
3400 const struct hda_verb *seq)
3401{
3402 for (; seq->nid; seq++)
3403 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3404 seq->param);
3405}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003406EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003407#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003408
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003409void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3410 unsigned int power_state,
3411 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003412{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003413 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003414 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003415
Takashi Iwaicb53c622007-08-10 17:21:45 +02003416 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003417 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003418 if (!(wcaps & AC_WCAP_POWER))
3419 continue;
3420 /* don't power down the widget if it controls eapd and
3421 * EAPD_BTLENABLE is set.
3422 */
3423 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3424 get_wcaps_type(wcaps) == AC_WID_PIN &&
3425 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3426 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003427 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003428 if (eapd & 0x02)
3429 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003430 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003431 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3432 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003433 }
3434
Takashi Iwaicb53c622007-08-10 17:21:45 +02003435 if (power_state == AC_PWRST_D0) {
3436 unsigned long end_time;
3437 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003438 /* wait until the codec reachs to D0 */
3439 end_time = jiffies + msecs_to_jiffies(500);
3440 do {
3441 state = snd_hda_codec_read(codec, fg, 0,
3442 AC_VERB_GET_POWER_STATE, 0);
3443 if (state == power_state)
3444 break;
3445 msleep(1);
3446 } while (time_after_eq(end_time, jiffies));
3447 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003448}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003449EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3450
3451/*
3452 * set power state of the codec
3453 */
3454static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3455 unsigned int power_state)
3456{
3457 if (codec->patch_ops.set_power_state) {
3458 codec->patch_ops.set_power_state(codec, fg, power_state);
3459 return;
3460 }
3461
3462 /* this delay seems necessary to avoid click noise at power-down */
3463 if (power_state == AC_PWRST_D3)
3464 msleep(100);
3465 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3466 power_state);
3467 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3468}
Takashi Iwai54d17402005-11-21 16:33:22 +01003469
Takashi Iwai11aeff02008-07-30 15:01:46 +02003470#ifdef CONFIG_SND_HDA_HWDEP
3471/* execute additional init verbs */
3472static void hda_exec_init_verbs(struct hda_codec *codec)
3473{
3474 if (codec->init_verbs.list)
3475 snd_hda_sequence_write(codec, codec->init_verbs.list);
3476}
3477#else
3478static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3479#endif
3480
Takashi Iwai2a439522011-07-26 09:52:50 +02003481#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003482/*
3483 * call suspend and power-down; used both from PM and power-save
3484 */
3485static void hda_call_codec_suspend(struct hda_codec *codec)
3486{
3487 if (codec->patch_ops.suspend)
3488 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003489 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003490 hda_set_power_state(codec,
3491 codec->afg ? codec->afg : codec->mfg,
3492 AC_PWRST_D3);
3493#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003494 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003495 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003496 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003497 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003498 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003499#endif
3500}
3501
3502/*
3503 * kick up codec; used both from PM and power-save
3504 */
3505static void hda_call_codec_resume(struct hda_codec *codec)
3506{
Takashi Iwai7f308302012-05-08 16:52:23 +02003507 /* set as if powered on for avoiding re-entering the resume
3508 * in the resume / power-save sequence
3509 */
3510 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003511 hda_set_power_state(codec,
3512 codec->afg ? codec->afg : codec->mfg,
3513 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003514 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003515 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003516 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003517 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003518 if (codec->patch_ops.resume)
3519 codec->patch_ops.resume(codec);
3520 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003521 if (codec->patch_ops.init)
3522 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003523 snd_hda_codec_resume_amp(codec);
3524 snd_hda_codec_resume_cache(codec);
3525 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003526 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003527}
Takashi Iwai2a439522011-07-26 09:52:50 +02003528#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003529
Takashi Iwai54d17402005-11-21 16:33:22 +01003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531/**
3532 * snd_hda_build_controls - build mixer controls
3533 * @bus: the BUS
3534 *
3535 * Creates mixer controls for each codec included in the bus.
3536 *
3537 * Returns 0 if successful, otherwise a negative error code.
3538 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003539int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003541 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Takashi Iwai0ba21762007-04-16 11:29:14 +02003543 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003544 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003545 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003546 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003547 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003548 err = snd_hda_codec_reset(codec);
3549 if (err < 0) {
3550 printk(KERN_ERR
3551 "hda_codec: cannot revert codec\n");
3552 return err;
3553 }
3554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003556 return 0;
3557}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003558EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003560int snd_hda_codec_build_controls(struct hda_codec *codec)
3561{
3562 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003563 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003564 /* continue to initialize... */
3565 if (codec->patch_ops.init)
3566 err = codec->patch_ops.init(codec);
3567 if (!err && codec->patch_ops.build_controls)
3568 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003569 if (err < 0)
3570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return 0;
3572}
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574/*
3575 * stream formats
3576 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003577struct hda_rate_tbl {
3578 unsigned int hz;
3579 unsigned int alsa_bits;
3580 unsigned int hda_fmt;
3581};
3582
Takashi Iwai92f10b32010-08-03 14:21:00 +02003583/* rate = base * mult / div */
3584#define HDA_RATE(base, mult, div) \
3585 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3586 (((div) - 1) << AC_FMT_DIV_SHIFT))
3587
Takashi Iwaibefdf312005-08-22 13:57:55 +02003588static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003590
3591 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003592 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3593 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3594 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3595 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3596 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3597 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3598 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3599 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3600 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3601 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3602 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003603#define AC_PAR_PCM_RATE_BITS 11
3604 /* up to bits 10, 384kHZ isn't supported properly */
3605
3606 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003607 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003608
Takashi Iwaibefdf312005-08-22 13:57:55 +02003609 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610};
3611
3612/**
3613 * snd_hda_calc_stream_format - calculate format bitset
3614 * @rate: the sample rate
3615 * @channels: the number of channels
3616 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3617 * @maxbps: the max. bps
3618 *
3619 * Calculate the format bitset from the given rate, channels and th PCM format.
3620 *
3621 * Return zero if invalid.
3622 */
3623unsigned int snd_hda_calc_stream_format(unsigned int rate,
3624 unsigned int channels,
3625 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003626 unsigned int maxbps,
3627 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 int i;
3630 unsigned int val = 0;
3631
Takashi Iwaibefdf312005-08-22 13:57:55 +02003632 for (i = 0; rate_bits[i].hz; i++)
3633 if (rate_bits[i].hz == rate) {
3634 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 break;
3636 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003637 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 snd_printdd("invalid rate %d\n", rate);
3639 return 0;
3640 }
3641
3642 if (channels == 0 || channels > 8) {
3643 snd_printdd("invalid channels %d\n", channels);
3644 return 0;
3645 }
3646 val |= channels - 1;
3647
3648 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003649 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003650 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003651 break;
3652 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003653 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 case 20:
3656 case 24:
3657 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003658 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003659 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003661 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003663 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 break;
3665 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003666 snd_printdd("invalid format width %d\n",
3667 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return 0;
3669 }
3670
Anssi Hannula32c168c2010-08-03 13:28:57 +03003671 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003672 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return val;
3675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003676EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003678static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3679{
3680 unsigned int val = 0;
3681 if (nid != codec->afg &&
3682 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3683 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3684 if (!val || val == -1)
3685 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3686 if (!val || val == -1)
3687 return 0;
3688 return val;
3689}
3690
3691static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3692{
3693 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3694 get_pcm_param);
3695}
3696
3697static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3698{
3699 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3700 if (!streams || streams == -1)
3701 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3702 if (!streams || streams == -1)
3703 return 0;
3704 return streams;
3705}
3706
3707static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3708{
3709 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3710 get_stream_param);
3711}
3712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713/**
3714 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3715 * @codec: the HDA codec
3716 * @nid: NID to query
3717 * @ratesp: the pointer to store the detected rate bitflags
3718 * @formatsp: the pointer to store the detected formats
3719 * @bpsp: the pointer to store the detected format widths
3720 *
3721 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3722 * or @bsps argument is ignored.
3723 *
3724 * Returns 0 if successful, otherwise a negative error code.
3725 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003726int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3728{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003729 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003731 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003732 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 if (ratesp) {
3735 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003736 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003738 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003740 if (rates == 0) {
3741 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3742 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3743 nid, val,
3744 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3745 return -EIO;
3746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 *ratesp = rates;
3748 }
3749
3750 if (formatsp || bpsp) {
3751 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003752 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003754 streams = query_stream_param(codec, nid);
3755 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 bps = 0;
3759 if (streams & AC_SUPFMT_PCM) {
3760 if (val & AC_SUPPCM_BITS_8) {
3761 formats |= SNDRV_PCM_FMTBIT_U8;
3762 bps = 8;
3763 }
3764 if (val & AC_SUPPCM_BITS_16) {
3765 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3766 bps = 16;
3767 }
3768 if (wcaps & AC_WCAP_DIGITAL) {
3769 if (val & AC_SUPPCM_BITS_32)
3770 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3771 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3772 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3773 if (val & AC_SUPPCM_BITS_24)
3774 bps = 24;
3775 else if (val & AC_SUPPCM_BITS_20)
3776 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003777 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3778 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3780 if (val & AC_SUPPCM_BITS_32)
3781 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 else if (val & AC_SUPPCM_BITS_24)
3783 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003784 else if (val & AC_SUPPCM_BITS_20)
3785 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
3787 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003788 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003790 if (!bps)
3791 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003792 }
3793 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003794 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 /* temporary hack: we have still no proper support
3796 * for the direct AC3 stream...
3797 */
3798 formats |= SNDRV_PCM_FMTBIT_U8;
3799 bps = 8;
3800 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003801 if (formats == 0) {
3802 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3803 "(nid=0x%x, val=0x%x, ovrd=%i, "
3804 "streams=0x%x)\n",
3805 nid, val,
3806 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3807 streams);
3808 return -EIO;
3809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 if (formatsp)
3811 *formatsp = formats;
3812 if (bpsp)
3813 *bpsp = bps;
3814 }
3815
3816 return 0;
3817}
Stephen Warren384a48d2011-06-01 11:14:21 -06003818EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003821 * snd_hda_is_supported_format - Check the validity of the format
3822 * @codec: HD-audio codec
3823 * @nid: NID to check
3824 * @format: the HD-audio format value to check
3825 *
3826 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 *
3828 * Returns 1 if supported, 0 if not.
3829 */
3830int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3831 unsigned int format)
3832{
3833 int i;
3834 unsigned int val = 0, rate, stream;
3835
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003836 val = query_pcm_param(codec, nid);
3837 if (!val)
3838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003841 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003842 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 if (val & (1 << i))
3844 break;
3845 return 0;
3846 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003847 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return 0;
3849
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003850 stream = query_stream_param(codec, nid);
3851 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 return 0;
3853
3854 if (stream & AC_SUPFMT_PCM) {
3855 switch (format & 0xf0) {
3856 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003857 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return 0;
3859 break;
3860 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003861 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return 0;
3863 break;
3864 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003865 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 0;
3867 break;
3868 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003869 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return 0;
3871 break;
3872 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003873 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return 0;
3875 break;
3876 default:
3877 return 0;
3878 }
3879 } else {
3880 /* FIXME: check for float32 and AC3? */
3881 }
3882
3883 return 1;
3884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003885EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887/*
3888 * PCM stuff
3889 */
3890static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3891 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003892 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
3894 return 0;
3895}
3896
3897static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3898 struct hda_codec *codec,
3899 unsigned int stream_tag,
3900 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003901 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
3903 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3904 return 0;
3905}
3906
3907static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3908 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003909 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Takashi Iwai888afa12008-03-18 09:57:50 +01003911 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return 0;
3913}
3914
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003915static int set_pcm_default_values(struct hda_codec *codec,
3916 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003918 int err;
3919
Takashi Iwai0ba21762007-04-16 11:29:14 +02003920 /* query support PCM information from the given NID */
3921 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003922 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003923 info->rates ? NULL : &info->rates,
3924 info->formats ? NULL : &info->formats,
3925 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003926 if (err < 0)
3927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
3929 if (info->ops.open == NULL)
3930 info->ops.open = hda_pcm_default_open_close;
3931 if (info->ops.close == NULL)
3932 info->ops.close = hda_pcm_default_open_close;
3933 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003934 if (snd_BUG_ON(!info->nid))
3935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 info->ops.prepare = hda_pcm_default_prepare;
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003939 if (snd_BUG_ON(!info->nid))
3940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 info->ops.cleanup = hda_pcm_default_cleanup;
3942 }
3943 return 0;
3944}
3945
Takashi Iwaieb541332010-08-06 13:48:11 +02003946/*
3947 * codec prepare/cleanup entries
3948 */
3949int snd_hda_codec_prepare(struct hda_codec *codec,
3950 struct hda_pcm_stream *hinfo,
3951 unsigned int stream,
3952 unsigned int format,
3953 struct snd_pcm_substream *substream)
3954{
3955 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003956 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003957 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3958 if (ret >= 0)
3959 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003960 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003961 return ret;
3962}
3963EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3964
3965void snd_hda_codec_cleanup(struct hda_codec *codec,
3966 struct hda_pcm_stream *hinfo,
3967 struct snd_pcm_substream *substream)
3968{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003969 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003970 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003971 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003972}
3973EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3974
Takashi Iwaid5191e52009-11-16 14:58:17 +01003975/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003976const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3977 "Audio", "SPDIF", "HDMI", "Modem"
3978};
3979
Takashi Iwai176d5332008-07-30 15:01:44 +02003980/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003981 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003982 *
3983 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003984 */
3985static int get_empty_pcm_device(struct hda_bus *bus, int type)
3986{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003987 /* audio device indices; not linear to keep compatibility */
3988 static int audio_idx[HDA_PCM_NTYPES][5] = {
3989 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3990 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003991 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003992 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003993 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003994 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003995
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003996 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003997 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3998 return -EINVAL;
3999 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004000
4001 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4002 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4003 return audio_idx[type][i];
4004
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004005 /* non-fixed slots starting from 10 */
4006 for (i = 10; i < 32; i++) {
4007 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4008 return i;
4009 }
4010
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004011 snd_printk(KERN_WARNING "Too many %s devices\n",
4012 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004013 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004014}
4015
4016/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004017 * attach a new PCM stream
4018 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004019static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004020{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004021 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004022 struct hda_pcm_stream *info;
4023 int stream, err;
4024
Takashi Iwaib91f0802008-11-04 08:43:08 +01004025 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004026 return -EINVAL;
4027 for (stream = 0; stream < 2; stream++) {
4028 info = &pcm->stream[stream];
4029 if (info->substreams) {
4030 err = set_pcm_default_values(codec, info);
4031 if (err < 0)
4032 return err;
4033 }
4034 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004035 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004036}
4037
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004038/* assign all PCMs of the given codec */
4039int snd_hda_codec_build_pcms(struct hda_codec *codec)
4040{
4041 unsigned int pcm;
4042 int err;
4043
4044 if (!codec->num_pcms) {
4045 if (!codec->patch_ops.build_pcms)
4046 return 0;
4047 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004048 if (err < 0) {
4049 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004050 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004051 err = snd_hda_codec_reset(codec);
4052 if (err < 0) {
4053 printk(KERN_ERR
4054 "hda_codec: cannot revert codec\n");
4055 return err;
4056 }
4057 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004058 }
4059 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4060 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4061 int dev;
4062
4063 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004064 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004065
4066 if (!cpcm->pcm) {
4067 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4068 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004069 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004070 cpcm->device = dev;
4071 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004072 if (err < 0) {
4073 printk(KERN_ERR "hda_codec: cannot attach "
4074 "PCM stream %d for codec #%d\n",
4075 dev, codec->addr);
4076 continue; /* no fatal error */
4077 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004078 }
4079 }
4080 return 0;
4081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083/**
4084 * snd_hda_build_pcms - build PCM information
4085 * @bus: the BUS
4086 *
4087 * Create PCM information for each codec included in the bus.
4088 *
4089 * The build_pcms codec patch is requested to set up codec->num_pcms and
4090 * codec->pcm_info properly. The array is referred by the top-level driver
4091 * to create its PCM instances.
4092 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4093 * callback.
4094 *
4095 * At least, substreams, channels_min and channels_max must be filled for
4096 * each stream. substreams = 0 indicates that the stream doesn't exist.
4097 * When rates and/or formats are zero, the supported values are queried
4098 * from the given nid. The nid is used also by the default ops.prepare
4099 * and ops.cleanup callbacks.
4100 *
4101 * The driver needs to call ops.open in its open callback. Similarly,
4102 * ops.close is supposed to be called in the close callback.
4103 * ops.prepare should be called in the prepare or hw_params callback
4104 * with the proper parameters for set up.
4105 * ops.cleanup should be called in hw_free for clean up of streams.
4106 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004107 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004109int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004111 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Takashi Iwai0ba21762007-04-16 11:29:14 +02004113 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004114 int err = snd_hda_codec_build_pcms(codec);
4115 if (err < 0)
4116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 }
4118 return 0;
4119}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004120EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122/**
4123 * snd_hda_check_board_config - compare the current codec with the config table
4124 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004125 * @num_configs: number of config enums
4126 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 * @tbl: configuration table, terminated by null entries
4128 *
4129 * Compares the modelname or PCI subsystem id of the current codec with the
4130 * given configuration table. If a matching entry is found, returns its
4131 * config value (supposed to be 0 or positive).
4132 *
4133 * If no entries are matching, the function returns a negative value.
4134 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004135int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004136 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004137 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004139 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004140 int i;
4141 for (i = 0; i < num_configs; i++) {
4142 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004143 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004144 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4145 "selected\n", models[i]);
4146 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 }
4148 }
4149 }
4150
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004151 if (!codec->bus->pci || !tbl)
4152 return -1;
4153
4154 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4155 if (!tbl)
4156 return -1;
4157 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004158#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004159 char tmp[10];
4160 const char *model = NULL;
4161 if (models)
4162 model = models[tbl->value];
4163 if (!model) {
4164 sprintf(tmp, "#%d", tbl->value);
4165 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004167 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4168 "for config %x:%x (%s)\n",
4169 model, tbl->subvendor, tbl->subdevice,
4170 (tbl->name ? tbl->name : "Unknown device"));
4171#endif
4172 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
4174 return -1;
4175}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004176EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004179 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004180 subsystem ID with the
4181 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004182
4183 This is important for Gateway notebooks with SB450 HDA Audio
4184 where the vendor ID of the PCI device is:
4185 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4186 and the vendor/subvendor are found only at the codec.
4187
4188 * @codec: the HDA codec
4189 * @num_configs: number of config enums
4190 * @models: array of model name strings
4191 * @tbl: configuration table, terminated by null entries
4192 *
4193 * Compares the modelname or PCI subsystem id of the current codec with the
4194 * given configuration table. If a matching entry is found, returns its
4195 * config value (supposed to be 0 or positive).
4196 *
4197 * If no entries are matching, the function returns a negative value.
4198 */
4199int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004200 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004201 const struct snd_pci_quirk *tbl)
4202{
4203 const struct snd_pci_quirk *q;
4204
4205 /* Search for codec ID */
4206 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004207 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4208 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4209 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004210 break;
4211 }
4212
4213 if (!q->subvendor)
4214 return -1;
4215
4216 tbl = q;
4217
4218 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004219#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004220 char tmp[10];
4221 const char *model = NULL;
4222 if (models)
4223 model = models[tbl->value];
4224 if (!model) {
4225 sprintf(tmp, "#%d", tbl->value);
4226 model = tmp;
4227 }
4228 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4229 "for config %x:%x (%s)\n",
4230 model, tbl->subvendor, tbl->subdevice,
4231 (tbl->name ? tbl->name : "Unknown device"));
4232#endif
4233 return tbl->value;
4234 }
4235 return -1;
4236}
4237EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4238
4239/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 * snd_hda_add_new_ctls - create controls from the array
4241 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004242 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 *
4244 * This helper function creates and add new controls in the given array.
4245 * The array must be terminated with an empty entry as terminator.
4246 *
4247 * Returns 0 if successful, or a negative error code.
4248 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004249int snd_hda_add_new_ctls(struct hda_codec *codec,
4250 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004255 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004256 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004257 if (knew->iface == -1) /* skip this codec private value */
4258 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004259 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004260 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004261 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004262 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004263 if (addr > 0)
4264 kctl->id.device = addr;
4265 if (idx > 0)
4266 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004267 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004268 if (!err)
4269 break;
4270 /* try first with another device index corresponding to
4271 * the codec addr; if it still fails (or it's the
4272 * primary codec), then try another control index
4273 */
4274 if (!addr && codec->addr)
4275 addr = codec->addr;
4276 else if (!idx && !knew->index) {
4277 idx = find_empty_mixer_ctl_idx(codec,
4278 knew->name);
4279 if (idx <= 0)
4280 return err;
4281 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004282 return err;
4283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285 return 0;
4286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004287EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Takashi Iwaicb53c622007-08-10 17:21:45 +02004289#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004290static void hda_power_work(struct work_struct *work)
4291{
4292 struct hda_codec *codec =
4293 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004294 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004295
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004296 spin_lock(&codec->power_lock);
Maxim Levitsky2e492462007-09-03 15:26:57 +02004297 if (!codec->power_on || codec->power_count) {
4298 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004299 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004300 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004301 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004302
Takashi Iwaid66fee52011-08-02 15:39:31 +02004303 trace_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004304 spin_unlock(&codec->power_lock);
4305
Takashi Iwaicb53c622007-08-10 17:21:45 +02004306 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004307 if (bus->ops.pm_notify)
4308 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004309}
4310
4311static void hda_keep_power_on(struct hda_codec *codec)
4312{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004313 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004314 codec->power_count++;
4315 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004316 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004317 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004318}
4319
Takashi Iwaid5191e52009-11-16 14:58:17 +01004320/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004321void snd_hda_update_power_acct(struct hda_codec *codec)
4322{
4323 unsigned long delta = jiffies - codec->power_jiffies;
4324 if (codec->power_on)
4325 codec->power_on_acct += delta;
4326 else
4327 codec->power_off_acct += delta;
4328 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004329}
4330
Takashi Iwaid5191e52009-11-16 14:58:17 +01004331/**
4332 * snd_hda_power_up - Power-up the codec
4333 * @codec: HD-audio codec
4334 *
4335 * Increment the power-up counter and power up the hardware really when
4336 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004337 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004338void snd_hda_power_up(struct hda_codec *codec)
4339{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004340 struct hda_bus *bus = codec->bus;
4341
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004342 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004343 codec->power_count++;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004344 if (codec->power_on || codec->power_transition) {
4345 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004346 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004347 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004348
Takashi Iwaid66fee52011-08-02 15:39:31 +02004349 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004350 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004351 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004352 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004353 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004354 spin_unlock(&codec->power_lock);
4355
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004356 if (bus->ops.pm_notify)
4357 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004358 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004359
4360 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004361 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004362 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004363 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004364}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004365EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004366
4367#define power_save(codec) \
4368 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004369
Takashi Iwaid5191e52009-11-16 14:58:17 +01004370/**
4371 * snd_hda_power_down - Power-down the codec
4372 * @codec: HD-audio codec
4373 *
4374 * Decrement the power-up counter and schedules the power-off work if
4375 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004376 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004377void snd_hda_power_down(struct hda_codec *codec)
4378{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004379 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004380 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004381 if (!codec->power_on || codec->power_count || codec->power_transition) {
4382 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004383 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004384 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004385 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004386 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004387 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004388 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004389 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004390 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004391}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004392EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004393
Takashi Iwaid5191e52009-11-16 14:58:17 +01004394/**
4395 * snd_hda_check_amp_list_power - Check the amp list and update the power
4396 * @codec: HD-audio codec
4397 * @check: the object containing an AMP list and the status
4398 * @nid: NID to check / update
4399 *
4400 * Check whether the given NID is in the amp list. If it's in the list,
4401 * check the current AMP status, and update the the power-status according
4402 * to the mute status.
4403 *
4404 * This function is supposed to be set or called from the check_power_status
4405 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004406 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004407int snd_hda_check_amp_list_power(struct hda_codec *codec,
4408 struct hda_loopback_check *check,
4409 hda_nid_t nid)
4410{
Takashi Iwai031024e2011-05-02 11:29:30 +02004411 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004412 int ch, v;
4413
4414 if (!check->amplist)
4415 return 0;
4416 for (p = check->amplist; p->nid; p++) {
4417 if (p->nid == nid)
4418 break;
4419 }
4420 if (!p->nid)
4421 return 0; /* nothing changed */
4422
4423 for (p = check->amplist; p->nid; p++) {
4424 for (ch = 0; ch < 2; ch++) {
4425 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4426 p->idx);
4427 if (!(v & HDA_AMP_MUTE) && v > 0) {
4428 if (!check->power_on) {
4429 check->power_on = 1;
4430 snd_hda_power_up(codec);
4431 }
4432 return 1;
4433 }
4434 }
4435 }
4436 if (check->power_on) {
4437 check->power_on = 0;
4438 snd_hda_power_down(codec);
4439 }
4440 return 0;
4441}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004442EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004445/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004446 * Channel mode helper
4447 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004448
4449/**
4450 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4451 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004452int snd_hda_ch_mode_info(struct hda_codec *codec,
4453 struct snd_ctl_elem_info *uinfo,
4454 const struct hda_channel_mode *chmode,
4455 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004456{
4457 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4458 uinfo->count = 1;
4459 uinfo->value.enumerated.items = num_chmodes;
4460 if (uinfo->value.enumerated.item >= num_chmodes)
4461 uinfo->value.enumerated.item = num_chmodes - 1;
4462 sprintf(uinfo->value.enumerated.name, "%dch",
4463 chmode[uinfo->value.enumerated.item].channels);
4464 return 0;
4465}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004466EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004467
Takashi Iwaid5191e52009-11-16 14:58:17 +01004468/**
4469 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4470 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004471int snd_hda_ch_mode_get(struct hda_codec *codec,
4472 struct snd_ctl_elem_value *ucontrol,
4473 const struct hda_channel_mode *chmode,
4474 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004475 int max_channels)
4476{
4477 int i;
4478
4479 for (i = 0; i < num_chmodes; i++) {
4480 if (max_channels == chmode[i].channels) {
4481 ucontrol->value.enumerated.item[0] = i;
4482 break;
4483 }
4484 }
4485 return 0;
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004488
Takashi Iwaid5191e52009-11-16 14:58:17 +01004489/**
4490 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4491 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004492int snd_hda_ch_mode_put(struct hda_codec *codec,
4493 struct snd_ctl_elem_value *ucontrol,
4494 const struct hda_channel_mode *chmode,
4495 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004496 int *max_channelsp)
4497{
4498 unsigned int mode;
4499
4500 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004501 if (mode >= num_chmodes)
4502 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004503 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004504 return 0;
4505 /* change the current channel setting */
4506 *max_channelsp = chmode[mode].channels;
4507 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004508 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004509 return 1;
4510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004511EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513/*
4514 * input MUX helper
4515 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004516
4517/**
4518 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4519 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004520int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4521 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
4523 unsigned int index;
4524
4525 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4526 uinfo->count = 1;
4527 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004528 if (!imux->num_items)
4529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 index = uinfo->value.enumerated.item;
4531 if (index >= imux->num_items)
4532 index = imux->num_items - 1;
4533 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4534 return 0;
4535}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004536EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Takashi Iwaid5191e52009-11-16 14:58:17 +01004538/**
4539 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4540 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004541int snd_hda_input_mux_put(struct hda_codec *codec,
4542 const struct hda_input_mux *imux,
4543 struct snd_ctl_elem_value *ucontrol,
4544 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 unsigned int *cur_val)
4546{
4547 unsigned int idx;
4548
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004549 if (!imux->num_items)
4550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 idx = ucontrol->value.enumerated.item[0];
4552 if (idx >= imux->num_items)
4553 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004554 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004556 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4557 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 *cur_val = idx;
4559 return 1;
4560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004561EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
4563
4564/*
4565 * Multi-channel / digital-out PCM helper functions
4566 */
4567
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004568/* setup SPDIF output stream */
4569static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4570 unsigned int stream_tag, unsigned int format)
4571{
Stephen Warren7c9359762011-06-01 11:14:17 -06004572 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4573
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004574 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004575 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004576 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004577 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004578 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004579 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004580 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004581 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004582 for (d = codec->slave_dig_outs; *d; d++)
4583 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4584 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004585 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004586 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004587 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004588 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004589 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004590}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004591
Takashi Iwai2f728532008-09-25 16:32:41 +02004592static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4593{
4594 snd_hda_codec_cleanup_stream(codec, nid);
4595 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004596 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004597 for (d = codec->slave_dig_outs; *d; d++)
4598 snd_hda_codec_cleanup_stream(codec, *d);
4599 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004600}
4601
Takashi Iwaid5191e52009-11-16 14:58:17 +01004602/**
4603 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4604 * @bus: HD-audio bus
4605 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004606void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4607{
4608 struct hda_codec *codec;
4609
4610 if (!bus)
4611 return;
4612 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004613 if (hda_codec_is_power_on(codec) &&
4614 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004615 codec->patch_ops.reboot_notify(codec);
4616 }
4617}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004618EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004619
Takashi Iwaid5191e52009-11-16 14:58:17 +01004620/**
4621 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004623int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4624 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Ingo Molnar62932df2006-01-16 16:34:20 +01004626 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004627 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4628 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004629 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004631 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 return 0;
4633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004634EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Takashi Iwaid5191e52009-11-16 14:58:17 +01004636/**
4637 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4638 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004639int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4640 struct hda_multi_out *mout,
4641 unsigned int stream_tag,
4642 unsigned int format,
4643 struct snd_pcm_substream *substream)
4644{
4645 mutex_lock(&codec->spdif_mutex);
4646 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4647 mutex_unlock(&codec->spdif_mutex);
4648 return 0;
4649}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004650EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004651
Takashi Iwaid5191e52009-11-16 14:58:17 +01004652/**
4653 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4654 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004655int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4656 struct hda_multi_out *mout)
4657{
4658 mutex_lock(&codec->spdif_mutex);
4659 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4660 mutex_unlock(&codec->spdif_mutex);
4661 return 0;
4662}
4663EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4664
Takashi Iwaid5191e52009-11-16 14:58:17 +01004665/**
4666 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004668int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4669 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Ingo Molnar62932df2006-01-16 16:34:20 +01004671 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004673 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return 0;
4675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004676EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Takashi Iwaid5191e52009-11-16 14:58:17 +01004678/**
4679 * snd_hda_multi_out_analog_open - open analog outputs
4680 *
4681 * Open analog outputs and set up the hw-constraints.
4682 * If the digital outputs can be opened as slave, open the digital
4683 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004685int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4686 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004687 struct snd_pcm_substream *substream,
4688 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Takashi Iwai9a081602008-02-12 18:37:26 +01004690 struct snd_pcm_runtime *runtime = substream->runtime;
4691 runtime->hw.channels_max = mout->max_channels;
4692 if (mout->dig_out_nid) {
4693 if (!mout->analog_rates) {
4694 mout->analog_rates = hinfo->rates;
4695 mout->analog_formats = hinfo->formats;
4696 mout->analog_maxbps = hinfo->maxbps;
4697 } else {
4698 runtime->hw.rates = mout->analog_rates;
4699 runtime->hw.formats = mout->analog_formats;
4700 hinfo->maxbps = mout->analog_maxbps;
4701 }
4702 if (!mout->spdif_rates) {
4703 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4704 &mout->spdif_rates,
4705 &mout->spdif_formats,
4706 &mout->spdif_maxbps);
4707 }
4708 mutex_lock(&codec->spdif_mutex);
4709 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004710 if ((runtime->hw.rates & mout->spdif_rates) &&
4711 (runtime->hw.formats & mout->spdif_formats)) {
4712 runtime->hw.rates &= mout->spdif_rates;
4713 runtime->hw.formats &= mout->spdif_formats;
4714 if (mout->spdif_maxbps < hinfo->maxbps)
4715 hinfo->maxbps = mout->spdif_maxbps;
4716 } else {
4717 mout->share_spdif = 0;
4718 /* FIXME: need notify? */
4719 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004720 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004721 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4724 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004726EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Takashi Iwaid5191e52009-11-16 14:58:17 +01004728/**
4729 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4730 *
4731 * Set up the i/o for analog out.
4732 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004734int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4735 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 unsigned int stream_tag,
4737 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004738 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Takashi Iwaidda14412011-05-02 11:29:30 +02004740 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004742 struct hda_spdif_out *spdif =
4743 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 int i;
4745
Ingo Molnar62932df2006-01-16 16:34:20 +01004746 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004747 if (mout->dig_out_nid && mout->share_spdif &&
4748 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004750 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4751 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004752 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004754 setup_dig_out_stream(codec, mout->dig_out_nid,
4755 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 } else {
4757 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004758 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 }
4760 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004761 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004764 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4765 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004766 if (!mout->no_share_stream &&
4767 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004769 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4770 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004771 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004772 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4773 if (!mout->no_share_stream && mout->hp_out_nid[i])
4774 snd_hda_codec_setup_stream(codec,
4775 mout->hp_out_nid[i],
4776 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004777 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004778 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004779 snd_hda_codec_setup_stream(codec,
4780 mout->extra_out_nid[i],
4781 stream_tag, 0, format);
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 /* surrounds */
4784 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004785 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004786 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4787 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004788 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004789 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4790 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
4792 return 0;
4793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004794EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Takashi Iwaid5191e52009-11-16 14:58:17 +01004796/**
4797 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004799int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4800 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Takashi Iwaidda14412011-05-02 11:29:30 +02004802 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 int i;
4804
4805 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004806 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004808 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004809 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4810 if (mout->hp_out_nid[i])
4811 snd_hda_codec_cleanup_stream(codec,
4812 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004813 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4814 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004815 snd_hda_codec_cleanup_stream(codec,
4816 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004817 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004819 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 mout->dig_out_used = 0;
4821 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004822 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 return 0;
4824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004825EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Takashi Iwai47408602012-04-20 13:06:53 +02004827/**
4828 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4829 *
4830 * Guess the suitable VREF pin bits to be set as the pin-control value.
4831 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4832 */
4833unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4834{
4835 unsigned int pincap;
4836 unsigned int oldval;
4837 oldval = snd_hda_codec_read(codec, pin, 0,
4838 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4839 pincap = snd_hda_query_pin_caps(codec, pin);
4840 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4841 /* Exception: if the default pin setup is vref50, we give it priority */
4842 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4843 return AC_PINCTL_VREF_80;
4844 else if (pincap & AC_PINCAP_VREF_50)
4845 return AC_PINCTL_VREF_50;
4846 else if (pincap & AC_PINCAP_VREF_100)
4847 return AC_PINCTL_VREF_100;
4848 else if (pincap & AC_PINCAP_VREF_GRD)
4849 return AC_PINCTL_VREF_GRD;
4850 return AC_PINCTL_VREF_HIZ;
4851}
4852EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4853
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004854int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4855 unsigned int val, bool cached)
4856{
4857 if (val) {
4858 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004859 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004860 if (!(cap & AC_PINCAP_OUT))
4861 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4862 else if ((val & AC_PINCTL_HP_EN) &&
4863 !(cap & AC_PINCAP_HP_DRV))
4864 val &= ~AC_PINCTL_HP_EN;
4865 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004866 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004867 if (!(cap & AC_PINCAP_IN))
4868 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4869 }
4870 }
4871 if (cached)
4872 return snd_hda_codec_update_cache(codec, pin, 0,
4873 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4874 else
4875 return snd_hda_codec_write(codec, pin, 0,
4876 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4877}
4878EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4879
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004880/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004881 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004882 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004883
Takashi Iwaidda14412011-05-02 11:29:30 +02004884static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004885{
4886 for (; *list; list++)
4887 if (*list == nid)
4888 return 1;
4889 return 0;
4890}
4891
Steve Longerbeam81937d32007-05-08 15:33:03 +02004892
4893/*
4894 * Sort an associated group of pins according to their sequence numbers.
4895 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004896static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004897 int num_pins)
4898{
4899 int i, j;
4900 short seq;
4901 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004902
Steve Longerbeam81937d32007-05-08 15:33:03 +02004903 for (i = 0; i < num_pins; i++) {
4904 for (j = i + 1; j < num_pins; j++) {
4905 if (sequences[i] > sequences[j]) {
4906 seq = sequences[i];
4907 sequences[i] = sequences[j];
4908 sequences[j] = seq;
4909 nid = pins[i];
4910 pins[i] = pins[j];
4911 pins[j] = nid;
4912 }
4913 }
4914 }
4915}
4916
4917
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004918/* add the found input-pin to the cfg->inputs[] table */
4919static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4920 int type)
4921{
4922 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4923 cfg->inputs[cfg->num_inputs].pin = nid;
4924 cfg->inputs[cfg->num_inputs].type = type;
4925 cfg->num_inputs++;
4926 }
4927}
4928
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004929/* sort inputs in the order of AUTO_PIN_* type */
4930static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4931{
4932 int i, j;
4933
4934 for (i = 0; i < cfg->num_inputs; i++) {
4935 for (j = i + 1; j < cfg->num_inputs; j++) {
4936 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4937 struct auto_pin_cfg_item tmp;
4938 tmp = cfg->inputs[i];
4939 cfg->inputs[i] = cfg->inputs[j];
4940 cfg->inputs[j] = tmp;
4941 }
4942 }
4943 }
4944}
4945
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02004946/* Reorder the surround channels
4947 * ALSA sequence is front/surr/clfe/side
4948 * HDA sequence is:
4949 * 4-ch: front/surr => OK as it is
4950 * 6-ch: front/clfe/surr
4951 * 8-ch: front/clfe/rear/side|fc
4952 */
4953static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4954{
4955 hda_nid_t nid;
4956
4957 switch (nums) {
4958 case 3:
4959 case 4:
4960 nid = pins[1];
4961 pins[1] = pins[2];
4962 pins[2] = nid;
4963 break;
4964 }
4965}
4966
Takashi Iwai82bc9552006-03-21 11:24:42 +01004967/*
4968 * Parse all pin widgets and store the useful pin nids to cfg
4969 *
4970 * The number of line-outs or any primary output is stored in line_outs,
4971 * and the corresponding output pins are assigned to line_out_pins[],
4972 * in the order of front, rear, CLFE, side, ...
4973 *
4974 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004975 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004976 * is detected, one of speaker of HP pins is assigned as the primary
4977 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4978 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004979 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004980 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004981 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4982 * respectively.
4983 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004984int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4985 struct auto_pin_cfg *cfg,
4986 const hda_nid_t *ignore_nids,
4987 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004988{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004989 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004990 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004991 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4992 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004993 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004994 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004995
4996 memset(cfg, 0, sizeof(*cfg));
4997
Steve Longerbeam81937d32007-05-08 15:33:03 +02004998 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4999 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01005000 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02005001 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005002
Takashi Iwai2f451d22011-11-10 12:36:46 +01005003 codec->ignore_misc_bit = true;
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01005004 end_nid = codec->start_nid + codec->num_nodes;
5005 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01005006 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02005007 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005008 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02005009 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005010
5011 /* read all default configuration for pin complex */
5012 if (wid_type != AC_WID_PIN)
5013 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01005014 /* ignore the given nids (e.g. pc-beep returns error) */
5015 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
5016 continue;
5017
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01005018 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai2f451d22011-11-10 12:36:46 +01005019 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
5020 AC_DEFCFG_MISC_NO_PRESENCE))
5021 codec->ignore_misc_bit = false;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02005022 conn = get_defcfg_connect(def_conf);
5023 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005024 continue;
5025 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02005026 dev = get_defcfg_device(def_conf);
5027
5028 /* workaround for buggy BIOS setups */
5029 if (dev == AC_JACK_LINE_OUT) {
5030 if (conn == AC_JACK_PORT_FIXED)
5031 dev = AC_JACK_SPEAKER;
5032 }
5033
5034 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005035 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005036 seq = get_defcfg_sequence(def_conf);
5037 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005038
5039 if (!(wid_caps & AC_WCAP_STEREO))
5040 if (!cfg->mono_out_pin)
5041 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02005042 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005043 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02005044 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005045 assoc_line_out = assoc;
5046 else if (assoc_line_out != assoc)
5047 continue;
5048 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
5049 continue;
5050 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02005051 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005052 cfg->line_outs++;
5053 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01005054 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02005055 seq = get_defcfg_sequence(def_conf);
5056 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005057 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
5058 continue;
5059 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02005060 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01005061 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01005062 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005063 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01005064 seq = get_defcfg_sequence(def_conf);
5065 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005066 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
5067 continue;
5068 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01005069 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005070 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005071 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02005072 case AC_JACK_MIC_IN:
5073 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005074 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02005075 case AC_JACK_LINE_IN:
5076 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005077 break;
5078 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005079 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005080 break;
5081 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005082 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005083 break;
5084 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005085 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005086 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5087 continue;
5088 cfg->dig_out_pins[cfg->dig_outs] = nid;
5089 cfg->dig_out_type[cfg->dig_outs] =
5090 (loc == AC_JACK_LOC_HDMI) ?
5091 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5092 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005093 break;
5094 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01005095 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005096 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01005097 if (loc == AC_JACK_LOC_HDMI)
5098 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5099 else
5100 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005101 break;
5102 }
5103 }
5104
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005105 /* FIX-UP:
5106 * If no line-out is defined but multiple HPs are found,
5107 * some of them might be the real line-outs.
5108 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005109 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5110 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005111 int i = 0;
5112 while (i < cfg->hp_outs) {
5113 /* The real HPs should have the sequence 0x0f */
5114 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5115 i++;
5116 continue;
5117 }
5118 /* Move it to the line-out table */
5119 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5120 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5121 cfg->line_outs++;
5122 cfg->hp_outs--;
5123 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5124 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02005125 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005126 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5127 }
Takashi Iwai03642c92010-09-08 15:28:19 +02005128 memset(cfg->hp_pins + cfg->hp_outs, 0,
5129 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01005130 if (!cfg->hp_outs)
5131 cfg->line_out_type = AUTO_PIN_HP_OUT;
5132
Takashi Iwai5832fcf2008-02-12 18:30:12 +01005133 }
5134
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005135 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02005136 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5137 cfg->line_outs);
5138 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5139 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01005140 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5141 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005142
Steve Longerbeam81937d32007-05-08 15:33:03 +02005143 /*
5144 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5145 * as a primary output
5146 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02005147 if (!cfg->line_outs &&
5148 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02005149 if (cfg->speaker_outs) {
5150 cfg->line_outs = cfg->speaker_outs;
5151 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5152 sizeof(cfg->speaker_pins));
5153 cfg->speaker_outs = 0;
5154 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5155 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5156 } else if (cfg->hp_outs) {
5157 cfg->line_outs = cfg->hp_outs;
5158 memcpy(cfg->line_out_pins, cfg->hp_pins,
5159 sizeof(cfg->hp_pins));
5160 cfg->hp_outs = 0;
5161 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5162 cfg->line_out_type = AUTO_PIN_HP_OUT;
5163 }
5164 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005165
Takashi Iwai8fa7ab42011-10-26 16:06:27 +02005166 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5167 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5168 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005169
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02005170 sort_autocfg_input_pins(cfg);
5171
Takashi Iwai82bc9552006-03-21 11:24:42 +01005172 /*
5173 * debug prints of the parsed results
5174 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02005175 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 +01005176 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5177 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02005178 cfg->line_out_pins[4],
5179 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5180 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5181 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01005182 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5183 cfg->speaker_outs, cfg->speaker_pins[0],
5184 cfg->speaker_pins[1], cfg->speaker_pins[2],
5185 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02005186 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5187 cfg->hp_outs, cfg->hp_pins[0],
5188 cfg->hp_pins[1], cfg->hp_pins[2],
5189 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01005190 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01005191 if (cfg->dig_outs)
5192 snd_printd(" dig-out=0x%x/0x%x\n",
5193 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005194 snd_printd(" inputs:");
5195 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02005196 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02005197 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02005198 cfg->inputs[i].pin);
5199 }
5200 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01005201 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01005202 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005203
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005204 return 0;
5205}
Takashi Iwaid025feb2011-08-23 15:24:39 +02005206EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02005207
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005208int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005209{
5210 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005211 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005212 if (conn == AC_JACK_PORT_NONE)
5213 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005214 /* Windows may claim the internal mic to be BOTH, too */
5215 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005216 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02005217 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005218 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005219 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005220 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005221 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005222 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005223 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005224 return INPUT_PIN_ATTR_FRONT;
5225 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005226}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005227EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005228
Takashi Iwai990061c2010-09-09 22:08:44 +02005229/**
5230 * hda_get_input_pin_label - Give a label for the given input pin
5231 *
5232 * When check_location is true, the function checks the pin location
5233 * for mic and line-in pins, and set an appropriate prefix like "Front",
5234 * "Rear", "Internal".
5235 */
5236
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005237static const char *hda_get_input_pin_label(struct hda_codec *codec,
5238 hda_nid_t pin, bool check_location)
Takashi Iwai10a20af2010-09-09 16:28:02 +02005239{
Takashi Iwaia1c98512010-09-09 21:36:27 +02005240 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01005241 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005242 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5243 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005244 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01005245
Takashi Iwai10a20af2010-09-09 16:28:02 +02005246 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005247
5248 switch (get_defcfg_device(def_conf)) {
5249 case AC_JACK_MIC_IN:
5250 if (!check_location)
5251 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005252 attr = snd_hda_get_input_pin_attr(def_conf);
5253 if (!attr)
5254 return "None";
5255 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02005256 case AC_JACK_LINE_IN:
5257 if (!check_location)
5258 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005259 attr = snd_hda_get_input_pin_attr(def_conf);
5260 if (!attr)
5261 return "None";
5262 if (attr == INPUT_PIN_ATTR_DOCK)
5263 return "Dock Line";
5264 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005265 case AC_JACK_AUX:
5266 return "Aux";
5267 case AC_JACK_CD:
5268 return "CD";
5269 case AC_JACK_SPDIF_IN:
5270 return "SPDIF In";
5271 case AC_JACK_DIG_OTHER_IN:
5272 return "Digital In";
5273 default:
5274 return "Misc";
5275 }
5276}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005277
Takashi Iwaia1c98512010-09-09 21:36:27 +02005278/* Check whether the location prefix needs to be added to the label.
5279 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5280 * have to put "Front" prefix to each label. In such a case, returns false.
5281 */
5282static int check_mic_location_need(struct hda_codec *codec,
5283 const struct auto_pin_cfg *cfg,
5284 int input)
5285{
5286 unsigned int defc;
5287 int i, attr, attr2;
5288
5289 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005290 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005291 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005292 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005293 return 1;
5294
5295 attr = 0;
5296 for (i = 0; i < cfg->num_inputs; i++) {
5297 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005298 attr2 = snd_hda_get_input_pin_attr(defc);
5299 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005300 if (attr && attr != attr2)
5301 return 1; /* different locations found */
5302 attr = attr2;
5303 }
5304 }
5305 return 0;
5306}
5307
Takashi Iwai990061c2010-09-09 22:08:44 +02005308/**
5309 * hda_get_autocfg_input_label - Get a label for the given input
5310 *
5311 * Get a label for the given input pin defined by the autocfg item.
5312 * Unlike hda_get_input_pin_label(), this function checks all inputs
5313 * defined in autocfg and avoids the redundant mic/line prefix as much as
5314 * possible.
5315 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005316const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5317 const struct auto_pin_cfg *cfg,
5318 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005319{
5320 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005321 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005322
Takashi Iwai10a20af2010-09-09 16:28:02 +02005323 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5324 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5325 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005326 if (has_multiple_pins && type == AUTO_PIN_MIC)
5327 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005328 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5329 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005330}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005331EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5332
Takashi Iwai358b6e62011-11-21 14:34:20 +01005333/* return the position of NID in the list, or -1 if not found */
5334static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5335{
5336 int i;
5337 for (i = 0; i < nums; i++)
5338 if (list[i] == nid)
5339 return i;
5340 return -1;
5341}
5342
Takashi Iwai201e06f2011-11-16 15:33:26 +01005343/* get a unique suffix or an index number */
5344static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5345 int num_pins, int *indexp)
5346{
5347 static const char * const channel_sfx[] = {
Takashi Iwaid6018bb2011-11-21 14:17:16 +01005348 " Front", " Surround", " CLFE", " Side"
Takashi Iwai201e06f2011-11-16 15:33:26 +01005349 };
5350 int i;
5351
Takashi Iwai358b6e62011-11-21 14:34:20 +01005352 i = find_idx_in_nid_list(nid, pins, num_pins);
5353 if (i < 0)
5354 return NULL;
5355 if (num_pins == 1)
5356 return "";
5357 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5358 if (indexp)
5359 *indexp = i;
5360 return "";
Takashi Iwai201e06f2011-11-16 15:33:26 +01005361 }
Takashi Iwai358b6e62011-11-21 14:34:20 +01005362 return channel_sfx[i];
Takashi Iwai201e06f2011-11-16 15:33:26 +01005363}
5364
5365static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5366 const struct auto_pin_cfg *cfg,
5367 const char *name, char *label, int maxlen,
5368 int *indexp)
5369{
5370 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5371 int attr = snd_hda_get_input_pin_attr(def_conf);
5372 const char *pfx = "", *sfx = "";
5373
5374 /* handle as a speaker if it's a fixed line-out */
Takashi Iwaie49a3432012-03-01 18:14:41 +01005375 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005376 name = "Speaker";
5377 /* check the location */
5378 switch (attr) {
5379 case INPUT_PIN_ATTR_DOCK:
5380 pfx = "Dock ";
5381 break;
5382 case INPUT_PIN_ATTR_FRONT:
5383 pfx = "Front ";
5384 break;
5385 }
5386 if (cfg) {
5387 /* try to give a unique suffix if needed */
5388 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5389 indexp);
5390 if (!sfx)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005391 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5392 indexp);
Takashi Iwai358b6e62011-11-21 14:34:20 +01005393 if (!sfx) {
5394 /* don't add channel suffix for Headphone controls */
5395 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5396 cfg->hp_outs);
5397 if (idx >= 0)
5398 *indexp = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005399 sfx = "";
Takashi Iwai358b6e62011-11-21 14:34:20 +01005400 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005401 }
5402 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5403 return 1;
5404}
5405
Takashi Iwai990061c2010-09-09 22:08:44 +02005406/**
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005407 * snd_hda_get_pin_label - Get a label for the given I/O pin
5408 *
5409 * Get a label for the given pin. This function works for both input and
5410 * output pins. When @cfg is given as non-NULL, the function tries to get
5411 * an optimized label using hda_get_autocfg_input_label().
Takashi Iwai201e06f2011-11-16 15:33:26 +01005412 *
5413 * This function tries to give a unique label string for the pin as much as
5414 * possible. For example, when the multiple line-outs are present, it adds
5415 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5416 * If no unique name with a suffix is available and @indexp is non-NULL, the
5417 * index number is stored in the pointer.
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005418 */
Takashi Iwai201e06f2011-11-16 15:33:26 +01005419int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5420 const struct auto_pin_cfg *cfg,
5421 char *label, int maxlen, int *indexp)
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005422{
5423 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai201e06f2011-11-16 15:33:26 +01005424 const char *name = NULL;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005425 int i;
5426
Takashi Iwai201e06f2011-11-16 15:33:26 +01005427 if (indexp)
5428 *indexp = 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005429 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005430 return 0;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005431
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005432 switch (get_defcfg_device(def_conf)) {
5433 case AC_JACK_LINE_OUT:
Takashi Iwaie49a3432012-03-01 18:14:41 +01005434 return fill_audio_out_name(codec, nid, cfg, "Line Out",
Takashi Iwai201e06f2011-11-16 15:33:26 +01005435 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005436 case AC_JACK_SPEAKER:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005437 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5438 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005439 case AC_JACK_HP_OUT:
Takashi Iwai201e06f2011-11-16 15:33:26 +01005440 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5441 label, maxlen, indexp);
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005442 case AC_JACK_SPDIF_OUT:
5443 case AC_JACK_DIG_OTHER_OUT:
5444 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
Takashi Iwai201e06f2011-11-16 15:33:26 +01005445 name = "HDMI";
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005446 else
Takashi Iwai201e06f2011-11-16 15:33:26 +01005447 name = "SPDIF";
5448 if (cfg && indexp) {
Takashi Iwai358b6e62011-11-21 14:34:20 +01005449 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5450 cfg->dig_outs);
5451 if (i >= 0)
5452 *indexp = i;
Takashi Iwai201e06f2011-11-16 15:33:26 +01005453 }
5454 break;
5455 default:
5456 if (cfg) {
5457 for (i = 0; i < cfg->num_inputs; i++) {
5458 if (cfg->inputs[i].pin != nid)
5459 continue;
5460 name = hda_get_autocfg_input_label(codec, cfg, i);
5461 if (name)
5462 break;
5463 }
5464 }
5465 if (!name)
5466 name = hda_get_input_pin_label(codec, nid, true);
5467 break;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005468 }
Takashi Iwai201e06f2011-11-16 15:33:26 +01005469 if (!name)
5470 return 0;
5471 strlcpy(label, name, maxlen);
5472 return 1;
Takashi Iwai04f5ade2011-10-27 20:47:07 +02005473}
5474EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5475
Takashi Iwai990061c2010-09-09 22:08:44 +02005476/**
5477 * snd_hda_add_imux_item - Add an item to input_mux
5478 *
5479 * When the same label is used already in the existing items, the number
5480 * suffix is appended to the label. This label index number is stored
5481 * to type_idx when non-NULL pointer is given.
5482 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005483int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5484 int index, int *type_idx)
5485{
5486 int i, label_idx = 0;
5487 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5488 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5489 return -EINVAL;
5490 }
5491 for (i = 0; i < imux->num_items; i++) {
5492 if (!strncmp(label, imux->items[i].label, strlen(label)))
5493 label_idx++;
5494 }
5495 if (type_idx)
5496 *type_idx = label_idx;
5497 if (label_idx > 0)
5498 snprintf(imux->items[imux->num_items].label,
5499 sizeof(imux->items[imux->num_items].label),
5500 "%s %d", label, label_idx);
5501 else
5502 strlcpy(imux->items[imux->num_items].label, label,
5503 sizeof(imux->items[imux->num_items].label));
5504 imux->items[imux->num_items].index = index;
5505 imux->num_items++;
5506 return 0;
5507}
5508EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005509
Takashi Iwai4a471b72005-12-07 13:56:29 +01005510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511#ifdef CONFIG_PM
5512/*
5513 * power management
5514 */
5515
5516/**
5517 * snd_hda_suspend - suspend the codecs
5518 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 *
5520 * Returns 0 if successful.
5521 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005522int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005524 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Takashi Iwai0ba21762007-04-16 11:29:14 +02005526 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005527 if (hda_codec_is_power_on(codec))
5528 hda_call_codec_suspend(codec);
5529 if (codec->patch_ops.post_suspend)
5530 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 }
5532 return 0;
5533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005534EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536/**
5537 * snd_hda_resume - resume the codecs
5538 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 *
5540 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005541 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005542 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005543 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 */
5545int snd_hda_resume(struct hda_bus *bus)
5546{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005547 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Takashi Iwai0ba21762007-04-16 11:29:14 +02005549 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005550 if (codec->patch_ops.pre_resume)
5551 codec->patch_ops.pre_resume(codec);
Takashi Iwai7f308302012-05-08 16:52:23 +02005552 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return 0;
5555}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005556EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005557#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005558
5559/*
5560 * generic arrays
5561 */
5562
Takashi Iwaid5191e52009-11-16 14:58:17 +01005563/**
5564 * snd_array_new - get a new element from the given array
5565 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005566 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005567 * Get a new element from the given array. If it exceeds the
5568 * pre-allocated array size, re-allocate the array.
5569 *
5570 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005571 */
5572void *snd_array_new(struct snd_array *array)
5573{
5574 if (array->used >= array->alloced) {
5575 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005576 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005577 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005578 void *nlist;
5579 if (snd_BUG_ON(num >= 4096))
5580 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005581 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005582 if (!nlist)
5583 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005584 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005585 array->list = nlist;
5586 array->alloced = num;
5587 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005588 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005589}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005590EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005591
Takashi Iwaid5191e52009-11-16 14:58:17 +01005592/**
5593 * snd_array_free - free the given array elements
5594 * @array: the array object
5595 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005596void snd_array_free(struct snd_array *array)
5597{
5598 kfree(array->list);
5599 array->used = 0;
5600 array->alloced = 0;
5601 array->list = NULL;
5602}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005603EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005604
Takashi Iwaid5191e52009-11-16 14:58:17 +01005605/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005606 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5607 * @pcm: PCM caps bits
5608 * @buf: the string buffer to write
5609 * @buflen: the max buffer length
5610 *
5611 * used by hda_proc.c and hda_eld.c
5612 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005613void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5614{
5615 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5616 int i, j;
5617
5618 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5619 if (pcm & (AC_SUPPCM_BITS_8 << i))
5620 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5621
5622 buf[j] = '\0'; /* necessary when j == 0 */
5623}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005624EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005625
5626MODULE_DESCRIPTION("HDA codec core");
5627MODULE_LICENSE("GPL");