blob: eb09a3348325358b0a741d3408d48cc4e5e8fffd [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
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200337/* read the connection and add to the cache */
338static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200339{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200340 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200341 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344 if (len < 0)
345 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200346 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347}
Takashi Iwaidce20792011-06-24 14:10:28 +0200348
349/**
350 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * @codec: the HDA codec
352 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * @max_conns: max. number of connections to store
355 *
356 * Parses the connection list of the given widget and stores the list
357 * of NIDs.
358 *
359 * Returns the number of connections, or a negative error code.
360 */
361int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200362 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200363{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200364 struct snd_array *array = &codec->conn_lists;
365 int len;
366 hda_nid_t *p;
367 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200368
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200369 again:
370 mutex_lock(&codec->hash_mutex);
371 len = -1;
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(array, nid);
374 if (p) {
375 len = p[1];
376 if (conn_list && len > max_conns) {
377 snd_printk(KERN_ERR "hda_codec: "
378 "Too many connections %d for NID 0x%x\n",
379 len, nid);
380 mutex_unlock(&codec->hash_mutex);
381 return -EINVAL;
382 }
383 if (conn_list && len)
384 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200385 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 mutex_unlock(&codec->hash_mutex);
387 if (len >= 0)
388 return len;
389 if (snd_BUG_ON(added))
390 return -EINVAL;
391
392 len = read_and_add_raw_conns(codec, nid);
393 if (len < 0)
394 return len;
395 added = true;
396 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397}
398EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200400/**
401 * snd_hda_get_raw_connections - copy connection list without cache
402 * @codec: the HDA codec
403 * @nid: NID to parse
404 * @conn_list: connection list array
405 * @max_conns: max. number of connections to store
406 *
407 * Like snd_hda_get_connections(), copy the connection list but without
408 * checking through the connection-list cache.
409 * Currently called only from hda_proc.c, so not exported.
410 */
411int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
412 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100415 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200417 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100418 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Takashi Iwaida3cec32008-08-08 17:12:14 +0200420 if (snd_BUG_ON(!conn_list || max_conns <= 0))
421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200423 wcaps = get_wcaps(codec, nid);
424 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200425 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
426 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
429 if (parm & AC_CLIST_LONG) {
430 /* long form */
431 shift = 16;
432 num_elems = 2;
433 } else {
434 /* short form */
435 shift = 8;
436 num_elems = 4;
437 }
438 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mask = (1 << (shift-1)) - 1;
440
Takashi Iwai0ba21762007-04-16 11:29:14 +0200441 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0; /* no connection */
443
444 if (conn_len == 1) {
445 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200446 parm = snd_hda_codec_read(codec, nid, 0,
447 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200448 if (parm == -1 && codec->bus->rirb_error)
449 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 conn_list[0] = parm & mask;
451 return 1;
452 }
453
454 /* multi connection */
455 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100456 prev_nid = 0;
457 for (i = 0; i < conn_len; i++) {
458 int range_val;
459 hda_nid_t val, n;
460
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200461 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100462 parm = snd_hda_codec_read(codec, nid, 0,
463 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200464 if (parm == -1 && codec->bus->rirb_error)
465 return -EIO;
466 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200467 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100468 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200469 if (val == 0) {
470 snd_printk(KERN_WARNING "hda_codec: "
471 "invalid CONNECT_LIST verb %x[%i]:%x\n",
472 nid, i, parm);
473 return 0;
474 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100475 parm >>= shift;
476 if (range_val) {
477 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 if (!prev_nid || prev_nid >= val) {
479 snd_printk(KERN_WARNING "hda_codec: "
480 "invalid dep_range_val %x:%x\n",
481 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100482 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 for (n = prev_nid + 1; n <= val; n++) {
485 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200486 snd_printk(KERN_ERR "hda_codec: "
487 "Too many connections %d for NID 0x%x\n",
488 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100489 return -EINVAL;
490 }
491 conn_list[conns++] = n;
492 }
493 } else {
494 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200495 snd_printk(KERN_ERR "hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
497 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100498 return -EINVAL;
499 }
500 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100502 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 return conns;
505}
506
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200507static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
508{
509 hda_nid_t *p = snd_array_new(array);
510 if (!p)
511 return false;
512 *p = nid;
513 return true;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200517 * snd_hda_override_conn_list - add/modify the connection-list to cache
518 * @codec: the HDA codec
519 * @nid: NID to parse
520 * @len: number of connection list entries
521 * @list: the list of connection entries
522 *
523 * Add or modify the given connection-list to the cache. If the corresponding
524 * cache already exists, invalidate it and append a new one.
525 *
526 * Returns zero or a negative error code.
527 */
528int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
529 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200531 struct snd_array *array = &codec->conn_lists;
532 hda_nid_t *p;
533 int i, old_used;
534
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200535 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200536 p = lookup_conn_list(array, nid);
537 if (p)
538 *p = -1; /* invalidate the old entry */
539
540 old_used = array->used;
541 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
542 goto error_add;
543 for (i = 0; i < len; i++)
544 if (!add_conn_list(array, list[i]))
545 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200546 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200547 return 0;
548
549 error_add:
550 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200551 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200552 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200554EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
555
556/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200557 * snd_hda_get_conn_index - get the connection index of the given NID
558 * @codec: the HDA codec
559 * @mux: NID containing the list
560 * @nid: NID to select
561 * @recursive: 1 when searching NID recursively, otherwise 0
562 *
563 * Parses the connection list of the widget @mux and checks whether the
564 * widget @nid is present. If it is, return the connection index.
565 * Otherwise it returns -1.
566 */
567int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
568 hda_nid_t nid, int recursive)
569{
570 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
571 int i, nums;
572
573 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
574 for (i = 0; i < nums; i++)
575 if (conn[i] == nid)
576 return i;
577 if (!recursive)
578 return -1;
579 if (recursive > 5) {
580 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
581 return -1;
582 }
583 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200584 for (i = 0; i < nums; i++) {
585 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
586 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
587 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
589 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200591 return -1;
592}
593EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595/**
596 * snd_hda_queue_unsol_event - add an unsolicited event to queue
597 * @bus: the BUS
598 * @res: unsolicited event (lower 32bit of RIRB entry)
599 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
600 *
601 * Adds the given event to the queue. The events are processed in
602 * the workqueue asynchronously. Call this function in the interrupt
603 * hanlder when RIRB receives an unsolicited event.
604 *
605 * Returns 0 if successful, or a negative error code.
606 */
607int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
608{
609 struct hda_bus_unsolicited *unsol;
610 unsigned int wp;
611
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200612 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200613 unsol = bus->unsol;
614 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616
617 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
618 unsol->wp = wp;
619
620 wp <<= 1;
621 unsol->queue[wp] = res;
622 unsol->queue[wp + 1] = res_ex;
623
Takashi Iwai6acaed32009-01-12 10:09:24 +0100624 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 0;
627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100628EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800631 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
David Howellsc4028952006-11-22 14:57:56 +0000633static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
David Howellsc4028952006-11-22 14:57:56 +0000635 struct hda_bus_unsolicited *unsol =
636 container_of(work, struct hda_bus_unsolicited, work);
637 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct hda_codec *codec;
639 unsigned int rp, caddr, res;
640
641 while (unsol->rp != unsol->wp) {
642 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
643 unsol->rp = rp;
644 rp <<= 1;
645 res = unsol->queue[rp];
646 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200647 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 codec = bus->caddr_tbl[caddr & 0x0f];
650 if (codec && codec->patch_ops.unsol_event)
651 codec->patch_ops.unsol_event(codec, res);
652 }
653}
654
655/*
656 * initialize unsolicited queue
657 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200658static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct hda_bus_unsolicited *unsol;
661
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100662 if (bus->unsol) /* already initialized */
663 return 0;
664
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200665 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200666 if (!unsol) {
667 snd_printk(KERN_ERR "hda_codec: "
668 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -ENOMEM;
670 }
David Howellsc4028952006-11-22 14:57:56 +0000671 INIT_WORK(&unsol->work, process_unsol_events);
672 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 bus->unsol = unsol;
674 return 0;
675}
676
677/*
678 * destructor
679 */
680static void snd_hda_codec_free(struct hda_codec *codec);
681
682static int snd_hda_bus_free(struct hda_bus *bus)
683{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200684 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Takashi Iwai0ba21762007-04-16 11:29:14 +0200686 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100688 if (bus->workq)
689 flush_workqueue(bus->workq);
690 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 snd_hda_codec_free(codec);
694 }
695 if (bus->ops.private_free)
696 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100697 if (bus->workq)
698 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus);
700 return 0;
701}
702
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100703static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100706 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return snd_hda_bus_free(bus);
708}
709
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200710#ifdef CONFIG_SND_HDA_HWDEP
711static int snd_hda_bus_dev_register(struct snd_device *device)
712{
713 struct hda_bus *bus = device->device_data;
714 struct hda_codec *codec;
715 list_for_each_entry(codec, &bus->codec_list, list) {
716 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100717 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718 }
719 return 0;
720}
721#else
722#define snd_hda_bus_dev_register NULL
723#endif
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/**
726 * snd_hda_bus_new - create a HDA bus
727 * @card: the card entry
728 * @temp: the template for hda_bus information
729 * @busp: the pointer to store the created bus instance
730 *
731 * Returns 0 if successful, or a negative error code.
732 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100733int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200734 const struct hda_bus_template *temp,
735 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hda_bus *bus;
738 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100739 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200740 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .dev_free = snd_hda_bus_dev_free,
742 };
743
Takashi Iwaida3cec32008-08-08 17:12:14 +0200744 if (snd_BUG_ON(!temp))
745 return -EINVAL;
746 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (busp)
750 *busp = NULL;
751
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200752 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (bus == NULL) {
754 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
755 return -ENOMEM;
756 }
757
758 bus->card = card;
759 bus->private_data = temp->private_data;
760 bus->pci = temp->pci;
761 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100762 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 bus->ops = temp->ops;
764
Ingo Molnar62932df2006-01-16 16:34:20 +0100765 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200766 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 INIT_LIST_HEAD(&bus->codec_list);
768
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100769 snprintf(bus->workq_name, sizeof(bus->workq_name),
770 "hd-audio%d", card->number);
771 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100772 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100773 snd_printk(KERN_ERR "cannot create workqueue %s\n",
774 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100775 kfree(bus);
776 return -ENOMEM;
777 }
778
Takashi Iwai0ba21762007-04-16 11:29:14 +0200779 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
780 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 snd_hda_bus_free(bus);
782 return err;
783 }
784 if (busp)
785 *busp = bus;
786 return 0;
787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100788EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Takashi Iwai82467612007-07-27 19:15:54 +0200790#ifdef CONFIG_SND_HDA_GENERIC
791#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200792 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200793#else
794#define is_generic_config(codec) 0
795#endif
796
Takashi Iwai645f10c2008-11-28 15:07:37 +0100797#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100798#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
799#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100800#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100801#endif
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * find a matching codec preset
805 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200806static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200807find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809 struct hda_codec_preset_list *tbl;
810 const struct hda_codec_preset *preset;
811 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Takashi Iwai82467612007-07-27 19:15:54 +0200813 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100814 return NULL; /* use the generic parser */
815
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816 again:
817 mutex_lock(&preset_mutex);
818 list_for_each_entry(tbl, &hda_preset_tables, list) {
819 if (!try_module_get(tbl->owner)) {
820 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
821 continue;
822 }
823 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100825 if (preset->afg && preset->afg != codec->afg)
826 continue;
827 if (preset->mfg && preset->mfg != codec->mfg)
828 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200829 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200831 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200832 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100833 preset->rev == codec->revision_id)) {
834 mutex_unlock(&preset_mutex);
835 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 module_put(tbl->owner);
840 }
841 mutex_unlock(&preset_mutex);
842
843 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
844 char name[32];
845 if (!mod_requested)
846 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
847 codec->vendor_id);
848 else
849 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
850 (codec->vendor_id >> 16) & 0xffff);
851 request_module(name);
852 mod_requested++;
853 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 return NULL;
856}
857
858/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200859 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200861static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 const struct hda_vendor_id *c;
864 const char *vendor = NULL;
865 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200866 char tmp[16];
867
868 if (codec->vendor_name)
869 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 for (c = hda_vendor_ids; c->id; c++) {
872 if (c->id == vendor_id) {
873 vendor = c->name;
874 break;
875 }
876 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sprintf(tmp, "Generic %04x", vendor_id);
879 vendor = tmp;
880 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200881 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
882 if (!codec->vendor_name)
883 return -ENOMEM;
884
885 get_chip_name:
886 if (codec->chip_name)
887 return 0;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200890 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
891 else {
892 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
893 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
894 }
895 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200896 return -ENOMEM;
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200901 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100903static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200905 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 hda_nid_t nid;
907
908 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
909 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200910 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200911 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200912 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200913 case AC_GRP_AUDIO_FUNCTION:
914 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200915 codec->afg_function_id = function_id & 0xff;
916 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200917 break;
918 case AC_GRP_MODEM_FUNCTION:
919 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 codec->mfg_function_id = function_id & 0xff;
921 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 break;
923 default:
924 break;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100930 * read widget caps for each widget and store in cache
931 */
932static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
933{
934 int i;
935 hda_nid_t nid;
936
937 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
938 &codec->start_nid);
939 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200940 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100941 return -ENOMEM;
942 nid = codec->start_nid;
943 for (i = 0; i < codec->num_nodes; i++, nid++)
944 codec->wcaps[i] = snd_hda_param_read(codec, nid,
945 AC_PAR_AUDIO_WIDGET_CAP);
946 return 0;
947}
948
Takashi Iwai3be14142009-02-20 14:11:16 +0100949/* read all pin default configurations and save codec->init_pins */
950static int read_pin_defaults(struct hda_codec *codec)
951{
952 int i;
953 hda_nid_t nid = codec->start_nid;
954
955 for (i = 0; i < codec->num_nodes; i++, nid++) {
956 struct hda_pincfg *pin;
957 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200958 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100959 if (wid_type != AC_WID_PIN)
960 continue;
961 pin = snd_array_new(&codec->init_pins);
962 if (!pin)
963 return -ENOMEM;
964 pin->nid = nid;
965 pin->cfg = snd_hda_codec_read(codec, nid, 0,
966 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200967 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
968 AC_VERB_GET_PIN_WIDGET_CONTROL,
969 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100970 }
971 return 0;
972}
973
974/* look up the given pin config list and return the item matching with NID */
975static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
976 struct snd_array *array,
977 hda_nid_t nid)
978{
979 int i;
980 for (i = 0; i < array->used; i++) {
981 struct hda_pincfg *pin = snd_array_elem(array, i);
982 if (pin->nid == nid)
983 return pin;
984 }
985 return NULL;
986}
987
988/* write a config value for the given NID */
989static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
990 unsigned int cfg)
991{
992 int i;
993 for (i = 0; i < 4; i++) {
994 snd_hda_codec_write(codec, nid, 0,
995 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
996 cfg & 0xff);
997 cfg >>= 8;
998 }
999}
1000
1001/* set the current pin config value for the given NID.
1002 * the value is cached, and read via snd_hda_codec_get_pincfg()
1003 */
1004int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1005 hda_nid_t nid, unsigned int cfg)
1006{
1007 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001008 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001009
Takashi Iwaib82855a2009-12-27 11:24:56 +01001010 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1011 return -EINVAL;
1012
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001013 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 pin = look_up_pincfg(codec, list, nid);
1015 if (!pin) {
1016 pin = snd_array_new(list);
1017 if (!pin)
1018 return -ENOMEM;
1019 pin->nid = nid;
1020 }
1021 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001022
1023 /* change only when needed; e.g. if the pincfg is already present
1024 * in user_pins[], don't write it
1025 */
1026 cfg = snd_hda_codec_get_pincfg(codec, nid);
1027 if (oldcfg != cfg)
1028 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001029 return 0;
1030}
1031
Takashi Iwaid5191e52009-11-16 14:58:17 +01001032/**
1033 * snd_hda_codec_set_pincfg - Override a pin default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to set the pin config
1036 * @cfg: the pin default config value
1037 *
1038 * Override a pin default configuration value in the cache.
1039 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1040 * priority than the real hardware value.
1041 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001042int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1043 hda_nid_t nid, unsigned int cfg)
1044{
Takashi Iwai346ff702009-02-23 09:42:57 +01001045 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001046}
1047EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1048
Takashi Iwaid5191e52009-11-16 14:58:17 +01001049/**
1050 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1051 * @codec: the HDA codec
1052 * @nid: NID to get the pin config
1053 *
1054 * Get the current pin config value of the given pin NID.
1055 * If the pincfg value is cached or overridden via sysfs or driver,
1056 * returns the cached value.
1057 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001058unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1059{
1060 struct hda_pincfg *pin;
1061
Takashi Iwai3be14142009-02-20 14:11:16 +01001062#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001063 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001064 if (pin)
1065 return pin->cfg;
1066#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001067 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1068 if (pin)
1069 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1071 if (pin)
1072 return pin->cfg;
1073 return 0;
1074}
1075EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1076
1077/* restore all current pin configs */
1078static void restore_pincfgs(struct hda_codec *codec)
1079{
1080 int i;
1081 for (i = 0; i < codec->init_pins.used; i++) {
1082 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1083 set_pincfg(codec, pin->nid,
1084 snd_hda_codec_get_pincfg(codec, pin->nid));
1085 }
1086}
Takashi Iwai54d17402005-11-21 16:33:22 +01001087
Takashi Iwai92ee6162009-12-27 11:18:59 +01001088/**
1089 * snd_hda_shutup_pins - Shut up all pins
1090 * @codec: the HDA codec
1091 *
1092 * Clear all pin controls to shup up before suspend for avoiding click noise.
1093 * The controls aren't cached so that they can be resumed properly.
1094 */
1095void snd_hda_shutup_pins(struct hda_codec *codec)
1096{
1097 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001098 /* don't shut up pins when unloading the driver; otherwise it breaks
1099 * the default pin setup at the next load of the driver
1100 */
1101 if (codec->bus->shutdown)
1102 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001103 for (i = 0; i < codec->init_pins.used; i++) {
1104 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1105 /* use read here for syncing after issuing each verb */
1106 snd_hda_codec_read(codec, pin->nid, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1108 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001109 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001110}
1111EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1112
Takashi Iwai2a439522011-07-26 09:52:50 +02001113#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001114/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1115static void restore_shutup_pins(struct hda_codec *codec)
1116{
1117 int i;
1118 if (!codec->pins_shutup)
1119 return;
1120 if (codec->bus->shutdown)
1121 return;
1122 for (i = 0; i < codec->init_pins.used; i++) {
1123 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1124 snd_hda_codec_write(codec, pin->nid, 0,
1125 AC_VERB_SET_PIN_WIDGET_CONTROL,
1126 pin->ctrl);
1127 }
1128 codec->pins_shutup = 0;
1129}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001130#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001131
Takashi Iwai01751f52007-08-10 16:59:39 +02001132static void init_hda_cache(struct hda_cache_rec *cache,
1133 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001134static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001135
Takashi Iwai3be14142009-02-20 14:11:16 +01001136/* restore the initial pin cfgs and release all pincfg lists */
1137static void restore_init_pincfgs(struct hda_codec *codec)
1138{
Takashi Iwai346ff702009-02-23 09:42:57 +01001139 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001140 * so that only the values in init_pins are restored
1141 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001142 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001143#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001144 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001145#endif
1146 restore_pincfgs(codec);
1147 snd_array_free(&codec->init_pins);
1148}
1149
Takashi Iwai54d17402005-11-21 16:33:22 +01001150/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001151 * audio-converter setup caches
1152 */
1153struct hda_cvt_setup {
1154 hda_nid_t nid;
1155 u8 stream_tag;
1156 u8 channel_id;
1157 u16 format_id;
1158 unsigned char active; /* cvt is currently used */
1159 unsigned char dirty; /* setups should be cleared */
1160};
1161
1162/* get or create a cache entry for the given audio converter NID */
1163static struct hda_cvt_setup *
1164get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1165{
1166 struct hda_cvt_setup *p;
1167 int i;
1168
1169 for (i = 0; i < codec->cvt_setups.used; i++) {
1170 p = snd_array_elem(&codec->cvt_setups, i);
1171 if (p->nid == nid)
1172 return p;
1173 }
1174 p = snd_array_new(&codec->cvt_setups);
1175 if (p)
1176 p->nid = nid;
1177 return p;
1178}
1179
1180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * codec destructor
1182 */
1183static void snd_hda_codec_free(struct hda_codec *codec)
1184{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001187 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001188#ifdef CONFIG_SND_HDA_POWER_SAVE
1189 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001190 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001193 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001194 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001195 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001196 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 codec->bus->caddr_tbl[codec->addr] = NULL;
1198 if (codec->patch_ops.free)
1199 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001200 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001202 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001203 kfree(codec->vendor_name);
1204 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001205 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001206 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 kfree(codec);
1208}
1209
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001210static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1211 unsigned int power_state);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/**
1214 * snd_hda_codec_new - create a HDA codec
1215 * @bus: the bus to assign
1216 * @codec_addr: the codec address
1217 * @codecp: the pointer to store the generated codec
1218 *
1219 * Returns 0 if successful, or a negative error code.
1220 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001221int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1222 unsigned int codec_addr,
1223 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001226 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int err;
1228
Takashi Iwaida3cec32008-08-08 17:12:14 +02001229 if (snd_BUG_ON(!bus))
1230 return -EINVAL;
1231 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001235 snd_printk(KERN_ERR "hda_codec: "
1236 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EBUSY;
1238 }
1239
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001240 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (codec == NULL) {
1242 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1243 return -ENOMEM;
1244 }
1245
1246 codec->bus = bus;
1247 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001248 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001249 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001250 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001251 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001252 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001253 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1254 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001255 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001256 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001257 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001258 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001259 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Takashi Iwaicb53c622007-08-10 17:21:45 +02001261#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001262 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001263 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1264 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1265 * the caller has to power down appropriatley after initialization
1266 * phase.
1267 */
1268 hda_keep_power_on(codec);
1269#endif
1270
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001271 if (codec->bus->modelname) {
1272 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1273 if (!codec->modelname) {
1274 snd_hda_codec_free(codec);
1275 return -ENODEV;
1276 }
1277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_add_tail(&codec->list, &bus->codec_list);
1280 bus->caddr_tbl[codec_addr] = codec;
1281
Takashi Iwai0ba21762007-04-16 11:29:14 +02001282 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1283 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001284 if (codec->vendor_id == -1)
1285 /* read again, hopefully the access method was corrected
1286 * in the last read...
1287 */
1288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001290 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_SUBSYSTEM_ID);
1292 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001295 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001298 err = -ENODEV;
1299 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Takashi Iwai3be14142009-02-20 14:11:16 +01001302 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1303 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001304 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001306 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 err = read_pin_defaults(codec);
1308 if (err < 0)
1309 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001310
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001312 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 codec->subsystem_id =
1314 snd_hda_codec_read(codec, nid, 0,
1315 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 }
1317
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001318 /* power-up all before initialization */
1319 hda_set_power_state(codec,
1320 codec->afg ? codec->afg : codec->mfg,
1321 AC_PWRST_D0);
1322
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001323 snd_hda_codec_proc_new(codec);
1324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001325 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001326
1327 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1328 codec->subsystem_id, codec->revision_id);
1329 snd_component_add(codec->bus->card, component);
1330
1331 if (codecp)
1332 *codecp = codec;
1333 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001334
1335 error:
1336 snd_hda_codec_free(codec);
1337 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001339EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340
Takashi Iwaid5191e52009-11-16 14:58:17 +01001341/**
1342 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1343 * @codec: the HDA codec
1344 *
1345 * Start parsing of the given codec tree and (re-)initialize the whole
1346 * patch instance.
1347 *
1348 * Returns 0 if successful or a negative error code.
1349 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001350int snd_hda_codec_configure(struct hda_codec *codec)
1351{
1352 int err;
1353
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001354 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001355 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001356 err = get_codec_name(codec);
1357 if (err < 0)
1358 return err;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Takashi Iwai82467612007-07-27 19:15:54 +02001361 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001363 goto patched;
1364 }
Takashi Iwai82467612007-07-27 19:15:54 +02001365 if (codec->preset && codec->preset->patch) {
1366 err = codec->preset->patch(codec);
1367 goto patched;
1368 }
1369
1370 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001371 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001372 if (err < 0)
1373 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001374
1375 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001376 if (!err && codec->patch_ops.unsol_event)
1377 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001378 /* audio codec should override the mixer name */
1379 if (!err && (codec->afg || !*codec->bus->card->mixername))
1380 snprintf(codec->bus->card->mixername,
1381 sizeof(codec->bus->card->mixername),
1382 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001385EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/**
1388 * snd_hda_codec_setup_stream - set up the codec for streaming
1389 * @codec: the CODEC to set up
1390 * @nid: the NID to set up
1391 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1392 * @channel_id: channel id to pass, zero based.
1393 * @format: stream format.
1394 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1396 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int channel_id, int format)
1398{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001399 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001400 struct hda_cvt_setup *p;
1401 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001402 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001403 int i;
1404
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001406 return;
1407
Takashi Iwai0ba21762007-04-16 11:29:14 +02001408 snd_printdd("hda_codec_setup_stream: "
1409 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001411 p = get_hda_cvt_setup(codec, nid);
1412 if (!p)
1413 return;
1414 /* update the stream-id if changed */
1415 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1416 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1417 newval = (stream_tag << 4) | channel_id;
1418 if (oldval != newval)
1419 snd_hda_codec_write(codec, nid, 0,
1420 AC_VERB_SET_CHANNEL_STREAMID,
1421 newval);
1422 p->stream_tag = stream_tag;
1423 p->channel_id = channel_id;
1424 }
1425 /* update the format-id if changed */
1426 if (p->format_id != format) {
1427 oldval = snd_hda_codec_read(codec, nid, 0,
1428 AC_VERB_GET_STREAM_FORMAT, 0);
1429 if (oldval != format) {
1430 msleep(1);
1431 snd_hda_codec_write(codec, nid, 0,
1432 AC_VERB_SET_STREAM_FORMAT,
1433 format);
1434 }
1435 p->format_id = format;
1436 }
1437 p->active = 1;
1438 p->dirty = 0;
1439
1440 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001441 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001442 list_for_each_entry(c, &codec->bus->codec_list, list) {
1443 for (i = 0; i < c->cvt_setups.used; i++) {
1444 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001445 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001446 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001447 p->dirty = 1;
1448 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001451EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Takashi Iwaif0cea792010-08-13 11:56:53 +02001453static void really_cleanup_stream(struct hda_codec *codec,
1454 struct hda_cvt_setup *q);
1455
Takashi Iwaid5191e52009-11-16 14:58:17 +01001456/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001457 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001458 * @codec: the CODEC to clean up
1459 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001461 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001462void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1463 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001464{
Takashi Iwaieb541332010-08-06 13:48:11 +02001465 struct hda_cvt_setup *p;
1466
Takashi Iwai888afa12008-03-18 09:57:50 +01001467 if (!nid)
1468 return;
1469
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001470 if (codec->no_sticky_stream)
1471 do_now = 1;
1472
Takashi Iwai888afa12008-03-18 09:57:50 +01001473 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001474 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001475 if (p) {
1476 /* here we just clear the active flag when do_now isn't set;
1477 * actual clean-ups will be done later in
1478 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1479 */
1480 if (do_now)
1481 really_cleanup_stream(codec, p);
1482 else
1483 p->active = 0;
1484 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001485}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001486EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001487
Takashi Iwaieb541332010-08-06 13:48:11 +02001488static void really_cleanup_stream(struct hda_codec *codec,
1489 struct hda_cvt_setup *q)
1490{
1491 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001492 if (q->stream_tag || q->channel_id)
1493 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1494 if (q->format_id)
1495 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1496);
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 memset(q, 0, sizeof(*q));
1498 q->nid = nid;
1499}
1500
1501/* clean up the all conflicting obsolete streams */
1502static void purify_inactive_streams(struct hda_codec *codec)
1503{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001504 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001505 int i;
1506
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001507 list_for_each_entry(c, &codec->bus->codec_list, list) {
1508 for (i = 0; i < c->cvt_setups.used; i++) {
1509 struct hda_cvt_setup *p;
1510 p = snd_array_elem(&c->cvt_setups, i);
1511 if (p->dirty)
1512 really_cleanup_stream(c, p);
1513 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001514 }
1515}
1516
Takashi Iwai2a439522011-07-26 09:52:50 +02001517#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001518/* clean up all streams; called from suspend */
1519static void hda_cleanup_all_streams(struct hda_codec *codec)
1520{
1521 int i;
1522
1523 for (i = 0; i < codec->cvt_setups.used; i++) {
1524 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1525 if (p->stream_tag)
1526 really_cleanup_stream(codec, p);
1527 }
1528}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001529#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
1532 * amp access functions
1533 */
1534
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001535/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001536#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001537#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001538#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1539#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001541#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001544static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001545 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Takashi Iwai01751f52007-08-10 16:59:39 +02001547 memset(cache, 0, sizeof(*cache));
1548 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001549 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001550}
1551
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001552static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001553{
Takashi Iwai603c4012008-07-30 15:01:44 +02001554 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001558static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Takashi Iwai01751f52007-08-10 16:59:39 +02001560 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1561 u16 cur = cache->hash[idx];
1562 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001565 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (info->key == key)
1567 return info;
1568 cur = info->next;
1569 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001570 return NULL;
1571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001573/* query the hash. allocate an entry if not found. */
1574static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1575 u32 key)
1576{
1577 struct hda_cache_head *info = get_hash(cache, key);
1578 if (!info) {
1579 u16 idx, cur;
1580 /* add a new hash entry */
1581 info = snd_array_new(&cache->buf);
1582 if (!info)
1583 return NULL;
1584 cur = snd_array_index(&cache->buf, info);
1585 info->key = key;
1586 info->val = 0;
1587 idx = key % (u16)ARRAY_SIZE(cache->hash);
1588 info->next = cache->hash[idx];
1589 cache->hash[idx] = cur;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return info;
1592}
1593
Takashi Iwai01751f52007-08-10 16:59:39 +02001594/* query and allocate an amp hash entry */
1595static inline struct hda_amp_info *
1596get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1597{
1598 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1599}
1600
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001601/* overwrite the value with the key in the caps hash */
1602static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1603{
1604 struct hda_amp_info *info;
1605
1606 mutex_lock(&codec->hash_mutex);
1607 info = get_alloc_amp_hash(codec, key);
1608 if (!info) {
1609 mutex_unlock(&codec->hash_mutex);
1610 return -EINVAL;
1611 }
1612 info->amp_caps = val;
1613 info->head.val |= INFO_AMP_CAPS;
1614 mutex_unlock(&codec->hash_mutex);
1615 return 0;
1616}
1617
1618/* query the value from the caps hash; if not found, fetch the current
1619 * value from the given function and store in the hash
1620 */
1621static unsigned int
1622query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1623 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1624{
1625 struct hda_amp_info *info;
1626 unsigned int val;
1627
1628 mutex_lock(&codec->hash_mutex);
1629 info = get_alloc_amp_hash(codec, key);
1630 if (!info) {
1631 mutex_unlock(&codec->hash_mutex);
1632 return 0;
1633 }
1634 if (!(info->head.val & INFO_AMP_CAPS)) {
1635 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1636 val = func(codec, nid, dir);
1637 write_caps_hash(codec, key, val);
1638 } else {
1639 val = info->amp_caps;
1640 mutex_unlock(&codec->hash_mutex);
1641 }
1642 return val;
1643}
1644
1645static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1646 int direction)
1647{
1648 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1649 nid = codec->afg;
1650 return snd_hda_param_read(codec, nid,
1651 direction == HDA_OUTPUT ?
1652 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1653}
1654
Takashi Iwaid5191e52009-11-16 14:58:17 +01001655/**
1656 * query_amp_caps - query AMP capabilities
1657 * @codec: the HD-auio codec
1658 * @nid: the NID to query
1659 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1660 *
1661 * Query AMP capabilities for the given widget and direction.
1662 * Returns the obtained capability bits.
1663 *
1664 * When cap bits have been already read, this doesn't read again but
1665 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001667u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001669 return query_caps_hash(codec, nid, direction,
1670 HDA_HASH_KEY(nid, direction, 0),
1671 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001673EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Takashi Iwaid5191e52009-11-16 14:58:17 +01001675/**
1676 * snd_hda_override_amp_caps - Override the AMP capabilities
1677 * @codec: the CODEC to clean up
1678 * @nid: the NID to clean up
1679 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1680 * @caps: the capability bits to set
1681 *
1682 * Override the cached AMP caps bits value by the given one.
1683 * This function is useful if the driver needs to adjust the AMP ranges,
1684 * e.g. limit to 0dB, etc.
1685 *
1686 * Returns zero if successful or a negative error code.
1687 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001688int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1689 unsigned int caps)
1690{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001691 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001692}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001693EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001694
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001695static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1696 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001697{
1698 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1699}
1700
Takashi Iwaid5191e52009-11-16 14:58:17 +01001701/**
1702 * snd_hda_query_pin_caps - Query PIN capabilities
1703 * @codec: the HD-auio codec
1704 * @nid: the NID to query
1705 *
1706 * Query PIN capabilities for the given widget.
1707 * Returns the obtained capability bits.
1708 *
1709 * When cap bits have been already read, this doesn't read again but
1710 * returns the cached value.
1711 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001712u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1713{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001714 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001715 read_pin_cap);
1716}
Takashi Iwai1327a322009-03-23 13:07:47 +01001717EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1718
Wu Fengguang864f92b2009-11-18 12:38:02 +08001719/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001720 * snd_hda_override_pin_caps - Override the pin capabilities
1721 * @codec: the CODEC
1722 * @nid: the NID to override
1723 * @caps: the capability bits to set
1724 *
1725 * Override the cached PIN capabilitiy bits value by the given one.
1726 *
1727 * Returns zero if successful or a negative error code.
1728 */
1729int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1730 unsigned int caps)
1731{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001732 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001733}
1734EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1735
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001736/* read or sync the hash value with the current value;
1737 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001739static struct hda_amp_info *
1740update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1741 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001743 struct hda_amp_info *info;
1744 unsigned int parm, val = 0;
1745 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001747 retry:
1748 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1749 if (!info)
1750 return NULL;
1751 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1752 if (!val_read) {
1753 mutex_unlock(&codec->hash_mutex);
1754 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1755 parm |= direction == HDA_OUTPUT ?
1756 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1757 parm |= index;
1758 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001759 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001760 val &= 0xff;
1761 val_read = true;
1762 mutex_lock(&codec->hash_mutex);
1763 goto retry;
1764 }
1765 info->vol[ch] = val;
1766 info->head.val |= INFO_AMP_VOL(ch);
1767 }
1768 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001772 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001774static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775 hda_nid_t nid, int ch, int direction, int index,
1776 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 u32 parm;
1779
1780 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1781 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1782 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001783 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1784 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1785 ; /* set the zero value as a fake mute */
1786 else
1787 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1789}
1790
Takashi Iwaid5191e52009-11-16 14:58:17 +01001791/**
1792 * snd_hda_codec_amp_read - Read AMP value
1793 * @codec: HD-audio codec
1794 * @nid: NID to read the AMP value
1795 * @ch: channel (left=0 or right=1)
1796 * @direction: #HDA_INPUT or #HDA_OUTPUT
1797 * @index: the index value (only for input direction)
1798 *
1799 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Takashi Iwai834be882006-03-01 14:16:17 +01001801int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1802 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001805 unsigned int val = 0;
1806
1807 mutex_lock(&codec->hash_mutex);
1808 info = update_amp_hash(codec, nid, ch, direction, index);
1809 if (info)
1810 val = info->vol[ch];
1811 mutex_unlock(&codec->hash_mutex);
1812 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001814EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Takashi Iwaid5191e52009-11-16 14:58:17 +01001816/**
1817 * snd_hda_codec_amp_update - update the AMP value
1818 * @codec: HD-audio codec
1819 * @nid: NID to read the AMP value
1820 * @ch: channel (left=0 or right=1)
1821 * @direction: #HDA_INPUT or #HDA_OUTPUT
1822 * @idx: the index value (only for input direction)
1823 * @mask: bit mask to set
1824 * @val: the bits value to set
1825 *
1826 * Update the AMP value with a bit mask.
1827 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001828 */
Takashi Iwai834be882006-03-01 14:16:17 +01001829int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1830 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001832 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001833
Takashi Iwai467126462010-03-29 09:19:38 +02001834 if (snd_BUG_ON(mask & ~0xff))
1835 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001836 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001837
1838 mutex_lock(&codec->hash_mutex);
1839 info = update_amp_hash(codec, nid, ch, direction, idx);
1840 if (!info) {
1841 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001843 }
1844 val |= info->vol[ch] & ~mask;
1845 if (info->vol[ch] == val) {
1846 mutex_unlock(&codec->hash_mutex);
1847 return 0;
1848 }
1849 info->vol[ch] = val;
1850 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001851 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 1;
1853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Takashi Iwaid5191e52009-11-16 14:58:17 +01001856/**
1857 * snd_hda_codec_amp_stereo - update the AMP stereo values
1858 * @codec: HD-audio codec
1859 * @nid: NID to read the AMP value
1860 * @direction: #HDA_INPUT or #HDA_OUTPUT
1861 * @idx: the index value (only for input direction)
1862 * @mask: bit mask to set
1863 * @val: the bits value to set
1864 *
1865 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1866 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001867 */
1868int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1869 int direction, int idx, int mask, int val)
1870{
1871 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001872
1873 if (snd_BUG_ON(mask & ~0xff))
1874 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001875 for (ch = 0; ch < 2; ch++)
1876 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1877 idx, mask, val);
1878 return ret;
1879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001880EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001881
Takashi Iwai2a439522011-07-26 09:52:50 +02001882#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001883/**
1884 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1885 * @codec: HD-audio codec
1886 *
1887 * Resume the all amp commands from the cache.
1888 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001889void snd_hda_codec_resume_amp(struct hda_codec *codec)
1890{
Takashi Iwai603c4012008-07-30 15:01:44 +02001891 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001892 int i;
1893
Takashi Iwai603c4012008-07-30 15:01:44 +02001894 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001895 u32 key = buffer->head.key;
1896 hda_nid_t nid;
1897 unsigned int idx, dir, ch;
1898 if (!key)
1899 continue;
1900 nid = key & 0xff;
1901 idx = (key >> 16) & 0xff;
1902 dir = (key >> 24) & 0xff;
1903 for (ch = 0; ch < 2; ch++) {
1904 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1905 continue;
1906 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1907 buffer->vol[ch]);
1908 }
1909 }
1910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001911EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001912#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001914static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1915 unsigned int ofs)
1916{
1917 u32 caps = query_amp_caps(codec, nid, dir);
1918 /* get num steps */
1919 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1920 if (ofs < caps)
1921 caps -= ofs;
1922 return caps;
1923}
1924
Takashi Iwaid5191e52009-11-16 14:58:17 +01001925/**
1926 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1927 *
1928 * The control element is supposed to have the private_value field
1929 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1930 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001931int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1935 u16 nid = get_amp_nid(kcontrol);
1936 u8 chs = get_amp_channels(kcontrol);
1937 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001938 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001940 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1941 uinfo->count = chs == 3 ? 2 : 1;
1942 uinfo->value.integer.min = 0;
1943 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1944 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001945 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001946 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1947 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -EINVAL;
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
1951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001952EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001954
1955static inline unsigned int
1956read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1957 int ch, int dir, int idx, unsigned int ofs)
1958{
1959 unsigned int val;
1960 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1961 val &= HDA_AMP_VOLMASK;
1962 if (val >= ofs)
1963 val -= ofs;
1964 else
1965 val = 0;
1966 return val;
1967}
1968
1969static inline int
1970update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1971 int ch, int dir, int idx, unsigned int ofs,
1972 unsigned int val)
1973{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001974 unsigned int maxval;
1975
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001976 if (val > 0)
1977 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001978 /* ofs = 0: raw max value */
1979 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001980 if (val > maxval)
1981 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001982 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1983 HDA_AMP_VOLMASK, val);
1984}
1985
Takashi Iwaid5191e52009-11-16 14:58:17 +01001986/**
1987 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1988 *
1989 * The control element is supposed to have the private_value field
1990 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1991 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001992int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1996 hda_nid_t nid = get_amp_nid(kcontrol);
1997 int chs = get_amp_channels(kcontrol);
1998 int dir = get_amp_direction(kcontrol);
1999 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 long *valp = ucontrol->value.integer.value;
2002
2003 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002004 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 0;
2008}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002009EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Takashi Iwaid5191e52009-11-16 14:58:17 +01002011/**
2012 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2013 *
2014 * The control element is supposed to have the private_value field
2015 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2016 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002017int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2021 hda_nid_t nid = get_amp_nid(kcontrol);
2022 int chs = get_amp_channels(kcontrol);
2023 int dir = get_amp_direction(kcontrol);
2024 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002025 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 long *valp = ucontrol->value.integer.value;
2027 int change = 0;
2028
Takashi Iwaicb53c622007-08-10 17:21:45 +02002029 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002030 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002031 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002032 valp++;
2033 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002034 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002035 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002036 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return change;
2038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002039EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Takashi Iwaid5191e52009-11-16 14:58:17 +01002041/**
2042 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2043 *
2044 * The control element is supposed to have the private_value field
2045 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2046 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002047int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2048 unsigned int size, unsigned int __user *_tlv)
2049{
2050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2051 hda_nid_t nid = get_amp_nid(kcontrol);
2052 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002053 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002054 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002055 u32 caps, val1, val2;
2056
2057 if (size < 4 * sizeof(unsigned int))
2058 return -ENOMEM;
2059 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002060 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2061 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002062 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002063 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002064 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002065 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002066 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002067 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2068 return -EFAULT;
2069 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2070 return -EFAULT;
2071 if (put_user(val1, _tlv + 2))
2072 return -EFAULT;
2073 if (put_user(val2, _tlv + 3))
2074 return -EFAULT;
2075 return 0;
2076}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002077EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2081 * @codec: HD-audio codec
2082 * @nid: NID of a reference widget
2083 * @dir: #HDA_INPUT or #HDA_OUTPUT
2084 * @tlv: TLV data to be stored, at least 4 elements
2085 *
2086 * Set (static) TLV data for a virtual master volume using the AMP caps
2087 * obtained from the reference NID.
2088 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002089 */
2090void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2091 unsigned int *tlv)
2092{
2093 u32 caps;
2094 int nums, step;
2095
2096 caps = query_amp_caps(codec, nid, dir);
2097 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2098 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2099 step = (step + 1) * 25;
2100 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2101 tlv[1] = 2 * sizeof(unsigned int);
2102 tlv[2] = -nums * step;
2103 tlv[3] = step;
2104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002105EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002106
2107/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002108static struct snd_kcontrol *
2109_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2110 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002111{
2112 struct snd_ctl_elem_id id;
2113 memset(&id, 0, sizeof(id));
2114 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002115 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002116 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2117 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002118 strcpy(id.name, name);
2119 return snd_ctl_find_id(codec->bus->card, &id);
2120}
2121
Takashi Iwaid5191e52009-11-16 14:58:17 +01002122/**
2123 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2124 * @codec: HD-audio codec
2125 * @name: ctl id name string
2126 *
2127 * Get the control element with the given id string and IFACE_MIXER.
2128 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002129struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2130 const char *name)
2131{
2132 return _snd_hda_find_mixer_ctl(codec, name, 0);
2133}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002134EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002135
Takashi Iwai1afe2062010-12-23 10:17:52 +01002136static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2137{
2138 int idx;
2139 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2140 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2141 return idx;
2142 }
2143 return -EBUSY;
2144}
2145
Takashi Iwaid5191e52009-11-16 14:58:17 +01002146/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002147 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002148 * @codec: HD-audio codec
2149 * @nid: corresponding NID (optional)
2150 * @kctl: the control element to assign
2151 *
2152 * Add the given control element to an array inside the codec instance.
2153 * All control elements belonging to a codec are supposed to be added
2154 * by this function so that a proper clean-up works at the free or
2155 * reconfiguration time.
2156 *
2157 * If non-zero @nid is passed, the NID is assigned to the control element.
2158 * The assignment is shown in the codec proc file.
2159 *
2160 * snd_hda_ctl_add() checks the control subdev id field whether
2161 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002162 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2163 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002165int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2166 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002167{
2168 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002169 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002170 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002171
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002172 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002173 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002174 if (nid == 0)
2175 nid = get_amp_nid_(kctl->private_value);
2176 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002177 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2178 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002179 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002180 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002181 err = snd_ctl_add(codec->bus->card, kctl);
2182 if (err < 0)
2183 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002184 item = snd_array_new(&codec->mixers);
2185 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002186 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002187 item->kctl = kctl;
2188 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002189 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002190 return 0;
2191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002192EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002193
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002195 * snd_hda_add_nid - Assign a NID to a control element
2196 * @codec: HD-audio codec
2197 * @nid: corresponding NID (optional)
2198 * @kctl: the control element to assign
2199 * @index: index to kctl
2200 *
2201 * Add the given control element to an array inside the codec instance.
2202 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2203 * NID:KCTL mapping - for example "Capture Source" selector.
2204 */
2205int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2206 unsigned int index, hda_nid_t nid)
2207{
2208 struct hda_nid_item *item;
2209
2210 if (nid > 0) {
2211 item = snd_array_new(&codec->nids);
2212 if (!item)
2213 return -ENOMEM;
2214 item->kctl = kctl;
2215 item->index = index;
2216 item->nid = nid;
2217 return 0;
2218 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002219 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2220 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002221 return -EINVAL;
2222}
2223EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2224
2225/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002226 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2227 * @codec: HD-audio codec
2228 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002229void snd_hda_ctls_clear(struct hda_codec *codec)
2230{
2231 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002232 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002233 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002234 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002235 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002236 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002237}
2238
Takashi Iwaia65d6292009-02-23 16:57:04 +01002239/* pseudo device locking
2240 * toggle card->shutdown to allow/disallow the device access (as a hack)
2241 */
2242static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002243{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002244 spin_lock(&card->files_lock);
2245 if (card->shutdown) {
2246 spin_unlock(&card->files_lock);
2247 return -EINVAL;
2248 }
2249 card->shutdown = 1;
2250 spin_unlock(&card->files_lock);
2251 return 0;
2252}
2253
2254static void hda_unlock_devices(struct snd_card *card)
2255{
2256 spin_lock(&card->files_lock);
2257 card->shutdown = 0;
2258 spin_unlock(&card->files_lock);
2259}
2260
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261/**
2262 * snd_hda_codec_reset - Clear all objects assigned to the codec
2263 * @codec: HD-audio codec
2264 *
2265 * This frees the all PCM and control elements assigned to the codec, and
2266 * clears the caches and restores the pin default configurations.
2267 *
2268 * When a device is being used, it returns -EBSY. If successfully freed,
2269 * returns zero.
2270 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002271int snd_hda_codec_reset(struct hda_codec *codec)
2272{
2273 struct snd_card *card = codec->bus->card;
2274 int i, pcm;
2275
2276 if (hda_lock_devices(card) < 0)
2277 return -EBUSY;
2278 /* check whether the codec isn't used by any mixer or PCM streams */
2279 if (!list_empty(&card->ctl_files)) {
2280 hda_unlock_devices(card);
2281 return -EBUSY;
2282 }
2283 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2284 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2285 if (!cpcm->pcm)
2286 continue;
2287 if (cpcm->pcm->streams[0].substream_opened ||
2288 cpcm->pcm->streams[1].substream_opened) {
2289 hda_unlock_devices(card);
2290 return -EBUSY;
2291 }
2292 }
2293
2294 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002295
2296#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002297 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002298 codec->power_on = 0;
2299 codec->power_transition = 0;
2300 codec->power_jiffies = jiffies;
Takashi Iwai6acaed32009-01-12 10:09:24 +01002301 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002302#endif
2303 snd_hda_ctls_clear(codec);
2304 /* relase PCMs */
2305 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002306 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002307 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002308 clear_bit(codec->pcm_info[i].device,
2309 codec->bus->pcm_dev_bits);
2310 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002311 }
2312 if (codec->patch_ops.free)
2313 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002314 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002315 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002316 codec->spec = NULL;
2317 free_hda_cache(&codec->amp_cache);
2318 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002319 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2320 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002321 /* free only driver_pins so that init_pins + user_pins are restored */
2322 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002323 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002324 codec->num_pcms = 0;
2325 codec->pcm_info = NULL;
2326 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002327 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2328 codec->slave_dig_outs = NULL;
2329 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002330 module_put(codec->owner);
2331 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002332
2333 /* allow device access again */
2334 hda_unlock_devices(card);
2335 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002336}
2337
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002338typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2339
2340/* apply the function to all matching slave ctls in the mixer list */
2341static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002342 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002343{
2344 struct hda_nid_item *items;
2345 const char * const *s;
2346 int i, err;
2347
2348 items = codec->mixers.list;
2349 for (i = 0; i < codec->mixers.used; i++) {
2350 struct snd_kcontrol *sctl = items[i].kctl;
2351 if (!sctl || !sctl->id.name ||
2352 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2353 continue;
2354 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002355 char tmpname[sizeof(sctl->id.name)];
2356 const char *name = *s;
2357 if (suffix) {
2358 snprintf(tmpname, sizeof(tmpname), "%s %s",
2359 name, suffix);
2360 name = tmpname;
2361 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002362 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002363 err = func(data, sctl);
2364 if (err)
2365 return err;
2366 break;
2367 }
2368 }
2369 }
2370 return 0;
2371}
2372
2373static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2374{
2375 return 1;
2376}
2377
Takashi Iwai18478e82012-03-09 17:51:10 +01002378/* guess the value corresponding to 0dB */
2379static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2380{
2381 int _tlv[4];
2382 const int *tlv = NULL;
2383 int val = -1;
2384
2385 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2386 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2387 mm_segment_t fs = get_fs();
2388 set_fs(get_ds());
2389 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2390 tlv = _tlv;
2391 set_fs(fs);
2392 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2393 tlv = kctl->tlv.p;
2394 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2395 val = -tlv[2] / tlv[3];
2396 return val;
2397}
2398
2399/* call kctl->put with the given value(s) */
2400static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2401{
2402 struct snd_ctl_elem_value *ucontrol;
2403 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2404 if (!ucontrol)
2405 return -ENOMEM;
2406 ucontrol->value.integer.value[0] = val;
2407 ucontrol->value.integer.value[1] = val;
2408 kctl->put(kctl, ucontrol);
2409 kfree(ucontrol);
2410 return 0;
2411}
2412
2413/* initialize the slave volume with 0dB */
2414static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2415{
2416 int offset = get_kctl_0dB_offset(slave);
2417 if (offset > 0)
2418 put_kctl_with_value(slave, offset);
2419 return 0;
2420}
2421
2422/* unmute the slave */
2423static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2424{
2425 return put_kctl_with_value(slave, 1);
2426}
2427
Takashi Iwaid5191e52009-11-16 14:58:17 +01002428/**
2429 * snd_hda_add_vmaster - create a virtual master control and add slaves
2430 * @codec: HD-audio codec
2431 * @name: vmaster control name
2432 * @tlv: TLV data (optional)
2433 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002434 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002435 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002436 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002437 *
2438 * Create a virtual master control with the given name. The TLV data
2439 * must be either NULL or a valid data.
2440 *
2441 * @slaves is a NULL-terminated array of strings, each of which is a
2442 * slave control name. All controls with these names are assigned to
2443 * the new virtual master control.
2444 *
2445 * This function returns zero if successful or a negative error code.
2446 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002447int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002448 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002449 const char *suffix, bool init_slave_vol,
2450 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002451{
2452 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002453 int err;
2454
Takashi Iwai29e58532012-03-12 12:25:03 +01002455 if (ctl_ret)
2456 *ctl_ret = NULL;
2457
Takashi Iwai9322ca52012-02-03 14:28:01 +01002458 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002459 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002460 snd_printdd("No slave found for %s\n", name);
2461 return 0;
2462 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002463 kctl = snd_ctl_make_virtual_master(name, tlv);
2464 if (!kctl)
2465 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002466 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002467 if (err < 0)
2468 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002469
Takashi Iwai9322ca52012-02-03 14:28:01 +01002470 err = map_slaves(codec, slaves, suffix,
2471 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002472 if (err < 0)
2473 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002474
2475 /* init with master mute & zero volume */
2476 put_kctl_with_value(kctl, 0);
2477 if (init_slave_vol)
2478 map_slaves(codec, slaves, suffix,
2479 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2480
Takashi Iwai29e58532012-03-12 12:25:03 +01002481 if (ctl_ret)
2482 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002483 return 0;
2484}
Takashi Iwai18478e82012-03-09 17:51:10 +01002485EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002486
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002487/*
2488 * mute-LED control using vmaster
2489 */
2490static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_info *uinfo)
2492{
2493 static const char * const texts[] = {
2494 "Off", "On", "Follow Master"
2495 };
2496 unsigned int index;
2497
2498 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2499 uinfo->count = 1;
2500 uinfo->value.enumerated.items = 3;
2501 index = uinfo->value.enumerated.item;
2502 if (index >= 3)
2503 index = 2;
2504 strcpy(uinfo->value.enumerated.name, texts[index]);
2505 return 0;
2506}
2507
2508static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2509 struct snd_ctl_elem_value *ucontrol)
2510{
2511 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2512 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2513 return 0;
2514}
2515
2516static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_value *ucontrol)
2518{
2519 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2520 unsigned int old_mode = hook->mute_mode;
2521
2522 hook->mute_mode = ucontrol->value.enumerated.item[0];
2523 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2524 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2525 if (old_mode == hook->mute_mode)
2526 return 0;
2527 snd_hda_sync_vmaster_hook(hook);
2528 return 1;
2529}
2530
2531static struct snd_kcontrol_new vmaster_mute_mode = {
2532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2533 .name = "Mute-LED Mode",
2534 .info = vmaster_mute_mode_info,
2535 .get = vmaster_mute_mode_get,
2536 .put = vmaster_mute_mode_put,
2537};
2538
2539/*
2540 * Add a mute-LED hook with the given vmaster switch kctl
2541 * "Mute-LED Mode" control is automatically created and associated with
2542 * the given hook.
2543 */
2544int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002545 struct hda_vmaster_mute_hook *hook,
2546 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002547{
2548 struct snd_kcontrol *kctl;
2549
2550 if (!hook->hook || !hook->sw_kctl)
2551 return 0;
2552 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2553 hook->codec = codec;
2554 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002555 if (!expose_enum_ctl)
2556 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002557 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2558 if (!kctl)
2559 return -ENOMEM;
2560 return snd_hda_ctl_add(codec, 0, kctl);
2561}
2562EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2563
2564/*
2565 * Call the hook with the current value for synchronization
2566 * Should be called in init callback
2567 */
2568void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2569{
2570 if (!hook->hook || !hook->codec)
2571 return;
2572 switch (hook->mute_mode) {
2573 case HDA_VMUTE_FOLLOW_MASTER:
2574 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2575 break;
2576 default:
2577 hook->hook(hook->codec, hook->mute_mode);
2578 break;
2579 }
2580}
2581EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2582
2583
Takashi Iwaid5191e52009-11-16 14:58:17 +01002584/**
2585 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2586 *
2587 * The control element is supposed to have the private_value field
2588 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2589 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002590int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 int chs = get_amp_channels(kcontrol);
2594
2595 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2596 uinfo->count = chs == 3 ? 2 : 1;
2597 uinfo->value.integer.min = 0;
2598 uinfo->value.integer.max = 1;
2599 return 0;
2600}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002601EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Takashi Iwaid5191e52009-11-16 14:58:17 +01002603/**
2604 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2605 *
2606 * The control element is supposed to have the private_value field
2607 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2608 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613 hda_nid_t nid = get_amp_nid(kcontrol);
2614 int chs = get_amp_channels(kcontrol);
2615 int dir = get_amp_direction(kcontrol);
2616 int idx = get_amp_index(kcontrol);
2617 long *valp = ucontrol->value.integer.value;
2618
2619 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002620 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002621 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002623 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002624 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return 0;
2626}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002627EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Takashi Iwaid5191e52009-11-16 14:58:17 +01002629/**
2630 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2631 *
2632 * The control element is supposed to have the private_value field
2633 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2634 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002635int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2639 hda_nid_t nid = get_amp_nid(kcontrol);
2640 int chs = get_amp_channels(kcontrol);
2641 int dir = get_amp_direction(kcontrol);
2642 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 long *valp = ucontrol->value.integer.value;
2644 int change = 0;
2645
Takashi Iwaicb53c622007-08-10 17:21:45 +02002646 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002647 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002648 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002649 HDA_AMP_MUTE,
2650 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002651 valp++;
2652 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002653 if (chs & 2)
2654 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002655 HDA_AMP_MUTE,
2656 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002657 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002658 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return change;
2660}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002661EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Takashi Iwai67d634c2009-11-16 15:35:59 +01002663#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002664/**
2665 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2666 *
2667 * This function calls snd_hda_enable_beep_device(), which behaves differently
2668 * depending on beep_mode option.
2669 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002670int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2672{
2673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2674 long *valp = ucontrol->value.integer.value;
2675
2676 snd_hda_enable_beep_device(codec, *valp);
2677 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2678}
2679EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002680#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682/*
Takashi Iwai985be542005-11-02 18:26:49 +01002683 * bound volume controls
2684 *
2685 * bind multiple volumes (# indices, from 0)
2686 */
2687
2688#define AMP_VAL_IDX_SHIFT 19
2689#define AMP_VAL_IDX_MASK (0x0f<<19)
2690
Takashi Iwaid5191e52009-11-16 14:58:17 +01002691/**
2692 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2693 *
2694 * The control element is supposed to have the private_value field
2695 * set up via HDA_BIND_MUTE*() macros.
2696 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002699{
2700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2701 unsigned long pval;
2702 int err;
2703
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002704 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002705 pval = kcontrol->private_value;
2706 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2707 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2708 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002709 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002710 return err;
2711}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002712EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002713
Takashi Iwaid5191e52009-11-16 14:58:17 +01002714/**
2715 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2716 *
2717 * The control element is supposed to have the private_value field
2718 * set up via HDA_BIND_MUTE*() macros.
2719 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002720int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2721 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002722{
2723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2724 unsigned long pval;
2725 int i, indices, err = 0, change = 0;
2726
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002727 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002728 pval = kcontrol->private_value;
2729 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2730 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002731 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2732 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002733 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2734 if (err < 0)
2735 break;
2736 change |= err;
2737 }
2738 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002739 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002740 return err < 0 ? err : change;
2741}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002742EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002743
Takashi Iwaid5191e52009-11-16 14:58:17 +01002744/**
2745 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2746 *
2747 * The control element is supposed to have the private_value field
2748 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002749 */
2750int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2751 struct snd_ctl_elem_info *uinfo)
2752{
2753 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2754 struct hda_bind_ctls *c;
2755 int err;
2756
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002757 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002758 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002759 kcontrol->private_value = *c->values;
2760 err = c->ops->info(kcontrol, uinfo);
2761 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002762 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002763 return err;
2764}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002765EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002766
Takashi Iwaid5191e52009-11-16 14:58:17 +01002767/**
2768 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2769 *
2770 * The control element is supposed to have the private_value field
2771 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2772 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002773int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
2775{
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 struct hda_bind_ctls *c;
2778 int err;
2779
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002780 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002781 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002782 kcontrol->private_value = *c->values;
2783 err = c->ops->get(kcontrol, ucontrol);
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;
2787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002788EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002789
Takashi Iwaid5191e52009-11-16 14:58:17 +01002790/**
2791 * snd_hda_mixer_bind_ctls_put - Put 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() or HDA_BIND_SW() macros.
2795 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002796int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 struct hda_bind_ctls *c;
2801 unsigned long *vals;
2802 int err = 0, change = 0;
2803
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002804 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002805 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002806 for (vals = c->values; *vals; vals++) {
2807 kcontrol->private_value = *vals;
2808 err = c->ops->put(kcontrol, ucontrol);
2809 if (err < 0)
2810 break;
2811 change |= err;
2812 }
2813 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002814 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002815 return err < 0 ? err : change;
2816}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002817EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002818
Takashi Iwaid5191e52009-11-16 14:58:17 +01002819/**
2820 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2821 *
2822 * The control element is supposed to have the private_value field
2823 * set up via HDA_BIND_VOL() macro.
2824 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002825int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2826 unsigned int size, unsigned int __user *tlv)
2827{
2828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2829 struct hda_bind_ctls *c;
2830 int err;
2831
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002832 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002833 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002834 kcontrol->private_value = *c->values;
2835 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2836 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002837 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002838 return err;
2839}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002840EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002841
2842struct hda_ctl_ops snd_hda_bind_vol = {
2843 .info = snd_hda_mixer_amp_volume_info,
2844 .get = snd_hda_mixer_amp_volume_get,
2845 .put = snd_hda_mixer_amp_volume_put,
2846 .tlv = snd_hda_mixer_amp_tlv
2847};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002848EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002849
2850struct hda_ctl_ops snd_hda_bind_sw = {
2851 .info = snd_hda_mixer_amp_switch_info,
2852 .get = snd_hda_mixer_amp_switch_get,
2853 .put = snd_hda_mixer_amp_switch_put,
2854 .tlv = snd_hda_mixer_amp_tlv
2855};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002857
2858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 * SPDIF out controls
2860 */
2861
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2866 uinfo->count = 1;
2867 return 0;
2868}
2869
Takashi Iwai0ba21762007-04-16 11:29:14 +02002870static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2874 IEC958_AES0_NONAUDIO |
2875 IEC958_AES0_CON_EMPHASIS_5015 |
2876 IEC958_AES0_CON_NOT_COPYRIGHT;
2877 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2878 IEC958_AES1_CON_ORIGINAL;
2879 return 0;
2880}
2881
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2886 IEC958_AES0_NONAUDIO |
2887 IEC958_AES0_PRO_EMPHASIS_5015;
2888 return 0;
2889}
2890
Takashi Iwai0ba21762007-04-16 11:29:14 +02002891static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002895 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002896 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002898 mutex_lock(&codec->spdif_mutex);
2899 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002900 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2901 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2902 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2903 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002904 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 return 0;
2907}
2908
2909/* convert from SPDIF status bits to HDA SPDIF bits
2910 * bit 0 (DigEn) is always set zero (to be filled later)
2911 */
2912static unsigned short convert_from_spdif_status(unsigned int sbits)
2913{
2914 unsigned short val = 0;
2915
2916 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002917 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2922 IEC958_AES0_PRO_EMPHASIS_5015)
2923 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002925 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2926 IEC958_AES0_CON_EMPHASIS_5015)
2927 val |= AC_DIG1_EMPHASIS;
2928 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2929 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002931 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2933 }
2934 return val;
2935}
2936
2937/* convert to SPDIF status bits from HDA SPDIF bits
2938 */
2939static unsigned int convert_to_spdif_status(unsigned short val)
2940{
2941 unsigned int sbits = 0;
2942
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 sbits |= IEC958_AES0_PROFESSIONAL;
2947 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002948 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2950 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002953 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002955 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2957 sbits |= val & (0x7f << 8);
2958 }
2959 return sbits;
2960}
2961
Takashi Iwai2f728532008-09-25 16:32:41 +02002962/* set digital convert verbs both for the given NID and its slaves */
2963static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2964 int verb, int val)
2965{
Takashi Iwaidda14412011-05-02 11:29:30 +02002966 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002967
Takashi Iwai9e976972008-11-25 08:17:20 +01002968 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002969 d = codec->slave_dig_outs;
2970 if (!d)
2971 return;
2972 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002973 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002974}
2975
2976static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2977 int dig1, int dig2)
2978{
2979 if (dig1 != -1)
2980 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2981 if (dig2 != -1)
2982 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2983}
2984
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002989 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002990 struct hda_spdif_out *spdif;
2991 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 unsigned short val;
2993 int change;
2994
Ingo Molnar62932df2006-01-16 16:34:20 +01002995 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002996 spdif = snd_array_elem(&codec->spdif_out, idx);
2997 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002998 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3000 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3001 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003002 val = convert_from_spdif_status(spdif->status);
3003 val |= spdif->ctls & 1;
3004 change = spdif->ctls != val;
3005 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003006 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003007 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003008 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return change;
3010}
3011
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003012#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Takashi Iwai0ba21762007-04-16 11:29:14 +02003014static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3015 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003018 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003019 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003021 mutex_lock(&codec->spdif_mutex);
3022 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003023 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003024 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return 0;
3026}
3027
Stephen Warren74b654c2011-06-01 11:14:18 -06003028static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3029 int dig1, int dig2)
3030{
3031 set_dig_out_convert(codec, nid, dig1, dig2);
3032 /* unmute amp switch (if any) */
3033 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3034 (dig1 & AC_DIG1_ENABLE))
3035 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3036 HDA_AMP_MUTE, 0);
3037}
3038
Takashi Iwai0ba21762007-04-16 11:29:14 +02003039static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3040 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003043 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003044 struct hda_spdif_out *spdif;
3045 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 unsigned short val;
3047 int change;
3048
Ingo Molnar62932df2006-01-16 16:34:20 +01003049 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003050 spdif = snd_array_elem(&codec->spdif_out, idx);
3051 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003052 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003055 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003056 spdif->ctls = val;
3057 if (change && nid != (u16)-1)
3058 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003059 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return change;
3061}
3062
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003063static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 {
3065 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003067 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 .info = snd_hda_spdif_mask_info,
3069 .get = snd_hda_spdif_cmask_get,
3070 },
3071 {
3072 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003074 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 .info = snd_hda_spdif_mask_info,
3076 .get = snd_hda_spdif_pmask_get,
3077 },
3078 {
3079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003080 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .info = snd_hda_spdif_mask_info,
3082 .get = snd_hda_spdif_default_get,
3083 .put = snd_hda_spdif_default_put,
3084 },
3085 {
3086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003087 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 .info = snd_hda_spdif_out_switch_info,
3089 .get = snd_hda_spdif_out_switch_get,
3090 .put = snd_hda_spdif_out_switch_put,
3091 },
3092 { } /* end */
3093};
3094
3095/**
3096 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3097 * @codec: the HDA codec
3098 * @nid: audio out widget NID
3099 *
3100 * Creates controls related with the SPDIF output.
3101 * Called from each patch supporting the SPDIF out.
3102 *
3103 * Returns 0 if successful, or a negative error code.
3104 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003105int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3106 hda_nid_t associated_nid,
3107 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003110 struct snd_kcontrol *kctl;
3111 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003112 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003113 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Takashi Iwai1afe2062010-12-23 10:17:52 +01003115 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3116 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003117 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3118 return -EBUSY;
3119 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003120 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3122 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003123 if (!kctl)
3124 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003125 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003126 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003127 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return err;
3130 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003131 spdif->nid = cvt_nid;
3132 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003133 AC_VERB_GET_DIGI_CONVERT_1, 0);
3134 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return 0;
3136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003137EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003139/* get the hda_spdif_out entry from the given NID
3140 * call within spdif_mutex lock
3141 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003142struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3143 hda_nid_t nid)
3144{
3145 int i;
3146 for (i = 0; i < codec->spdif_out.used; i++) {
3147 struct hda_spdif_out *spdif =
3148 snd_array_elem(&codec->spdif_out, i);
3149 if (spdif->nid == nid)
3150 return spdif;
3151 }
3152 return NULL;
3153}
3154EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3155
Stephen Warren74b654c2011-06-01 11:14:18 -06003156void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3157{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003158 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003159
3160 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003161 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003162 spdif->nid = (u16)-1;
3163 mutex_unlock(&codec->spdif_mutex);
3164}
3165EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3166
3167void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3168{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003169 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003170 unsigned short val;
3171
3172 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003173 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003174 if (spdif->nid != nid) {
3175 spdif->nid = nid;
3176 val = spdif->ctls;
3177 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3178 }
3179 mutex_unlock(&codec->spdif_mutex);
3180}
3181EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003184 * SPDIF sharing with analog output
3185 */
3186static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
3188{
3189 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3190 ucontrol->value.integer.value[0] = mout->share_spdif;
3191 return 0;
3192}
3193
3194static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
3196{
3197 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3198 mout->share_spdif = !!ucontrol->value.integer.value[0];
3199 return 0;
3200}
3201
3202static struct snd_kcontrol_new spdif_share_sw = {
3203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3204 .name = "IEC958 Default PCM Playback Switch",
3205 .info = snd_ctl_boolean_mono_info,
3206 .get = spdif_share_sw_get,
3207 .put = spdif_share_sw_put,
3208};
3209
Takashi Iwaid5191e52009-11-16 14:58:17 +01003210/**
3211 * snd_hda_create_spdif_share_sw - create Default PCM switch
3212 * @codec: the HDA codec
3213 * @mout: multi-out instance
3214 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003215int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3216 struct hda_multi_out *mout)
3217{
3218 if (!mout->dig_out_nid)
3219 return 0;
3220 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003221 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3222 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003223}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003224EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003225
3226/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 * SPDIF input
3228 */
3229
3230#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3231
Takashi Iwai0ba21762007-04-16 11:29:14 +02003232static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3233 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3236
3237 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3238 return 0;
3239}
3240
Takashi Iwai0ba21762007-04-16 11:29:14 +02003241static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3242 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3245 hda_nid_t nid = kcontrol->private_value;
3246 unsigned int val = !!ucontrol->value.integer.value[0];
3247 int change;
3248
Ingo Molnar62932df2006-01-16 16:34:20 +01003249 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003251 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003253 snd_hda_codec_write_cache(codec, nid, 0,
3254 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003256 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return change;
3258}
3259
Takashi Iwai0ba21762007-04-16 11:29:14 +02003260static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3261 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3264 hda_nid_t nid = kcontrol->private_value;
3265 unsigned short val;
3266 unsigned int sbits;
3267
Andrew Paprocki3982d172007-12-19 12:13:44 +01003268 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 sbits = convert_to_spdif_status(val);
3270 ucontrol->value.iec958.status[0] = sbits;
3271 ucontrol->value.iec958.status[1] = sbits >> 8;
3272 ucontrol->value.iec958.status[2] = sbits >> 16;
3273 ucontrol->value.iec958.status[3] = sbits >> 24;
3274 return 0;
3275}
3276
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003277static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 {
3279 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003280 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 .info = snd_hda_spdif_in_switch_info,
3282 .get = snd_hda_spdif_in_switch_get,
3283 .put = snd_hda_spdif_in_switch_put,
3284 },
3285 {
3286 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003288 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 .info = snd_hda_spdif_mask_info,
3290 .get = snd_hda_spdif_in_status_get,
3291 },
3292 { } /* end */
3293};
3294
3295/**
3296 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3297 * @codec: the HDA codec
3298 * @nid: audio in widget NID
3299 *
3300 * Creates controls related with the SPDIF input.
3301 * Called from each patch supporting the SPDIF in.
3302 *
3303 * Returns 0 if successful, or a negative error code.
3304 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003305int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003308 struct snd_kcontrol *kctl;
3309 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003310 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Takashi Iwai1afe2062010-12-23 10:17:52 +01003312 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3313 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003314 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3315 return -EBUSY;
3316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3318 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003319 if (!kctl)
3320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003322 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003323 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return err;
3325 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003326 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003327 snd_hda_codec_read(codec, nid, 0,
3328 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003329 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return 0;
3331}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003332EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Takashi Iwai2a439522011-07-26 09:52:50 +02003334#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003335/*
3336 * command cache
3337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003339/* build a 32bit cache key with the widget id and the command parameter */
3340#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3341#define get_cmd_cache_nid(key) ((key) & 0xff)
3342#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3343
3344/**
3345 * snd_hda_codec_write_cache - send a single command with caching
3346 * @codec: the HDA codec
3347 * @nid: NID to send the command
3348 * @direct: direct flag
3349 * @verb: the verb to send
3350 * @parm: the parameter for the verb
3351 *
3352 * Send a single command without waiting for response.
3353 *
3354 * Returns 0 if successful, or a negative error code.
3355 */
3356int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3357 int direct, unsigned int verb, unsigned int parm)
3358{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003359 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3360 struct hda_cache_head *c;
3361 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003362
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003363 if (err < 0)
3364 return err;
3365 /* parm may contain the verb stuff for get/set amp */
3366 verb = verb | (parm >> 8);
3367 parm &= 0xff;
3368 key = build_cmd_cache_key(nid, verb);
3369 mutex_lock(&codec->bus->cmd_mutex);
3370 c = get_alloc_hash(&codec->cmd_cache, key);
3371 if (c)
3372 c->val = parm;
3373 mutex_unlock(&codec->bus->cmd_mutex);
3374 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003375}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003376EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003377
Takashi Iwaid5191e52009-11-16 14:58:17 +01003378/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003379 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3380 * @codec: the HDA codec
3381 * @nid: NID to send the command
3382 * @direct: direct flag
3383 * @verb: the verb to send
3384 * @parm: the parameter for the verb
3385 *
3386 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3387 * command if the parameter is already identical with the cached value.
3388 * If not, it sends the command and refreshes the cache.
3389 *
3390 * Returns 0 if successful, or a negative error code.
3391 */
3392int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3393 int direct, unsigned int verb, unsigned int parm)
3394{
3395 struct hda_cache_head *c;
3396 u32 key;
3397
3398 /* parm may contain the verb stuff for get/set amp */
3399 verb = verb | (parm >> 8);
3400 parm &= 0xff;
3401 key = build_cmd_cache_key(nid, verb);
3402 mutex_lock(&codec->bus->cmd_mutex);
3403 c = get_hash(&codec->cmd_cache, key);
3404 if (c && c->val == parm) {
3405 mutex_unlock(&codec->bus->cmd_mutex);
3406 return 0;
3407 }
3408 mutex_unlock(&codec->bus->cmd_mutex);
3409 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3410}
3411EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3412
3413/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003414 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3415 * @codec: HD-audio codec
3416 *
3417 * Execute all verbs recorded in the command caches to resume.
3418 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003419void snd_hda_codec_resume_cache(struct hda_codec *codec)
3420{
Takashi Iwai603c4012008-07-30 15:01:44 +02003421 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003422 int i;
3423
Takashi Iwai603c4012008-07-30 15:01:44 +02003424 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003425 u32 key = buffer->key;
3426 if (!key)
3427 continue;
3428 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3429 get_cmd_cache_cmd(key), buffer->val);
3430 }
3431}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003432EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003433
3434/**
3435 * snd_hda_sequence_write_cache - sequence writes with caching
3436 * @codec: the HDA codec
3437 * @seq: VERB array to send
3438 *
3439 * Send the commands sequentially from the given array.
3440 * Thte commands are recorded on cache for power-save and resume.
3441 * The array must be terminated with NID=0.
3442 */
3443void snd_hda_sequence_write_cache(struct hda_codec *codec,
3444 const struct hda_verb *seq)
3445{
3446 for (; seq->nid; seq++)
3447 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3448 seq->param);
3449}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003450EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003451#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003452
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003453void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3454 unsigned int power_state,
3455 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003456{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003457 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003458 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003459
Takashi Iwaicb53c622007-08-10 17:21:45 +02003460 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003461 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003462 if (!(wcaps & AC_WCAP_POWER))
3463 continue;
3464 /* don't power down the widget if it controls eapd and
3465 * EAPD_BTLENABLE is set.
3466 */
3467 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3468 get_wcaps_type(wcaps) == AC_WID_PIN &&
3469 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3470 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003471 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003472 if (eapd & 0x02)
3473 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003474 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003475 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3476 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003477 }
3478
Takashi Iwaicb53c622007-08-10 17:21:45 +02003479 if (power_state == AC_PWRST_D0) {
3480 unsigned long end_time;
3481 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003482 /* wait until the codec reachs to D0 */
3483 end_time = jiffies + msecs_to_jiffies(500);
3484 do {
3485 state = snd_hda_codec_read(codec, fg, 0,
3486 AC_VERB_GET_POWER_STATE, 0);
3487 if (state == power_state)
3488 break;
3489 msleep(1);
3490 } while (time_after_eq(end_time, jiffies));
3491 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003492}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003493EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3494
3495/*
3496 * set power state of the codec
3497 */
3498static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3499 unsigned int power_state)
3500{
3501 if (codec->patch_ops.set_power_state) {
3502 codec->patch_ops.set_power_state(codec, fg, power_state);
3503 return;
3504 }
3505
3506 /* this delay seems necessary to avoid click noise at power-down */
3507 if (power_state == AC_PWRST_D3)
3508 msleep(100);
3509 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3510 power_state);
3511 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3512}
Takashi Iwai54d17402005-11-21 16:33:22 +01003513
Takashi Iwai11aeff02008-07-30 15:01:46 +02003514#ifdef CONFIG_SND_HDA_HWDEP
3515/* execute additional init verbs */
3516static void hda_exec_init_verbs(struct hda_codec *codec)
3517{
3518 if (codec->init_verbs.list)
3519 snd_hda_sequence_write(codec, codec->init_verbs.list);
3520}
3521#else
3522static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3523#endif
3524
Takashi Iwai2a439522011-07-26 09:52:50 +02003525#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003526/*
3527 * call suspend and power-down; used both from PM and power-save
3528 */
3529static void hda_call_codec_suspend(struct hda_codec *codec)
3530{
3531 if (codec->patch_ops.suspend)
3532 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003533 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003534 hda_set_power_state(codec,
3535 codec->afg ? codec->afg : codec->mfg,
3536 AC_PWRST_D3);
3537#ifdef CONFIG_SND_HDA_POWER_SAVE
3538 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003539 spin_lock(&codec->power_lock);
3540 snd_hda_update_power_acct(codec);
3541 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003542 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003543 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003544 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003545 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003546#endif
3547}
3548
3549/*
3550 * kick up codec; used both from PM and power-save
3551 */
3552static void hda_call_codec_resume(struct hda_codec *codec)
3553{
Takashi Iwai7f308302012-05-08 16:52:23 +02003554 /* set as if powered on for avoiding re-entering the resume
3555 * in the resume / power-save sequence
3556 */
3557 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003558 hda_set_power_state(codec,
3559 codec->afg ? codec->afg : codec->mfg,
3560 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003561 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003562 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003563 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003564 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003565 if (codec->patch_ops.resume)
3566 codec->patch_ops.resume(codec);
3567 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003568 if (codec->patch_ops.init)
3569 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003570 snd_hda_codec_resume_amp(codec);
3571 snd_hda_codec_resume_cache(codec);
3572 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003573 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003574}
Takashi Iwai2a439522011-07-26 09:52:50 +02003575#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003576
Takashi Iwai54d17402005-11-21 16:33:22 +01003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578/**
3579 * snd_hda_build_controls - build mixer controls
3580 * @bus: the BUS
3581 *
3582 * Creates mixer controls for each codec included in the bus.
3583 *
3584 * Returns 0 if successful, otherwise a negative error code.
3585 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003586int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003588 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Takashi Iwai0ba21762007-04-16 11:29:14 +02003590 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003591 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003592 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003593 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003594 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003595 err = snd_hda_codec_reset(codec);
3596 if (err < 0) {
3597 printk(KERN_ERR
3598 "hda_codec: cannot revert codec\n");
3599 return err;
3600 }
3601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003603 return 0;
3604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003605EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003607int snd_hda_codec_build_controls(struct hda_codec *codec)
3608{
3609 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003610 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003611 /* continue to initialize... */
3612 if (codec->patch_ops.init)
3613 err = codec->patch_ops.init(codec);
3614 if (!err && codec->patch_ops.build_controls)
3615 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003616 if (err < 0)
3617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return 0;
3619}
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621/*
3622 * stream formats
3623 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003624struct hda_rate_tbl {
3625 unsigned int hz;
3626 unsigned int alsa_bits;
3627 unsigned int hda_fmt;
3628};
3629
Takashi Iwai92f10b32010-08-03 14:21:00 +02003630/* rate = base * mult / div */
3631#define HDA_RATE(base, mult, div) \
3632 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3633 (((div) - 1) << AC_FMT_DIV_SHIFT))
3634
Takashi Iwaibefdf312005-08-22 13:57:55 +02003635static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003637
3638 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003639 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3640 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3641 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3642 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3643 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3644 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3645 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3646 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3647 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3648 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3649 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003650#define AC_PAR_PCM_RATE_BITS 11
3651 /* up to bits 10, 384kHZ isn't supported properly */
3652
3653 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003654 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003655
Takashi Iwaibefdf312005-08-22 13:57:55 +02003656 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657};
3658
3659/**
3660 * snd_hda_calc_stream_format - calculate format bitset
3661 * @rate: the sample rate
3662 * @channels: the number of channels
3663 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3664 * @maxbps: the max. bps
3665 *
3666 * Calculate the format bitset from the given rate, channels and th PCM format.
3667 *
3668 * Return zero if invalid.
3669 */
3670unsigned int snd_hda_calc_stream_format(unsigned int rate,
3671 unsigned int channels,
3672 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003673 unsigned int maxbps,
3674 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
3676 int i;
3677 unsigned int val = 0;
3678
Takashi Iwaibefdf312005-08-22 13:57:55 +02003679 for (i = 0; rate_bits[i].hz; i++)
3680 if (rate_bits[i].hz == rate) {
3681 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 break;
3683 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003684 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 snd_printdd("invalid rate %d\n", rate);
3686 return 0;
3687 }
3688
3689 if (channels == 0 || channels > 8) {
3690 snd_printdd("invalid channels %d\n", channels);
3691 return 0;
3692 }
3693 val |= channels - 1;
3694
3695 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003696 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003697 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003698 break;
3699 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003700 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 case 20:
3703 case 24:
3704 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003705 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003706 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003708 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003710 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 break;
3712 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003713 snd_printdd("invalid format width %d\n",
3714 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return 0;
3716 }
3717
Anssi Hannula32c168c2010-08-03 13:28:57 +03003718 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003719 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return val;
3722}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003723EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003725static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3726 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003727{
3728 unsigned int val = 0;
3729 if (nid != codec->afg &&
3730 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3731 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3732 if (!val || val == -1)
3733 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3734 if (!val || val == -1)
3735 return 0;
3736 return val;
3737}
3738
3739static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3740{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003741 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003742 get_pcm_param);
3743}
3744
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003745static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3746 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003747{
3748 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3749 if (!streams || streams == -1)
3750 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3751 if (!streams || streams == -1)
3752 return 0;
3753 return streams;
3754}
3755
3756static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3757{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003758 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003759 get_stream_param);
3760}
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762/**
3763 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3764 * @codec: the HDA codec
3765 * @nid: NID to query
3766 * @ratesp: the pointer to store the detected rate bitflags
3767 * @formatsp: the pointer to store the detected formats
3768 * @bpsp: the pointer to store the detected format widths
3769 *
3770 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3771 * or @bsps argument is ignored.
3772 *
3773 * Returns 0 if successful, otherwise a negative error code.
3774 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003775int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3777{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003778 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003780 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003781 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 if (ratesp) {
3784 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003785 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003787 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003789 if (rates == 0) {
3790 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3791 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3792 nid, val,
3793 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3794 return -EIO;
3795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 *ratesp = rates;
3797 }
3798
3799 if (formatsp || bpsp) {
3800 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003801 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003803 streams = query_stream_param(codec, nid);
3804 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 bps = 0;
3808 if (streams & AC_SUPFMT_PCM) {
3809 if (val & AC_SUPPCM_BITS_8) {
3810 formats |= SNDRV_PCM_FMTBIT_U8;
3811 bps = 8;
3812 }
3813 if (val & AC_SUPPCM_BITS_16) {
3814 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3815 bps = 16;
3816 }
3817 if (wcaps & AC_WCAP_DIGITAL) {
3818 if (val & AC_SUPPCM_BITS_32)
3819 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3820 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3821 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3822 if (val & AC_SUPPCM_BITS_24)
3823 bps = 24;
3824 else if (val & AC_SUPPCM_BITS_20)
3825 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003826 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3827 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3829 if (val & AC_SUPPCM_BITS_32)
3830 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 else if (val & AC_SUPPCM_BITS_24)
3832 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003833 else if (val & AC_SUPPCM_BITS_20)
3834 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
3836 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003837#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003838 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003840 if (!bps)
3841 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003842 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003843#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003844 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003845 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 /* temporary hack: we have still no proper support
3847 * for the direct AC3 stream...
3848 */
3849 formats |= SNDRV_PCM_FMTBIT_U8;
3850 bps = 8;
3851 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003852 if (formats == 0) {
3853 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3854 "(nid=0x%x, val=0x%x, ovrd=%i, "
3855 "streams=0x%x)\n",
3856 nid, val,
3857 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3858 streams);
3859 return -EIO;
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (formatsp)
3862 *formatsp = formats;
3863 if (bpsp)
3864 *bpsp = bps;
3865 }
3866
3867 return 0;
3868}
Stephen Warren384a48d2011-06-01 11:14:21 -06003869EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
3871/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003872 * snd_hda_is_supported_format - Check the validity of the format
3873 * @codec: HD-audio codec
3874 * @nid: NID to check
3875 * @format: the HD-audio format value to check
3876 *
3877 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 *
3879 * Returns 1 if supported, 0 if not.
3880 */
3881int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3882 unsigned int format)
3883{
3884 int i;
3885 unsigned int val = 0, rate, stream;
3886
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003887 val = query_pcm_param(codec, nid);
3888 if (!val)
3889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003892 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003893 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 if (val & (1 << i))
3895 break;
3896 return 0;
3897 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003898 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return 0;
3900
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003901 stream = query_stream_param(codec, nid);
3902 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return 0;
3904
3905 if (stream & AC_SUPFMT_PCM) {
3906 switch (format & 0xf0) {
3907 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003908 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return 0;
3910 break;
3911 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003912 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return 0;
3914 break;
3915 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003916 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return 0;
3918 break;
3919 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003920 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return 0;
3922 break;
3923 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003924 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return 0;
3926 break;
3927 default:
3928 return 0;
3929 }
3930 } else {
3931 /* FIXME: check for float32 and AC3? */
3932 }
3933
3934 return 1;
3935}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003936EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938/*
3939 * PCM stuff
3940 */
3941static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3942 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003943 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 return 0;
3946}
3947
3948static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3949 struct hda_codec *codec,
3950 unsigned int stream_tag,
3951 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003952 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
3954 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3955 return 0;
3956}
3957
3958static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3959 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003960 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Takashi Iwai888afa12008-03-18 09:57:50 +01003962 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return 0;
3964}
3965
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003966static int set_pcm_default_values(struct hda_codec *codec,
3967 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003969 int err;
3970
Takashi Iwai0ba21762007-04-16 11:29:14 +02003971 /* query support PCM information from the given NID */
3972 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003973 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003974 info->rates ? NULL : &info->rates,
3975 info->formats ? NULL : &info->formats,
3976 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003977 if (err < 0)
3978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980 if (info->ops.open == NULL)
3981 info->ops.open = hda_pcm_default_open_close;
3982 if (info->ops.close == NULL)
3983 info->ops.close = hda_pcm_default_open_close;
3984 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003985 if (snd_BUG_ON(!info->nid))
3986 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 info->ops.prepare = hda_pcm_default_prepare;
3988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003990 if (snd_BUG_ON(!info->nid))
3991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 info->ops.cleanup = hda_pcm_default_cleanup;
3993 }
3994 return 0;
3995}
3996
Takashi Iwaieb541332010-08-06 13:48:11 +02003997/*
3998 * codec prepare/cleanup entries
3999 */
4000int snd_hda_codec_prepare(struct hda_codec *codec,
4001 struct hda_pcm_stream *hinfo,
4002 unsigned int stream,
4003 unsigned int format,
4004 struct snd_pcm_substream *substream)
4005{
4006 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004007 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004008 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4009 if (ret >= 0)
4010 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004011 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004012 return ret;
4013}
4014EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4015
4016void snd_hda_codec_cleanup(struct hda_codec *codec,
4017 struct hda_pcm_stream *hinfo,
4018 struct snd_pcm_substream *substream)
4019{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004020 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004021 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004022 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004023}
4024EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4025
Takashi Iwaid5191e52009-11-16 14:58:17 +01004026/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004027const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4028 "Audio", "SPDIF", "HDMI", "Modem"
4029};
4030
Takashi Iwai176d5332008-07-30 15:01:44 +02004031/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004032 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004033 *
4034 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004035 */
4036static int get_empty_pcm_device(struct hda_bus *bus, int type)
4037{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004038 /* audio device indices; not linear to keep compatibility */
4039 static int audio_idx[HDA_PCM_NTYPES][5] = {
4040 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4041 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004042 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004043 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004044 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004045 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004046
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004047 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004048 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4049 return -EINVAL;
4050 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004051
4052 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4053 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4054 return audio_idx[type][i];
4055
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004056 /* non-fixed slots starting from 10 */
4057 for (i = 10; i < 32; i++) {
4058 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4059 return i;
4060 }
4061
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004062 snd_printk(KERN_WARNING "Too many %s devices\n",
4063 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004064 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004065}
4066
4067/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004068 * attach a new PCM stream
4069 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004070static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004071{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004072 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004073 struct hda_pcm_stream *info;
4074 int stream, err;
4075
Takashi Iwaib91f0802008-11-04 08:43:08 +01004076 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004077 return -EINVAL;
4078 for (stream = 0; stream < 2; stream++) {
4079 info = &pcm->stream[stream];
4080 if (info->substreams) {
4081 err = set_pcm_default_values(codec, info);
4082 if (err < 0)
4083 return err;
4084 }
4085 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004086 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004087}
4088
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004089/* assign all PCMs of the given codec */
4090int snd_hda_codec_build_pcms(struct hda_codec *codec)
4091{
4092 unsigned int pcm;
4093 int err;
4094
4095 if (!codec->num_pcms) {
4096 if (!codec->patch_ops.build_pcms)
4097 return 0;
4098 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004099 if (err < 0) {
4100 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004101 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004102 err = snd_hda_codec_reset(codec);
4103 if (err < 0) {
4104 printk(KERN_ERR
4105 "hda_codec: cannot revert codec\n");
4106 return err;
4107 }
4108 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004109 }
4110 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4111 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4112 int dev;
4113
4114 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004115 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004116
4117 if (!cpcm->pcm) {
4118 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4119 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004120 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004121 cpcm->device = dev;
4122 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004123 if (err < 0) {
4124 printk(KERN_ERR "hda_codec: cannot attach "
4125 "PCM stream %d for codec #%d\n",
4126 dev, codec->addr);
4127 continue; /* no fatal error */
4128 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004129 }
4130 }
4131 return 0;
4132}
4133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134/**
4135 * snd_hda_build_pcms - build PCM information
4136 * @bus: the BUS
4137 *
4138 * Create PCM information for each codec included in the bus.
4139 *
4140 * The build_pcms codec patch is requested to set up codec->num_pcms and
4141 * codec->pcm_info properly. The array is referred by the top-level driver
4142 * to create its PCM instances.
4143 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4144 * callback.
4145 *
4146 * At least, substreams, channels_min and channels_max must be filled for
4147 * each stream. substreams = 0 indicates that the stream doesn't exist.
4148 * When rates and/or formats are zero, the supported values are queried
4149 * from the given nid. The nid is used also by the default ops.prepare
4150 * and ops.cleanup callbacks.
4151 *
4152 * The driver needs to call ops.open in its open callback. Similarly,
4153 * ops.close is supposed to be called in the close callback.
4154 * ops.prepare should be called in the prepare or hw_params callback
4155 * with the proper parameters for set up.
4156 * ops.cleanup should be called in hw_free for clean up of streams.
4157 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004158 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004160int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004162 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Takashi Iwai0ba21762007-04-16 11:29:14 +02004164 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004165 int err = snd_hda_codec_build_pcms(codec);
4166 if (err < 0)
4167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
4169 return 0;
4170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004171EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173/**
4174 * snd_hda_check_board_config - compare the current codec with the config table
4175 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004176 * @num_configs: number of config enums
4177 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 * @tbl: configuration table, terminated by null entries
4179 *
4180 * Compares the modelname or PCI subsystem id of the current codec with the
4181 * given configuration table. If a matching entry is found, returns its
4182 * config value (supposed to be 0 or positive).
4183 *
4184 * If no entries are matching, the function returns a negative value.
4185 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004186int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004187 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004188 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004190 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004191 int i;
4192 for (i = 0; i < num_configs; i++) {
4193 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004194 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004195 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4196 "selected\n", models[i]);
4197 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 }
4199 }
4200 }
4201
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004202 if (!codec->bus->pci || !tbl)
4203 return -1;
4204
4205 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4206 if (!tbl)
4207 return -1;
4208 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004209#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004210 char tmp[10];
4211 const char *model = NULL;
4212 if (models)
4213 model = models[tbl->value];
4214 if (!model) {
4215 sprintf(tmp, "#%d", tbl->value);
4216 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004218 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4219 "for config %x:%x (%s)\n",
4220 model, tbl->subvendor, tbl->subdevice,
4221 (tbl->name ? tbl->name : "Unknown device"));
4222#endif
4223 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 return -1;
4226}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004227EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004230 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004231 subsystem ID with the
4232 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004233
4234 This is important for Gateway notebooks with SB450 HDA Audio
4235 where the vendor ID of the PCI device is:
4236 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4237 and the vendor/subvendor are found only at the codec.
4238
4239 * @codec: the HDA codec
4240 * @num_configs: number of config enums
4241 * @models: array of model name strings
4242 * @tbl: configuration table, terminated by null entries
4243 *
4244 * Compares the modelname or PCI subsystem id of the current codec with the
4245 * given configuration table. If a matching entry is found, returns its
4246 * config value (supposed to be 0 or positive).
4247 *
4248 * If no entries are matching, the function returns a negative value.
4249 */
4250int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004251 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004252 const struct snd_pci_quirk *tbl)
4253{
4254 const struct snd_pci_quirk *q;
4255
4256 /* Search for codec ID */
4257 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004258 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4259 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4260 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004261 break;
4262 }
4263
4264 if (!q->subvendor)
4265 return -1;
4266
4267 tbl = q;
4268
4269 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004270#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004271 char tmp[10];
4272 const char *model = NULL;
4273 if (models)
4274 model = models[tbl->value];
4275 if (!model) {
4276 sprintf(tmp, "#%d", tbl->value);
4277 model = tmp;
4278 }
4279 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4280 "for config %x:%x (%s)\n",
4281 model, tbl->subvendor, tbl->subdevice,
4282 (tbl->name ? tbl->name : "Unknown device"));
4283#endif
4284 return tbl->value;
4285 }
4286 return -1;
4287}
4288EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4289
4290/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 * snd_hda_add_new_ctls - create controls from the array
4292 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004293 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 *
4295 * This helper function creates and add new controls in the given array.
4296 * The array must be terminated with an empty entry as terminator.
4297 *
4298 * Returns 0 if successful, or a negative error code.
4299 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004300int snd_hda_add_new_ctls(struct hda_codec *codec,
4301 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004303 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004306 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004307 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004308 if (knew->iface == -1) /* skip this codec private value */
4309 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004310 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004311 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004312 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004313 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004314 if (addr > 0)
4315 kctl->id.device = addr;
4316 if (idx > 0)
4317 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004318 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004319 if (!err)
4320 break;
4321 /* try first with another device index corresponding to
4322 * the codec addr; if it still fails (or it's the
4323 * primary codec), then try another control index
4324 */
4325 if (!addr && codec->addr)
4326 addr = codec->addr;
4327 else if (!idx && !knew->index) {
4328 idx = find_empty_mixer_ctl_idx(codec,
4329 knew->name);
4330 if (idx <= 0)
4331 return err;
4332 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004333 return err;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336 return 0;
4337}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004338EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Takashi Iwaicb53c622007-08-10 17:21:45 +02004340#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004341static void hda_power_work(struct work_struct *work)
4342{
4343 struct hda_codec *codec =
4344 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004345 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004346
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004347 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004348 if (codec->power_transition > 0) { /* during power-up sequence? */
4349 spin_unlock(&codec->power_lock);
4350 return;
4351 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004352 if (!codec->power_on || codec->power_count) {
4353 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004354 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004355 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004356 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004357 spin_unlock(&codec->power_lock);
4358
Takashi Iwaicb53c622007-08-10 17:21:45 +02004359 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004360 if (bus->ops.pm_notify)
4361 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004362}
4363
4364static void hda_keep_power_on(struct hda_codec *codec)
4365{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004366 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004367 codec->power_count++;
4368 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004369 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004370 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004371}
4372
Takashi Iwaid5191e52009-11-16 14:58:17 +01004373/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004374void snd_hda_update_power_acct(struct hda_codec *codec)
4375{
4376 unsigned long delta = jiffies - codec->power_jiffies;
4377 if (codec->power_on)
4378 codec->power_on_acct += delta;
4379 else
4380 codec->power_off_acct += delta;
4381 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004382}
4383
Takashi Iwaid5191e52009-11-16 14:58:17 +01004384/**
4385 * snd_hda_power_up - Power-up the codec
4386 * @codec: HD-audio codec
4387 *
4388 * Increment the power-up counter and power up the hardware really when
4389 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004390 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004391void snd_hda_power_up(struct hda_codec *codec)
4392{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004393 struct hda_bus *bus = codec->bus;
4394
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004395 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004396 codec->power_count++;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004397 if (codec->power_on || codec->power_transition > 0) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004398 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004399 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004400 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004401 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004402
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004403 cancel_delayed_work_sync(&codec->power_work);
4404
4405 spin_lock(&codec->power_lock);
Takashi Iwaid66fee52011-08-02 15:39:31 +02004406 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004407 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004408 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004409 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004410 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004411 spin_unlock(&codec->power_lock);
4412
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004413 if (bus->ops.pm_notify)
4414 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004415 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004416
4417 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004418 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004419 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004420}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004421EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004422
4423#define power_save(codec) \
4424 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004425
Takashi Iwaid5191e52009-11-16 14:58:17 +01004426/**
4427 * snd_hda_power_down - Power-down the codec
4428 * @codec: HD-audio codec
4429 *
4430 * Decrement the power-up counter and schedules the power-off work if
4431 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004432 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004433void snd_hda_power_down(struct hda_codec *codec)
4434{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004435 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004436 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004437 if (!codec->power_on || codec->power_count || codec->power_transition) {
4438 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004439 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004440 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004441 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004442 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004443 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004444 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004445 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004446 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004448EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004449
Takashi Iwaid5191e52009-11-16 14:58:17 +01004450/**
4451 * snd_hda_check_amp_list_power - Check the amp list and update the power
4452 * @codec: HD-audio codec
4453 * @check: the object containing an AMP list and the status
4454 * @nid: NID to check / update
4455 *
4456 * Check whether the given NID is in the amp list. If it's in the list,
4457 * check the current AMP status, and update the the power-status according
4458 * to the mute status.
4459 *
4460 * This function is supposed to be set or called from the check_power_status
4461 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004462 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004463int snd_hda_check_amp_list_power(struct hda_codec *codec,
4464 struct hda_loopback_check *check,
4465 hda_nid_t nid)
4466{
Takashi Iwai031024e2011-05-02 11:29:30 +02004467 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004468 int ch, v;
4469
4470 if (!check->amplist)
4471 return 0;
4472 for (p = check->amplist; p->nid; p++) {
4473 if (p->nid == nid)
4474 break;
4475 }
4476 if (!p->nid)
4477 return 0; /* nothing changed */
4478
4479 for (p = check->amplist; p->nid; p++) {
4480 for (ch = 0; ch < 2; ch++) {
4481 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4482 p->idx);
4483 if (!(v & HDA_AMP_MUTE) && v > 0) {
4484 if (!check->power_on) {
4485 check->power_on = 1;
4486 snd_hda_power_up(codec);
4487 }
4488 return 1;
4489 }
4490 }
4491 }
4492 if (check->power_on) {
4493 check->power_on = 0;
4494 snd_hda_power_down(codec);
4495 }
4496 return 0;
4497}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004498EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004501/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004502 * Channel mode helper
4503 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004504
4505/**
4506 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4507 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004508int snd_hda_ch_mode_info(struct hda_codec *codec,
4509 struct snd_ctl_elem_info *uinfo,
4510 const struct hda_channel_mode *chmode,
4511 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004512{
4513 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4514 uinfo->count = 1;
4515 uinfo->value.enumerated.items = num_chmodes;
4516 if (uinfo->value.enumerated.item >= num_chmodes)
4517 uinfo->value.enumerated.item = num_chmodes - 1;
4518 sprintf(uinfo->value.enumerated.name, "%dch",
4519 chmode[uinfo->value.enumerated.item].channels);
4520 return 0;
4521}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004522EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004523
Takashi Iwaid5191e52009-11-16 14:58:17 +01004524/**
4525 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4526 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004527int snd_hda_ch_mode_get(struct hda_codec *codec,
4528 struct snd_ctl_elem_value *ucontrol,
4529 const struct hda_channel_mode *chmode,
4530 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004531 int max_channels)
4532{
4533 int i;
4534
4535 for (i = 0; i < num_chmodes; i++) {
4536 if (max_channels == chmode[i].channels) {
4537 ucontrol->value.enumerated.item[0] = i;
4538 break;
4539 }
4540 }
4541 return 0;
4542}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004543EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004544
Takashi Iwaid5191e52009-11-16 14:58:17 +01004545/**
4546 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4547 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004548int snd_hda_ch_mode_put(struct hda_codec *codec,
4549 struct snd_ctl_elem_value *ucontrol,
4550 const struct hda_channel_mode *chmode,
4551 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004552 int *max_channelsp)
4553{
4554 unsigned int mode;
4555
4556 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004557 if (mode >= num_chmodes)
4558 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004559 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004560 return 0;
4561 /* change the current channel setting */
4562 *max_channelsp = chmode[mode].channels;
4563 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004564 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004565 return 1;
4566}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004567EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569/*
4570 * input MUX helper
4571 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004572
4573/**
4574 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4575 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004576int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4577 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 unsigned int index;
4580
4581 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4582 uinfo->count = 1;
4583 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004584 if (!imux->num_items)
4585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 index = uinfo->value.enumerated.item;
4587 if (index >= imux->num_items)
4588 index = imux->num_items - 1;
4589 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4590 return 0;
4591}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004592EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Takashi Iwaid5191e52009-11-16 14:58:17 +01004594/**
4595 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4596 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004597int snd_hda_input_mux_put(struct hda_codec *codec,
4598 const struct hda_input_mux *imux,
4599 struct snd_ctl_elem_value *ucontrol,
4600 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 unsigned int *cur_val)
4602{
4603 unsigned int idx;
4604
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004605 if (!imux->num_items)
4606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 idx = ucontrol->value.enumerated.item[0];
4608 if (idx >= imux->num_items)
4609 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004610 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004612 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4613 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 *cur_val = idx;
4615 return 1;
4616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004617EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619
4620/*
4621 * Multi-channel / digital-out PCM helper functions
4622 */
4623
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004624/* setup SPDIF output stream */
4625static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4626 unsigned int stream_tag, unsigned int format)
4627{
Stephen Warren7c9359762011-06-01 11:14:17 -06004628 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4629
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004630 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004631 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004632 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004633 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004634 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004635 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004636 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004637 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004638 for (d = codec->slave_dig_outs; *d; d++)
4639 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4640 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004641 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004642 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004643 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004644 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004645 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004646}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004647
Takashi Iwai2f728532008-09-25 16:32:41 +02004648static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4649{
4650 snd_hda_codec_cleanup_stream(codec, nid);
4651 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004652 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004653 for (d = codec->slave_dig_outs; *d; d++)
4654 snd_hda_codec_cleanup_stream(codec, *d);
4655 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004656}
4657
Takashi Iwaid5191e52009-11-16 14:58:17 +01004658/**
4659 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4660 * @bus: HD-audio bus
4661 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004662void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4663{
4664 struct hda_codec *codec;
4665
4666 if (!bus)
4667 return;
4668 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004669 if (hda_codec_is_power_on(codec) &&
4670 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004671 codec->patch_ops.reboot_notify(codec);
4672 }
4673}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004674EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004675
Takashi Iwaid5191e52009-11-16 14:58:17 +01004676/**
4677 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004679int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4680 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Ingo Molnar62932df2006-01-16 16:34:20 +01004682 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004683 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4684 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004685 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004687 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return 0;
4689}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004690EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Takashi Iwaid5191e52009-11-16 14:58:17 +01004692/**
4693 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4694 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004695int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4696 struct hda_multi_out *mout,
4697 unsigned int stream_tag,
4698 unsigned int format,
4699 struct snd_pcm_substream *substream)
4700{
4701 mutex_lock(&codec->spdif_mutex);
4702 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4703 mutex_unlock(&codec->spdif_mutex);
4704 return 0;
4705}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004706EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004707
Takashi Iwaid5191e52009-11-16 14:58:17 +01004708/**
4709 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4710 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004711int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4712 struct hda_multi_out *mout)
4713{
4714 mutex_lock(&codec->spdif_mutex);
4715 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4716 mutex_unlock(&codec->spdif_mutex);
4717 return 0;
4718}
4719EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4720
Takashi Iwaid5191e52009-11-16 14:58:17 +01004721/**
4722 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004724int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4725 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
Ingo Molnar62932df2006-01-16 16:34:20 +01004727 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004729 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return 0;
4731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004732EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Takashi Iwaid5191e52009-11-16 14:58:17 +01004734/**
4735 * snd_hda_multi_out_analog_open - open analog outputs
4736 *
4737 * Open analog outputs and set up the hw-constraints.
4738 * If the digital outputs can be opened as slave, open the digital
4739 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004741int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4742 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004743 struct snd_pcm_substream *substream,
4744 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
Takashi Iwai9a081602008-02-12 18:37:26 +01004746 struct snd_pcm_runtime *runtime = substream->runtime;
4747 runtime->hw.channels_max = mout->max_channels;
4748 if (mout->dig_out_nid) {
4749 if (!mout->analog_rates) {
4750 mout->analog_rates = hinfo->rates;
4751 mout->analog_formats = hinfo->formats;
4752 mout->analog_maxbps = hinfo->maxbps;
4753 } else {
4754 runtime->hw.rates = mout->analog_rates;
4755 runtime->hw.formats = mout->analog_formats;
4756 hinfo->maxbps = mout->analog_maxbps;
4757 }
4758 if (!mout->spdif_rates) {
4759 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4760 &mout->spdif_rates,
4761 &mout->spdif_formats,
4762 &mout->spdif_maxbps);
4763 }
4764 mutex_lock(&codec->spdif_mutex);
4765 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004766 if ((runtime->hw.rates & mout->spdif_rates) &&
4767 (runtime->hw.formats & mout->spdif_formats)) {
4768 runtime->hw.rates &= mout->spdif_rates;
4769 runtime->hw.formats &= mout->spdif_formats;
4770 if (mout->spdif_maxbps < hinfo->maxbps)
4771 hinfo->maxbps = mout->spdif_maxbps;
4772 } else {
4773 mout->share_spdif = 0;
4774 /* FIXME: need notify? */
4775 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004776 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004777 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4780 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004782EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Takashi Iwaid5191e52009-11-16 14:58:17 +01004784/**
4785 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4786 *
4787 * Set up the i/o for analog out.
4788 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004790int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4791 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 unsigned int stream_tag,
4793 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004794 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Takashi Iwaidda14412011-05-02 11:29:30 +02004796 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004798 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 int i;
4800
Ingo Molnar62932df2006-01-16 16:34:20 +01004801 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004802 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004803 if (mout->dig_out_nid && mout->share_spdif &&
4804 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004806 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4807 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004808 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004810 setup_dig_out_stream(codec, mout->dig_out_nid,
4811 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 } else {
4813 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004814 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
4816 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004817 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004820 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4821 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004822 if (!mout->no_share_stream &&
4823 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004825 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4826 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004827 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004828 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4829 if (!mout->no_share_stream && mout->hp_out_nid[i])
4830 snd_hda_codec_setup_stream(codec,
4831 mout->hp_out_nid[i],
4832 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004833 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004834 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004835 snd_hda_codec_setup_stream(codec,
4836 mout->extra_out_nid[i],
4837 stream_tag, 0, format);
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 /* surrounds */
4840 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004841 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004842 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4843 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004844 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004845 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4846 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848 return 0;
4849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004850EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Takashi Iwaid5191e52009-11-16 14:58:17 +01004852/**
4853 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004855int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4856 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Takashi Iwaidda14412011-05-02 11:29:30 +02004858 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 int i;
4860
4861 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004862 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004864 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004865 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4866 if (mout->hp_out_nid[i])
4867 snd_hda_codec_cleanup_stream(codec,
4868 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004869 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4870 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004871 snd_hda_codec_cleanup_stream(codec,
4872 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004873 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004875 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 mout->dig_out_used = 0;
4877 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004878 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return 0;
4880}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004881EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Takashi Iwai47408602012-04-20 13:06:53 +02004883/**
4884 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4885 *
4886 * Guess the suitable VREF pin bits to be set as the pin-control value.
4887 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4888 */
4889unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4890{
4891 unsigned int pincap;
4892 unsigned int oldval;
4893 oldval = snd_hda_codec_read(codec, pin, 0,
4894 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4895 pincap = snd_hda_query_pin_caps(codec, pin);
4896 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4897 /* Exception: if the default pin setup is vref50, we give it priority */
4898 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4899 return AC_PINCTL_VREF_80;
4900 else if (pincap & AC_PINCAP_VREF_50)
4901 return AC_PINCTL_VREF_50;
4902 else if (pincap & AC_PINCAP_VREF_100)
4903 return AC_PINCTL_VREF_100;
4904 else if (pincap & AC_PINCAP_VREF_GRD)
4905 return AC_PINCTL_VREF_GRD;
4906 return AC_PINCTL_VREF_HIZ;
4907}
4908EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4909
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004910int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4911 unsigned int val, bool cached)
4912{
4913 if (val) {
4914 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004915 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004916 if (!(cap & AC_PINCAP_OUT))
4917 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4918 else if ((val & AC_PINCTL_HP_EN) &&
4919 !(cap & AC_PINCAP_HP_DRV))
4920 val &= ~AC_PINCTL_HP_EN;
4921 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004922 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004923 if (!(cap & AC_PINCAP_IN))
4924 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4925 }
4926 }
4927 if (cached)
4928 return snd_hda_codec_update_cache(codec, pin, 0,
4929 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4930 else
4931 return snd_hda_codec_write(codec, pin, 0,
4932 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4933}
4934EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4935
Takashi Iwai990061c2010-09-09 22:08:44 +02004936/**
4937 * snd_hda_add_imux_item - Add an item to input_mux
4938 *
4939 * When the same label is used already in the existing items, the number
4940 * suffix is appended to the label. This label index number is stored
4941 * to type_idx when non-NULL pointer is given.
4942 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004943int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4944 int index, int *type_idx)
4945{
4946 int i, label_idx = 0;
4947 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4948 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4949 return -EINVAL;
4950 }
4951 for (i = 0; i < imux->num_items; i++) {
4952 if (!strncmp(label, imux->items[i].label, strlen(label)))
4953 label_idx++;
4954 }
4955 if (type_idx)
4956 *type_idx = label_idx;
4957 if (label_idx > 0)
4958 snprintf(imux->items[imux->num_items].label,
4959 sizeof(imux->items[imux->num_items].label),
4960 "%s %d", label, label_idx);
4961 else
4962 strlcpy(imux->items[imux->num_items].label, label,
4963 sizeof(imux->items[imux->num_items].label));
4964 imux->items[imux->num_items].index = index;
4965 imux->num_items++;
4966 return 0;
4967}
4968EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004969
Takashi Iwai4a471b72005-12-07 13:56:29 +01004970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971#ifdef CONFIG_PM
4972/*
4973 * power management
4974 */
4975
4976/**
4977 * snd_hda_suspend - suspend the codecs
4978 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 *
4980 * Returns 0 if successful.
4981 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004982int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004984 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwai0ba21762007-04-16 11:29:14 +02004986 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004987 if (hda_codec_is_power_on(codec))
4988 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
4990 return 0;
4991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004992EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
4994/**
4995 * snd_hda_resume - resume the codecs
4996 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 *
4998 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004999 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005000 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005001 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 */
5003int snd_hda_resume(struct hda_bus *bus)
5004{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005005 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Takashi Iwai0ba21762007-04-16 11:29:14 +02005007 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005008 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 return 0;
5011}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005012EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005013#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005014
5015/*
5016 * generic arrays
5017 */
5018
Takashi Iwaid5191e52009-11-16 14:58:17 +01005019/**
5020 * snd_array_new - get a new element from the given array
5021 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005022 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005023 * Get a new element from the given array. If it exceeds the
5024 * pre-allocated array size, re-allocate the array.
5025 *
5026 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005027 */
5028void *snd_array_new(struct snd_array *array)
5029{
5030 if (array->used >= array->alloced) {
5031 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005032 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005033 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005034 void *nlist;
5035 if (snd_BUG_ON(num >= 4096))
5036 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005037 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005038 if (!nlist)
5039 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005040 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005041 array->list = nlist;
5042 array->alloced = num;
5043 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005044 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005046EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005047
Takashi Iwaid5191e52009-11-16 14:58:17 +01005048/**
5049 * snd_array_free - free the given array elements
5050 * @array: the array object
5051 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005052void snd_array_free(struct snd_array *array)
5053{
5054 kfree(array->list);
5055 array->used = 0;
5056 array->alloced = 0;
5057 array->list = NULL;
5058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005059EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005060
Takashi Iwaid5191e52009-11-16 14:58:17 +01005061/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005062 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5063 * @pcm: PCM caps bits
5064 * @buf: the string buffer to write
5065 * @buflen: the max buffer length
5066 *
5067 * used by hda_proc.c and hda_eld.c
5068 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005069void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5070{
5071 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5072 int i, j;
5073
5074 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5075 if (pcm & (AC_SUPPCM_BITS_8 << i))
5076 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5077
5078 buf[j] = '\0'; /* necessary when j == 0 */
5079}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005080EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005081
5082MODULE_DESCRIPTION("HDA codec core");
5083MODULE_LICENSE("GPL");