blob: 854dd0c25f896f258c1e5870299e13f2cefd9833 [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 Iwai59cad162012-06-26 15:00:20 +02001187 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001188 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001189#ifdef CONFIG_SND_HDA_POWER_SAVE
1190 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001191 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001194 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001195 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001196 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001197 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001198 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 codec->bus->caddr_tbl[codec->addr] = NULL;
1200 if (codec->patch_ops.free)
1201 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001202 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001203 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001204 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001205 kfree(codec->vendor_name);
1206 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001207 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001208 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 kfree(codec);
1210}
1211
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001212static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1213 unsigned int power_state);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * snd_hda_codec_new - create a HDA codec
1217 * @bus: the bus to assign
1218 * @codec_addr: the codec address
1219 * @codecp: the pointer to store the generated codec
1220 *
1221 * Returns 0 if successful, or a negative error code.
1222 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001223int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1224 unsigned int codec_addr,
1225 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001228 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
Takashi Iwaida3cec32008-08-08 17:12:14 +02001231 if (snd_BUG_ON(!bus))
1232 return -EINVAL;
1233 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001237 snd_printk(KERN_ERR "hda_codec: "
1238 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EBUSY;
1240 }
1241
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001242 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (codec == NULL) {
1244 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1245 return -ENOMEM;
1246 }
1247
1248 codec->bus = bus;
1249 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001250 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001251 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001252 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001253 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001254 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001255 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1256 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001257 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001258 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001259 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001260 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001261 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Takashi Iwaicb53c622007-08-10 17:21:45 +02001263#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001264 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001265 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1266 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1267 * the caller has to power down appropriatley after initialization
1268 * phase.
1269 */
1270 hda_keep_power_on(codec);
1271#endif
1272
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001273 if (codec->bus->modelname) {
1274 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1275 if (!codec->modelname) {
1276 snd_hda_codec_free(codec);
1277 return -ENODEV;
1278 }
1279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 list_add_tail(&codec->list, &bus->codec_list);
1282 bus->caddr_tbl[codec_addr] = codec;
1283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001286 if (codec->vendor_id == -1)
1287 /* read again, hopefully the access method was corrected
1288 * in the last read...
1289 */
1290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_SUBSYSTEM_ID);
1294 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001299 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = -ENODEV;
1301 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1305 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001306 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001308 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_pin_defaults(codec);
1310 if (err < 0)
1311 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001312
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001314 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 codec->subsystem_id =
1316 snd_hda_codec_read(codec, nid, 0,
1317 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001318 }
1319
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001320 /* power-up all before initialization */
1321 hda_set_power_state(codec,
1322 codec->afg ? codec->afg : codec->mfg,
1323 AC_PWRST_D0);
1324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001325 snd_hda_codec_proc_new(codec);
1326
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001327 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328
1329 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1330 codec->subsystem_id, codec->revision_id);
1331 snd_component_add(codec->bus->card, component);
1332
1333 if (codecp)
1334 *codecp = codec;
1335 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001336
1337 error:
1338 snd_hda_codec_free(codec);
1339 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001341EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001342
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343/**
1344 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1345 * @codec: the HDA codec
1346 *
1347 * Start parsing of the given codec tree and (re-)initialize the whole
1348 * patch instance.
1349 *
1350 * Returns 0 if successful or a negative error code.
1351 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352int snd_hda_codec_configure(struct hda_codec *codec)
1353{
1354 int err;
1355
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001356 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001357 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001358 err = get_codec_name(codec);
1359 if (err < 0)
1360 return err;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai82467612007-07-27 19:15:54 +02001363 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001365 goto patched;
1366 }
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (codec->preset && codec->preset->patch) {
1368 err = codec->preset->patch(codec);
1369 goto patched;
1370 }
1371
1372 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001373 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001374 if (err < 0)
1375 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001376
1377 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001378 if (!err && codec->patch_ops.unsol_event)
1379 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001380 /* audio codec should override the mixer name */
1381 if (!err && (codec->afg || !*codec->bus->card->mixername))
1382 snprintf(codec->bus->card->mixername,
1383 sizeof(codec->bus->card->mixername),
1384 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001387EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389/**
1390 * snd_hda_codec_setup_stream - set up the codec for streaming
1391 * @codec: the CODEC to set up
1392 * @nid: the NID to set up
1393 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1394 * @channel_id: channel id to pass, zero based.
1395 * @format: stream format.
1396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001397void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1398 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int channel_id, int format)
1400{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001401 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001402 struct hda_cvt_setup *p;
1403 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001404 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001405 int i;
1406
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001408 return;
1409
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 snd_printdd("hda_codec_setup_stream: "
1411 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001413 p = get_hda_cvt_setup(codec, nid);
1414 if (!p)
1415 return;
1416 /* update the stream-id if changed */
1417 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1418 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1419 newval = (stream_tag << 4) | channel_id;
1420 if (oldval != newval)
1421 snd_hda_codec_write(codec, nid, 0,
1422 AC_VERB_SET_CHANNEL_STREAMID,
1423 newval);
1424 p->stream_tag = stream_tag;
1425 p->channel_id = channel_id;
1426 }
1427 /* update the format-id if changed */
1428 if (p->format_id != format) {
1429 oldval = snd_hda_codec_read(codec, nid, 0,
1430 AC_VERB_GET_STREAM_FORMAT, 0);
1431 if (oldval != format) {
1432 msleep(1);
1433 snd_hda_codec_write(codec, nid, 0,
1434 AC_VERB_SET_STREAM_FORMAT,
1435 format);
1436 }
1437 p->format_id = format;
1438 }
1439 p->active = 1;
1440 p->dirty = 0;
1441
1442 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001443 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001444 list_for_each_entry(c, &codec->bus->codec_list, list) {
1445 for (i = 0; i < c->cvt_setups.used; i++) {
1446 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001447 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001448 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001449 p->dirty = 1;
1450 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001453EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Takashi Iwaif0cea792010-08-13 11:56:53 +02001455static void really_cleanup_stream(struct hda_codec *codec,
1456 struct hda_cvt_setup *q);
1457
Takashi Iwaid5191e52009-11-16 14:58:17 +01001458/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001459 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001460 * @codec: the CODEC to clean up
1461 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001462 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1465 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001466{
Takashi Iwaieb541332010-08-06 13:48:11 +02001467 struct hda_cvt_setup *p;
1468
Takashi Iwai888afa12008-03-18 09:57:50 +01001469 if (!nid)
1470 return;
1471
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001472 if (codec->no_sticky_stream)
1473 do_now = 1;
1474
Takashi Iwai888afa12008-03-18 09:57:50 +01001475 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001476 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001477 if (p) {
1478 /* here we just clear the active flag when do_now isn't set;
1479 * actual clean-ups will be done later in
1480 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1481 */
1482 if (do_now)
1483 really_cleanup_stream(codec, p);
1484 else
1485 p->active = 0;
1486 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001487}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001489
Takashi Iwaieb541332010-08-06 13:48:11 +02001490static void really_cleanup_stream(struct hda_codec *codec,
1491 struct hda_cvt_setup *q)
1492{
1493 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001494 if (q->stream_tag || q->channel_id)
1495 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1496 if (q->format_id)
1497 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1498);
Takashi Iwaieb541332010-08-06 13:48:11 +02001499 memset(q, 0, sizeof(*q));
1500 q->nid = nid;
1501}
1502
1503/* clean up the all conflicting obsolete streams */
1504static void purify_inactive_streams(struct hda_codec *codec)
1505{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001506 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001507 int i;
1508
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001509 list_for_each_entry(c, &codec->bus->codec_list, list) {
1510 for (i = 0; i < c->cvt_setups.used; i++) {
1511 struct hda_cvt_setup *p;
1512 p = snd_array_elem(&c->cvt_setups, i);
1513 if (p->dirty)
1514 really_cleanup_stream(c, p);
1515 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001516 }
1517}
1518
Takashi Iwai2a439522011-07-26 09:52:50 +02001519#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001520/* clean up all streams; called from suspend */
1521static void hda_cleanup_all_streams(struct hda_codec *codec)
1522{
1523 int i;
1524
1525 for (i = 0; i < codec->cvt_setups.used; i++) {
1526 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1527 if (p->stream_tag)
1528 really_cleanup_stream(codec, p);
1529 }
1530}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001531#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/*
1534 * amp access functions
1535 */
1536
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001537/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001538#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001539#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001540#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1541#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001543#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001546static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001547 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwai01751f52007-08-10 16:59:39 +02001549 memset(cache, 0, sizeof(*cache));
1550 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001551 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001552}
1553
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001554static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001555{
Takashi Iwai603c4012008-07-30 15:01:44 +02001556 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001560static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Takashi Iwai01751f52007-08-10 16:59:39 +02001562 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1563 u16 cur = cache->hash[idx];
1564 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001567 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (info->key == key)
1569 return info;
1570 cur = info->next;
1571 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001572 return NULL;
1573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001575/* query the hash. allocate an entry if not found. */
1576static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1577 u32 key)
1578{
1579 struct hda_cache_head *info = get_hash(cache, key);
1580 if (!info) {
1581 u16 idx, cur;
1582 /* add a new hash entry */
1583 info = snd_array_new(&cache->buf);
1584 if (!info)
1585 return NULL;
1586 cur = snd_array_index(&cache->buf, info);
1587 info->key = key;
1588 info->val = 0;
1589 idx = key % (u16)ARRAY_SIZE(cache->hash);
1590 info->next = cache->hash[idx];
1591 cache->hash[idx] = cur;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return info;
1594}
1595
Takashi Iwai01751f52007-08-10 16:59:39 +02001596/* query and allocate an amp hash entry */
1597static inline struct hda_amp_info *
1598get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1599{
1600 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1601}
1602
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001603/* overwrite the value with the key in the caps hash */
1604static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1605{
1606 struct hda_amp_info *info;
1607
1608 mutex_lock(&codec->hash_mutex);
1609 info = get_alloc_amp_hash(codec, key);
1610 if (!info) {
1611 mutex_unlock(&codec->hash_mutex);
1612 return -EINVAL;
1613 }
1614 info->amp_caps = val;
1615 info->head.val |= INFO_AMP_CAPS;
1616 mutex_unlock(&codec->hash_mutex);
1617 return 0;
1618}
1619
1620/* query the value from the caps hash; if not found, fetch the current
1621 * value from the given function and store in the hash
1622 */
1623static unsigned int
1624query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1625 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1626{
1627 struct hda_amp_info *info;
1628 unsigned int val;
1629
1630 mutex_lock(&codec->hash_mutex);
1631 info = get_alloc_amp_hash(codec, key);
1632 if (!info) {
1633 mutex_unlock(&codec->hash_mutex);
1634 return 0;
1635 }
1636 if (!(info->head.val & INFO_AMP_CAPS)) {
1637 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1638 val = func(codec, nid, dir);
1639 write_caps_hash(codec, key, val);
1640 } else {
1641 val = info->amp_caps;
1642 mutex_unlock(&codec->hash_mutex);
1643 }
1644 return val;
1645}
1646
1647static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1648 int direction)
1649{
1650 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1651 nid = codec->afg;
1652 return snd_hda_param_read(codec, nid,
1653 direction == HDA_OUTPUT ?
1654 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1655}
1656
Takashi Iwaid5191e52009-11-16 14:58:17 +01001657/**
1658 * query_amp_caps - query AMP capabilities
1659 * @codec: the HD-auio codec
1660 * @nid: the NID to query
1661 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1662 *
1663 * Query AMP capabilities for the given widget and direction.
1664 * Returns the obtained capability bits.
1665 *
1666 * When cap bits have been already read, this doesn't read again but
1667 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001669u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001671 return query_caps_hash(codec, nid, direction,
1672 HDA_HASH_KEY(nid, direction, 0),
1673 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001675EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Takashi Iwaid5191e52009-11-16 14:58:17 +01001677/**
1678 * snd_hda_override_amp_caps - Override the AMP capabilities
1679 * @codec: the CODEC to clean up
1680 * @nid: the NID to clean up
1681 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1682 * @caps: the capability bits to set
1683 *
1684 * Override the cached AMP caps bits value by the given one.
1685 * This function is useful if the driver needs to adjust the AMP ranges,
1686 * e.g. limit to 0dB, etc.
1687 *
1688 * Returns zero if successful or a negative error code.
1689 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001690int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1691 unsigned int caps)
1692{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001693 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001694}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001695EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001696
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001697static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1698 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001699{
1700 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1701}
1702
Takashi Iwaid5191e52009-11-16 14:58:17 +01001703/**
1704 * snd_hda_query_pin_caps - Query PIN capabilities
1705 * @codec: the HD-auio codec
1706 * @nid: the NID to query
1707 *
1708 * Query PIN capabilities for the given widget.
1709 * Returns the obtained capability bits.
1710 *
1711 * When cap bits have been already read, this doesn't read again but
1712 * returns the cached value.
1713 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001714u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1715{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001716 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001717 read_pin_cap);
1718}
Takashi Iwai1327a322009-03-23 13:07:47 +01001719EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1720
Wu Fengguang864f92b2009-11-18 12:38:02 +08001721/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001722 * snd_hda_override_pin_caps - Override the pin capabilities
1723 * @codec: the CODEC
1724 * @nid: the NID to override
1725 * @caps: the capability bits to set
1726 *
1727 * Override the cached PIN capabilitiy bits value by the given one.
1728 *
1729 * Returns zero if successful or a negative error code.
1730 */
1731int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1732 unsigned int caps)
1733{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001734 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001735}
1736EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1737
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001738/* read or sync the hash value with the current value;
1739 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001741static struct hda_amp_info *
1742update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1743 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001745 struct hda_amp_info *info;
1746 unsigned int parm, val = 0;
1747 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001749 retry:
1750 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1751 if (!info)
1752 return NULL;
1753 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1754 if (!val_read) {
1755 mutex_unlock(&codec->hash_mutex);
1756 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1757 parm |= direction == HDA_OUTPUT ?
1758 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1759 parm |= index;
1760 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001761 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762 val &= 0xff;
1763 val_read = true;
1764 mutex_lock(&codec->hash_mutex);
1765 goto retry;
1766 }
1767 info->vol[ch] = val;
1768 info->head.val |= INFO_AMP_VOL(ch);
1769 }
1770 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001776static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001777 hda_nid_t nid, int ch, int direction, int index,
1778 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 u32 parm;
1781
1782 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1783 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1784 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001785 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1786 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1787 ; /* set the zero value as a fake mute */
1788 else
1789 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1791}
1792
Takashi Iwaid5191e52009-11-16 14:58:17 +01001793/**
1794 * snd_hda_codec_amp_read - Read AMP value
1795 * @codec: HD-audio codec
1796 * @nid: NID to read the AMP value
1797 * @ch: channel (left=0 or right=1)
1798 * @direction: #HDA_INPUT or #HDA_OUTPUT
1799 * @index: the index value (only for input direction)
1800 *
1801 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Takashi Iwai834be882006-03-01 14:16:17 +01001803int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1804 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001806 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001807 unsigned int val = 0;
1808
1809 mutex_lock(&codec->hash_mutex);
1810 info = update_amp_hash(codec, nid, ch, direction, index);
1811 if (info)
1812 val = info->vol[ch];
1813 mutex_unlock(&codec->hash_mutex);
1814 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001816EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Takashi Iwaid5191e52009-11-16 14:58:17 +01001818/**
1819 * snd_hda_codec_amp_update - update the AMP value
1820 * @codec: HD-audio codec
1821 * @nid: NID to read the AMP value
1822 * @ch: channel (left=0 or right=1)
1823 * @direction: #HDA_INPUT or #HDA_OUTPUT
1824 * @idx: the index value (only for input direction)
1825 * @mask: bit mask to set
1826 * @val: the bits value to set
1827 *
1828 * Update the AMP value with a bit mask.
1829 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001830 */
Takashi Iwai834be882006-03-01 14:16:17 +01001831int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1832 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001834 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001835
Takashi Iwai467126462010-03-29 09:19:38 +02001836 if (snd_BUG_ON(mask & ~0xff))
1837 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001838 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839
1840 mutex_lock(&codec->hash_mutex);
1841 info = update_amp_hash(codec, nid, ch, direction, idx);
1842 if (!info) {
1843 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001845 }
1846 val |= info->vol[ch] & ~mask;
1847 if (info->vol[ch] == val) {
1848 mutex_unlock(&codec->hash_mutex);
1849 return 0;
1850 }
1851 info->vol[ch] = val;
1852 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001853 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return 1;
1855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001856EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Takashi Iwaid5191e52009-11-16 14:58:17 +01001858/**
1859 * snd_hda_codec_amp_stereo - update the AMP stereo values
1860 * @codec: HD-audio codec
1861 * @nid: NID to read the AMP value
1862 * @direction: #HDA_INPUT or #HDA_OUTPUT
1863 * @idx: the index value (only for input direction)
1864 * @mask: bit mask to set
1865 * @val: the bits value to set
1866 *
1867 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1868 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001869 */
1870int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1871 int direction, int idx, int mask, int val)
1872{
1873 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001874
1875 if (snd_BUG_ON(mask & ~0xff))
1876 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001877 for (ch = 0; ch < 2; ch++)
1878 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1879 idx, mask, val);
1880 return ret;
1881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001882EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001883
Takashi Iwai2a439522011-07-26 09:52:50 +02001884#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001885/**
1886 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1887 * @codec: HD-audio codec
1888 *
1889 * Resume the all amp commands from the cache.
1890 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001891void snd_hda_codec_resume_amp(struct hda_codec *codec)
1892{
Takashi Iwai603c4012008-07-30 15:01:44 +02001893 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001894 int i;
1895
Takashi Iwai603c4012008-07-30 15:01:44 +02001896 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001897 u32 key = buffer->head.key;
1898 hda_nid_t nid;
1899 unsigned int idx, dir, ch;
1900 if (!key)
1901 continue;
1902 nid = key & 0xff;
1903 idx = (key >> 16) & 0xff;
1904 dir = (key >> 24) & 0xff;
1905 for (ch = 0; ch < 2; ch++) {
1906 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1907 continue;
1908 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1909 buffer->vol[ch]);
1910 }
1911 }
1912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001913EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001914#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001916static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1917 unsigned int ofs)
1918{
1919 u32 caps = query_amp_caps(codec, nid, dir);
1920 /* get num steps */
1921 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1922 if (ofs < caps)
1923 caps -= ofs;
1924 return caps;
1925}
1926
Takashi Iwaid5191e52009-11-16 14:58:17 +01001927/**
1928 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1929 *
1930 * The control element is supposed to have the private_value field
1931 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1932 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001933int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1937 u16 nid = get_amp_nid(kcontrol);
1938 u8 chs = get_amp_channels(kcontrol);
1939 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001940 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001942 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1943 uinfo->count = chs == 3 ? 2 : 1;
1944 uinfo->value.integer.min = 0;
1945 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1946 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001947 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001948 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1949 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return -EINVAL;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001954EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001956
1957static inline unsigned int
1958read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1959 int ch, int dir, int idx, unsigned int ofs)
1960{
1961 unsigned int val;
1962 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1963 val &= HDA_AMP_VOLMASK;
1964 if (val >= ofs)
1965 val -= ofs;
1966 else
1967 val = 0;
1968 return val;
1969}
1970
1971static inline int
1972update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1973 int ch, int dir, int idx, unsigned int ofs,
1974 unsigned int val)
1975{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001976 unsigned int maxval;
1977
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001978 if (val > 0)
1979 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001980 /* ofs = 0: raw max value */
1981 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001982 if (val > maxval)
1983 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001984 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1985 HDA_AMP_VOLMASK, val);
1986}
1987
Takashi Iwaid5191e52009-11-16 14:58:17 +01001988/**
1989 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1990 *
1991 * The control element is supposed to have the private_value field
1992 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1993 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001994int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1995 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1998 hda_nid_t nid = get_amp_nid(kcontrol);
1999 int chs = get_amp_channels(kcontrol);
2000 int dir = get_amp_direction(kcontrol);
2001 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 long *valp = ucontrol->value.integer.value;
2004
2005 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002008 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return 0;
2010}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002011EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Takashi Iwaid5191e52009-11-16 14:58:17 +01002013/**
2014 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2015 *
2016 * The control element is supposed to have the private_value field
2017 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2018 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002019int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2020 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2023 hda_nid_t nid = get_amp_nid(kcontrol);
2024 int chs = get_amp_channels(kcontrol);
2025 int dir = get_amp_direction(kcontrol);
2026 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 long *valp = ucontrol->value.integer.value;
2029 int change = 0;
2030
Takashi Iwaicb53c622007-08-10 17:21:45 +02002031 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002032 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002033 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002034 valp++;
2035 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002036 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002037 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002038 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return change;
2040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002041EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Takashi Iwaid5191e52009-11-16 14:58:17 +01002043/**
2044 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2045 *
2046 * The control element is supposed to have the private_value field
2047 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2048 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002049int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2050 unsigned int size, unsigned int __user *_tlv)
2051{
2052 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2053 hda_nid_t nid = get_amp_nid(kcontrol);
2054 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002055 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002056 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002057 u32 caps, val1, val2;
2058
2059 if (size < 4 * sizeof(unsigned int))
2060 return -ENOMEM;
2061 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002062 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2063 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002064 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002065 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002066 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002067 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002068 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002069 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2070 return -EFAULT;
2071 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2072 return -EFAULT;
2073 if (put_user(val1, _tlv + 2))
2074 return -EFAULT;
2075 if (put_user(val2, _tlv + 3))
2076 return -EFAULT;
2077 return 0;
2078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002079EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002080
Takashi Iwaid5191e52009-11-16 14:58:17 +01002081/**
2082 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2083 * @codec: HD-audio codec
2084 * @nid: NID of a reference widget
2085 * @dir: #HDA_INPUT or #HDA_OUTPUT
2086 * @tlv: TLV data to be stored, at least 4 elements
2087 *
2088 * Set (static) TLV data for a virtual master volume using the AMP caps
2089 * obtained from the reference NID.
2090 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002091 */
2092void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2093 unsigned int *tlv)
2094{
2095 u32 caps;
2096 int nums, step;
2097
2098 caps = query_amp_caps(codec, nid, dir);
2099 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2100 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2101 step = (step + 1) * 25;
2102 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2103 tlv[1] = 2 * sizeof(unsigned int);
2104 tlv[2] = -nums * step;
2105 tlv[3] = step;
2106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002107EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002108
2109/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002110static struct snd_kcontrol *
2111_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2112 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002113{
2114 struct snd_ctl_elem_id id;
2115 memset(&id, 0, sizeof(id));
2116 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002117 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002118 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2119 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002120 strcpy(id.name, name);
2121 return snd_ctl_find_id(codec->bus->card, &id);
2122}
2123
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124/**
2125 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2126 * @codec: HD-audio codec
2127 * @name: ctl id name string
2128 *
2129 * Get the control element with the given id string and IFACE_MIXER.
2130 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002131struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2132 const char *name)
2133{
2134 return _snd_hda_find_mixer_ctl(codec, name, 0);
2135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002136EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002137
Takashi Iwai1afe2062010-12-23 10:17:52 +01002138static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2139{
2140 int idx;
2141 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2142 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2143 return idx;
2144 }
2145 return -EBUSY;
2146}
2147
Takashi Iwaid5191e52009-11-16 14:58:17 +01002148/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002149 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150 * @codec: HD-audio codec
2151 * @nid: corresponding NID (optional)
2152 * @kctl: the control element to assign
2153 *
2154 * Add the given control element to an array inside the codec instance.
2155 * All control elements belonging to a codec are supposed to be added
2156 * by this function so that a proper clean-up works at the free or
2157 * reconfiguration time.
2158 *
2159 * If non-zero @nid is passed, the NID is assigned to the control element.
2160 * The assignment is shown in the codec proc file.
2161 *
2162 * snd_hda_ctl_add() checks the control subdev id field whether
2163 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002164 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2165 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002166 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002167int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2168 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002169{
2170 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002171 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002172 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002173
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002174 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002175 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002176 if (nid == 0)
2177 nid = get_amp_nid_(kctl->private_value);
2178 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002179 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2180 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002181 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002182 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002183 err = snd_ctl_add(codec->bus->card, kctl);
2184 if (err < 0)
2185 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002186 item = snd_array_new(&codec->mixers);
2187 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002188 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002189 item->kctl = kctl;
2190 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002191 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002192 return 0;
2193}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002194EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002197 * snd_hda_add_nid - Assign a NID to a control element
2198 * @codec: HD-audio codec
2199 * @nid: corresponding NID (optional)
2200 * @kctl: the control element to assign
2201 * @index: index to kctl
2202 *
2203 * Add the given control element to an array inside the codec instance.
2204 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2205 * NID:KCTL mapping - for example "Capture Source" selector.
2206 */
2207int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2208 unsigned int index, hda_nid_t nid)
2209{
2210 struct hda_nid_item *item;
2211
2212 if (nid > 0) {
2213 item = snd_array_new(&codec->nids);
2214 if (!item)
2215 return -ENOMEM;
2216 item->kctl = kctl;
2217 item->index = index;
2218 item->nid = nid;
2219 return 0;
2220 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002221 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2222 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002223 return -EINVAL;
2224}
2225EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2226
2227/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002228 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2229 * @codec: HD-audio codec
2230 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002231void snd_hda_ctls_clear(struct hda_codec *codec)
2232{
2233 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002234 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002235 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002236 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002237 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002238 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002239}
2240
Takashi Iwaia65d6292009-02-23 16:57:04 +01002241/* pseudo device locking
2242 * toggle card->shutdown to allow/disallow the device access (as a hack)
2243 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002244int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002245{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002246 struct snd_card *card = bus->card;
2247 struct hda_codec *codec;
2248
Takashi Iwaia65d6292009-02-23 16:57:04 +01002249 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002250 if (card->shutdown)
2251 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002252 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002253 if (!list_empty(&card->ctl_files))
2254 goto err_clear;
2255
2256 list_for_each_entry(codec, &bus->codec_list, list) {
2257 int pcm;
2258 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2259 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2260 if (!cpcm->pcm)
2261 continue;
2262 if (cpcm->pcm->streams[0].substream_opened ||
2263 cpcm->pcm->streams[1].substream_opened)
2264 goto err_clear;
2265 }
2266 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002267 spin_unlock(&card->files_lock);
2268 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002269
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002270 err_clear:
2271 card->shutdown = 0;
2272 err_unlock:
2273 spin_unlock(&card->files_lock);
2274 return -EINVAL;
2275}
2276EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2277
2278void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002279{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002280 struct snd_card *card = bus->card;
2281
2282 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002283 spin_lock(&card->files_lock);
2284 card->shutdown = 0;
2285 spin_unlock(&card->files_lock);
2286}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002287EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002288
Takashi Iwaid5191e52009-11-16 14:58:17 +01002289/**
2290 * snd_hda_codec_reset - Clear all objects assigned to the codec
2291 * @codec: HD-audio codec
2292 *
2293 * This frees the all PCM and control elements assigned to the codec, and
2294 * clears the caches and restores the pin default configurations.
2295 *
2296 * When a device is being used, it returns -EBSY. If successfully freed,
2297 * returns zero.
2298 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002299int snd_hda_codec_reset(struct hda_codec *codec)
2300{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002301 struct hda_bus *bus = codec->bus;
2302 struct snd_card *card = bus->card;
2303 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002304
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002305 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002306 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002307
2308 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002309
2310#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002311 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002312 codec->power_on = 0;
2313 codec->power_transition = 0;
2314 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002315 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002316#endif
2317 snd_hda_ctls_clear(codec);
2318 /* relase PCMs */
2319 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002320 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002321 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002322 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002324 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002325 }
2326 if (codec->patch_ops.free)
2327 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002328 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002329 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002330 codec->spec = NULL;
2331 free_hda_cache(&codec->amp_cache);
2332 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002333 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2334 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002335 /* free only driver_pins so that init_pins + user_pins are restored */
2336 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002337 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002338 codec->num_pcms = 0;
2339 codec->pcm_info = NULL;
2340 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002341 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2342 codec->slave_dig_outs = NULL;
2343 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002344 module_put(codec->owner);
2345 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002346
2347 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002348 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002349 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002350}
2351
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002352typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2353
2354/* apply the function to all matching slave ctls in the mixer list */
2355static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002356 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002357{
2358 struct hda_nid_item *items;
2359 const char * const *s;
2360 int i, err;
2361
2362 items = codec->mixers.list;
2363 for (i = 0; i < codec->mixers.used; i++) {
2364 struct snd_kcontrol *sctl = items[i].kctl;
2365 if (!sctl || !sctl->id.name ||
2366 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2367 continue;
2368 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002369 char tmpname[sizeof(sctl->id.name)];
2370 const char *name = *s;
2371 if (suffix) {
2372 snprintf(tmpname, sizeof(tmpname), "%s %s",
2373 name, suffix);
2374 name = tmpname;
2375 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002376 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002377 err = func(data, sctl);
2378 if (err)
2379 return err;
2380 break;
2381 }
2382 }
2383 }
2384 return 0;
2385}
2386
2387static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2388{
2389 return 1;
2390}
2391
Takashi Iwai18478e82012-03-09 17:51:10 +01002392/* guess the value corresponding to 0dB */
2393static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2394{
2395 int _tlv[4];
2396 const int *tlv = NULL;
2397 int val = -1;
2398
2399 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2400 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2401 mm_segment_t fs = get_fs();
2402 set_fs(get_ds());
2403 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2404 tlv = _tlv;
2405 set_fs(fs);
2406 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2407 tlv = kctl->tlv.p;
2408 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2409 val = -tlv[2] / tlv[3];
2410 return val;
2411}
2412
2413/* call kctl->put with the given value(s) */
2414static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2415{
2416 struct snd_ctl_elem_value *ucontrol;
2417 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2418 if (!ucontrol)
2419 return -ENOMEM;
2420 ucontrol->value.integer.value[0] = val;
2421 ucontrol->value.integer.value[1] = val;
2422 kctl->put(kctl, ucontrol);
2423 kfree(ucontrol);
2424 return 0;
2425}
2426
2427/* initialize the slave volume with 0dB */
2428static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2429{
2430 int offset = get_kctl_0dB_offset(slave);
2431 if (offset > 0)
2432 put_kctl_with_value(slave, offset);
2433 return 0;
2434}
2435
2436/* unmute the slave */
2437static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2438{
2439 return put_kctl_with_value(slave, 1);
2440}
2441
Takashi Iwaid5191e52009-11-16 14:58:17 +01002442/**
2443 * snd_hda_add_vmaster - create a virtual master control and add slaves
2444 * @codec: HD-audio codec
2445 * @name: vmaster control name
2446 * @tlv: TLV data (optional)
2447 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002448 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002449 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002450 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002451 *
2452 * Create a virtual master control with the given name. The TLV data
2453 * must be either NULL or a valid data.
2454 *
2455 * @slaves is a NULL-terminated array of strings, each of which is a
2456 * slave control name. All controls with these names are assigned to
2457 * the new virtual master control.
2458 *
2459 * This function returns zero if successful or a negative error code.
2460 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002461int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002462 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002463 const char *suffix, bool init_slave_vol,
2464 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002465{
2466 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002467 int err;
2468
Takashi Iwai29e58532012-03-12 12:25:03 +01002469 if (ctl_ret)
2470 *ctl_ret = NULL;
2471
Takashi Iwai9322ca52012-02-03 14:28:01 +01002472 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002473 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002474 snd_printdd("No slave found for %s\n", name);
2475 return 0;
2476 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002477 kctl = snd_ctl_make_virtual_master(name, tlv);
2478 if (!kctl)
2479 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002480 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002481 if (err < 0)
2482 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002483
Takashi Iwai9322ca52012-02-03 14:28:01 +01002484 err = map_slaves(codec, slaves, suffix,
2485 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002486 if (err < 0)
2487 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002488
2489 /* init with master mute & zero volume */
2490 put_kctl_with_value(kctl, 0);
2491 if (init_slave_vol)
2492 map_slaves(codec, slaves, suffix,
2493 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2494
Takashi Iwai29e58532012-03-12 12:25:03 +01002495 if (ctl_ret)
2496 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002497 return 0;
2498}
Takashi Iwai18478e82012-03-09 17:51:10 +01002499EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002500
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002501/*
2502 * mute-LED control using vmaster
2503 */
2504static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_info *uinfo)
2506{
2507 static const char * const texts[] = {
2508 "Off", "On", "Follow Master"
2509 };
2510 unsigned int index;
2511
2512 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2513 uinfo->count = 1;
2514 uinfo->value.enumerated.items = 3;
2515 index = uinfo->value.enumerated.item;
2516 if (index >= 3)
2517 index = 2;
2518 strcpy(uinfo->value.enumerated.name, texts[index]);
2519 return 0;
2520}
2521
2522static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
2525 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2526 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2527 return 0;
2528}
2529
2530static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2531 struct snd_ctl_elem_value *ucontrol)
2532{
2533 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2534 unsigned int old_mode = hook->mute_mode;
2535
2536 hook->mute_mode = ucontrol->value.enumerated.item[0];
2537 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2538 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2539 if (old_mode == hook->mute_mode)
2540 return 0;
2541 snd_hda_sync_vmaster_hook(hook);
2542 return 1;
2543}
2544
2545static struct snd_kcontrol_new vmaster_mute_mode = {
2546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2547 .name = "Mute-LED Mode",
2548 .info = vmaster_mute_mode_info,
2549 .get = vmaster_mute_mode_get,
2550 .put = vmaster_mute_mode_put,
2551};
2552
2553/*
2554 * Add a mute-LED hook with the given vmaster switch kctl
2555 * "Mute-LED Mode" control is automatically created and associated with
2556 * the given hook.
2557 */
2558int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002559 struct hda_vmaster_mute_hook *hook,
2560 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002561{
2562 struct snd_kcontrol *kctl;
2563
2564 if (!hook->hook || !hook->sw_kctl)
2565 return 0;
2566 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2567 hook->codec = codec;
2568 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002569 if (!expose_enum_ctl)
2570 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002571 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2572 if (!kctl)
2573 return -ENOMEM;
2574 return snd_hda_ctl_add(codec, 0, kctl);
2575}
2576EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2577
2578/*
2579 * Call the hook with the current value for synchronization
2580 * Should be called in init callback
2581 */
2582void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2583{
2584 if (!hook->hook || !hook->codec)
2585 return;
2586 switch (hook->mute_mode) {
2587 case HDA_VMUTE_FOLLOW_MASTER:
2588 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2589 break;
2590 default:
2591 hook->hook(hook->codec, hook->mute_mode);
2592 break;
2593 }
2594}
2595EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2596
2597
Takashi Iwaid5191e52009-11-16 14:58:17 +01002598/**
2599 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2600 *
2601 * The control element is supposed to have the private_value field
2602 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2603 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002604int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 int chs = get_amp_channels(kcontrol);
2608
2609 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2610 uinfo->count = chs == 3 ? 2 : 1;
2611 uinfo->value.integer.min = 0;
2612 uinfo->value.integer.max = 1;
2613 return 0;
2614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002615EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Takashi Iwaid5191e52009-11-16 14:58:17 +01002617/**
2618 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2619 *
2620 * The control element is supposed to have the private_value field
2621 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2622 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002623int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2627 hda_nid_t nid = get_amp_nid(kcontrol);
2628 int chs = get_amp_channels(kcontrol);
2629 int dir = get_amp_direction(kcontrol);
2630 int idx = get_amp_index(kcontrol);
2631 long *valp = ucontrol->value.integer.value;
2632
2633 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002634 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002635 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002637 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002638 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return 0;
2640}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002641EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Takashi Iwaid5191e52009-11-16 14:58:17 +01002643/**
2644 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2645 *
2646 * The control element is supposed to have the private_value field
2647 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2648 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002649int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2653 hda_nid_t nid = get_amp_nid(kcontrol);
2654 int chs = get_amp_channels(kcontrol);
2655 int dir = get_amp_direction(kcontrol);
2656 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 long *valp = ucontrol->value.integer.value;
2658 int change = 0;
2659
Takashi Iwaicb53c622007-08-10 17:21:45 +02002660 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002661 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002662 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002663 HDA_AMP_MUTE,
2664 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002665 valp++;
2666 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002667 if (chs & 2)
2668 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002669 HDA_AMP_MUTE,
2670 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002671 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002672 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return change;
2674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002675EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Takashi Iwai67d634c2009-11-16 15:35:59 +01002677#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002678/**
2679 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2680 *
2681 * This function calls snd_hda_enable_beep_device(), which behaves differently
2682 * depending on beep_mode option.
2683 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002684int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 long *valp = ucontrol->value.integer.value;
2689
2690 snd_hda_enable_beep_device(codec, *valp);
2691 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2692}
2693EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002694#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696/*
Takashi Iwai985be542005-11-02 18:26:49 +01002697 * bound volume controls
2698 *
2699 * bind multiple volumes (# indices, from 0)
2700 */
2701
2702#define AMP_VAL_IDX_SHIFT 19
2703#define AMP_VAL_IDX_MASK (0x0f<<19)
2704
Takashi Iwaid5191e52009-11-16 14:58:17 +01002705/**
2706 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2707 *
2708 * The control element is supposed to have the private_value field
2709 * set up via HDA_BIND_MUTE*() macros.
2710 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002711int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002713{
2714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2715 unsigned long pval;
2716 int err;
2717
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002718 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002719 pval = kcontrol->private_value;
2720 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2721 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2722 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002723 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002724 return err;
2725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002726EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002727
Takashi Iwaid5191e52009-11-16 14:58:17 +01002728/**
2729 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2730 *
2731 * The control element is supposed to have the private_value field
2732 * set up via HDA_BIND_MUTE*() macros.
2733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002734int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002736{
2737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2738 unsigned long pval;
2739 int i, indices, err = 0, change = 0;
2740
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002741 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002742 pval = kcontrol->private_value;
2743 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2744 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002745 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2746 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002747 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2748 if (err < 0)
2749 break;
2750 change |= err;
2751 }
2752 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002753 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002754 return err < 0 ? err : change;
2755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002756EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002757
Takashi Iwaid5191e52009-11-16 14:58:17 +01002758/**
2759 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2760 *
2761 * The control element is supposed to have the private_value field
2762 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002763 */
2764int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_info *uinfo)
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768 struct hda_bind_ctls *c;
2769 int err;
2770
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002771 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002772 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002773 kcontrol->private_value = *c->values;
2774 err = c->ops->info(kcontrol, uinfo);
2775 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002776 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 return err;
2778}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002779EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002780
Takashi Iwaid5191e52009-11-16 14:58:17 +01002781/**
2782 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2783 *
2784 * The control element is supposed to have the private_value field
2785 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2786 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002787int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2788 struct snd_ctl_elem_value *ucontrol)
2789{
2790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2791 struct hda_bind_ctls *c;
2792 int err;
2793
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002794 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002795 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002796 kcontrol->private_value = *c->values;
2797 err = c->ops->get(kcontrol, ucontrol);
2798 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002799 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002800 return err;
2801}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002802EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002803
Takashi Iwaid5191e52009-11-16 14:58:17 +01002804/**
2805 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2806 *
2807 * The control element is supposed to have the private_value field
2808 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2809 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002810int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2812{
2813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2814 struct hda_bind_ctls *c;
2815 unsigned long *vals;
2816 int err = 0, change = 0;
2817
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002818 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002819 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002820 for (vals = c->values; *vals; vals++) {
2821 kcontrol->private_value = *vals;
2822 err = c->ops->put(kcontrol, ucontrol);
2823 if (err < 0)
2824 break;
2825 change |= err;
2826 }
2827 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002828 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002829 return err < 0 ? err : change;
2830}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002831EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002832
Takashi Iwaid5191e52009-11-16 14:58:17 +01002833/**
2834 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2835 *
2836 * The control element is supposed to have the private_value field
2837 * set up via HDA_BIND_VOL() macro.
2838 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002839int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2840 unsigned int size, unsigned int __user *tlv)
2841{
2842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2843 struct hda_bind_ctls *c;
2844 int err;
2845
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002846 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002847 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002848 kcontrol->private_value = *c->values;
2849 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2850 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002851 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002852 return err;
2853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002854EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002855
2856struct hda_ctl_ops snd_hda_bind_vol = {
2857 .info = snd_hda_mixer_amp_volume_info,
2858 .get = snd_hda_mixer_amp_volume_get,
2859 .put = snd_hda_mixer_amp_volume_put,
2860 .tlv = snd_hda_mixer_amp_tlv
2861};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002862EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002863
2864struct hda_ctl_ops snd_hda_bind_sw = {
2865 .info = snd_hda_mixer_amp_switch_info,
2866 .get = snd_hda_mixer_amp_switch_get,
2867 .put = snd_hda_mixer_amp_switch_put,
2868 .tlv = snd_hda_mixer_amp_tlv
2869};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002870EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002871
2872/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * SPDIF out controls
2874 */
2875
Takashi Iwai0ba21762007-04-16 11:29:14 +02002876static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
2879 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2880 uinfo->count = 1;
2881 return 0;
2882}
2883
Takashi Iwai0ba21762007-04-16 11:29:14 +02002884static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2885 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2888 IEC958_AES0_NONAUDIO |
2889 IEC958_AES0_CON_EMPHASIS_5015 |
2890 IEC958_AES0_CON_NOT_COPYRIGHT;
2891 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2892 IEC958_AES1_CON_ORIGINAL;
2893 return 0;
2894}
2895
Takashi Iwai0ba21762007-04-16 11:29:14 +02002896static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2900 IEC958_AES0_NONAUDIO |
2901 IEC958_AES0_PRO_EMPHASIS_5015;
2902 return 0;
2903}
2904
Takashi Iwai0ba21762007-04-16 11:29:14 +02002905static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2906 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002909 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002910 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002912 mutex_lock(&codec->spdif_mutex);
2913 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002914 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2915 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2916 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2917 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002918 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 return 0;
2921}
2922
2923/* convert from SPDIF status bits to HDA SPDIF bits
2924 * bit 0 (DigEn) is always set zero (to be filled later)
2925 */
2926static unsigned short convert_from_spdif_status(unsigned int sbits)
2927{
2928 unsigned short val = 0;
2929
2930 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002931 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002933 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2936 IEC958_AES0_PRO_EMPHASIS_5015)
2937 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2940 IEC958_AES0_CON_EMPHASIS_5015)
2941 val |= AC_DIG1_EMPHASIS;
2942 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2943 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2947 }
2948 return val;
2949}
2950
2951/* convert to SPDIF status bits from HDA SPDIF bits
2952 */
2953static unsigned int convert_to_spdif_status(unsigned short val)
2954{
2955 unsigned int sbits = 0;
2956
Takashi Iwai0ba21762007-04-16 11:29:14 +02002957 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002959 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 sbits |= IEC958_AES0_PROFESSIONAL;
2961 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002962 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2964 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002965 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002967 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002969 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2971 sbits |= val & (0x7f << 8);
2972 }
2973 return sbits;
2974}
2975
Takashi Iwai2f728532008-09-25 16:32:41 +02002976/* set digital convert verbs both for the given NID and its slaves */
2977static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2978 int verb, int val)
2979{
Takashi Iwaidda14412011-05-02 11:29:30 +02002980 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002981
Takashi Iwai9e976972008-11-25 08:17:20 +01002982 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002983 d = codec->slave_dig_outs;
2984 if (!d)
2985 return;
2986 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002987 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002988}
2989
2990static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2991 int dig1, int dig2)
2992{
2993 if (dig1 != -1)
2994 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2995 if (dig2 != -1)
2996 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2997}
2998
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
3002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003003 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003004 struct hda_spdif_out *spdif;
3005 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 unsigned short val;
3007 int change;
3008
Ingo Molnar62932df2006-01-16 16:34:20 +01003009 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003010 spdif = snd_array_elem(&codec->spdif_out, idx);
3011 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003012 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3014 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3015 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003016 val = convert_from_spdif_status(spdif->status);
3017 val |= spdif->ctls & 1;
3018 change = spdif->ctls != val;
3019 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003020 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003021 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003022 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return change;
3024}
3025
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003026#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Takashi Iwai0ba21762007-04-16 11:29:14 +02003028static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3029 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003032 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003033 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003035 mutex_lock(&codec->spdif_mutex);
3036 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003037 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003038 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return 0;
3040}
3041
Stephen Warren74b654c2011-06-01 11:14:18 -06003042static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3043 int dig1, int dig2)
3044{
3045 set_dig_out_convert(codec, nid, dig1, dig2);
3046 /* unmute amp switch (if any) */
3047 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3048 (dig1 & AC_DIG1_ENABLE))
3049 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3050 HDA_AMP_MUTE, 0);
3051}
3052
Takashi Iwai0ba21762007-04-16 11:29:14 +02003053static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3054 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003057 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003058 struct hda_spdif_out *spdif;
3059 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 unsigned short val;
3061 int change;
3062
Ingo Molnar62932df2006-01-16 16:34:20 +01003063 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 spdif = snd_array_elem(&codec->spdif_out, idx);
3065 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003066 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003068 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003069 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003070 spdif->ctls = val;
3071 if (change && nid != (u16)-1)
3072 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003073 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return change;
3075}
3076
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003077static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 {
3079 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003081 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 .info = snd_hda_spdif_mask_info,
3083 .get = snd_hda_spdif_cmask_get,
3084 },
3085 {
3086 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3087 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003088 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 .info = snd_hda_spdif_mask_info,
3090 .get = snd_hda_spdif_pmask_get,
3091 },
3092 {
3093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003094 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .info = snd_hda_spdif_mask_info,
3096 .get = snd_hda_spdif_default_get,
3097 .put = snd_hda_spdif_default_put,
3098 },
3099 {
3100 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003101 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 .info = snd_hda_spdif_out_switch_info,
3103 .get = snd_hda_spdif_out_switch_get,
3104 .put = snd_hda_spdif_out_switch_put,
3105 },
3106 { } /* end */
3107};
3108
3109/**
3110 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3111 * @codec: the HDA codec
3112 * @nid: audio out widget NID
3113 *
3114 * Creates controls related with the SPDIF output.
3115 * Called from each patch supporting the SPDIF out.
3116 *
3117 * Returns 0 if successful, or a negative error code.
3118 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003119int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3120 hda_nid_t associated_nid,
3121 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003124 struct snd_kcontrol *kctl;
3125 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003126 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003127 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Takashi Iwai1afe2062010-12-23 10:17:52 +01003129 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3130 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003131 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3132 return -EBUSY;
3133 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003134 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3136 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003137 if (!kctl)
3138 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003139 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003140 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003141 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003142 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return err;
3144 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003145 spdif->nid = cvt_nid;
3146 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003147 AC_VERB_GET_DIGI_CONVERT_1, 0);
3148 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return 0;
3150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003151EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003153/* get the hda_spdif_out entry from the given NID
3154 * call within spdif_mutex lock
3155 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003156struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3157 hda_nid_t nid)
3158{
3159 int i;
3160 for (i = 0; i < codec->spdif_out.used; i++) {
3161 struct hda_spdif_out *spdif =
3162 snd_array_elem(&codec->spdif_out, i);
3163 if (spdif->nid == nid)
3164 return spdif;
3165 }
3166 return NULL;
3167}
3168EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3169
Stephen Warren74b654c2011-06-01 11:14:18 -06003170void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3171{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003172 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003173
3174 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003175 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003176 spdif->nid = (u16)-1;
3177 mutex_unlock(&codec->spdif_mutex);
3178}
3179EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3180
3181void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3182{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003183 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003184 unsigned short val;
3185
3186 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003187 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003188 if (spdif->nid != nid) {
3189 spdif->nid = nid;
3190 val = spdif->ctls;
3191 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3192 }
3193 mutex_unlock(&codec->spdif_mutex);
3194}
3195EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003198 * SPDIF sharing with analog output
3199 */
3200static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3201 struct snd_ctl_elem_value *ucontrol)
3202{
3203 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3204 ucontrol->value.integer.value[0] = mout->share_spdif;
3205 return 0;
3206}
3207
3208static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3212 mout->share_spdif = !!ucontrol->value.integer.value[0];
3213 return 0;
3214}
3215
3216static struct snd_kcontrol_new spdif_share_sw = {
3217 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3218 .name = "IEC958 Default PCM Playback Switch",
3219 .info = snd_ctl_boolean_mono_info,
3220 .get = spdif_share_sw_get,
3221 .put = spdif_share_sw_put,
3222};
3223
Takashi Iwaid5191e52009-11-16 14:58:17 +01003224/**
3225 * snd_hda_create_spdif_share_sw - create Default PCM switch
3226 * @codec: the HDA codec
3227 * @mout: multi-out instance
3228 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003229int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3230 struct hda_multi_out *mout)
3231{
3232 if (!mout->dig_out_nid)
3233 return 0;
3234 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003235 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3236 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003237}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003238EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003239
3240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 * SPDIF input
3242 */
3243
3244#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3245
Takashi Iwai0ba21762007-04-16 11:29:14 +02003246static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
3249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3250
3251 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3252 return 0;
3253}
3254
Takashi Iwai0ba21762007-04-16 11:29:14 +02003255static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3256 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
3258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3259 hda_nid_t nid = kcontrol->private_value;
3260 unsigned int val = !!ucontrol->value.integer.value[0];
3261 int change;
3262
Ingo Molnar62932df2006-01-16 16:34:20 +01003263 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003265 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003267 snd_hda_codec_write_cache(codec, nid, 0,
3268 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003270 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return change;
3272}
3273
Takashi Iwai0ba21762007-04-16 11:29:14 +02003274static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278 hda_nid_t nid = kcontrol->private_value;
3279 unsigned short val;
3280 unsigned int sbits;
3281
Andrew Paprocki3982d172007-12-19 12:13:44 +01003282 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 sbits = convert_to_spdif_status(val);
3284 ucontrol->value.iec958.status[0] = sbits;
3285 ucontrol->value.iec958.status[1] = sbits >> 8;
3286 ucontrol->value.iec958.status[2] = sbits >> 16;
3287 ucontrol->value.iec958.status[3] = sbits >> 24;
3288 return 0;
3289}
3290
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003291static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 {
3293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003294 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 .info = snd_hda_spdif_in_switch_info,
3296 .get = snd_hda_spdif_in_switch_get,
3297 .put = snd_hda_spdif_in_switch_put,
3298 },
3299 {
3300 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3301 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003302 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 .info = snd_hda_spdif_mask_info,
3304 .get = snd_hda_spdif_in_status_get,
3305 },
3306 { } /* end */
3307};
3308
3309/**
3310 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3311 * @codec: the HDA codec
3312 * @nid: audio in widget NID
3313 *
3314 * Creates controls related with the SPDIF input.
3315 * Called from each patch supporting the SPDIF in.
3316 *
3317 * Returns 0 if successful, or a negative error code.
3318 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003319int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003322 struct snd_kcontrol *kctl;
3323 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003324 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Takashi Iwai1afe2062010-12-23 10:17:52 +01003326 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3327 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003328 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3329 return -EBUSY;
3330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3332 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003333 if (!kctl)
3334 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003336 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003337 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return err;
3339 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003340 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003341 snd_hda_codec_read(codec, nid, 0,
3342 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return 0;
3345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003346EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Takashi Iwai2a439522011-07-26 09:52:50 +02003348#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003349/*
3350 * command cache
3351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003353/* build a 32bit cache key with the widget id and the command parameter */
3354#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3355#define get_cmd_cache_nid(key) ((key) & 0xff)
3356#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3357
3358/**
3359 * snd_hda_codec_write_cache - send a single command with caching
3360 * @codec: the HDA codec
3361 * @nid: NID to send the command
3362 * @direct: direct flag
3363 * @verb: the verb to send
3364 * @parm: the parameter for the verb
3365 *
3366 * Send a single command without waiting for response.
3367 *
3368 * Returns 0 if successful, or a negative error code.
3369 */
3370int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3371 int direct, unsigned int verb, unsigned int parm)
3372{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003373 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3374 struct hda_cache_head *c;
3375 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003376
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003377 if (err < 0)
3378 return err;
3379 /* parm may contain the verb stuff for get/set amp */
3380 verb = verb | (parm >> 8);
3381 parm &= 0xff;
3382 key = build_cmd_cache_key(nid, verb);
3383 mutex_lock(&codec->bus->cmd_mutex);
3384 c = get_alloc_hash(&codec->cmd_cache, key);
3385 if (c)
3386 c->val = parm;
3387 mutex_unlock(&codec->bus->cmd_mutex);
3388 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003389}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003390EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003391
Takashi Iwaid5191e52009-11-16 14:58:17 +01003392/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003393 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3394 * @codec: the HDA codec
3395 * @nid: NID to send the command
3396 * @direct: direct flag
3397 * @verb: the verb to send
3398 * @parm: the parameter for the verb
3399 *
3400 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3401 * command if the parameter is already identical with the cached value.
3402 * If not, it sends the command and refreshes the cache.
3403 *
3404 * Returns 0 if successful, or a negative error code.
3405 */
3406int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3407 int direct, unsigned int verb, unsigned int parm)
3408{
3409 struct hda_cache_head *c;
3410 u32 key;
3411
3412 /* parm may contain the verb stuff for get/set amp */
3413 verb = verb | (parm >> 8);
3414 parm &= 0xff;
3415 key = build_cmd_cache_key(nid, verb);
3416 mutex_lock(&codec->bus->cmd_mutex);
3417 c = get_hash(&codec->cmd_cache, key);
3418 if (c && c->val == parm) {
3419 mutex_unlock(&codec->bus->cmd_mutex);
3420 return 0;
3421 }
3422 mutex_unlock(&codec->bus->cmd_mutex);
3423 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3424}
3425EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3426
3427/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003428 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3429 * @codec: HD-audio codec
3430 *
3431 * Execute all verbs recorded in the command caches to resume.
3432 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003433void snd_hda_codec_resume_cache(struct hda_codec *codec)
3434{
Takashi Iwai603c4012008-07-30 15:01:44 +02003435 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003436 int i;
3437
Takashi Iwai603c4012008-07-30 15:01:44 +02003438 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003439 u32 key = buffer->key;
3440 if (!key)
3441 continue;
3442 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3443 get_cmd_cache_cmd(key), buffer->val);
3444 }
3445}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003446EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003447
3448/**
3449 * snd_hda_sequence_write_cache - sequence writes with caching
3450 * @codec: the HDA codec
3451 * @seq: VERB array to send
3452 *
3453 * Send the commands sequentially from the given array.
3454 * Thte commands are recorded on cache for power-save and resume.
3455 * The array must be terminated with NID=0.
3456 */
3457void snd_hda_sequence_write_cache(struct hda_codec *codec,
3458 const struct hda_verb *seq)
3459{
3460 for (; seq->nid; seq++)
3461 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3462 seq->param);
3463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003464EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003465#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003466
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003467void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3468 unsigned int power_state,
3469 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003470{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003471 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003472 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003473
Takashi Iwaicb53c622007-08-10 17:21:45 +02003474 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003475 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003476 if (!(wcaps & AC_WCAP_POWER))
3477 continue;
3478 /* don't power down the widget if it controls eapd and
3479 * EAPD_BTLENABLE is set.
3480 */
3481 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3482 get_wcaps_type(wcaps) == AC_WID_PIN &&
3483 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3484 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003485 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003486 if (eapd & 0x02)
3487 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003488 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003489 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3490 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003491 }
3492
Takashi Iwaicb53c622007-08-10 17:21:45 +02003493 if (power_state == AC_PWRST_D0) {
3494 unsigned long end_time;
3495 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003496 /* wait until the codec reachs to D0 */
3497 end_time = jiffies + msecs_to_jiffies(500);
3498 do {
3499 state = snd_hda_codec_read(codec, fg, 0,
3500 AC_VERB_GET_POWER_STATE, 0);
3501 if (state == power_state)
3502 break;
3503 msleep(1);
3504 } while (time_after_eq(end_time, jiffies));
3505 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003506}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003507EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3508
3509/*
3510 * set power state of the codec
3511 */
3512static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3513 unsigned int power_state)
3514{
3515 if (codec->patch_ops.set_power_state) {
3516 codec->patch_ops.set_power_state(codec, fg, power_state);
3517 return;
3518 }
3519
3520 /* this delay seems necessary to avoid click noise at power-down */
3521 if (power_state == AC_PWRST_D3)
3522 msleep(100);
3523 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3524 power_state);
3525 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3526}
Takashi Iwai54d17402005-11-21 16:33:22 +01003527
Takashi Iwai11aeff02008-07-30 15:01:46 +02003528#ifdef CONFIG_SND_HDA_HWDEP
3529/* execute additional init verbs */
3530static void hda_exec_init_verbs(struct hda_codec *codec)
3531{
3532 if (codec->init_verbs.list)
3533 snd_hda_sequence_write(codec, codec->init_verbs.list);
3534}
3535#else
3536static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3537#endif
3538
Takashi Iwai2a439522011-07-26 09:52:50 +02003539#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003540/*
3541 * call suspend and power-down; used both from PM and power-save
3542 */
3543static void hda_call_codec_suspend(struct hda_codec *codec)
3544{
3545 if (codec->patch_ops.suspend)
3546 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003547 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003548 hda_set_power_state(codec,
3549 codec->afg ? codec->afg : codec->mfg,
3550 AC_PWRST_D3);
3551#ifdef CONFIG_SND_HDA_POWER_SAVE
3552 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003553 spin_lock(&codec->power_lock);
3554 snd_hda_update_power_acct(codec);
3555 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003556 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003557 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003558 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003559 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003560#endif
3561}
3562
3563/*
3564 * kick up codec; used both from PM and power-save
3565 */
3566static void hda_call_codec_resume(struct hda_codec *codec)
3567{
Takashi Iwai7f308302012-05-08 16:52:23 +02003568 /* set as if powered on for avoiding re-entering the resume
3569 * in the resume / power-save sequence
3570 */
3571 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003572 hda_set_power_state(codec,
3573 codec->afg ? codec->afg : codec->mfg,
3574 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003575 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003576 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003577 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003578 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003579 if (codec->patch_ops.resume)
3580 codec->patch_ops.resume(codec);
3581 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003582 if (codec->patch_ops.init)
3583 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003584 snd_hda_codec_resume_amp(codec);
3585 snd_hda_codec_resume_cache(codec);
3586 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003587 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003588}
Takashi Iwai2a439522011-07-26 09:52:50 +02003589#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003590
Takashi Iwai54d17402005-11-21 16:33:22 +01003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592/**
3593 * snd_hda_build_controls - build mixer controls
3594 * @bus: the BUS
3595 *
3596 * Creates mixer controls for each codec included in the bus.
3597 *
3598 * Returns 0 if successful, otherwise a negative error code.
3599 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003600int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003602 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Takashi Iwai0ba21762007-04-16 11:29:14 +02003604 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003605 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003606 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003607 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003608 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003609 err = snd_hda_codec_reset(codec);
3610 if (err < 0) {
3611 printk(KERN_ERR
3612 "hda_codec: cannot revert codec\n");
3613 return err;
3614 }
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003617 return 0;
3618}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003619EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003621int snd_hda_codec_build_controls(struct hda_codec *codec)
3622{
3623 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003624 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003625 /* continue to initialize... */
3626 if (codec->patch_ops.init)
3627 err = codec->patch_ops.init(codec);
3628 if (!err && codec->patch_ops.build_controls)
3629 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003630 if (err < 0)
3631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 return 0;
3633}
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635/*
3636 * stream formats
3637 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003638struct hda_rate_tbl {
3639 unsigned int hz;
3640 unsigned int alsa_bits;
3641 unsigned int hda_fmt;
3642};
3643
Takashi Iwai92f10b32010-08-03 14:21:00 +02003644/* rate = base * mult / div */
3645#define HDA_RATE(base, mult, div) \
3646 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3647 (((div) - 1) << AC_FMT_DIV_SHIFT))
3648
Takashi Iwaibefdf312005-08-22 13:57:55 +02003649static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003651
3652 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003653 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3654 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3655 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3656 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3657 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3658 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3659 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3660 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3661 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3662 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3663 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003664#define AC_PAR_PCM_RATE_BITS 11
3665 /* up to bits 10, 384kHZ isn't supported properly */
3666
3667 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003668 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003669
Takashi Iwaibefdf312005-08-22 13:57:55 +02003670 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671};
3672
3673/**
3674 * snd_hda_calc_stream_format - calculate format bitset
3675 * @rate: the sample rate
3676 * @channels: the number of channels
3677 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3678 * @maxbps: the max. bps
3679 *
3680 * Calculate the format bitset from the given rate, channels and th PCM format.
3681 *
3682 * Return zero if invalid.
3683 */
3684unsigned int snd_hda_calc_stream_format(unsigned int rate,
3685 unsigned int channels,
3686 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003687 unsigned int maxbps,
3688 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
3690 int i;
3691 unsigned int val = 0;
3692
Takashi Iwaibefdf312005-08-22 13:57:55 +02003693 for (i = 0; rate_bits[i].hz; i++)
3694 if (rate_bits[i].hz == rate) {
3695 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 break;
3697 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003698 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 snd_printdd("invalid rate %d\n", rate);
3700 return 0;
3701 }
3702
3703 if (channels == 0 || channels > 8) {
3704 snd_printdd("invalid channels %d\n", channels);
3705 return 0;
3706 }
3707 val |= channels - 1;
3708
3709 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003710 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003711 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003712 break;
3713 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003714 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 case 20:
3717 case 24:
3718 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003719 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003720 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003722 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003724 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 break;
3726 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003727 snd_printdd("invalid format width %d\n",
3728 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return 0;
3730 }
3731
Anssi Hannula32c168c2010-08-03 13:28:57 +03003732 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003733 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return val;
3736}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003737EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003739static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3740 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003741{
3742 unsigned int val = 0;
3743 if (nid != codec->afg &&
3744 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3745 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3746 if (!val || val == -1)
3747 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3748 if (!val || val == -1)
3749 return 0;
3750 return val;
3751}
3752
3753static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3754{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003755 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003756 get_pcm_param);
3757}
3758
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003759static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3760 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003761{
3762 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3763 if (!streams || streams == -1)
3764 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3765 if (!streams || streams == -1)
3766 return 0;
3767 return streams;
3768}
3769
3770static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3771{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003772 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003773 get_stream_param);
3774}
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776/**
3777 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3778 * @codec: the HDA codec
3779 * @nid: NID to query
3780 * @ratesp: the pointer to store the detected rate bitflags
3781 * @formatsp: the pointer to store the detected formats
3782 * @bpsp: the pointer to store the detected format widths
3783 *
3784 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3785 * or @bsps argument is ignored.
3786 *
3787 * Returns 0 if successful, otherwise a negative error code.
3788 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003789int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3791{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003792 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003794 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003795 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 if (ratesp) {
3798 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003799 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003801 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003803 if (rates == 0) {
3804 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3805 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3806 nid, val,
3807 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3808 return -EIO;
3809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 *ratesp = rates;
3811 }
3812
3813 if (formatsp || bpsp) {
3814 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003815 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003817 streams = query_stream_param(codec, nid);
3818 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 bps = 0;
3822 if (streams & AC_SUPFMT_PCM) {
3823 if (val & AC_SUPPCM_BITS_8) {
3824 formats |= SNDRV_PCM_FMTBIT_U8;
3825 bps = 8;
3826 }
3827 if (val & AC_SUPPCM_BITS_16) {
3828 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3829 bps = 16;
3830 }
3831 if (wcaps & AC_WCAP_DIGITAL) {
3832 if (val & AC_SUPPCM_BITS_32)
3833 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3834 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3835 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3836 if (val & AC_SUPPCM_BITS_24)
3837 bps = 24;
3838 else if (val & AC_SUPPCM_BITS_20)
3839 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003840 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3841 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3843 if (val & AC_SUPPCM_BITS_32)
3844 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 else if (val & AC_SUPPCM_BITS_24)
3846 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003847 else if (val & AC_SUPPCM_BITS_20)
3848 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
3850 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003851#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003852 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003854 if (!bps)
3855 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003856 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003857#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003858 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003859 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 /* temporary hack: we have still no proper support
3861 * for the direct AC3 stream...
3862 */
3863 formats |= SNDRV_PCM_FMTBIT_U8;
3864 bps = 8;
3865 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003866 if (formats == 0) {
3867 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3868 "(nid=0x%x, val=0x%x, ovrd=%i, "
3869 "streams=0x%x)\n",
3870 nid, val,
3871 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3872 streams);
3873 return -EIO;
3874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (formatsp)
3876 *formatsp = formats;
3877 if (bpsp)
3878 *bpsp = bps;
3879 }
3880
3881 return 0;
3882}
Stephen Warren384a48d2011-06-01 11:14:21 -06003883EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003886 * snd_hda_is_supported_format - Check the validity of the format
3887 * @codec: HD-audio codec
3888 * @nid: NID to check
3889 * @format: the HD-audio format value to check
3890 *
3891 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 *
3893 * Returns 1 if supported, 0 if not.
3894 */
3895int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3896 unsigned int format)
3897{
3898 int i;
3899 unsigned int val = 0, rate, stream;
3900
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003901 val = query_pcm_param(codec, nid);
3902 if (!val)
3903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003906 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003907 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if (val & (1 << i))
3909 break;
3910 return 0;
3911 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003912 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return 0;
3914
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003915 stream = query_stream_param(codec, nid);
3916 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return 0;
3918
3919 if (stream & AC_SUPFMT_PCM) {
3920 switch (format & 0xf0) {
3921 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003922 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return 0;
3924 break;
3925 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003926 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return 0;
3928 break;
3929 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003930 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return 0;
3932 break;
3933 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003934 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return 0;
3936 break;
3937 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003938 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return 0;
3940 break;
3941 default:
3942 return 0;
3943 }
3944 } else {
3945 /* FIXME: check for float32 and AC3? */
3946 }
3947
3948 return 1;
3949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003950EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952/*
3953 * PCM stuff
3954 */
3955static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3956 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003957 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
3959 return 0;
3960}
3961
3962static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3963 struct hda_codec *codec,
3964 unsigned int stream_tag,
3965 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003966 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
3968 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3969 return 0;
3970}
3971
3972static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3973 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003974 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
Takashi Iwai888afa12008-03-18 09:57:50 +01003976 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return 0;
3978}
3979
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003980static int set_pcm_default_values(struct hda_codec *codec,
3981 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003983 int err;
3984
Takashi Iwai0ba21762007-04-16 11:29:14 +02003985 /* query support PCM information from the given NID */
3986 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003987 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003988 info->rates ? NULL : &info->rates,
3989 info->formats ? NULL : &info->formats,
3990 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003991 if (err < 0)
3992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 }
3994 if (info->ops.open == NULL)
3995 info->ops.open = hda_pcm_default_open_close;
3996 if (info->ops.close == NULL)
3997 info->ops.close = hda_pcm_default_open_close;
3998 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003999 if (snd_BUG_ON(!info->nid))
4000 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 info->ops.prepare = hda_pcm_default_prepare;
4002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004004 if (snd_BUG_ON(!info->nid))
4005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 info->ops.cleanup = hda_pcm_default_cleanup;
4007 }
4008 return 0;
4009}
4010
Takashi Iwaieb541332010-08-06 13:48:11 +02004011/*
4012 * codec prepare/cleanup entries
4013 */
4014int snd_hda_codec_prepare(struct hda_codec *codec,
4015 struct hda_pcm_stream *hinfo,
4016 unsigned int stream,
4017 unsigned int format,
4018 struct snd_pcm_substream *substream)
4019{
4020 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004021 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004022 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4023 if (ret >= 0)
4024 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004025 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004026 return ret;
4027}
4028EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4029
4030void snd_hda_codec_cleanup(struct hda_codec *codec,
4031 struct hda_pcm_stream *hinfo,
4032 struct snd_pcm_substream *substream)
4033{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004034 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004035 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004036 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004037}
4038EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4039
Takashi Iwaid5191e52009-11-16 14:58:17 +01004040/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004041const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4042 "Audio", "SPDIF", "HDMI", "Modem"
4043};
4044
Takashi Iwai176d5332008-07-30 15:01:44 +02004045/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004046 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004047 *
4048 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004049 */
4050static int get_empty_pcm_device(struct hda_bus *bus, int type)
4051{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004052 /* audio device indices; not linear to keep compatibility */
4053 static int audio_idx[HDA_PCM_NTYPES][5] = {
4054 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4055 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004056 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004057 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004058 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004059 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004060
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004061 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004062 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4063 return -EINVAL;
4064 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004065
4066 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4067 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4068 return audio_idx[type][i];
4069
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004070 /* non-fixed slots starting from 10 */
4071 for (i = 10; i < 32; i++) {
4072 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4073 return i;
4074 }
4075
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004076 snd_printk(KERN_WARNING "Too many %s devices\n",
4077 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004078 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004079}
4080
4081/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004082 * attach a new PCM stream
4083 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004084static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004085{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004086 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004087 struct hda_pcm_stream *info;
4088 int stream, err;
4089
Takashi Iwaib91f0802008-11-04 08:43:08 +01004090 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004091 return -EINVAL;
4092 for (stream = 0; stream < 2; stream++) {
4093 info = &pcm->stream[stream];
4094 if (info->substreams) {
4095 err = set_pcm_default_values(codec, info);
4096 if (err < 0)
4097 return err;
4098 }
4099 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004100 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004101}
4102
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004103/* assign all PCMs of the given codec */
4104int snd_hda_codec_build_pcms(struct hda_codec *codec)
4105{
4106 unsigned int pcm;
4107 int err;
4108
4109 if (!codec->num_pcms) {
4110 if (!codec->patch_ops.build_pcms)
4111 return 0;
4112 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004113 if (err < 0) {
4114 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004115 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004116 err = snd_hda_codec_reset(codec);
4117 if (err < 0) {
4118 printk(KERN_ERR
4119 "hda_codec: cannot revert codec\n");
4120 return err;
4121 }
4122 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004123 }
4124 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4125 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4126 int dev;
4127
4128 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004129 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004130
4131 if (!cpcm->pcm) {
4132 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4133 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004134 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004135 cpcm->device = dev;
4136 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004137 if (err < 0) {
4138 printk(KERN_ERR "hda_codec: cannot attach "
4139 "PCM stream %d for codec #%d\n",
4140 dev, codec->addr);
4141 continue; /* no fatal error */
4142 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004143 }
4144 }
4145 return 0;
4146}
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148/**
4149 * snd_hda_build_pcms - build PCM information
4150 * @bus: the BUS
4151 *
4152 * Create PCM information for each codec included in the bus.
4153 *
4154 * The build_pcms codec patch is requested to set up codec->num_pcms and
4155 * codec->pcm_info properly. The array is referred by the top-level driver
4156 * to create its PCM instances.
4157 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4158 * callback.
4159 *
4160 * At least, substreams, channels_min and channels_max must be filled for
4161 * each stream. substreams = 0 indicates that the stream doesn't exist.
4162 * When rates and/or formats are zero, the supported values are queried
4163 * from the given nid. The nid is used also by the default ops.prepare
4164 * and ops.cleanup callbacks.
4165 *
4166 * The driver needs to call ops.open in its open callback. Similarly,
4167 * ops.close is supposed to be called in the close callback.
4168 * ops.prepare should be called in the prepare or hw_params callback
4169 * with the proper parameters for set up.
4170 * ops.cleanup should be called in hw_free for clean up of streams.
4171 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004172 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004174int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004176 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Takashi Iwai0ba21762007-04-16 11:29:14 +02004178 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004179 int err = snd_hda_codec_build_pcms(codec);
4180 if (err < 0)
4181 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 }
4183 return 0;
4184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004185EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/**
4188 * snd_hda_check_board_config - compare the current codec with the config table
4189 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004190 * @num_configs: number of config enums
4191 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 * @tbl: configuration table, terminated by null entries
4193 *
4194 * Compares the modelname or PCI subsystem id of the current codec with the
4195 * given configuration table. If a matching entry is found, returns its
4196 * config value (supposed to be 0 or positive).
4197 *
4198 * If no entries are matching, the function returns a negative value.
4199 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004200int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004201 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004202 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004204 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004205 int i;
4206 for (i = 0; i < num_configs; i++) {
4207 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004208 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004209 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4210 "selected\n", models[i]);
4211 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
4213 }
4214 }
4215
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004216 if (!codec->bus->pci || !tbl)
4217 return -1;
4218
4219 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4220 if (!tbl)
4221 return -1;
4222 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004223#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004224 char tmp[10];
4225 const char *model = NULL;
4226 if (models)
4227 model = models[tbl->value];
4228 if (!model) {
4229 sprintf(tmp, "#%d", tbl->value);
4230 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004232 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4233 "for config %x:%x (%s)\n",
4234 model, tbl->subvendor, tbl->subdevice,
4235 (tbl->name ? tbl->name : "Unknown device"));
4236#endif
4237 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239 return -1;
4240}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004241EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004244 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004245 subsystem ID with the
4246 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004247
4248 This is important for Gateway notebooks with SB450 HDA Audio
4249 where the vendor ID of the PCI device is:
4250 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4251 and the vendor/subvendor are found only at the codec.
4252
4253 * @codec: the HDA codec
4254 * @num_configs: number of config enums
4255 * @models: array of model name strings
4256 * @tbl: configuration table, terminated by null entries
4257 *
4258 * Compares the modelname or PCI subsystem id of the current codec with the
4259 * given configuration table. If a matching entry is found, returns its
4260 * config value (supposed to be 0 or positive).
4261 *
4262 * If no entries are matching, the function returns a negative value.
4263 */
4264int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004265 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004266 const struct snd_pci_quirk *tbl)
4267{
4268 const struct snd_pci_quirk *q;
4269
4270 /* Search for codec ID */
4271 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004272 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4273 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4274 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004275 break;
4276 }
4277
4278 if (!q->subvendor)
4279 return -1;
4280
4281 tbl = q;
4282
4283 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004284#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004285 char tmp[10];
4286 const char *model = NULL;
4287 if (models)
4288 model = models[tbl->value];
4289 if (!model) {
4290 sprintf(tmp, "#%d", tbl->value);
4291 model = tmp;
4292 }
4293 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4294 "for config %x:%x (%s)\n",
4295 model, tbl->subvendor, tbl->subdevice,
4296 (tbl->name ? tbl->name : "Unknown device"));
4297#endif
4298 return tbl->value;
4299 }
4300 return -1;
4301}
4302EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4303
4304/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 * snd_hda_add_new_ctls - create controls from the array
4306 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004307 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 *
4309 * This helper function creates and add new controls in the given array.
4310 * The array must be terminated with an empty entry as terminator.
4311 *
4312 * Returns 0 if successful, or a negative error code.
4313 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004314int snd_hda_add_new_ctls(struct hda_codec *codec,
4315 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004320 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004321 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004322 if (knew->iface == -1) /* skip this codec private value */
4323 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004324 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004325 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004326 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004327 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004328 if (addr > 0)
4329 kctl->id.device = addr;
4330 if (idx > 0)
4331 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004332 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004333 if (!err)
4334 break;
4335 /* try first with another device index corresponding to
4336 * the codec addr; if it still fails (or it's the
4337 * primary codec), then try another control index
4338 */
4339 if (!addr && codec->addr)
4340 addr = codec->addr;
4341 else if (!idx && !knew->index) {
4342 idx = find_empty_mixer_ctl_idx(codec,
4343 knew->name);
4344 if (idx <= 0)
4345 return err;
4346 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004347 return err;
4348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 }
4350 return 0;
4351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004352EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Takashi Iwaicb53c622007-08-10 17:21:45 +02004354#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004355static void hda_power_work(struct work_struct *work)
4356{
4357 struct hda_codec *codec =
4358 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004359 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004360
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004361 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004362 if (codec->power_transition > 0) { /* during power-up sequence? */
4363 spin_unlock(&codec->power_lock);
4364 return;
4365 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004366 if (!codec->power_on || codec->power_count) {
4367 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004368 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004369 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004370 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004371 spin_unlock(&codec->power_lock);
4372
Takashi Iwaicb53c622007-08-10 17:21:45 +02004373 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004374 if (bus->ops.pm_notify)
4375 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004376}
4377
4378static void hda_keep_power_on(struct hda_codec *codec)
4379{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004380 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004381 codec->power_count++;
4382 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004383 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004384 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004385}
4386
Takashi Iwaid5191e52009-11-16 14:58:17 +01004387/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004388void snd_hda_update_power_acct(struct hda_codec *codec)
4389{
4390 unsigned long delta = jiffies - codec->power_jiffies;
4391 if (codec->power_on)
4392 codec->power_on_acct += delta;
4393 else
4394 codec->power_off_acct += delta;
4395 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004396}
4397
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004398/* Transition to powered up, if wait_power_down then wait for a pending
4399 * transition to D3 to complete. A pending D3 transition is indicated
4400 * with power_transition == -1. */
4401static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004402{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004403 struct hda_bus *bus = codec->bus;
4404
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004405 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004406 codec->power_count++;
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004407 /* Return if power_on or transitioning to power_on, unless currently
4408 * powering down. */
4409 if ((codec->power_on || codec->power_transition > 0) &&
4410 !(wait_power_down && codec->power_transition < 0)) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004411 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004412 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004413 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004414 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004415
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004416 cancel_delayed_work_sync(&codec->power_work);
4417
4418 spin_lock(&codec->power_lock);
Takashi Iwaid66fee52011-08-02 15:39:31 +02004419 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004420 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004421 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004422 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004423 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004424 spin_unlock(&codec->power_lock);
4425
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004426 if (bus->ops.pm_notify)
4427 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004428 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004429
4430 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004431 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004432 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004433}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004434
4435/**
4436 * snd_hda_power_up - Power-up the codec
4437 * @codec: HD-audio codec
4438 *
4439 * Increment the power-up counter and power up the hardware really when
4440 * not turned on yet.
4441 */
4442void snd_hda_power_up(struct hda_codec *codec)
4443{
4444 __snd_hda_power_up(codec, false);
4445}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004446EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004447
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004448/**
4449 * snd_hda_power_up_d3wait - Power-up the codec after waiting for any pending
4450 * D3 transition to complete. This differs from snd_hda_power_up() when
4451 * power_transition == -1. snd_hda_power_up sees this case as a nop,
4452 * snd_hda_power_up_d3wait waits for the D3 transition to complete then powers
4453 * back up.
4454 * @codec: HD-audio codec
4455 *
4456 * Cancel any power down operation hapenning on the work queue, then power up.
4457 */
4458void snd_hda_power_up_d3wait(struct hda_codec *codec)
4459{
4460 /* This will cancel and wait for pending power_work to complete. */
4461 __snd_hda_power_up(codec, true);
4462}
4463EXPORT_SYMBOL_HDA(snd_hda_power_up_d3wait);
4464
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004465#define power_save(codec) \
4466 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004467
Takashi Iwaid5191e52009-11-16 14:58:17 +01004468/**
4469 * snd_hda_power_down - Power-down the codec
4470 * @codec: HD-audio codec
4471 *
4472 * Decrement the power-up counter and schedules the power-off work if
4473 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004474 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004475void snd_hda_power_down(struct hda_codec *codec)
4476{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004477 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004478 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004479 if (!codec->power_on || codec->power_count || codec->power_transition) {
4480 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004481 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004482 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004483 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004484 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004485 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004486 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004487 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004488 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004490EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004491
Takashi Iwaid5191e52009-11-16 14:58:17 +01004492/**
4493 * snd_hda_check_amp_list_power - Check the amp list and update the power
4494 * @codec: HD-audio codec
4495 * @check: the object containing an AMP list and the status
4496 * @nid: NID to check / update
4497 *
4498 * Check whether the given NID is in the amp list. If it's in the list,
4499 * check the current AMP status, and update the the power-status according
4500 * to the mute status.
4501 *
4502 * This function is supposed to be set or called from the check_power_status
4503 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004504 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004505int snd_hda_check_amp_list_power(struct hda_codec *codec,
4506 struct hda_loopback_check *check,
4507 hda_nid_t nid)
4508{
Takashi Iwai031024e2011-05-02 11:29:30 +02004509 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004510 int ch, v;
4511
4512 if (!check->amplist)
4513 return 0;
4514 for (p = check->amplist; p->nid; p++) {
4515 if (p->nid == nid)
4516 break;
4517 }
4518 if (!p->nid)
4519 return 0; /* nothing changed */
4520
4521 for (p = check->amplist; p->nid; p++) {
4522 for (ch = 0; ch < 2; ch++) {
4523 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4524 p->idx);
4525 if (!(v & HDA_AMP_MUTE) && v > 0) {
4526 if (!check->power_on) {
4527 check->power_on = 1;
4528 snd_hda_power_up(codec);
4529 }
4530 return 1;
4531 }
4532 }
4533 }
4534 if (check->power_on) {
4535 check->power_on = 0;
4536 snd_hda_power_down(codec);
4537 }
4538 return 0;
4539}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004540EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004543/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004544 * Channel mode helper
4545 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004546
4547/**
4548 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4549 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004550int snd_hda_ch_mode_info(struct hda_codec *codec,
4551 struct snd_ctl_elem_info *uinfo,
4552 const struct hda_channel_mode *chmode,
4553 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004554{
4555 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4556 uinfo->count = 1;
4557 uinfo->value.enumerated.items = num_chmodes;
4558 if (uinfo->value.enumerated.item >= num_chmodes)
4559 uinfo->value.enumerated.item = num_chmodes - 1;
4560 sprintf(uinfo->value.enumerated.name, "%dch",
4561 chmode[uinfo->value.enumerated.item].channels);
4562 return 0;
4563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004564EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004565
Takashi Iwaid5191e52009-11-16 14:58:17 +01004566/**
4567 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4568 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004569int snd_hda_ch_mode_get(struct hda_codec *codec,
4570 struct snd_ctl_elem_value *ucontrol,
4571 const struct hda_channel_mode *chmode,
4572 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004573 int max_channels)
4574{
4575 int i;
4576
4577 for (i = 0; i < num_chmodes; i++) {
4578 if (max_channels == chmode[i].channels) {
4579 ucontrol->value.enumerated.item[0] = i;
4580 break;
4581 }
4582 }
4583 return 0;
4584}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004585EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004586
Takashi Iwaid5191e52009-11-16 14:58:17 +01004587/**
4588 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4589 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590int snd_hda_ch_mode_put(struct hda_codec *codec,
4591 struct snd_ctl_elem_value *ucontrol,
4592 const struct hda_channel_mode *chmode,
4593 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004594 int *max_channelsp)
4595{
4596 unsigned int mode;
4597
4598 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004599 if (mode >= num_chmodes)
4600 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004601 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004602 return 0;
4603 /* change the current channel setting */
4604 *max_channelsp = chmode[mode].channels;
4605 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004606 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004607 return 1;
4608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004609EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611/*
4612 * input MUX helper
4613 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004614
4615/**
4616 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4617 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004618int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4619 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
4621 unsigned int index;
4622
4623 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4624 uinfo->count = 1;
4625 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004626 if (!imux->num_items)
4627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 index = uinfo->value.enumerated.item;
4629 if (index >= imux->num_items)
4630 index = imux->num_items - 1;
4631 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4632 return 0;
4633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004634EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Takashi Iwaid5191e52009-11-16 14:58:17 +01004636/**
4637 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4638 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004639int snd_hda_input_mux_put(struct hda_codec *codec,
4640 const struct hda_input_mux *imux,
4641 struct snd_ctl_elem_value *ucontrol,
4642 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 unsigned int *cur_val)
4644{
4645 unsigned int idx;
4646
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004647 if (!imux->num_items)
4648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 idx = ucontrol->value.enumerated.item[0];
4650 if (idx >= imux->num_items)
4651 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004652 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004654 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4655 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 *cur_val = idx;
4657 return 1;
4658}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004659EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661
4662/*
4663 * Multi-channel / digital-out PCM helper functions
4664 */
4665
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004666/* setup SPDIF output stream */
4667static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4668 unsigned int stream_tag, unsigned int format)
4669{
Stephen Warren7c9359762011-06-01 11:14:17 -06004670 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4671
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004672 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004673 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004674 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004675 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004676 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004677 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004678 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004679 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004680 for (d = codec->slave_dig_outs; *d; d++)
4681 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4682 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004683 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004684 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004685 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004686 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004687 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004688}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004689
Takashi Iwai2f728532008-09-25 16:32:41 +02004690static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4691{
4692 snd_hda_codec_cleanup_stream(codec, nid);
4693 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004694 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004695 for (d = codec->slave_dig_outs; *d; d++)
4696 snd_hda_codec_cleanup_stream(codec, *d);
4697 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004698}
4699
Takashi Iwaid5191e52009-11-16 14:58:17 +01004700/**
4701 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4702 * @bus: HD-audio bus
4703 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004704void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4705{
4706 struct hda_codec *codec;
4707
4708 if (!bus)
4709 return;
4710 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004711 if (hda_codec_is_power_on(codec) &&
4712 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004713 codec->patch_ops.reboot_notify(codec);
4714 }
4715}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004716EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004717
Takashi Iwaid5191e52009-11-16 14:58:17 +01004718/**
4719 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004721int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4722 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Ingo Molnar62932df2006-01-16 16:34:20 +01004724 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004725 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4726 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004727 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
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_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Takashi Iwaid5191e52009-11-16 14:58:17 +01004734/**
4735 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4736 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004737int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4738 struct hda_multi_out *mout,
4739 unsigned int stream_tag,
4740 unsigned int format,
4741 struct snd_pcm_substream *substream)
4742{
4743 mutex_lock(&codec->spdif_mutex);
4744 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4745 mutex_unlock(&codec->spdif_mutex);
4746 return 0;
4747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004748EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004749
Takashi Iwaid5191e52009-11-16 14:58:17 +01004750/**
4751 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4752 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004753int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4754 struct hda_multi_out *mout)
4755{
4756 mutex_lock(&codec->spdif_mutex);
4757 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4758 mutex_unlock(&codec->spdif_mutex);
4759 return 0;
4760}
4761EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4762
Takashi Iwaid5191e52009-11-16 14:58:17 +01004763/**
4764 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004766int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4767 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Ingo Molnar62932df2006-01-16 16:34:20 +01004769 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004771 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return 0;
4773}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004774EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Takashi Iwaid5191e52009-11-16 14:58:17 +01004776/**
4777 * snd_hda_multi_out_analog_open - open analog outputs
4778 *
4779 * Open analog outputs and set up the hw-constraints.
4780 * If the digital outputs can be opened as slave, open the digital
4781 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004783int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4784 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004785 struct snd_pcm_substream *substream,
4786 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
Takashi Iwai9a081602008-02-12 18:37:26 +01004788 struct snd_pcm_runtime *runtime = substream->runtime;
4789 runtime->hw.channels_max = mout->max_channels;
4790 if (mout->dig_out_nid) {
4791 if (!mout->analog_rates) {
4792 mout->analog_rates = hinfo->rates;
4793 mout->analog_formats = hinfo->formats;
4794 mout->analog_maxbps = hinfo->maxbps;
4795 } else {
4796 runtime->hw.rates = mout->analog_rates;
4797 runtime->hw.formats = mout->analog_formats;
4798 hinfo->maxbps = mout->analog_maxbps;
4799 }
4800 if (!mout->spdif_rates) {
4801 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4802 &mout->spdif_rates,
4803 &mout->spdif_formats,
4804 &mout->spdif_maxbps);
4805 }
4806 mutex_lock(&codec->spdif_mutex);
4807 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004808 if ((runtime->hw.rates & mout->spdif_rates) &&
4809 (runtime->hw.formats & mout->spdif_formats)) {
4810 runtime->hw.rates &= mout->spdif_rates;
4811 runtime->hw.formats &= mout->spdif_formats;
4812 if (mout->spdif_maxbps < hinfo->maxbps)
4813 hinfo->maxbps = mout->spdif_maxbps;
4814 } else {
4815 mout->share_spdif = 0;
4816 /* FIXME: need notify? */
4817 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004818 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004819 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4822 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004824EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Takashi Iwaid5191e52009-11-16 14:58:17 +01004826/**
4827 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4828 *
4829 * Set up the i/o for analog out.
4830 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004832int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4833 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 unsigned int stream_tag,
4835 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004836 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Takashi Iwaidda14412011-05-02 11:29:30 +02004838 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004840 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 int i;
4842
Ingo Molnar62932df2006-01-16 16:34:20 +01004843 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004844 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004845 if (mout->dig_out_nid && mout->share_spdif &&
4846 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004848 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4849 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004850 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004852 setup_dig_out_stream(codec, mout->dig_out_nid,
4853 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 } else {
4855 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004856 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
4858 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004859 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
4861 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004862 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4863 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004864 if (!mout->no_share_stream &&
4865 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004867 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4868 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004869 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004870 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4871 if (!mout->no_share_stream && mout->hp_out_nid[i])
4872 snd_hda_codec_setup_stream(codec,
4873 mout->hp_out_nid[i],
4874 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004875 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004876 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004877 snd_hda_codec_setup_stream(codec,
4878 mout->extra_out_nid[i],
4879 stream_tag, 0, format);
4880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 /* surrounds */
4882 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004883 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004884 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4885 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004886 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004887 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4888 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 }
4890 return 0;
4891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004892EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Takashi Iwaid5191e52009-11-16 14:58:17 +01004894/**
4895 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004897int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4898 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
Takashi Iwaidda14412011-05-02 11:29:30 +02004900 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 int i;
4902
4903 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004904 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004906 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004907 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4908 if (mout->hp_out_nid[i])
4909 snd_hda_codec_cleanup_stream(codec,
4910 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004911 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4912 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004913 snd_hda_codec_cleanup_stream(codec,
4914 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004915 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004917 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 mout->dig_out_used = 0;
4919 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004920 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return 0;
4922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004923EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Takashi Iwai47408602012-04-20 13:06:53 +02004925/**
4926 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4927 *
4928 * Guess the suitable VREF pin bits to be set as the pin-control value.
4929 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4930 */
4931unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4932{
4933 unsigned int pincap;
4934 unsigned int oldval;
4935 oldval = snd_hda_codec_read(codec, pin, 0,
4936 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4937 pincap = snd_hda_query_pin_caps(codec, pin);
4938 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4939 /* Exception: if the default pin setup is vref50, we give it priority */
4940 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4941 return AC_PINCTL_VREF_80;
4942 else if (pincap & AC_PINCAP_VREF_50)
4943 return AC_PINCTL_VREF_50;
4944 else if (pincap & AC_PINCAP_VREF_100)
4945 return AC_PINCTL_VREF_100;
4946 else if (pincap & AC_PINCAP_VREF_GRD)
4947 return AC_PINCTL_VREF_GRD;
4948 return AC_PINCTL_VREF_HIZ;
4949}
4950EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4951
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004952int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4953 unsigned int val, bool cached)
4954{
4955 if (val) {
4956 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004957 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004958 if (!(cap & AC_PINCAP_OUT))
4959 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4960 else if ((val & AC_PINCTL_HP_EN) &&
4961 !(cap & AC_PINCAP_HP_DRV))
4962 val &= ~AC_PINCTL_HP_EN;
4963 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004964 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004965 if (!(cap & AC_PINCAP_IN))
4966 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4967 }
4968 }
4969 if (cached)
4970 return snd_hda_codec_update_cache(codec, pin, 0,
4971 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4972 else
4973 return snd_hda_codec_write(codec, pin, 0,
4974 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4975}
4976EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4977
Takashi Iwai990061c2010-09-09 22:08:44 +02004978/**
4979 * snd_hda_add_imux_item - Add an item to input_mux
4980 *
4981 * When the same label is used already in the existing items, the number
4982 * suffix is appended to the label. This label index number is stored
4983 * to type_idx when non-NULL pointer is given.
4984 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004985int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4986 int index, int *type_idx)
4987{
4988 int i, label_idx = 0;
4989 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4990 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4991 return -EINVAL;
4992 }
4993 for (i = 0; i < imux->num_items; i++) {
4994 if (!strncmp(label, imux->items[i].label, strlen(label)))
4995 label_idx++;
4996 }
4997 if (type_idx)
4998 *type_idx = label_idx;
4999 if (label_idx > 0)
5000 snprintf(imux->items[imux->num_items].label,
5001 sizeof(imux->items[imux->num_items].label),
5002 "%s %d", label, label_idx);
5003 else
5004 strlcpy(imux->items[imux->num_items].label, label,
5005 sizeof(imux->items[imux->num_items].label));
5006 imux->items[imux->num_items].index = index;
5007 imux->num_items++;
5008 return 0;
5009}
5010EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005011
Takashi Iwai4a471b72005-12-07 13:56:29 +01005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013#ifdef CONFIG_PM
5014/*
5015 * power management
5016 */
5017
5018/**
5019 * snd_hda_suspend - suspend the codecs
5020 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 *
5022 * Returns 0 if successful.
5023 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005024int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005026 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Takashi Iwai0ba21762007-04-16 11:29:14 +02005028 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005029 if (hda_codec_is_power_on(codec))
5030 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
5032 return 0;
5033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005034EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
5036/**
5037 * snd_hda_resume - resume the codecs
5038 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 *
5040 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005041 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005042 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005043 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 */
5045int snd_hda_resume(struct hda_bus *bus)
5046{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005047 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
Takashi Iwai0ba21762007-04-16 11:29:14 +02005049 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005050 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 return 0;
5053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005054EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005055#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005056
5057/*
5058 * generic arrays
5059 */
5060
Takashi Iwaid5191e52009-11-16 14:58:17 +01005061/**
5062 * snd_array_new - get a new element from the given array
5063 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005064 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005065 * Get a new element from the given array. If it exceeds the
5066 * pre-allocated array size, re-allocate the array.
5067 *
5068 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005069 */
5070void *snd_array_new(struct snd_array *array)
5071{
5072 if (array->used >= array->alloced) {
5073 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005074 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005075 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005076 void *nlist;
5077 if (snd_BUG_ON(num >= 4096))
5078 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005079 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005080 if (!nlist)
5081 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005082 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005083 array->list = nlist;
5084 array->alloced = num;
5085 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005086 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005088EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005089
Takashi Iwaid5191e52009-11-16 14:58:17 +01005090/**
5091 * snd_array_free - free the given array elements
5092 * @array: the array object
5093 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005094void snd_array_free(struct snd_array *array)
5095{
5096 kfree(array->list);
5097 array->used = 0;
5098 array->alloced = 0;
5099 array->list = NULL;
5100}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005101EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005102
Takashi Iwaid5191e52009-11-16 14:58:17 +01005103/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005104 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5105 * @pcm: PCM caps bits
5106 * @buf: the string buffer to write
5107 * @buflen: the max buffer length
5108 *
5109 * used by hda_proc.c and hda_eld.c
5110 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005111void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5112{
5113 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5114 int i, j;
5115
5116 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5117 if (pcm & (AC_SUPPCM_BITS_8 << i))
5118 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5119
5120 buf[j] = '\0'; /* necessary when j == 0 */
5121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005122EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005123
5124MODULE_DESCRIPTION("HDA codec core");
5125MODULE_LICENSE("GPL");