blob: 8a72f7b30a33b24cddf980e63551301589a4c9a2 [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 Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +020098static 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 Iwai68467f52012-08-28 09:14:29 -0700101static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
102{
103 if (bus->ops.pm_notify)
104 bus->ops.pm_notify(bus, power_up);
105}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200106#else
107static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200108#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700109#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200110#endif
111
Takashi Iwaid5191e52009-11-16 14:58:17 +0100112/**
113 * snd_hda_get_jack_location - Give a location string of the jack
114 * @cfg: pin default config value
115 *
116 * Parse the pin default config value and returns the string of the
117 * jack location, e.g. "Rear", "Front", etc.
118 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119const char *snd_hda_get_jack_location(u32 cfg)
120{
121 static char *bases[7] = {
122 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
123 };
124 static unsigned char specials_idx[] = {
125 0x07, 0x08,
126 0x17, 0x18, 0x19,
127 0x37, 0x38
128 };
129 static char *specials[] = {
130 "Rear Panel", "Drive Bar",
131 "Riser", "HDMI", "ATAPI",
132 "Mobile-In", "Mobile-Out"
133 };
134 int i;
135 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
136 if ((cfg & 0x0f) < 7)
137 return bases[cfg & 0x0f];
138 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
139 if (cfg == specials_idx[i])
140 return specials[i];
141 }
142 return "UNKNOWN";
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack connectivity, i.e. external or internal connection.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_connectivity(u32 cfg)
154{
155 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
156
157 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100159EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400160
Takashi Iwaid5191e52009-11-16 14:58:17 +0100161/**
162 * snd_hda_get_jack_type - Give a type string of the jack
163 * @cfg: pin default config value
164 *
165 * Parse the pin default config value and returns the string of the
166 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
167 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400168const char *snd_hda_get_jack_type(u32 cfg)
169{
170 static char *jack_types[16] = {
171 "Line Out", "Speaker", "HP Out", "CD",
172 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
173 "Line In", "Aux", "Mic", "Telephony",
174 "SPDIF In", "Digitial In", "Reserved", "Other"
175 };
176
177 return jack_types[(cfg & AC_DEFCFG_DEVICE)
178 >> AC_DEFCFG_DEVICE_SHIFT];
179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100180EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400181
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100182/*
183 * Compose a 32bit command word to be sent to the HD-audio controller
184 */
185static inline unsigned int
186make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
187 unsigned int verb, unsigned int parm)
188{
189 u32 val;
190
Takashi Iwai82e1b802009-07-17 12:47:34 +0200191 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
192 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800193 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
194 codec->addr, direct, nid, verb, parm);
195 return ~0;
196 }
197
198 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100199 val |= (u32)direct << 27;
200 val |= (u32)nid << 20;
201 val |= verb << 8;
202 val |= parm;
203 return val;
204}
205
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206/*
207 * Send and receive a verb
208 */
209static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
210 unsigned int *res)
211{
212 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200214
Wu Fengguang6430aee2009-07-17 16:49:19 +0800215 if (cmd == ~0)
216 return -1;
217
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 if (res)
219 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200220 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221 snd_hda_power_up(codec);
222 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200223 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800226 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 trace_hda_get_response(codec, *res);
228 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229 mutex_unlock(&bus->cmd_mutex);
230 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 if (res && *res == -1 && bus->rirb_error) {
232 if (bus->response_reset) {
233 snd_printd("hda_codec: resetting BUS due to "
234 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200236 bus->ops.bus_reset(bus);
237 }
238 goto again;
239 }
240 /* clear reset-flag when the communication gets recovered */
241 if (!err)
242 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 return err;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/**
247 * snd_hda_codec_read - send a command and get the response
248 * @codec: the HDA codec
249 * @nid: NID to send the command
250 * @direct: direct flag
251 * @verb: the verb to send
252 * @parm: the parameter for the verb
253 *
254 * Send a single command and read the corresponding response.
255 *
256 * Returns the obtained response value, or -1 for an error.
257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200258unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
259 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned int verb, unsigned int parm)
261{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200262 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
263 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700264 if (codec_exec_verb(codec, cmd, &res))
265 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return res;
267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100268EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/**
271 * snd_hda_codec_write - send a single command without waiting for response
272 * @codec: the HDA codec
273 * @nid: NID to send the command
274 * @direct: direct flag
275 * @verb: the verb to send
276 * @parm: the parameter for the verb
277 *
278 * Send a single command without waiting for response.
279 *
280 * Returns 0 if successful, or a negative error code.
281 */
282int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
283 unsigned int verb, unsigned int parm)
284{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200285 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100286 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200287 return codec_exec_verb(codec, cmd,
288 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100290EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
293 * snd_hda_sequence_write - sequence writes
294 * @codec: the HDA codec
295 * @seq: VERB array to send
296 *
297 * Send the commands sequentially from the given array.
298 * The array must be terminated with NID=0.
299 */
300void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
301{
302 for (; seq->nid; seq++)
303 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100305EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/**
308 * snd_hda_get_sub_nodes - get the range of sub nodes
309 * @codec: the HDA codec
310 * @nid: NID to parse
311 * @start_id: the pointer to store the start NID
312 *
313 * Parse the NID and store the start NID of its sub-nodes.
314 * Returns the number of sub-nodes.
315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200316int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
317 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned int parm;
320
321 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200322 if (parm == -1)
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *start_id = (parm >> 16) & 0x7fff;
325 return (int)(parm & 0x7fff);
326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100327EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200329/* look up the cached results */
330static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
331{
332 int i, len;
333 for (i = 0; i < array->used; ) {
334 hda_nid_t *p = snd_array_elem(array, i);
335 if (nid == *p)
336 return p;
337 len = p[1];
338 i += len + 2;
339 }
340 return NULL;
341}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343/* read the connection and add to the cache */
344static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200347 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350 if (len < 0)
351 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353}
Takashi Iwaidce20792011-06-24 14:10:28 +0200354
355/**
356 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * @codec: the HDA codec
358 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200359 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @max_conns: max. number of connections to store
361 *
362 * Parses the connection list of the given widget and stores the list
363 * of NIDs.
364 *
365 * Returns the number of connections, or a negative error code.
366 */
367int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200368 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200369{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 struct snd_array *array = &codec->conn_lists;
371 int len;
372 hda_nid_t *p;
373 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200374
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200375 again:
376 mutex_lock(&codec->hash_mutex);
377 len = -1;
378 /* if the connection-list is already cached, read it */
379 p = lookup_conn_list(array, nid);
380 if (p) {
381 len = p[1];
382 if (conn_list && len > max_conns) {
383 snd_printk(KERN_ERR "hda_codec: "
384 "Too many connections %d for NID 0x%x\n",
385 len, nid);
386 mutex_unlock(&codec->hash_mutex);
387 return -EINVAL;
388 }
389 if (conn_list && len)
390 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200392 mutex_unlock(&codec->hash_mutex);
393 if (len >= 0)
394 return len;
395 if (snd_BUG_ON(added))
396 return -EINVAL;
397
398 len = read_and_add_raw_conns(codec, nid);
399 if (len < 0)
400 return len;
401 added = true;
402 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403}
404EXPORT_SYMBOL_HDA(snd_hda_get_connections);
405
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200406/**
407 * snd_hda_get_raw_connections - copy connection list without cache
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @conn_list: connection list array
411 * @max_conns: max. number of connections to store
412 *
413 * Like snd_hda_get_connections(), copy the connection list but without
414 * checking through the connection-list cache.
415 * Currently called only from hda_proc.c, so not exported.
416 */
417int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
418 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100421 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200423 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100424 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Takashi Iwaida3cec32008-08-08 17:12:14 +0200426 if (snd_BUG_ON(!conn_list || max_conns <= 0))
427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200429 wcaps = get_wcaps(codec, nid);
430 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200431 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
432 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
435 if (parm & AC_CLIST_LONG) {
436 /* long form */
437 shift = 16;
438 num_elems = 2;
439 } else {
440 /* short form */
441 shift = 8;
442 num_elems = 4;
443 }
444 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 mask = (1 << (shift-1)) - 1;
446
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0; /* no connection */
449
450 if (conn_len == 1) {
451 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 parm = snd_hda_codec_read(codec, nid, 0,
453 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200454 if (parm == -1 && codec->bus->rirb_error)
455 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 conn_list[0] = parm & mask;
457 return 1;
458 }
459
460 /* multi connection */
461 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100462 prev_nid = 0;
463 for (i = 0; i < conn_len; i++) {
464 int range_val;
465 hda_nid_t val, n;
466
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200467 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100468 parm = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (parm == -1 && codec->bus->rirb_error)
471 return -EIO;
472 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200475 if (val == 0) {
476 snd_printk(KERN_WARNING "hda_codec: "
477 "invalid CONNECT_LIST verb %x[%i]:%x\n",
478 nid, i, parm);
479 return 0;
480 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 parm >>= shift;
482 if (range_val) {
483 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200484 if (!prev_nid || prev_nid >= val) {
485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid dep_range_val %x:%x\n",
487 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 for (n = prev_nid + 1; n <= val; n++) {
491 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200492 snd_printk(KERN_ERR "hda_codec: "
493 "Too many connections %d for NID 0x%x\n",
494 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 return -EINVAL;
496 }
497 conn_list[conns++] = n;
498 }
499 } else {
500 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
503 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100504 return -EINVAL;
505 }
506 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 return conns;
511}
512
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200513static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
514{
515 hda_nid_t *p = snd_array_new(array);
516 if (!p)
517 return false;
518 *p = nid;
519 return true;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200523 * snd_hda_override_conn_list - add/modify the connection-list to cache
524 * @codec: the HDA codec
525 * @nid: NID to parse
526 * @len: number of connection list entries
527 * @list: the list of connection entries
528 *
529 * Add or modify the given connection-list to the cache. If the corresponding
530 * cache already exists, invalidate it and append a new one.
531 *
532 * Returns zero or a negative error code.
533 */
534int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
535 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200537 struct snd_array *array = &codec->conn_lists;
538 hda_nid_t *p;
539 int i, old_used;
540
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200541 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 p = lookup_conn_list(array, nid);
543 if (p)
544 *p = -1; /* invalidate the old entry */
545
546 old_used = array->used;
547 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
548 goto error_add;
549 for (i = 0; i < len; i++)
550 if (!add_conn_list(array, list[i]))
551 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200552 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200553 return 0;
554
555 error_add:
556 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200557 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
561
562/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
568 *
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
572 */
573int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
574 hda_nid_t nid, int recursive)
575{
576 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
577 int i, nums;
578
579 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
580 for (i = 0; i < nums; i++)
581 if (conn[i] == nid)
582 return i;
583 if (!recursive)
584 return -1;
585 if (recursive > 5) {
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
587 return -1;
588 }
589 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 for (i = 0; i < nums; i++) {
591 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
592 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
593 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200594 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
595 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200596 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 return -1;
598}
599EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/**
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
603 * @bus: the BUS
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
606 *
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
610 *
611 * Returns 0 if successful, or a negative error code.
612 */
613int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
614{
615 struct hda_bus_unsolicited *unsol;
616 unsigned int wp;
617
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200618 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200619 unsol = bus->unsol;
620 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622
623 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
624 unsol->wp = wp;
625
626 wp <<= 1;
627 unsol->queue[wp] = res;
628 unsol->queue[wp + 1] = res_ex;
629
Takashi Iwai6acaed32009-01-12 10:09:24 +0100630 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return 0;
633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100634EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800637 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
David Howellsc4028952006-11-22 14:57:56 +0000639static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David Howellsc4028952006-11-22 14:57:56 +0000641 struct hda_bus_unsolicited *unsol =
642 container_of(work, struct hda_bus_unsolicited, work);
643 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct hda_codec *codec;
645 unsigned int rp, caddr, res;
646
647 while (unsol->rp != unsol->wp) {
648 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
649 unsol->rp = rp;
650 rp <<= 1;
651 res = unsol->queue[rp];
652 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200653 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 codec = bus->caddr_tbl[caddr & 0x0f];
656 if (codec && codec->patch_ops.unsol_event)
657 codec->patch_ops.unsol_event(codec, res);
658 }
659}
660
661/*
662 * initialize unsolicited queue
663 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200664static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct hda_bus_unsolicited *unsol;
667
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100668 if (bus->unsol) /* already initialized */
669 return 0;
670
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200671 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200672 if (!unsol) {
673 snd_printk(KERN_ERR "hda_codec: "
674 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -ENOMEM;
676 }
David Howellsc4028952006-11-22 14:57:56 +0000677 INIT_WORK(&unsol->work, process_unsol_events);
678 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 bus->unsol = unsol;
680 return 0;
681}
682
683/*
684 * destructor
685 */
686static void snd_hda_codec_free(struct hda_codec *codec);
687
688static int snd_hda_bus_free(struct hda_bus *bus)
689{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100694 if (bus->workq)
695 flush_workqueue(bus->workq);
696 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200698 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snd_hda_codec_free(codec);
700 }
701 if (bus->ops.private_free)
702 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100703 if (bus->workq)
704 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(bus);
706 return 0;
707}
708
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100709static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100712 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return snd_hda_bus_free(bus);
714}
715
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200716#ifdef CONFIG_SND_HDA_HWDEP
717static int snd_hda_bus_dev_register(struct snd_device *device)
718{
719 struct hda_bus *bus = device->device_data;
720 struct hda_codec *codec;
721 list_for_each_entry(codec, &bus->codec_list, list) {
722 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100723 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200724 }
725 return 0;
726}
727#else
728#define snd_hda_bus_dev_register NULL
729#endif
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100739int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200740 const struct hda_bus_template *temp,
741 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct hda_bus *bus;
744 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200746 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .dev_free = snd_hda_bus_dev_free,
748 };
749
Takashi Iwaida3cec32008-08-08 17:12:14 +0200750 if (snd_BUG_ON(!temp))
751 return -EINVAL;
752 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (busp)
756 *busp = NULL;
757
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200758 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (bus == NULL) {
760 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
761 return -ENOMEM;
762 }
763
764 bus->card = card;
765 bus->private_data = temp->private_data;
766 bus->pci = temp->pci;
767 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100768 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 bus->ops = temp->ops;
770
Ingo Molnar62932df2006-01-16 16:34:20 +0100771 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200772 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&bus->codec_list);
774
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100775 snprintf(bus->workq_name, sizeof(bus->workq_name),
776 "hd-audio%d", card->number);
777 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100778 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100779 snd_printk(KERN_ERR "cannot create workqueue %s\n",
780 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 kfree(bus);
782 return -ENOMEM;
783 }
784
Takashi Iwai0ba21762007-04-16 11:29:14 +0200785 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
786 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_hda_bus_free(bus);
788 return err;
789 }
790 if (busp)
791 *busp = bus;
792 return 0;
793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100794EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai82467612007-07-27 19:15:54 +0200796#ifdef CONFIG_SND_HDA_GENERIC
797#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200798 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200799#else
800#define is_generic_config(codec) 0
801#endif
802
Takashi Iwai645f10c2008-11-28 15:07:37 +0100803#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100804#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
805#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#endif
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
810 * find a matching codec preset
811 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200812static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200813find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100815 struct hda_codec_preset_list *tbl;
816 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200817 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai82467612007-07-27 19:15:54 +0200819 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100820 return NULL; /* use the generic parser */
821
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100822 again:
823 mutex_lock(&preset_mutex);
824 list_for_each_entry(tbl, &hda_preset_tables, list) {
825 if (!try_module_get(tbl->owner)) {
826 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
827 continue;
828 }
829 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100831 if (preset->afg && preset->afg != codec->afg)
832 continue;
833 if (preset->mfg && preset->mfg != codec->mfg)
834 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200837 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 preset->rev == codec->revision_id)) {
840 mutex_unlock(&preset_mutex);
841 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 module_put(tbl->owner);
846 }
847 mutex_unlock(&preset_mutex);
848
849 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
850 char name[32];
851 if (!mod_requested)
852 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
853 codec->vendor_id);
854 else
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
856 (codec->vendor_id >> 16) & 0xffff);
857 request_module(name);
858 mod_requested++;
859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 return NULL;
862}
863
864/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200865 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 const struct hda_vendor_id *c;
870 const char *vendor = NULL;
871 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200872 char tmp[16];
873
874 if (codec->vendor_name)
875 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 for (c = hda_vendor_ids; c->id; c++) {
878 if (c->id == vendor_id) {
879 vendor = c->name;
880 break;
881 }
882 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200883 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sprintf(tmp, "Generic %04x", vendor_id);
885 vendor = tmp;
886 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200887 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
888 if (!codec->vendor_name)
889 return -ENOMEM;
890
891 get_chip_name:
892 if (codec->chip_name)
893 return 0;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200896 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
897 else {
898 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
899 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
900 }
901 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200902 return -ENOMEM;
903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200907 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200911 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 hda_nid_t nid;
913
914 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
915 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200916 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200917 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200918 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200919 case AC_GRP_AUDIO_FUNCTION:
920 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200921 codec->afg_function_id = function_id & 0xff;
922 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200923 break;
924 case AC_GRP_MODEM_FUNCTION:
925 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 codec->mfg_function_id = function_id & 0xff;
927 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 break;
929 default:
930 break;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100936 * read widget caps for each widget and store in cache
937 */
938static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
939{
940 int i;
941 hda_nid_t nid;
942
943 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
944 &codec->start_nid);
945 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200946 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100947 return -ENOMEM;
948 nid = codec->start_nid;
949 for (i = 0; i < codec->num_nodes; i++, nid++)
950 codec->wcaps[i] = snd_hda_param_read(codec, nid,
951 AC_PAR_AUDIO_WIDGET_CAP);
952 return 0;
953}
954
Takashi Iwai3be14142009-02-20 14:11:16 +0100955/* read all pin default configurations and save codec->init_pins */
956static int read_pin_defaults(struct hda_codec *codec)
957{
958 int i;
959 hda_nid_t nid = codec->start_nid;
960
961 for (i = 0; i < codec->num_nodes; i++, nid++) {
962 struct hda_pincfg *pin;
963 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200964 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100965 if (wid_type != AC_WID_PIN)
966 continue;
967 pin = snd_array_new(&codec->init_pins);
968 if (!pin)
969 return -ENOMEM;
970 pin->nid = nid;
971 pin->cfg = snd_hda_codec_read(codec, nid, 0,
972 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200973 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL,
975 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976 }
977 return 0;
978}
979
980/* look up the given pin config list and return the item matching with NID */
981static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
982 struct snd_array *array,
983 hda_nid_t nid)
984{
985 int i;
986 for (i = 0; i < array->used; i++) {
987 struct hda_pincfg *pin = snd_array_elem(array, i);
988 if (pin->nid == nid)
989 return pin;
990 }
991 return NULL;
992}
993
994/* write a config value for the given NID */
995static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
996 unsigned int cfg)
997{
998 int i;
999 for (i = 0; i < 4; i++) {
1000 snd_hda_codec_write(codec, nid, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1002 cfg & 0xff);
1003 cfg >>= 8;
1004 }
1005}
1006
1007/* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1009 */
1010int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1011 hda_nid_t nid, unsigned int cfg)
1012{
1013 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001014 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015
Takashi Iwaib82855a2009-12-27 11:24:56 +01001016 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1017 return -EINVAL;
1018
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001019 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001020 pin = look_up_pincfg(codec, list, nid);
1021 if (!pin) {
1022 pin = snd_array_new(list);
1023 if (!pin)
1024 return -ENOMEM;
1025 pin->nid = nid;
1026 }
1027 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001028
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1031 */
1032 cfg = snd_hda_codec_get_pincfg(codec, nid);
1033 if (oldcfg != cfg)
1034 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035 return 0;
1036}
1037
Takashi Iwaid5191e52009-11-16 14:58:17 +01001038/**
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1043 *
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1047 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001048int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1049 hda_nid_t nid, unsigned int cfg)
1050{
Takashi Iwai346ff702009-02-23 09:42:57 +01001051 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001052}
1053EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1054
Takashi Iwaid5191e52009-11-16 14:58:17 +01001055/**
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1059 *
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1063 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001064unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwai3be14142009-02-20 14:11:16 +01001068#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001069 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 if (pin)
1071 return pin->cfg;
1072#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001073 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1074 if (pin)
1075 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1077 if (pin)
1078 return pin->cfg;
1079 return 0;
1080}
1081EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1082
1083/* restore all current pin configs */
1084static void restore_pincfgs(struct hda_codec *codec)
1085{
1086 int i;
1087 for (i = 0; i < codec->init_pins.used; i++) {
1088 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1089 set_pincfg(codec, pin->nid,
1090 snd_hda_codec_get_pincfg(codec, pin->nid));
1091 }
1092}
Takashi Iwai54d17402005-11-21 16:33:22 +01001093
Takashi Iwai92ee6162009-12-27 11:18:59 +01001094/**
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1097 *
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1100 */
1101void snd_hda_shutup_pins(struct hda_codec *codec)
1102{
1103 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1106 */
1107 if (codec->bus->shutdown)
1108 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001109 for (i = 0; i < codec->init_pins.used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec, pin->nid, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1114 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001115 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001116}
1117EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1118
Takashi Iwai2a439522011-07-26 09:52:50 +02001119#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001120/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121static void restore_shutup_pins(struct hda_codec *codec)
1122{
1123 int i;
1124 if (!codec->pins_shutup)
1125 return;
1126 if (codec->bus->shutdown)
1127 return;
1128 for (i = 0; i < codec->init_pins.used; i++) {
1129 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1130 snd_hda_codec_write(codec, pin->nid, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL,
1132 pin->ctrl);
1133 }
1134 codec->pins_shutup = 0;
1135}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001136#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001137
Takashi Iwai01751f52007-08-10 16:59:39 +02001138static void init_hda_cache(struct hda_cache_rec *cache,
1139 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001140static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001141
Takashi Iwai3be14142009-02-20 14:11:16 +01001142/* restore the initial pin cfgs and release all pincfg lists */
1143static void restore_init_pincfgs(struct hda_codec *codec)
1144{
Takashi Iwai346ff702009-02-23 09:42:57 +01001145 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001146 * so that only the values in init_pins are restored
1147 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001148 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001149#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#endif
1152 restore_pincfgs(codec);
1153 snd_array_free(&codec->init_pins);
1154}
1155
Takashi Iwai54d17402005-11-21 16:33:22 +01001156/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001157 * audio-converter setup caches
1158 */
1159struct hda_cvt_setup {
1160 hda_nid_t nid;
1161 u8 stream_tag;
1162 u8 channel_id;
1163 u16 format_id;
1164 unsigned char active; /* cvt is currently used */
1165 unsigned char dirty; /* setups should be cleared */
1166};
1167
1168/* get or create a cache entry for the given audio converter NID */
1169static struct hda_cvt_setup *
1170get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1171{
1172 struct hda_cvt_setup *p;
1173 int i;
1174
1175 for (i = 0; i < codec->cvt_setups.used; i++) {
1176 p = snd_array_elem(&codec->cvt_setups, i);
1177 if (p->nid == nid)
1178 return p;
1179 }
1180 p = snd_array_new(&codec->cvt_setups);
1181 if (p)
1182 p->nid = nid;
1183 return p;
1184}
1185
1186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * codec destructor
1188 */
1189static void snd_hda_codec_free(struct hda_codec *codec)
1190{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return;
Takashi Iwai59cad162012-06-26 15:00:20 +02001193 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001194 restore_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001195#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001197 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001200 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001201 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001202 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001208#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -07001209 if (codec->power_on)
1210 hda_call_pm_notify(codec->bus, false);
1211#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001212 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001213 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001214 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001215 kfree(codec->vendor_name);
1216 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001217 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001218 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 kfree(codec);
1220}
1221
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001222static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1223 hda_nid_t fg, unsigned int power_state);
1224
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001225static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1226 unsigned int power_state);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/**
1229 * snd_hda_codec_new - create a HDA codec
1230 * @bus: the bus to assign
1231 * @codec_addr: the codec address
1232 * @codecp: the pointer to store the generated codec
1233 *
1234 * Returns 0 if successful, or a negative error code.
1235 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001236int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1237 unsigned int codec_addr,
1238 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001241 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int err;
1243
Takashi Iwaida3cec32008-08-08 17:12:14 +02001244 if (snd_BUG_ON(!bus))
1245 return -EINVAL;
1246 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001250 snd_printk(KERN_ERR "hda_codec: "
1251 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EBUSY;
1253 }
1254
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001255 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (codec == NULL) {
1257 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1258 return -ENOMEM;
1259 }
1260
1261 codec->bus = bus;
1262 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001263 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001264 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001265 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001266 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001267 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001268 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1269 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001270 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001271 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001272 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001273 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001274 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Takashi Iwai83012a72012-08-24 18:38:08 +02001276#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001277 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001278 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1279 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1280 * the caller has to power down appropriatley after initialization
1281 * phase.
1282 */
1283 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001284 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001285#endif
1286
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001287 if (codec->bus->modelname) {
1288 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1289 if (!codec->modelname) {
1290 snd_hda_codec_free(codec);
1291 return -ENODEV;
1292 }
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 list_add_tail(&codec->list, &bus->codec_list);
1296 bus->caddr_tbl[codec_addr] = codec;
1297
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001300 if (codec->vendor_id == -1)
1301 /* read again, hopefully the access method was corrected
1302 * in the last read...
1303 */
1304 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1305 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001306 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1307 AC_PAR_SUBSYSTEM_ID);
1308 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1309 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001311 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001313 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = -ENODEV;
1315 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Takashi Iwai3be14142009-02-20 14:11:16 +01001318 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1319 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001320 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001321 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001322 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001323 err = read_pin_defaults(codec);
1324 if (err < 0)
1325 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001326
Takashi Iwai0ba21762007-04-16 11:29:14 +02001327 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001328 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001329 codec->subsystem_id =
1330 snd_hda_codec_read(codec, nid, 0,
1331 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001332 }
1333
Takashi Iwai83012a72012-08-24 18:38:08 +02001334#ifdef CONFIG_PM
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001335 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec,
1336 codec->afg ? codec->afg : codec->mfg,
1337 AC_PWRST_CLKSTOP);
1338 if (!codec->d3_stop_clk)
1339 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001340#endif
Takashi Iwai983f6b92012-08-28 09:18:01 -07001341 codec->epss = snd_hda_codec_get_supported_ps(codec,
1342 codec->afg ? codec->afg : codec->mfg,
1343 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001344
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001345 /* power-up all before initialization */
1346 hda_set_power_state(codec,
1347 codec->afg ? codec->afg : codec->mfg,
1348 AC_PWRST_D0);
1349
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001350 snd_hda_codec_proc_new(codec);
1351
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353
1354 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1355 codec->subsystem_id, codec->revision_id);
1356 snd_component_add(codec->bus->card, component);
1357
1358 if (codecp)
1359 *codecp = codec;
1360 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001361
1362 error:
1363 snd_hda_codec_free(codec);
1364 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001366EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001367
Takashi Iwaid5191e52009-11-16 14:58:17 +01001368/**
1369 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1370 * @codec: the HDA codec
1371 *
1372 * Start parsing of the given codec tree and (re-)initialize the whole
1373 * patch instance.
1374 *
1375 * Returns 0 if successful or a negative error code.
1376 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001377int snd_hda_codec_configure(struct hda_codec *codec)
1378{
1379 int err;
1380
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001381 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001382 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001383 err = get_codec_name(codec);
1384 if (err < 0)
1385 return err;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Takashi Iwai82467612007-07-27 19:15:54 +02001388 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001390 goto patched;
1391 }
Takashi Iwai82467612007-07-27 19:15:54 +02001392 if (codec->preset && codec->preset->patch) {
1393 err = codec->preset->patch(codec);
1394 goto patched;
1395 }
1396
1397 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001398 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001399 if (err < 0)
1400 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001401
1402 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001403 if (!err && codec->patch_ops.unsol_event)
1404 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001405 /* audio codec should override the mixer name */
1406 if (!err && (codec->afg || !*codec->bus->card->mixername))
1407 snprintf(codec->bus->card->mixername,
1408 sizeof(codec->bus->card->mixername),
1409 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001412EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Takashi Iwaied360812012-08-08 17:12:52 +02001414/* update the stream-id if changed */
1415static void update_pcm_stream_id(struct hda_codec *codec,
1416 struct hda_cvt_setup *p, hda_nid_t nid,
1417 u32 stream_tag, int channel_id)
1418{
1419 unsigned int oldval, newval;
1420
1421 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1422 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1423 newval = (stream_tag << 4) | channel_id;
1424 if (oldval != newval)
1425 snd_hda_codec_write(codec, nid, 0,
1426 AC_VERB_SET_CHANNEL_STREAMID,
1427 newval);
1428 p->stream_tag = stream_tag;
1429 p->channel_id = channel_id;
1430 }
1431}
1432
1433/* update the format-id if changed */
1434static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1435 hda_nid_t nid, int format)
1436{
1437 unsigned int oldval;
1438
1439 if (p->format_id != format) {
1440 oldval = snd_hda_codec_read(codec, nid, 0,
1441 AC_VERB_GET_STREAM_FORMAT, 0);
1442 if (oldval != format) {
1443 msleep(1);
1444 snd_hda_codec_write(codec, nid, 0,
1445 AC_VERB_SET_STREAM_FORMAT,
1446 format);
1447 }
1448 p->format_id = format;
1449 }
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/**
1453 * snd_hda_codec_setup_stream - set up the codec for streaming
1454 * @codec: the CODEC to set up
1455 * @nid: the NID to set up
1456 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1457 * @channel_id: channel id to pass, zero based.
1458 * @format: stream format.
1459 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001460void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1461 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int channel_id, int format)
1463{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001464 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001465 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001466 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001467 int i;
1468
Takashi Iwai0ba21762007-04-16 11:29:14 +02001469 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001470 return;
1471
Takashi Iwai0ba21762007-04-16 11:29:14 +02001472 snd_printdd("hda_codec_setup_stream: "
1473 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001475 p = get_hda_cvt_setup(codec, nid);
1476 if (!p)
1477 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001478
1479 if (codec->pcm_format_first)
1480 update_pcm_format(codec, p, nid, format);
1481 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1482 if (!codec->pcm_format_first)
1483 update_pcm_format(codec, p, nid, format);
1484
Takashi Iwaieb541332010-08-06 13:48:11 +02001485 p->active = 1;
1486 p->dirty = 0;
1487
1488 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001489 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001490 list_for_each_entry(c, &codec->bus->codec_list, list) {
1491 for (i = 0; i < c->cvt_setups.used; i++) {
1492 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001493 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001494 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001495 p->dirty = 1;
1496 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001499EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Takashi Iwaif0cea792010-08-13 11:56:53 +02001501static void really_cleanup_stream(struct hda_codec *codec,
1502 struct hda_cvt_setup *q);
1503
Takashi Iwaid5191e52009-11-16 14:58:17 +01001504/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001505 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001506 * @codec: the CODEC to clean up
1507 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001508 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001509 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001510void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1511 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001512{
Takashi Iwaieb541332010-08-06 13:48:11 +02001513 struct hda_cvt_setup *p;
1514
Takashi Iwai888afa12008-03-18 09:57:50 +01001515 if (!nid)
1516 return;
1517
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001518 if (codec->no_sticky_stream)
1519 do_now = 1;
1520
Takashi Iwai888afa12008-03-18 09:57:50 +01001521 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001522 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001523 if (p) {
1524 /* here we just clear the active flag when do_now isn't set;
1525 * actual clean-ups will be done later in
1526 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1527 */
1528 if (do_now)
1529 really_cleanup_stream(codec, p);
1530 else
1531 p->active = 0;
1532 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001533}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001534EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001535
Takashi Iwaieb541332010-08-06 13:48:11 +02001536static void really_cleanup_stream(struct hda_codec *codec,
1537 struct hda_cvt_setup *q)
1538{
1539 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001540 if (q->stream_tag || q->channel_id)
1541 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1542 if (q->format_id)
1543 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1544);
Takashi Iwaieb541332010-08-06 13:48:11 +02001545 memset(q, 0, sizeof(*q));
1546 q->nid = nid;
1547}
1548
1549/* clean up the all conflicting obsolete streams */
1550static void purify_inactive_streams(struct hda_codec *codec)
1551{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001552 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001553 int i;
1554
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001555 list_for_each_entry(c, &codec->bus->codec_list, list) {
1556 for (i = 0; i < c->cvt_setups.used; i++) {
1557 struct hda_cvt_setup *p;
1558 p = snd_array_elem(&c->cvt_setups, i);
1559 if (p->dirty)
1560 really_cleanup_stream(c, p);
1561 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001562 }
1563}
1564
Takashi Iwai2a439522011-07-26 09:52:50 +02001565#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001566/* clean up all streams; called from suspend */
1567static void hda_cleanup_all_streams(struct hda_codec *codec)
1568{
1569 int i;
1570
1571 for (i = 0; i < codec->cvt_setups.used; i++) {
1572 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1573 if (p->stream_tag)
1574 really_cleanup_stream(codec, p);
1575 }
1576}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001577#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579/*
1580 * amp access functions
1581 */
1582
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001583/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001584#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001585#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001586#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1587#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001589#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001592static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001593 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Takashi Iwai01751f52007-08-10 16:59:39 +02001595 memset(cache, 0, sizeof(*cache));
1596 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001597 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001598}
1599
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001600static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001601{
Takashi Iwai603c4012008-07-30 15:01:44 +02001602 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001606static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Takashi Iwai01751f52007-08-10 16:59:39 +02001608 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1609 u16 cur = cache->hash[idx];
1610 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001613 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (info->key == key)
1615 return info;
1616 cur = info->next;
1617 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001618 return NULL;
1619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001621/* query the hash. allocate an entry if not found. */
1622static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1623 u32 key)
1624{
1625 struct hda_cache_head *info = get_hash(cache, key);
1626 if (!info) {
1627 u16 idx, cur;
1628 /* add a new hash entry */
1629 info = snd_array_new(&cache->buf);
1630 if (!info)
1631 return NULL;
1632 cur = snd_array_index(&cache->buf, info);
1633 info->key = key;
1634 info->val = 0;
1635 idx = key % (u16)ARRAY_SIZE(cache->hash);
1636 info->next = cache->hash[idx];
1637 cache->hash[idx] = cur;
1638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return info;
1640}
1641
Takashi Iwai01751f52007-08-10 16:59:39 +02001642/* query and allocate an amp hash entry */
1643static inline struct hda_amp_info *
1644get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1645{
1646 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1647}
1648
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001649/* overwrite the value with the key in the caps hash */
1650static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1651{
1652 struct hda_amp_info *info;
1653
1654 mutex_lock(&codec->hash_mutex);
1655 info = get_alloc_amp_hash(codec, key);
1656 if (!info) {
1657 mutex_unlock(&codec->hash_mutex);
1658 return -EINVAL;
1659 }
1660 info->amp_caps = val;
1661 info->head.val |= INFO_AMP_CAPS;
1662 mutex_unlock(&codec->hash_mutex);
1663 return 0;
1664}
1665
1666/* query the value from the caps hash; if not found, fetch the current
1667 * value from the given function and store in the hash
1668 */
1669static unsigned int
1670query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1671 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1672{
1673 struct hda_amp_info *info;
1674 unsigned int val;
1675
1676 mutex_lock(&codec->hash_mutex);
1677 info = get_alloc_amp_hash(codec, key);
1678 if (!info) {
1679 mutex_unlock(&codec->hash_mutex);
1680 return 0;
1681 }
1682 if (!(info->head.val & INFO_AMP_CAPS)) {
1683 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1684 val = func(codec, nid, dir);
1685 write_caps_hash(codec, key, val);
1686 } else {
1687 val = info->amp_caps;
1688 mutex_unlock(&codec->hash_mutex);
1689 }
1690 return val;
1691}
1692
1693static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1694 int direction)
1695{
1696 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1697 nid = codec->afg;
1698 return snd_hda_param_read(codec, nid,
1699 direction == HDA_OUTPUT ?
1700 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1701}
1702
Takashi Iwaid5191e52009-11-16 14:58:17 +01001703/**
1704 * query_amp_caps - query AMP capabilities
1705 * @codec: the HD-auio codec
1706 * @nid: the NID to query
1707 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1708 *
1709 * Query AMP capabilities for the given widget and direction.
1710 * Returns the obtained capability bits.
1711 *
1712 * When cap bits have been already read, this doesn't read again but
1713 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001715u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001717 return query_caps_hash(codec, nid, direction,
1718 HDA_HASH_KEY(nid, direction, 0),
1719 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001721EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Takashi Iwaid5191e52009-11-16 14:58:17 +01001723/**
1724 * snd_hda_override_amp_caps - Override the AMP capabilities
1725 * @codec: the CODEC to clean up
1726 * @nid: the NID to clean up
1727 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1728 * @caps: the capability bits to set
1729 *
1730 * Override the cached AMP caps bits value by the given one.
1731 * This function is useful if the driver needs to adjust the AMP ranges,
1732 * e.g. limit to 0dB, etc.
1733 *
1734 * Returns zero if successful or a negative error code.
1735 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001736int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1737 unsigned int caps)
1738{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001739 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001740}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001741EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001742
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001743static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1744 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001745{
1746 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1747}
1748
Takashi Iwaid5191e52009-11-16 14:58:17 +01001749/**
1750 * snd_hda_query_pin_caps - Query PIN capabilities
1751 * @codec: the HD-auio codec
1752 * @nid: the NID to query
1753 *
1754 * Query PIN capabilities for the given widget.
1755 * Returns the obtained capability bits.
1756 *
1757 * When cap bits have been already read, this doesn't read again but
1758 * returns the cached value.
1759 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001760u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1761{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001763 read_pin_cap);
1764}
Takashi Iwai1327a322009-03-23 13:07:47 +01001765EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1766
Wu Fengguang864f92b2009-11-18 12:38:02 +08001767/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001768 * snd_hda_override_pin_caps - Override the pin capabilities
1769 * @codec: the CODEC
1770 * @nid: the NID to override
1771 * @caps: the capability bits to set
1772 *
1773 * Override the cached PIN capabilitiy bits value by the given one.
1774 *
1775 * Returns zero if successful or a negative error code.
1776 */
1777int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1778 unsigned int caps)
1779{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001780 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001781}
1782EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1783
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001784/* read or sync the hash value with the current value;
1785 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001787static struct hda_amp_info *
1788update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1789 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001791 struct hda_amp_info *info;
1792 unsigned int parm, val = 0;
1793 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795 retry:
1796 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1797 if (!info)
1798 return NULL;
1799 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1800 if (!val_read) {
1801 mutex_unlock(&codec->hash_mutex);
1802 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1803 parm |= direction == HDA_OUTPUT ?
1804 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1805 parm |= index;
1806 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001807 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001808 val &= 0xff;
1809 val_read = true;
1810 mutex_lock(&codec->hash_mutex);
1811 goto retry;
1812 }
1813 info->vol[ch] = val;
1814 info->head.val |= INFO_AMP_VOL(ch);
1815 }
1816 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001820 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001822static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001823 hda_nid_t nid, int ch, int direction, int index,
1824 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 u32 parm;
1827
1828 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1829 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1830 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001831 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1832 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1833 ; /* set the zero value as a fake mute */
1834 else
1835 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1837}
1838
Takashi Iwaid5191e52009-11-16 14:58:17 +01001839/**
1840 * snd_hda_codec_amp_read - Read AMP value
1841 * @codec: HD-audio codec
1842 * @nid: NID to read the AMP value
1843 * @ch: channel (left=0 or right=1)
1844 * @direction: #HDA_INPUT or #HDA_OUTPUT
1845 * @index: the index value (only for input direction)
1846 *
1847 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
Takashi Iwai834be882006-03-01 14:16:17 +01001849int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1850 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001852 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001853 unsigned int val = 0;
1854
1855 mutex_lock(&codec->hash_mutex);
1856 info = update_amp_hash(codec, nid, ch, direction, index);
1857 if (info)
1858 val = info->vol[ch];
1859 mutex_unlock(&codec->hash_mutex);
1860 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001862EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Takashi Iwaid5191e52009-11-16 14:58:17 +01001864/**
1865 * snd_hda_codec_amp_update - update the AMP value
1866 * @codec: HD-audio codec
1867 * @nid: NID to read the AMP value
1868 * @ch: channel (left=0 or right=1)
1869 * @direction: #HDA_INPUT or #HDA_OUTPUT
1870 * @idx: the index value (only for input direction)
1871 * @mask: bit mask to set
1872 * @val: the bits value to set
1873 *
1874 * Update the AMP value with a bit mask.
1875 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001876 */
Takashi Iwai834be882006-03-01 14:16:17 +01001877int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1878 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001880 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001881
Takashi Iwai467126462010-03-29 09:19:38 +02001882 if (snd_BUG_ON(mask & ~0xff))
1883 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001884 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001885
1886 mutex_lock(&codec->hash_mutex);
1887 info = update_amp_hash(codec, nid, ch, direction, idx);
1888 if (!info) {
1889 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001891 }
1892 val |= info->vol[ch] & ~mask;
1893 if (info->vol[ch] == val) {
1894 mutex_unlock(&codec->hash_mutex);
1895 return 0;
1896 }
1897 info->vol[ch] = val;
1898 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001899 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 1;
1901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001902EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Takashi Iwaid5191e52009-11-16 14:58:17 +01001904/**
1905 * snd_hda_codec_amp_stereo - update the AMP stereo values
1906 * @codec: HD-audio codec
1907 * @nid: NID to read the AMP value
1908 * @direction: #HDA_INPUT or #HDA_OUTPUT
1909 * @idx: the index value (only for input direction)
1910 * @mask: bit mask to set
1911 * @val: the bits value to set
1912 *
1913 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1914 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001915 */
1916int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1917 int direction, int idx, int mask, int val)
1918{
1919 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001920
1921 if (snd_BUG_ON(mask & ~0xff))
1922 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001923 for (ch = 0; ch < 2; ch++)
1924 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1925 idx, mask, val);
1926 return ret;
1927}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001928EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001929
Takashi Iwai2a439522011-07-26 09:52:50 +02001930#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001931/**
1932 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1933 * @codec: HD-audio codec
1934 *
1935 * Resume the all amp commands from the cache.
1936 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001937void snd_hda_codec_resume_amp(struct hda_codec *codec)
1938{
Takashi Iwai603c4012008-07-30 15:01:44 +02001939 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001940 int i;
1941
Takashi Iwai603c4012008-07-30 15:01:44 +02001942 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001943 u32 key = buffer->head.key;
1944 hda_nid_t nid;
1945 unsigned int idx, dir, ch;
1946 if (!key)
1947 continue;
1948 nid = key & 0xff;
1949 idx = (key >> 16) & 0xff;
1950 dir = (key >> 24) & 0xff;
1951 for (ch = 0; ch < 2; ch++) {
1952 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1953 continue;
1954 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1955 buffer->vol[ch]);
1956 }
1957 }
1958}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001959EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001960#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001962static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1963 unsigned int ofs)
1964{
1965 u32 caps = query_amp_caps(codec, nid, dir);
1966 /* get num steps */
1967 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1968 if (ofs < caps)
1969 caps -= ofs;
1970 return caps;
1971}
1972
Takashi Iwaid5191e52009-11-16 14:58:17 +01001973/**
1974 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1975 *
1976 * The control element is supposed to have the private_value field
1977 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1978 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001979int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1983 u16 nid = get_amp_nid(kcontrol);
1984 u8 chs = get_amp_channels(kcontrol);
1985 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001986 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001988 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1989 uinfo->count = chs == 3 ? 2 : 1;
1990 uinfo->value.integer.min = 0;
1991 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1992 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001993 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001994 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1995 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return -EINVAL;
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002000EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002
2003static inline unsigned int
2004read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2005 int ch, int dir, int idx, unsigned int ofs)
2006{
2007 unsigned int val;
2008 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2009 val &= HDA_AMP_VOLMASK;
2010 if (val >= ofs)
2011 val -= ofs;
2012 else
2013 val = 0;
2014 return val;
2015}
2016
2017static inline int
2018update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2019 int ch, int dir, int idx, unsigned int ofs,
2020 unsigned int val)
2021{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002022 unsigned int maxval;
2023
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 if (val > 0)
2025 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002026 /* ofs = 0: raw max value */
2027 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002028 if (val > maxval)
2029 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002030 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2031 HDA_AMP_VOLMASK, val);
2032}
2033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2036 *
2037 * The control element is supposed to have the private_value field
2038 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002040int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2044 hda_nid_t nid = get_amp_nid(kcontrol);
2045 int chs = get_amp_channels(kcontrol);
2046 int dir = get_amp_direction(kcontrol);
2047 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002048 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 long *valp = ucontrol->value.integer.value;
2050
2051 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002052 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002054 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
2056}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002057EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Takashi Iwaid5191e52009-11-16 14:58:17 +01002059/**
2060 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2061 *
2062 * The control element is supposed to have the private_value field
2063 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2064 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002065int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 hda_nid_t nid = get_amp_nid(kcontrol);
2070 int chs = get_amp_channels(kcontrol);
2071 int dir = get_amp_direction(kcontrol);
2072 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002073 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 long *valp = ucontrol->value.integer.value;
2075 int change = 0;
2076
Takashi Iwaicb53c622007-08-10 17:21:45 +02002077 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002078 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002079 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002080 valp++;
2081 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002082 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002083 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002084 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return change;
2086}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002087EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwaid5191e52009-11-16 14:58:17 +01002089/**
2090 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2091 *
2092 * The control element is supposed to have the private_value field
2093 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2094 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002095int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2096 unsigned int size, unsigned int __user *_tlv)
2097{
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 hda_nid_t nid = get_amp_nid(kcontrol);
2100 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002101 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002102 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002103 u32 caps, val1, val2;
2104
2105 if (size < 4 * sizeof(unsigned int))
2106 return -ENOMEM;
2107 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2109 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002110 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002111 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002112 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002113 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002114 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002115 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2116 return -EFAULT;
2117 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2118 return -EFAULT;
2119 if (put_user(val1, _tlv + 2))
2120 return -EFAULT;
2121 if (put_user(val2, _tlv + 3))
2122 return -EFAULT;
2123 return 0;
2124}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002125EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002126
Takashi Iwaid5191e52009-11-16 14:58:17 +01002127/**
2128 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2129 * @codec: HD-audio codec
2130 * @nid: NID of a reference widget
2131 * @dir: #HDA_INPUT or #HDA_OUTPUT
2132 * @tlv: TLV data to be stored, at least 4 elements
2133 *
2134 * Set (static) TLV data for a virtual master volume using the AMP caps
2135 * obtained from the reference NID.
2136 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002137 */
2138void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2139 unsigned int *tlv)
2140{
2141 u32 caps;
2142 int nums, step;
2143
2144 caps = query_amp_caps(codec, nid, dir);
2145 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2146 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2147 step = (step + 1) * 25;
2148 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2149 tlv[1] = 2 * sizeof(unsigned int);
2150 tlv[2] = -nums * step;
2151 tlv[3] = step;
2152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002153EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002154
2155/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002156static struct snd_kcontrol *
2157_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2158 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002159{
2160 struct snd_ctl_elem_id id;
2161 memset(&id, 0, sizeof(id));
2162 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002163 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002164 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2165 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002166 strcpy(id.name, name);
2167 return snd_ctl_find_id(codec->bus->card, &id);
2168}
2169
Takashi Iwaid5191e52009-11-16 14:58:17 +01002170/**
2171 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2172 * @codec: HD-audio codec
2173 * @name: ctl id name string
2174 *
2175 * Get the control element with the given id string and IFACE_MIXER.
2176 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002177struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2178 const char *name)
2179{
2180 return _snd_hda_find_mixer_ctl(codec, name, 0);
2181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002182EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002183
Takashi Iwai1afe2062010-12-23 10:17:52 +01002184static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2185{
2186 int idx;
2187 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2188 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2189 return idx;
2190 }
2191 return -EBUSY;
2192}
2193
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002195 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196 * @codec: HD-audio codec
2197 * @nid: corresponding NID (optional)
2198 * @kctl: the control element to assign
2199 *
2200 * Add the given control element to an array inside the codec instance.
2201 * All control elements belonging to a codec are supposed to be added
2202 * by this function so that a proper clean-up works at the free or
2203 * reconfiguration time.
2204 *
2205 * If non-zero @nid is passed, the NID is assigned to the control element.
2206 * The assignment is shown in the codec proc file.
2207 *
2208 * snd_hda_ctl_add() checks the control subdev id field whether
2209 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002210 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2211 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002212 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002213int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2214 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002215{
2216 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002217 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002218 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002219
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002220 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002221 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002222 if (nid == 0)
2223 nid = get_amp_nid_(kctl->private_value);
2224 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002225 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2226 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002227 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002228 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002229 err = snd_ctl_add(codec->bus->card, kctl);
2230 if (err < 0)
2231 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002232 item = snd_array_new(&codec->mixers);
2233 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002234 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002235 item->kctl = kctl;
2236 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002237 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002238 return 0;
2239}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002240EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002241
Takashi Iwaid5191e52009-11-16 14:58:17 +01002242/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002243 * snd_hda_add_nid - Assign a NID to a control element
2244 * @codec: HD-audio codec
2245 * @nid: corresponding NID (optional)
2246 * @kctl: the control element to assign
2247 * @index: index to kctl
2248 *
2249 * Add the given control element to an array inside the codec instance.
2250 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2251 * NID:KCTL mapping - for example "Capture Source" selector.
2252 */
2253int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2254 unsigned int index, hda_nid_t nid)
2255{
2256 struct hda_nid_item *item;
2257
2258 if (nid > 0) {
2259 item = snd_array_new(&codec->nids);
2260 if (!item)
2261 return -ENOMEM;
2262 item->kctl = kctl;
2263 item->index = index;
2264 item->nid = nid;
2265 return 0;
2266 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002267 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2268 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002269 return -EINVAL;
2270}
2271EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2272
2273/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002274 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2275 * @codec: HD-audio codec
2276 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002277void snd_hda_ctls_clear(struct hda_codec *codec)
2278{
2279 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002280 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002281 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002282 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002283 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002284 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002285}
2286
Takashi Iwaia65d6292009-02-23 16:57:04 +01002287/* pseudo device locking
2288 * toggle card->shutdown to allow/disallow the device access (as a hack)
2289 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002290int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002291{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002292 struct snd_card *card = bus->card;
2293 struct hda_codec *codec;
2294
Takashi Iwaia65d6292009-02-23 16:57:04 +01002295 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002296 if (card->shutdown)
2297 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002298 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002299 if (!list_empty(&card->ctl_files))
2300 goto err_clear;
2301
2302 list_for_each_entry(codec, &bus->codec_list, list) {
2303 int pcm;
2304 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2305 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2306 if (!cpcm->pcm)
2307 continue;
2308 if (cpcm->pcm->streams[0].substream_opened ||
2309 cpcm->pcm->streams[1].substream_opened)
2310 goto err_clear;
2311 }
2312 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002313 spin_unlock(&card->files_lock);
2314 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002315
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002316 err_clear:
2317 card->shutdown = 0;
2318 err_unlock:
2319 spin_unlock(&card->files_lock);
2320 return -EINVAL;
2321}
2322EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2323
2324void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002325{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002326 struct snd_card *card = bus->card;
2327
2328 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002329 spin_lock(&card->files_lock);
2330 card->shutdown = 0;
2331 spin_unlock(&card->files_lock);
2332}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002333EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002334
Takashi Iwaid5191e52009-11-16 14:58:17 +01002335/**
2336 * snd_hda_codec_reset - Clear all objects assigned to the codec
2337 * @codec: HD-audio codec
2338 *
2339 * This frees the all PCM and control elements assigned to the codec, and
2340 * clears the caches and restores the pin default configurations.
2341 *
2342 * When a device is being used, it returns -EBSY. If successfully freed,
2343 * returns zero.
2344 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002345int snd_hda_codec_reset(struct hda_codec *codec)
2346{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002347 struct hda_bus *bus = codec->bus;
2348 struct snd_card *card = bus->card;
2349 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002350
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002351 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002352 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002353
2354 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002355
Takashi Iwai83012a72012-08-24 18:38:08 +02002356#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002357 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002358 codec->power_on = 0;
2359 codec->power_transition = 0;
2360 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002361 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002362#endif
2363 snd_hda_ctls_clear(codec);
2364 /* relase PCMs */
2365 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002366 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002367 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002368 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002369 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002370 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002371 }
2372 if (codec->patch_ops.free)
2373 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002374 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002375 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002376 codec->spec = NULL;
2377 free_hda_cache(&codec->amp_cache);
2378 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002379 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2380 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002381 /* free only driver_pins so that init_pins + user_pins are restored */
2382 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002383 restore_pincfgs(codec);
Takashi Iwai09a60712012-06-26 15:01:33 +02002384 snd_array_free(&codec->cvt_setups);
2385 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002386 codec->num_pcms = 0;
2387 codec->pcm_info = NULL;
2388 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002389 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2390 codec->slave_dig_outs = NULL;
2391 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002392 module_put(codec->owner);
2393 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002394
2395 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002396 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002397 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002398}
2399
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002400typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2401
2402/* apply the function to all matching slave ctls in the mixer list */
2403static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002404 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002405{
2406 struct hda_nid_item *items;
2407 const char * const *s;
2408 int i, err;
2409
2410 items = codec->mixers.list;
2411 for (i = 0; i < codec->mixers.used; i++) {
2412 struct snd_kcontrol *sctl = items[i].kctl;
2413 if (!sctl || !sctl->id.name ||
2414 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2415 continue;
2416 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002417 char tmpname[sizeof(sctl->id.name)];
2418 const char *name = *s;
2419 if (suffix) {
2420 snprintf(tmpname, sizeof(tmpname), "%s %s",
2421 name, suffix);
2422 name = tmpname;
2423 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002424 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002425 err = func(data, sctl);
2426 if (err)
2427 return err;
2428 break;
2429 }
2430 }
2431 }
2432 return 0;
2433}
2434
2435static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2436{
2437 return 1;
2438}
2439
Takashi Iwai18478e82012-03-09 17:51:10 +01002440/* guess the value corresponding to 0dB */
2441static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2442{
2443 int _tlv[4];
2444 const int *tlv = NULL;
2445 int val = -1;
2446
2447 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2448 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2449 mm_segment_t fs = get_fs();
2450 set_fs(get_ds());
2451 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2452 tlv = _tlv;
2453 set_fs(fs);
2454 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2455 tlv = kctl->tlv.p;
2456 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2457 val = -tlv[2] / tlv[3];
2458 return val;
2459}
2460
2461/* call kctl->put with the given value(s) */
2462static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2463{
2464 struct snd_ctl_elem_value *ucontrol;
2465 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2466 if (!ucontrol)
2467 return -ENOMEM;
2468 ucontrol->value.integer.value[0] = val;
2469 ucontrol->value.integer.value[1] = val;
2470 kctl->put(kctl, ucontrol);
2471 kfree(ucontrol);
2472 return 0;
2473}
2474
2475/* initialize the slave volume with 0dB */
2476static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2477{
2478 int offset = get_kctl_0dB_offset(slave);
2479 if (offset > 0)
2480 put_kctl_with_value(slave, offset);
2481 return 0;
2482}
2483
2484/* unmute the slave */
2485static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2486{
2487 return put_kctl_with_value(slave, 1);
2488}
2489
Takashi Iwaid5191e52009-11-16 14:58:17 +01002490/**
2491 * snd_hda_add_vmaster - create a virtual master control and add slaves
2492 * @codec: HD-audio codec
2493 * @name: vmaster control name
2494 * @tlv: TLV data (optional)
2495 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002496 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002497 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002498 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002499 *
2500 * Create a virtual master control with the given name. The TLV data
2501 * must be either NULL or a valid data.
2502 *
2503 * @slaves is a NULL-terminated array of strings, each of which is a
2504 * slave control name. All controls with these names are assigned to
2505 * the new virtual master control.
2506 *
2507 * This function returns zero if successful or a negative error code.
2508 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002509int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002510 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002511 const char *suffix, bool init_slave_vol,
2512 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002513{
2514 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002515 int err;
2516
Takashi Iwai29e58532012-03-12 12:25:03 +01002517 if (ctl_ret)
2518 *ctl_ret = NULL;
2519
Takashi Iwai9322ca52012-02-03 14:28:01 +01002520 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002521 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002522 snd_printdd("No slave found for %s\n", name);
2523 return 0;
2524 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002525 kctl = snd_ctl_make_virtual_master(name, tlv);
2526 if (!kctl)
2527 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002528 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002529 if (err < 0)
2530 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002531
Takashi Iwai9322ca52012-02-03 14:28:01 +01002532 err = map_slaves(codec, slaves, suffix,
2533 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002534 if (err < 0)
2535 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002536
2537 /* init with master mute & zero volume */
2538 put_kctl_with_value(kctl, 0);
2539 if (init_slave_vol)
2540 map_slaves(codec, slaves, suffix,
2541 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2542
Takashi Iwai29e58532012-03-12 12:25:03 +01002543 if (ctl_ret)
2544 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002545 return 0;
2546}
Takashi Iwai18478e82012-03-09 17:51:10 +01002547EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002548
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002549/*
2550 * mute-LED control using vmaster
2551 */
2552static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_info *uinfo)
2554{
2555 static const char * const texts[] = {
2556 "Off", "On", "Follow Master"
2557 };
2558 unsigned int index;
2559
2560 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2561 uinfo->count = 1;
2562 uinfo->value.enumerated.items = 3;
2563 index = uinfo->value.enumerated.item;
2564 if (index >= 3)
2565 index = 2;
2566 strcpy(uinfo->value.enumerated.name, texts[index]);
2567 return 0;
2568}
2569
2570static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2571 struct snd_ctl_elem_value *ucontrol)
2572{
2573 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2574 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2575 return 0;
2576}
2577
2578static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_value *ucontrol)
2580{
2581 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2582 unsigned int old_mode = hook->mute_mode;
2583
2584 hook->mute_mode = ucontrol->value.enumerated.item[0];
2585 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2586 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2587 if (old_mode == hook->mute_mode)
2588 return 0;
2589 snd_hda_sync_vmaster_hook(hook);
2590 return 1;
2591}
2592
2593static struct snd_kcontrol_new vmaster_mute_mode = {
2594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2595 .name = "Mute-LED Mode",
2596 .info = vmaster_mute_mode_info,
2597 .get = vmaster_mute_mode_get,
2598 .put = vmaster_mute_mode_put,
2599};
2600
2601/*
2602 * Add a mute-LED hook with the given vmaster switch kctl
2603 * "Mute-LED Mode" control is automatically created and associated with
2604 * the given hook.
2605 */
2606int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002607 struct hda_vmaster_mute_hook *hook,
2608 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002609{
2610 struct snd_kcontrol *kctl;
2611
2612 if (!hook->hook || !hook->sw_kctl)
2613 return 0;
2614 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2615 hook->codec = codec;
2616 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002617 if (!expose_enum_ctl)
2618 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002619 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2620 if (!kctl)
2621 return -ENOMEM;
2622 return snd_hda_ctl_add(codec, 0, kctl);
2623}
2624EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2625
2626/*
2627 * Call the hook with the current value for synchronization
2628 * Should be called in init callback
2629 */
2630void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2631{
2632 if (!hook->hook || !hook->codec)
2633 return;
2634 switch (hook->mute_mode) {
2635 case HDA_VMUTE_FOLLOW_MASTER:
2636 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2637 break;
2638 default:
2639 hook->hook(hook->codec, hook->mute_mode);
2640 break;
2641 }
2642}
2643EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2644
2645
Takashi Iwaid5191e52009-11-16 14:58:17 +01002646/**
2647 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2648 *
2649 * The control element is supposed to have the private_value field
2650 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2651 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002652int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2653 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
2655 int chs = get_amp_channels(kcontrol);
2656
2657 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2658 uinfo->count = chs == 3 ? 2 : 1;
2659 uinfo->value.integer.min = 0;
2660 uinfo->value.integer.max = 1;
2661 return 0;
2662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002663EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Takashi Iwaid5191e52009-11-16 14:58:17 +01002665/**
2666 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2667 *
2668 * The control element is supposed to have the private_value field
2669 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2670 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002671int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2675 hda_nid_t nid = get_amp_nid(kcontrol);
2676 int chs = get_amp_channels(kcontrol);
2677 int dir = get_amp_direction(kcontrol);
2678 int idx = get_amp_index(kcontrol);
2679 long *valp = ucontrol->value.integer.value;
2680
2681 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002683 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002685 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002686 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return 0;
2688}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002689EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwaid5191e52009-11-16 14:58:17 +01002691/**
2692 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2693 *
2694 * The control element is supposed to have the private_value field
2695 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2696 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2701 hda_nid_t nid = get_amp_nid(kcontrol);
2702 int chs = get_amp_channels(kcontrol);
2703 int dir = get_amp_direction(kcontrol);
2704 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 long *valp = ucontrol->value.integer.value;
2706 int change = 0;
2707
Takashi Iwaicb53c622007-08-10 17:21:45 +02002708 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002709 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002710 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002711 HDA_AMP_MUTE,
2712 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002713 valp++;
2714 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002715 if (chs & 2)
2716 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002717 HDA_AMP_MUTE,
2718 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002719 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002720 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return change;
2722}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002723EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725/*
Takashi Iwai985be542005-11-02 18:26:49 +01002726 * bound volume controls
2727 *
2728 * bind multiple volumes (# indices, from 0)
2729 */
2730
2731#define AMP_VAL_IDX_SHIFT 19
2732#define AMP_VAL_IDX_MASK (0x0f<<19)
2733
Takashi Iwaid5191e52009-11-16 14:58:17 +01002734/**
2735 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2736 *
2737 * The control element is supposed to have the private_value field
2738 * set up via HDA_BIND_MUTE*() macros.
2739 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002740int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002742{
2743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2744 unsigned long pval;
2745 int err;
2746
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002747 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002748 pval = kcontrol->private_value;
2749 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2750 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2751 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002752 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002753 return err;
2754}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002755EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002756
Takashi Iwaid5191e52009-11-16 14:58:17 +01002757/**
2758 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2759 *
2760 * The control element is supposed to have the private_value field
2761 * set up via HDA_BIND_MUTE*() macros.
2762 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002763int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002765{
2766 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2767 unsigned long pval;
2768 int i, indices, err = 0, change = 0;
2769
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002770 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002771 pval = kcontrol->private_value;
2772 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2773 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002774 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2775 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002776 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2777 if (err < 0)
2778 break;
2779 change |= err;
2780 }
2781 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002782 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002783 return err < 0 ? err : change;
2784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002785EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002786
Takashi Iwaid5191e52009-11-16 14:58:17 +01002787/**
2788 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2789 *
2790 * The control element is supposed to have the private_value field
2791 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002792 */
2793int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2794 struct snd_ctl_elem_info *uinfo)
2795{
2796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2797 struct hda_bind_ctls *c;
2798 int err;
2799
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002800 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002801 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002802 kcontrol->private_value = *c->values;
2803 err = c->ops->info(kcontrol, uinfo);
2804 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002805 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002806 return err;
2807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002808EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809
Takashi Iwaid5191e52009-11-16 14:58:17 +01002810/**
2811 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2812 *
2813 * The control element is supposed to have the private_value field
2814 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2815 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002816int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
2818{
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2820 struct hda_bind_ctls *c;
2821 int err;
2822
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002823 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002824 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002825 kcontrol->private_value = *c->values;
2826 err = c->ops->get(kcontrol, ucontrol);
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;
2830}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002831EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002832
Takashi Iwaid5191e52009-11-16 14:58:17 +01002833/**
2834 * snd_hda_mixer_bind_ctls_put - Put 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() or HDA_BIND_SW() macros.
2838 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002839int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
2842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2843 struct hda_bind_ctls *c;
2844 unsigned long *vals;
2845 int err = 0, change = 0;
2846
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002847 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002848 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002849 for (vals = c->values; *vals; vals++) {
2850 kcontrol->private_value = *vals;
2851 err = c->ops->put(kcontrol, ucontrol);
2852 if (err < 0)
2853 break;
2854 change |= err;
2855 }
2856 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002857 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002858 return err < 0 ? err : change;
2859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002860EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002861
Takashi Iwaid5191e52009-11-16 14:58:17 +01002862/**
2863 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2864 *
2865 * The control element is supposed to have the private_value field
2866 * set up via HDA_BIND_VOL() macro.
2867 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002868int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2869 unsigned int size, unsigned int __user *tlv)
2870{
2871 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2872 struct hda_bind_ctls *c;
2873 int err;
2874
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002875 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002876 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002877 kcontrol->private_value = *c->values;
2878 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2879 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002881 return err;
2882}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002883EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002884
2885struct hda_ctl_ops snd_hda_bind_vol = {
2886 .info = snd_hda_mixer_amp_volume_info,
2887 .get = snd_hda_mixer_amp_volume_get,
2888 .put = snd_hda_mixer_amp_volume_put,
2889 .tlv = snd_hda_mixer_amp_tlv
2890};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002891EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002892
2893struct hda_ctl_ops snd_hda_bind_sw = {
2894 .info = snd_hda_mixer_amp_switch_info,
2895 .get = snd_hda_mixer_amp_switch_get,
2896 .put = snd_hda_mixer_amp_switch_put,
2897 .tlv = snd_hda_mixer_amp_tlv
2898};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002899EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002900
2901/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 * SPDIF out controls
2903 */
2904
Takashi Iwai0ba21762007-04-16 11:29:14 +02002905static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2906 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2909 uinfo->count = 1;
2910 return 0;
2911}
2912
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2917 IEC958_AES0_NONAUDIO |
2918 IEC958_AES0_CON_EMPHASIS_5015 |
2919 IEC958_AES0_CON_NOT_COPYRIGHT;
2920 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2921 IEC958_AES1_CON_ORIGINAL;
2922 return 0;
2923}
2924
Takashi Iwai0ba21762007-04-16 11:29:14 +02002925static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
2928 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2929 IEC958_AES0_NONAUDIO |
2930 IEC958_AES0_PRO_EMPHASIS_5015;
2931 return 0;
2932}
2933
Takashi Iwai0ba21762007-04-16 11:29:14 +02002934static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002938 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002939 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002941 mutex_lock(&codec->spdif_mutex);
2942 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002943 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2944 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2945 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2946 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002947 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 return 0;
2950}
2951
2952/* convert from SPDIF status bits to HDA SPDIF bits
2953 * bit 0 (DigEn) is always set zero (to be filled later)
2954 */
2955static unsigned short convert_from_spdif_status(unsigned int sbits)
2956{
2957 unsigned short val = 0;
2958
2959 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002960 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002962 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002964 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2965 IEC958_AES0_PRO_EMPHASIS_5015)
2966 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002968 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2969 IEC958_AES0_CON_EMPHASIS_5015)
2970 val |= AC_DIG1_EMPHASIS;
2971 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2972 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2976 }
2977 return val;
2978}
2979
2980/* convert to SPDIF status bits from HDA SPDIF bits
2981 */
2982static unsigned int convert_to_spdif_status(unsigned short val)
2983{
2984 unsigned int sbits = 0;
2985
Takashi Iwai0ba21762007-04-16 11:29:14 +02002986 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 sbits |= IEC958_AES0_PROFESSIONAL;
2990 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2993 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002994 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002996 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002998 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3000 sbits |= val & (0x7f << 8);
3001 }
3002 return sbits;
3003}
3004
Takashi Iwai2f728532008-09-25 16:32:41 +02003005/* set digital convert verbs both for the given NID and its slaves */
3006static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3007 int verb, int val)
3008{
Takashi Iwaidda14412011-05-02 11:29:30 +02003009 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003010
Takashi Iwai9e976972008-11-25 08:17:20 +01003011 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003012 d = codec->slave_dig_outs;
3013 if (!d)
3014 return;
3015 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003016 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003017}
3018
3019static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3020 int dig1, int dig2)
3021{
3022 if (dig1 != -1)
3023 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3024 if (dig2 != -1)
3025 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3026}
3027
Takashi Iwai0ba21762007-04-16 11:29:14 +02003028static int snd_hda_spdif_default_put(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;
3034 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 unsigned short val;
3036 int change;
3037
Ingo Molnar62932df2006-01-16 16:34:20 +01003038 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003039 spdif = snd_array_elem(&codec->spdif_out, idx);
3040 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003041 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3043 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3044 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003045 val = convert_from_spdif_status(spdif->status);
3046 val |= spdif->ctls & 1;
3047 change = spdif->ctls != val;
3048 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003049 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003050 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003051 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return change;
3053}
3054
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003055#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003061 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003062 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 mutex_lock(&codec->spdif_mutex);
3065 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003066 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003067 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return 0;
3069}
3070
Stephen Warren74b654c2011-06-01 11:14:18 -06003071static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3072 int dig1, int dig2)
3073{
3074 set_dig_out_convert(codec, nid, dig1, dig2);
3075 /* unmute amp switch (if any) */
3076 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3077 (dig1 & AC_DIG1_ENABLE))
3078 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3079 HDA_AMP_MUTE, 0);
3080}
3081
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003086 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003087 struct hda_spdif_out *spdif;
3088 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 unsigned short val;
3090 int change;
3091
Ingo Molnar62932df2006-01-16 16:34:20 +01003092 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003093 spdif = snd_array_elem(&codec->spdif_out, idx);
3094 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003095 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003097 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003098 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003099 spdif->ctls = val;
3100 if (change && nid != (u16)-1)
3101 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003102 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return change;
3104}
3105
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003106static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 {
3108 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3109 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003110 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 .info = snd_hda_spdif_mask_info,
3112 .get = snd_hda_spdif_cmask_get,
3113 },
3114 {
3115 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3116 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003117 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 .info = snd_hda_spdif_mask_info,
3119 .get = snd_hda_spdif_pmask_get,
3120 },
3121 {
3122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003123 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .info = snd_hda_spdif_mask_info,
3125 .get = snd_hda_spdif_default_get,
3126 .put = snd_hda_spdif_default_put,
3127 },
3128 {
3129 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003130 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 .info = snd_hda_spdif_out_switch_info,
3132 .get = snd_hda_spdif_out_switch_get,
3133 .put = snd_hda_spdif_out_switch_put,
3134 },
3135 { } /* end */
3136};
3137
3138/**
3139 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3140 * @codec: the HDA codec
3141 * @nid: audio out widget NID
3142 *
3143 * Creates controls related with the SPDIF output.
3144 * Called from each patch supporting the SPDIF out.
3145 *
3146 * Returns 0 if successful, or a negative error code.
3147 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003148int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3149 hda_nid_t associated_nid,
3150 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003153 struct snd_kcontrol *kctl;
3154 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003155 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003156 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Takashi Iwai1afe2062010-12-23 10:17:52 +01003158 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3159 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003160 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3161 return -EBUSY;
3162 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003163 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3165 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003166 if (!kctl)
3167 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003168 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003169 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003170 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003171 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return err;
3173 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003174 spdif->nid = cvt_nid;
3175 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003176 AC_VERB_GET_DIGI_CONVERT_1, 0);
3177 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return 0;
3179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003180EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003182/* get the hda_spdif_out entry from the given NID
3183 * call within spdif_mutex lock
3184 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003185struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3186 hda_nid_t nid)
3187{
3188 int i;
3189 for (i = 0; i < codec->spdif_out.used; i++) {
3190 struct hda_spdif_out *spdif =
3191 snd_array_elem(&codec->spdif_out, i);
3192 if (spdif->nid == nid)
3193 return spdif;
3194 }
3195 return NULL;
3196}
3197EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3198
Stephen Warren74b654c2011-06-01 11:14:18 -06003199void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3200{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003201 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003202
3203 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003204 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003205 spdif->nid = (u16)-1;
3206 mutex_unlock(&codec->spdif_mutex);
3207}
3208EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3209
3210void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3211{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003212 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003213 unsigned short val;
3214
3215 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003216 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003217 if (spdif->nid != nid) {
3218 spdif->nid = nid;
3219 val = spdif->ctls;
3220 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3221 }
3222 mutex_unlock(&codec->spdif_mutex);
3223}
3224EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003227 * SPDIF sharing with analog output
3228 */
3229static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_value *ucontrol)
3231{
3232 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3233 ucontrol->value.integer.value[0] = mout->share_spdif;
3234 return 0;
3235}
3236
3237static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3238 struct snd_ctl_elem_value *ucontrol)
3239{
3240 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3241 mout->share_spdif = !!ucontrol->value.integer.value[0];
3242 return 0;
3243}
3244
3245static struct snd_kcontrol_new spdif_share_sw = {
3246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3247 .name = "IEC958 Default PCM Playback Switch",
3248 .info = snd_ctl_boolean_mono_info,
3249 .get = spdif_share_sw_get,
3250 .put = spdif_share_sw_put,
3251};
3252
Takashi Iwaid5191e52009-11-16 14:58:17 +01003253/**
3254 * snd_hda_create_spdif_share_sw - create Default PCM switch
3255 * @codec: the HDA codec
3256 * @mout: multi-out instance
3257 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003258int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3259 struct hda_multi_out *mout)
3260{
3261 if (!mout->dig_out_nid)
3262 return 0;
3263 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003264 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3265 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003266}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003267EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003268
3269/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 * SPDIF input
3271 */
3272
3273#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3274
Takashi Iwai0ba21762007-04-16 11:29:14 +02003275static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3276 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3279
3280 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3281 return 0;
3282}
3283
Takashi Iwai0ba21762007-04-16 11:29:14 +02003284static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3285 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
3287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3288 hda_nid_t nid = kcontrol->private_value;
3289 unsigned int val = !!ucontrol->value.integer.value[0];
3290 int change;
3291
Ingo Molnar62932df2006-01-16 16:34:20 +01003292 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003294 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003296 snd_hda_codec_write_cache(codec, nid, 0,
3297 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003299 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return change;
3301}
3302
Takashi Iwai0ba21762007-04-16 11:29:14 +02003303static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3307 hda_nid_t nid = kcontrol->private_value;
3308 unsigned short val;
3309 unsigned int sbits;
3310
Andrew Paprocki3982d172007-12-19 12:13:44 +01003311 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 sbits = convert_to_spdif_status(val);
3313 ucontrol->value.iec958.status[0] = sbits;
3314 ucontrol->value.iec958.status[1] = sbits >> 8;
3315 ucontrol->value.iec958.status[2] = sbits >> 16;
3316 ucontrol->value.iec958.status[3] = sbits >> 24;
3317 return 0;
3318}
3319
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003320static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 {
3322 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003323 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 .info = snd_hda_spdif_in_switch_info,
3325 .get = snd_hda_spdif_in_switch_get,
3326 .put = snd_hda_spdif_in_switch_put,
3327 },
3328 {
3329 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3330 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003331 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 .info = snd_hda_spdif_mask_info,
3333 .get = snd_hda_spdif_in_status_get,
3334 },
3335 { } /* end */
3336};
3337
3338/**
3339 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3340 * @codec: the HDA codec
3341 * @nid: audio in widget NID
3342 *
3343 * Creates controls related with the SPDIF input.
3344 * Called from each patch supporting the SPDIF in.
3345 *
3346 * Returns 0 if successful, or a negative error code.
3347 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003348int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003351 struct snd_kcontrol *kctl;
3352 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003353 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Takashi Iwai1afe2062010-12-23 10:17:52 +01003355 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3356 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003357 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3358 return -EBUSY;
3359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3361 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003362 if (!kctl)
3363 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003365 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003366 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return err;
3368 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003370 snd_hda_codec_read(codec, nid, 0,
3371 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003372 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return 0;
3374}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003375EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Takashi Iwai2a439522011-07-26 09:52:50 +02003377#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003378/*
3379 * command cache
3380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003382/* build a 32bit cache key with the widget id and the command parameter */
3383#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3384#define get_cmd_cache_nid(key) ((key) & 0xff)
3385#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3386
3387/**
3388 * snd_hda_codec_write_cache - send a single command with caching
3389 * @codec: the HDA codec
3390 * @nid: NID to send the command
3391 * @direct: direct flag
3392 * @verb: the verb to send
3393 * @parm: the parameter for the verb
3394 *
3395 * Send a single command without waiting for response.
3396 *
3397 * Returns 0 if successful, or a negative error code.
3398 */
3399int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3400 int direct, unsigned int verb, unsigned int parm)
3401{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003402 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3403 struct hda_cache_head *c;
3404 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003405
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003406 if (err < 0)
3407 return err;
3408 /* parm may contain the verb stuff for get/set amp */
3409 verb = verb | (parm >> 8);
3410 parm &= 0xff;
3411 key = build_cmd_cache_key(nid, verb);
3412 mutex_lock(&codec->bus->cmd_mutex);
3413 c = get_alloc_hash(&codec->cmd_cache, key);
3414 if (c)
3415 c->val = parm;
3416 mutex_unlock(&codec->bus->cmd_mutex);
3417 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003419EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003420
Takashi Iwaid5191e52009-11-16 14:58:17 +01003421/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003422 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3423 * @codec: the HDA codec
3424 * @nid: NID to send the command
3425 * @direct: direct flag
3426 * @verb: the verb to send
3427 * @parm: the parameter for the verb
3428 *
3429 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3430 * command if the parameter is already identical with the cached value.
3431 * If not, it sends the command and refreshes the cache.
3432 *
3433 * Returns 0 if successful, or a negative error code.
3434 */
3435int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3436 int direct, unsigned int verb, unsigned int parm)
3437{
3438 struct hda_cache_head *c;
3439 u32 key;
3440
3441 /* parm may contain the verb stuff for get/set amp */
3442 verb = verb | (parm >> 8);
3443 parm &= 0xff;
3444 key = build_cmd_cache_key(nid, verb);
3445 mutex_lock(&codec->bus->cmd_mutex);
3446 c = get_hash(&codec->cmd_cache, key);
3447 if (c && c->val == parm) {
3448 mutex_unlock(&codec->bus->cmd_mutex);
3449 return 0;
3450 }
3451 mutex_unlock(&codec->bus->cmd_mutex);
3452 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3453}
3454EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3455
3456/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003457 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3458 * @codec: HD-audio codec
3459 *
3460 * Execute all verbs recorded in the command caches to resume.
3461 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003462void snd_hda_codec_resume_cache(struct hda_codec *codec)
3463{
Takashi Iwai603c4012008-07-30 15:01:44 +02003464 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003465 int i;
3466
Takashi Iwai603c4012008-07-30 15:01:44 +02003467 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003468 u32 key = buffer->key;
3469 if (!key)
3470 continue;
3471 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3472 get_cmd_cache_cmd(key), buffer->val);
3473 }
3474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003475EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003476
3477/**
3478 * snd_hda_sequence_write_cache - sequence writes with caching
3479 * @codec: the HDA codec
3480 * @seq: VERB array to send
3481 *
3482 * Send the commands sequentially from the given array.
3483 * Thte commands are recorded on cache for power-save and resume.
3484 * The array must be terminated with NID=0.
3485 */
3486void snd_hda_sequence_write_cache(struct hda_codec *codec,
3487 const struct hda_verb *seq)
3488{
3489 for (; seq->nid; seq++)
3490 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3491 seq->param);
3492}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003493EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003494#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003495
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003496void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3497 unsigned int power_state,
3498 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003499{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003500 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003501 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003502
Takashi Iwaicb53c622007-08-10 17:21:45 +02003503 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003504 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003505 if (!(wcaps & AC_WCAP_POWER))
3506 continue;
3507 /* don't power down the widget if it controls eapd and
3508 * EAPD_BTLENABLE is set.
3509 */
3510 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3511 get_wcaps_type(wcaps) == AC_WID_PIN &&
3512 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3513 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003514 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003515 if (eapd & 0x02)
3516 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003517 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003518 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3519 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003520 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003521}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003522EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3523
3524/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003525 * supported power states check
3526 */
3527static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3528 unsigned int power_state)
3529{
3530 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3531
Mengdong Line037cb42012-08-10 14:11:58 +02003532 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003533 return false;
3534 if (sup & power_state)
3535 return true;
3536 else
3537 return false;
3538}
3539
3540/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003541 * wait until the state is reached, returns the current state
3542 */
3543static unsigned int hda_sync_power_state(struct hda_codec *codec,
3544 hda_nid_t fg,
3545 unsigned int power_state)
3546{
3547 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3548 unsigned int state, actual_state;
3549
3550 for (;;) {
3551 state = snd_hda_codec_read(codec, fg, 0,
3552 AC_VERB_GET_POWER_STATE, 0);
3553 if (state & AC_PWRST_ERROR)
3554 break;
3555 actual_state = (state >> 4) & 0x0f;
3556 if (actual_state == power_state)
3557 break;
3558 if (time_after_eq(jiffies, end_time))
3559 break;
3560 /* wait until the codec reachs to the target state */
3561 msleep(1);
3562 }
3563 return state;
3564}
3565
3566/*
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003567 * set power state of the codec
3568 */
3569static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3570 unsigned int power_state)
3571{
Wang Xingchao09617ce2012-06-08 10:26:08 +08003572 int count;
3573 unsigned int state;
3574
Takashi Iwai83012a72012-08-24 18:38:08 +02003575#ifdef CONFIG_PM
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003576 codec->d3_stop_clk_ok = 0;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08003577#endif
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003578
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003579 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003580 if (power_state == AC_PWRST_D3) {
3581 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003582 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003583 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003584
3585 /* repeat power states setting at most 10 times*/
3586 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003587 if (codec->patch_ops.set_power_state)
3588 codec->patch_ops.set_power_state(codec, fg,
3589 power_state);
3590 else {
3591 snd_hda_codec_read(codec, fg, 0,
3592 AC_VERB_SET_POWER_STATE,
3593 power_state);
3594 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3595 true);
3596 }
3597 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003598 if (!(state & AC_PWRST_ERROR))
3599 break;
3600 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003601
Takashi Iwai83012a72012-08-24 18:38:08 +02003602#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -07003603 if (!codec->bus->power_keep_link_on && power_state == AC_PWRST_D3
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003604 && codec->d3_stop_clk && (state & AC_PWRST_CLK_STOP_OK))
3605 codec->d3_stop_clk_ok = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08003606#endif
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003607}
Takashi Iwai54d17402005-11-21 16:33:22 +01003608
Takashi Iwai11aeff02008-07-30 15:01:46 +02003609#ifdef CONFIG_SND_HDA_HWDEP
3610/* execute additional init verbs */
3611static void hda_exec_init_verbs(struct hda_codec *codec)
3612{
3613 if (codec->init_verbs.list)
3614 snd_hda_sequence_write(codec, codec->init_verbs.list);
3615}
3616#else
3617static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3618#endif
3619
Takashi Iwai2a439522011-07-26 09:52:50 +02003620#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003621/*
3622 * call suspend and power-down; used both from PM and power-save
3623 */
3624static void hda_call_codec_suspend(struct hda_codec *codec)
3625{
3626 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003627 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003628 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003629 hda_set_power_state(codec,
3630 codec->afg ? codec->afg : codec->mfg,
3631 AC_PWRST_D3);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003632 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003633 spin_lock(&codec->power_lock);
3634 snd_hda_update_power_acct(codec);
3635 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003636 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003637 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003638 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003639 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003640}
3641
3642/*
3643 * kick up codec; used both from PM and power-save
3644 */
3645static void hda_call_codec_resume(struct hda_codec *codec)
3646{
Takashi Iwai7f308302012-05-08 16:52:23 +02003647 /* set as if powered on for avoiding re-entering the resume
3648 * in the resume / power-save sequence
3649 */
3650 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003651 hda_set_power_state(codec,
3652 codec->afg ? codec->afg : codec->mfg,
3653 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003654 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003655 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003656 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003657 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003658 if (codec->patch_ops.resume)
3659 codec->patch_ops.resume(codec);
3660 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003661 if (codec->patch_ops.init)
3662 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003663 snd_hda_codec_resume_amp(codec);
3664 snd_hda_codec_resume_cache(codec);
3665 }
Takashi Iwai8a535412012-08-22 16:40:24 +02003666 snd_hda_jack_report_sync(codec);
Takashi Iwai7f308302012-05-08 16:52:23 +02003667 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003668}
Takashi Iwai2a439522011-07-26 09:52:50 +02003669#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003670
Takashi Iwai54d17402005-11-21 16:33:22 +01003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672/**
3673 * snd_hda_build_controls - build mixer controls
3674 * @bus: the BUS
3675 *
3676 * Creates mixer controls for each codec included in the bus.
3677 *
3678 * Returns 0 if successful, otherwise a negative error code.
3679 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003680int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003682 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Takashi Iwai0ba21762007-04-16 11:29:14 +02003684 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003685 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003686 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003687 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003688 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003689 err = snd_hda_codec_reset(codec);
3690 if (err < 0) {
3691 printk(KERN_ERR
3692 "hda_codec: cannot revert codec\n");
3693 return err;
3694 }
3695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003697 return 0;
3698}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003699EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003701int snd_hda_codec_build_controls(struct hda_codec *codec)
3702{
3703 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003704 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003705 /* continue to initialize... */
3706 if (codec->patch_ops.init)
3707 err = codec->patch_ops.init(codec);
3708 if (!err && codec->patch_ops.build_controls)
3709 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003710 if (err < 0)
3711 return err;
Takashi Iwai8a535412012-08-22 16:40:24 +02003712 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return 0;
3714}
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716/*
3717 * stream formats
3718 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003719struct hda_rate_tbl {
3720 unsigned int hz;
3721 unsigned int alsa_bits;
3722 unsigned int hda_fmt;
3723};
3724
Takashi Iwai92f10b32010-08-03 14:21:00 +02003725/* rate = base * mult / div */
3726#define HDA_RATE(base, mult, div) \
3727 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3728 (((div) - 1) << AC_FMT_DIV_SHIFT))
3729
Takashi Iwaibefdf312005-08-22 13:57:55 +02003730static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003732
3733 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003734 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3735 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3736 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3737 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3738 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3739 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3740 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3741 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3742 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3743 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3744 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003745#define AC_PAR_PCM_RATE_BITS 11
3746 /* up to bits 10, 384kHZ isn't supported properly */
3747
3748 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003749 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003750
Takashi Iwaibefdf312005-08-22 13:57:55 +02003751 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752};
3753
3754/**
3755 * snd_hda_calc_stream_format - calculate format bitset
3756 * @rate: the sample rate
3757 * @channels: the number of channels
3758 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3759 * @maxbps: the max. bps
3760 *
3761 * Calculate the format bitset from the given rate, channels and th PCM format.
3762 *
3763 * Return zero if invalid.
3764 */
3765unsigned int snd_hda_calc_stream_format(unsigned int rate,
3766 unsigned int channels,
3767 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003768 unsigned int maxbps,
3769 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
3771 int i;
3772 unsigned int val = 0;
3773
Takashi Iwaibefdf312005-08-22 13:57:55 +02003774 for (i = 0; rate_bits[i].hz; i++)
3775 if (rate_bits[i].hz == rate) {
3776 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 break;
3778 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003779 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 snd_printdd("invalid rate %d\n", rate);
3781 return 0;
3782 }
3783
3784 if (channels == 0 || channels > 8) {
3785 snd_printdd("invalid channels %d\n", channels);
3786 return 0;
3787 }
3788 val |= channels - 1;
3789
3790 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003791 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003792 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003793 break;
3794 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003795 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 case 20:
3798 case 24:
3799 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003800 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003801 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003803 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003805 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 break;
3807 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003808 snd_printdd("invalid format width %d\n",
3809 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return 0;
3811 }
3812
Anssi Hannula32c168c2010-08-03 13:28:57 +03003813 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003814 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return val;
3817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003818EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003820static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3821 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003822{
3823 unsigned int val = 0;
3824 if (nid != codec->afg &&
3825 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3826 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3827 if (!val || val == -1)
3828 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3829 if (!val || val == -1)
3830 return 0;
3831 return val;
3832}
3833
3834static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3835{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003836 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003837 get_pcm_param);
3838}
3839
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003840static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3841 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003842{
3843 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3844 if (!streams || streams == -1)
3845 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3846 if (!streams || streams == -1)
3847 return 0;
3848 return streams;
3849}
3850
3851static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3852{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003853 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003854 get_stream_param);
3855}
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857/**
3858 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3859 * @codec: the HDA codec
3860 * @nid: NID to query
3861 * @ratesp: the pointer to store the detected rate bitflags
3862 * @formatsp: the pointer to store the detected formats
3863 * @bpsp: the pointer to store the detected format widths
3864 *
3865 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3866 * or @bsps argument is ignored.
3867 *
3868 * Returns 0 if successful, otherwise a negative error code.
3869 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003870int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3872{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003873 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003875 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003876 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 if (ratesp) {
3879 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003880 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003882 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003884 if (rates == 0) {
3885 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3886 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3887 nid, val,
3888 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3889 return -EIO;
3890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 *ratesp = rates;
3892 }
3893
3894 if (formatsp || bpsp) {
3895 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003896 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003898 streams = query_stream_param(codec, nid);
3899 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 bps = 0;
3903 if (streams & AC_SUPFMT_PCM) {
3904 if (val & AC_SUPPCM_BITS_8) {
3905 formats |= SNDRV_PCM_FMTBIT_U8;
3906 bps = 8;
3907 }
3908 if (val & AC_SUPPCM_BITS_16) {
3909 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3910 bps = 16;
3911 }
3912 if (wcaps & AC_WCAP_DIGITAL) {
3913 if (val & AC_SUPPCM_BITS_32)
3914 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3915 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3916 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3917 if (val & AC_SUPPCM_BITS_24)
3918 bps = 24;
3919 else if (val & AC_SUPPCM_BITS_20)
3920 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003921 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3922 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3924 if (val & AC_SUPPCM_BITS_32)
3925 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 else if (val & AC_SUPPCM_BITS_24)
3927 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003928 else if (val & AC_SUPPCM_BITS_20)
3929 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 }
3931 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003932#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003933 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003935 if (!bps)
3936 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003937 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003938#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003939 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003940 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 /* temporary hack: we have still no proper support
3942 * for the direct AC3 stream...
3943 */
3944 formats |= SNDRV_PCM_FMTBIT_U8;
3945 bps = 8;
3946 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003947 if (formats == 0) {
3948 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3949 "(nid=0x%x, val=0x%x, ovrd=%i, "
3950 "streams=0x%x)\n",
3951 nid, val,
3952 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3953 streams);
3954 return -EIO;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (formatsp)
3957 *formatsp = formats;
3958 if (bpsp)
3959 *bpsp = bps;
3960 }
3961
3962 return 0;
3963}
Stephen Warren384a48d2011-06-01 11:14:21 -06003964EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003967 * snd_hda_is_supported_format - Check the validity of the format
3968 * @codec: HD-audio codec
3969 * @nid: NID to check
3970 * @format: the HD-audio format value to check
3971 *
3972 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 *
3974 * Returns 1 if supported, 0 if not.
3975 */
3976int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3977 unsigned int format)
3978{
3979 int i;
3980 unsigned int val = 0, rate, stream;
3981
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003982 val = query_pcm_param(codec, nid);
3983 if (!val)
3984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003987 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003988 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (val & (1 << i))
3990 break;
3991 return 0;
3992 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003993 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return 0;
3995
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003996 stream = query_stream_param(codec, nid);
3997 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return 0;
3999
4000 if (stream & AC_SUPFMT_PCM) {
4001 switch (format & 0xf0) {
4002 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004003 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return 0;
4005 break;
4006 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004007 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return 0;
4009 break;
4010 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004011 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return 0;
4013 break;
4014 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004015 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 return 0;
4017 break;
4018 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004019 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return 0;
4021 break;
4022 default:
4023 return 0;
4024 }
4025 } else {
4026 /* FIXME: check for float32 and AC3? */
4027 }
4028
4029 return 1;
4030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004031EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033/*
4034 * PCM stuff
4035 */
4036static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4037 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004038 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
4040 return 0;
4041}
4042
4043static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4044 struct hda_codec *codec,
4045 unsigned int stream_tag,
4046 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004047 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
4049 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4050 return 0;
4051}
4052
4053static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4054 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004055 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Takashi Iwai888afa12008-03-18 09:57:50 +01004057 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return 0;
4059}
4060
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004061static int set_pcm_default_values(struct hda_codec *codec,
4062 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004064 int err;
4065
Takashi Iwai0ba21762007-04-16 11:29:14 +02004066 /* query support PCM information from the given NID */
4067 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004068 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004069 info->rates ? NULL : &info->rates,
4070 info->formats ? NULL : &info->formats,
4071 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004072 if (err < 0)
4073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075 if (info->ops.open == NULL)
4076 info->ops.open = hda_pcm_default_open_close;
4077 if (info->ops.close == NULL)
4078 info->ops.close = hda_pcm_default_open_close;
4079 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004080 if (snd_BUG_ON(!info->nid))
4081 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 info->ops.prepare = hda_pcm_default_prepare;
4083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004085 if (snd_BUG_ON(!info->nid))
4086 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 info->ops.cleanup = hda_pcm_default_cleanup;
4088 }
4089 return 0;
4090}
4091
Takashi Iwaieb541332010-08-06 13:48:11 +02004092/*
4093 * codec prepare/cleanup entries
4094 */
4095int snd_hda_codec_prepare(struct hda_codec *codec,
4096 struct hda_pcm_stream *hinfo,
4097 unsigned int stream,
4098 unsigned int format,
4099 struct snd_pcm_substream *substream)
4100{
4101 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004102 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004103 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4104 if (ret >= 0)
4105 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004106 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004107 return ret;
4108}
4109EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4110
4111void snd_hda_codec_cleanup(struct hda_codec *codec,
4112 struct hda_pcm_stream *hinfo,
4113 struct snd_pcm_substream *substream)
4114{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004115 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004116 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004117 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004118}
4119EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4120
Takashi Iwaid5191e52009-11-16 14:58:17 +01004121/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004122const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4123 "Audio", "SPDIF", "HDMI", "Modem"
4124};
4125
Takashi Iwai176d5332008-07-30 15:01:44 +02004126/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004127 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004128 *
4129 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004130 */
4131static int get_empty_pcm_device(struct hda_bus *bus, int type)
4132{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004133 /* audio device indices; not linear to keep compatibility */
4134 static int audio_idx[HDA_PCM_NTYPES][5] = {
4135 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4136 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004137 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004138 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004139 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004140 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004141
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004142 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004143 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4144 return -EINVAL;
4145 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004146
4147 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4148 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4149 return audio_idx[type][i];
4150
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004151 /* non-fixed slots starting from 10 */
4152 for (i = 10; i < 32; i++) {
4153 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4154 return i;
4155 }
4156
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004157 snd_printk(KERN_WARNING "Too many %s devices\n",
4158 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004159 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004160}
4161
4162/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004163 * attach a new PCM stream
4164 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004165static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004166{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004167 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004168 struct hda_pcm_stream *info;
4169 int stream, err;
4170
Takashi Iwaib91f0802008-11-04 08:43:08 +01004171 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004172 return -EINVAL;
4173 for (stream = 0; stream < 2; stream++) {
4174 info = &pcm->stream[stream];
4175 if (info->substreams) {
4176 err = set_pcm_default_values(codec, info);
4177 if (err < 0)
4178 return err;
4179 }
4180 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004181 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004182}
4183
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004184/* assign all PCMs of the given codec */
4185int snd_hda_codec_build_pcms(struct hda_codec *codec)
4186{
4187 unsigned int pcm;
4188 int err;
4189
4190 if (!codec->num_pcms) {
4191 if (!codec->patch_ops.build_pcms)
4192 return 0;
4193 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004194 if (err < 0) {
4195 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004196 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004197 err = snd_hda_codec_reset(codec);
4198 if (err < 0) {
4199 printk(KERN_ERR
4200 "hda_codec: cannot revert codec\n");
4201 return err;
4202 }
4203 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004204 }
4205 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4206 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4207 int dev;
4208
4209 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004210 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004211
4212 if (!cpcm->pcm) {
4213 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4214 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004215 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004216 cpcm->device = dev;
4217 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004218 if (err < 0) {
4219 printk(KERN_ERR "hda_codec: cannot attach "
4220 "PCM stream %d for codec #%d\n",
4221 dev, codec->addr);
4222 continue; /* no fatal error */
4223 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004224 }
4225 }
4226 return 0;
4227}
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229/**
4230 * snd_hda_build_pcms - build PCM information
4231 * @bus: the BUS
4232 *
4233 * Create PCM information for each codec included in the bus.
4234 *
4235 * The build_pcms codec patch is requested to set up codec->num_pcms and
4236 * codec->pcm_info properly. The array is referred by the top-level driver
4237 * to create its PCM instances.
4238 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4239 * callback.
4240 *
4241 * At least, substreams, channels_min and channels_max must be filled for
4242 * each stream. substreams = 0 indicates that the stream doesn't exist.
4243 * When rates and/or formats are zero, the supported values are queried
4244 * from the given nid. The nid is used also by the default ops.prepare
4245 * and ops.cleanup callbacks.
4246 *
4247 * The driver needs to call ops.open in its open callback. Similarly,
4248 * ops.close is supposed to be called in the close callback.
4249 * ops.prepare should be called in the prepare or hw_params callback
4250 * with the proper parameters for set up.
4251 * ops.cleanup should be called in hw_free for clean up of streams.
4252 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004253 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004255int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004257 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Takashi Iwai0ba21762007-04-16 11:29:14 +02004259 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004260 int err = snd_hda_codec_build_pcms(codec);
4261 if (err < 0)
4262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264 return 0;
4265}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004266EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268/**
4269 * snd_hda_check_board_config - compare the current codec with the config table
4270 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004271 * @num_configs: number of config enums
4272 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 * @tbl: configuration table, terminated by null entries
4274 *
4275 * Compares the modelname or PCI subsystem id of the current codec with the
4276 * given configuration table. If a matching entry is found, returns its
4277 * config value (supposed to be 0 or positive).
4278 *
4279 * If no entries are matching, the function returns a negative value.
4280 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004281int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004282 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004283 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004285 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004286 int i;
4287 for (i = 0; i < num_configs; i++) {
4288 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004289 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004290 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4291 "selected\n", models[i]);
4292 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294 }
4295 }
4296
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004297 if (!codec->bus->pci || !tbl)
4298 return -1;
4299
4300 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4301 if (!tbl)
4302 return -1;
4303 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004304#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004305 char tmp[10];
4306 const char *model = NULL;
4307 if (models)
4308 model = models[tbl->value];
4309 if (!model) {
4310 sprintf(tmp, "#%d", tbl->value);
4311 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004313 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4314 "for config %x:%x (%s)\n",
4315 model, tbl->subvendor, tbl->subdevice,
4316 (tbl->name ? tbl->name : "Unknown device"));
4317#endif
4318 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
4320 return -1;
4321}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004322EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004325 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004326 subsystem ID with the
4327 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004328
4329 This is important for Gateway notebooks with SB450 HDA Audio
4330 where the vendor ID of the PCI device is:
4331 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4332 and the vendor/subvendor are found only at the codec.
4333
4334 * @codec: the HDA codec
4335 * @num_configs: number of config enums
4336 * @models: array of model name strings
4337 * @tbl: configuration table, terminated by null entries
4338 *
4339 * Compares the modelname or PCI subsystem id of the current codec with the
4340 * given configuration table. If a matching entry is found, returns its
4341 * config value (supposed to be 0 or positive).
4342 *
4343 * If no entries are matching, the function returns a negative value.
4344 */
4345int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004346 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004347 const struct snd_pci_quirk *tbl)
4348{
4349 const struct snd_pci_quirk *q;
4350
4351 /* Search for codec ID */
4352 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004353 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4354 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4355 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004356 break;
4357 }
4358
4359 if (!q->subvendor)
4360 return -1;
4361
4362 tbl = q;
4363
4364 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004365#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004366 char tmp[10];
4367 const char *model = NULL;
4368 if (models)
4369 model = models[tbl->value];
4370 if (!model) {
4371 sprintf(tmp, "#%d", tbl->value);
4372 model = tmp;
4373 }
4374 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4375 "for config %x:%x (%s)\n",
4376 model, tbl->subvendor, tbl->subdevice,
4377 (tbl->name ? tbl->name : "Unknown device"));
4378#endif
4379 return tbl->value;
4380 }
4381 return -1;
4382}
4383EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4384
4385/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 * snd_hda_add_new_ctls - create controls from the array
4387 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004388 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 *
4390 * This helper function creates and add new controls in the given array.
4391 * The array must be terminated with an empty entry as terminator.
4392 *
4393 * Returns 0 if successful, or a negative error code.
4394 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004395int snd_hda_add_new_ctls(struct hda_codec *codec,
4396 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004398 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
4400 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004401 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004402 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004403 if (knew->iface == -1) /* skip this codec private value */
4404 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004405 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004406 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004407 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004408 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004409 if (addr > 0)
4410 kctl->id.device = addr;
4411 if (idx > 0)
4412 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004413 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004414 if (!err)
4415 break;
4416 /* try first with another device index corresponding to
4417 * the codec addr; if it still fails (or it's the
4418 * primary codec), then try another control index
4419 */
4420 if (!addr && codec->addr)
4421 addr = codec->addr;
4422 else if (!idx && !knew->index) {
4423 idx = find_empty_mixer_ctl_idx(codec,
4424 knew->name);
4425 if (idx <= 0)
4426 return err;
4427 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004428 return err;
4429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431 return 0;
4432}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004433EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Takashi Iwai83012a72012-08-24 18:38:08 +02004435#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004436static void hda_power_work(struct work_struct *work)
4437{
4438 struct hda_codec *codec =
4439 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004440 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004441
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004442 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004443 if (codec->power_transition > 0) { /* during power-up sequence? */
4444 spin_unlock(&codec->power_lock);
4445 return;
4446 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004447 if (!codec->power_on || codec->power_count) {
4448 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004449 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004450 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004451 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004452 spin_unlock(&codec->power_lock);
4453
Takashi Iwaicb53c622007-08-10 17:21:45 +02004454 hda_call_codec_suspend(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07004455 if (codec->d3_stop_clk_ok)
4456 hda_call_pm_notify(bus, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004457}
4458
4459static void hda_keep_power_on(struct hda_codec *codec)
4460{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004461 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004462 codec->power_count++;
4463 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004464 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004465 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004466}
4467
Takashi Iwaid5191e52009-11-16 14:58:17 +01004468/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004469void snd_hda_update_power_acct(struct hda_codec *codec)
4470{
4471 unsigned long delta = jiffies - codec->power_jiffies;
4472 if (codec->power_on)
4473 codec->power_on_acct += delta;
4474 else
4475 codec->power_off_acct += delta;
4476 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004477}
4478
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004479/* Transition to powered up, if wait_power_down then wait for a pending
4480 * transition to D3 to complete. A pending D3 transition is indicated
4481 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004482/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004483static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004484{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004485 struct hda_bus *bus = codec->bus;
4486
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004487 /* Return if power_on or transitioning to power_on, unless currently
4488 * powering down. */
4489 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004490 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004491 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004492 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004493
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004494 cancel_delayed_work_sync(&codec->power_work);
4495
4496 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004497 /* If the power down delayed work was cancelled above before starting,
4498 * then there is no need to go through power up here.
4499 */
4500 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004501 if (codec->power_transition < 0)
4502 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004503 return;
4504 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004505
Takashi Iwaid66fee52011-08-02 15:39:31 +02004506 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004507 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004508 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004509 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004510 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004511 spin_unlock(&codec->power_lock);
4512
Takashi Iwai68467f52012-08-28 09:14:29 -07004513 if (codec->d3_stop_clk_ok) /* flag set at suspend */
4514 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004515 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004516
4517 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004518 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004519}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004520
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004521#define power_save(codec) \
4522 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004523
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004524/* Transition to powered down */
4525static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004526{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004527 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004528 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004529
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004530 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004531 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004532 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004533 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004534 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004535}
4536
4537/**
4538 * snd_hda_power_save - Power-up/down/sync the codec
4539 * @codec: HD-audio codec
4540 * @delta: the counter delta to change
4541 *
4542 * Change the power-up counter via @delta, and power up or down the hardware
4543 * appropriately. For the power-down, queue to the delayed action.
4544 * Passing zero to @delta means to synchronize the power state.
4545 */
4546void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4547{
4548 spin_lock(&codec->power_lock);
4549 codec->power_count += delta;
4550 trace_hda_power_count(codec);
4551 if (delta > 0)
4552 __snd_hda_power_up(codec, d3wait);
4553 else
4554 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004555 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004556}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004557EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004558
Takashi Iwaid5191e52009-11-16 14:58:17 +01004559/**
4560 * snd_hda_check_amp_list_power - Check the amp list and update the power
4561 * @codec: HD-audio codec
4562 * @check: the object containing an AMP list and the status
4563 * @nid: NID to check / update
4564 *
4565 * Check whether the given NID is in the amp list. If it's in the list,
4566 * check the current AMP status, and update the the power-status according
4567 * to the mute status.
4568 *
4569 * This function is supposed to be set or called from the check_power_status
4570 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004571 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004572int snd_hda_check_amp_list_power(struct hda_codec *codec,
4573 struct hda_loopback_check *check,
4574 hda_nid_t nid)
4575{
Takashi Iwai031024e2011-05-02 11:29:30 +02004576 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004577 int ch, v;
4578
4579 if (!check->amplist)
4580 return 0;
4581 for (p = check->amplist; p->nid; p++) {
4582 if (p->nid == nid)
4583 break;
4584 }
4585 if (!p->nid)
4586 return 0; /* nothing changed */
4587
4588 for (p = check->amplist; p->nid; p++) {
4589 for (ch = 0; ch < 2; ch++) {
4590 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4591 p->idx);
4592 if (!(v & HDA_AMP_MUTE) && v > 0) {
4593 if (!check->power_on) {
4594 check->power_on = 1;
4595 snd_hda_power_up(codec);
4596 }
4597 return 1;
4598 }
4599 }
4600 }
4601 if (check->power_on) {
4602 check->power_on = 0;
4603 snd_hda_power_down(codec);
4604 }
4605 return 0;
4606}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004607EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004608#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004610/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004611 * Channel mode helper
4612 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004613
4614/**
4615 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4616 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004617int snd_hda_ch_mode_info(struct hda_codec *codec,
4618 struct snd_ctl_elem_info *uinfo,
4619 const struct hda_channel_mode *chmode,
4620 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004621{
4622 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4623 uinfo->count = 1;
4624 uinfo->value.enumerated.items = num_chmodes;
4625 if (uinfo->value.enumerated.item >= num_chmodes)
4626 uinfo->value.enumerated.item = num_chmodes - 1;
4627 sprintf(uinfo->value.enumerated.name, "%dch",
4628 chmode[uinfo->value.enumerated.item].channels);
4629 return 0;
4630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004631EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004632
Takashi Iwaid5191e52009-11-16 14:58:17 +01004633/**
4634 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4635 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004636int snd_hda_ch_mode_get(struct hda_codec *codec,
4637 struct snd_ctl_elem_value *ucontrol,
4638 const struct hda_channel_mode *chmode,
4639 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004640 int max_channels)
4641{
4642 int i;
4643
4644 for (i = 0; i < num_chmodes; i++) {
4645 if (max_channels == chmode[i].channels) {
4646 ucontrol->value.enumerated.item[0] = i;
4647 break;
4648 }
4649 }
4650 return 0;
4651}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004652EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004653
Takashi Iwaid5191e52009-11-16 14:58:17 +01004654/**
4655 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4656 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004657int snd_hda_ch_mode_put(struct hda_codec *codec,
4658 struct snd_ctl_elem_value *ucontrol,
4659 const struct hda_channel_mode *chmode,
4660 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004661 int *max_channelsp)
4662{
4663 unsigned int mode;
4664
4665 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004666 if (mode >= num_chmodes)
4667 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004668 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004669 return 0;
4670 /* change the current channel setting */
4671 *max_channelsp = chmode[mode].channels;
4672 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004673 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004674 return 1;
4675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004676EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/*
4679 * input MUX helper
4680 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004681
4682/**
4683 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4684 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004685int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4686 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
4688 unsigned int index;
4689
4690 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4691 uinfo->count = 1;
4692 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004693 if (!imux->num_items)
4694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 index = uinfo->value.enumerated.item;
4696 if (index >= imux->num_items)
4697 index = imux->num_items - 1;
4698 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4699 return 0;
4700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004701EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Takashi Iwaid5191e52009-11-16 14:58:17 +01004703/**
4704 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4705 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004706int snd_hda_input_mux_put(struct hda_codec *codec,
4707 const struct hda_input_mux *imux,
4708 struct snd_ctl_elem_value *ucontrol,
4709 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 unsigned int *cur_val)
4711{
4712 unsigned int idx;
4713
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004714 if (!imux->num_items)
4715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 idx = ucontrol->value.enumerated.item[0];
4717 if (idx >= imux->num_items)
4718 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004719 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004721 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4722 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 *cur_val = idx;
4724 return 1;
4725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004726EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728
4729/*
4730 * Multi-channel / digital-out PCM helper functions
4731 */
4732
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004733/* setup SPDIF output stream */
4734static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4735 unsigned int stream_tag, unsigned int format)
4736{
Stephen Warren7c9359762011-06-01 11:14:17 -06004737 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4738
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004739 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004740 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004741 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004742 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004743 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004744 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004745 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004746 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004747 for (d = codec->slave_dig_outs; *d; d++)
4748 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4749 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004750 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004751 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004752 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004753 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004754 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004755}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004756
Takashi Iwai2f728532008-09-25 16:32:41 +02004757static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4758{
4759 snd_hda_codec_cleanup_stream(codec, nid);
4760 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004761 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004762 for (d = codec->slave_dig_outs; *d; d++)
4763 snd_hda_codec_cleanup_stream(codec, *d);
4764 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004765}
4766
Takashi Iwaid5191e52009-11-16 14:58:17 +01004767/**
4768 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4769 * @bus: HD-audio bus
4770 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004771void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4772{
4773 struct hda_codec *codec;
4774
4775 if (!bus)
4776 return;
4777 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004778 if (hda_codec_is_power_on(codec) &&
4779 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004780 codec->patch_ops.reboot_notify(codec);
4781 }
4782}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004783EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004784
Takashi Iwaid5191e52009-11-16 14:58:17 +01004785/**
4786 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004788int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4789 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Ingo Molnar62932df2006-01-16 16:34:20 +01004791 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004792 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4793 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004794 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004796 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return 0;
4798}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004799EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Takashi Iwaid5191e52009-11-16 14:58:17 +01004801/**
4802 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4803 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004804int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4805 struct hda_multi_out *mout,
4806 unsigned int stream_tag,
4807 unsigned int format,
4808 struct snd_pcm_substream *substream)
4809{
4810 mutex_lock(&codec->spdif_mutex);
4811 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4812 mutex_unlock(&codec->spdif_mutex);
4813 return 0;
4814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004815EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004816
Takashi Iwaid5191e52009-11-16 14:58:17 +01004817/**
4818 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4819 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004820int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4821 struct hda_multi_out *mout)
4822{
4823 mutex_lock(&codec->spdif_mutex);
4824 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4825 mutex_unlock(&codec->spdif_mutex);
4826 return 0;
4827}
4828EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4829
Takashi Iwaid5191e52009-11-16 14:58:17 +01004830/**
4831 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004833int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4834 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Ingo Molnar62932df2006-01-16 16:34:20 +01004836 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004838 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return 0;
4840}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004841EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Takashi Iwaid5191e52009-11-16 14:58:17 +01004843/**
4844 * snd_hda_multi_out_analog_open - open analog outputs
4845 *
4846 * Open analog outputs and set up the hw-constraints.
4847 * If the digital outputs can be opened as slave, open the digital
4848 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004850int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4851 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004852 struct snd_pcm_substream *substream,
4853 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Takashi Iwai9a081602008-02-12 18:37:26 +01004855 struct snd_pcm_runtime *runtime = substream->runtime;
4856 runtime->hw.channels_max = mout->max_channels;
4857 if (mout->dig_out_nid) {
4858 if (!mout->analog_rates) {
4859 mout->analog_rates = hinfo->rates;
4860 mout->analog_formats = hinfo->formats;
4861 mout->analog_maxbps = hinfo->maxbps;
4862 } else {
4863 runtime->hw.rates = mout->analog_rates;
4864 runtime->hw.formats = mout->analog_formats;
4865 hinfo->maxbps = mout->analog_maxbps;
4866 }
4867 if (!mout->spdif_rates) {
4868 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4869 &mout->spdif_rates,
4870 &mout->spdif_formats,
4871 &mout->spdif_maxbps);
4872 }
4873 mutex_lock(&codec->spdif_mutex);
4874 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004875 if ((runtime->hw.rates & mout->spdif_rates) &&
4876 (runtime->hw.formats & mout->spdif_formats)) {
4877 runtime->hw.rates &= mout->spdif_rates;
4878 runtime->hw.formats &= mout->spdif_formats;
4879 if (mout->spdif_maxbps < hinfo->maxbps)
4880 hinfo->maxbps = mout->spdif_maxbps;
4881 } else {
4882 mout->share_spdif = 0;
4883 /* FIXME: need notify? */
4884 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004885 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004886 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4889 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4890}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004891EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
Takashi Iwaid5191e52009-11-16 14:58:17 +01004893/**
4894 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4895 *
4896 * Set up the i/o for analog out.
4897 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004899int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4900 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 unsigned int stream_tag,
4902 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004903 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
Takashi Iwaidda14412011-05-02 11:29:30 +02004905 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004907 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 int i;
4909
Ingo Molnar62932df2006-01-16 16:34:20 +01004910 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004911 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004912 if (mout->dig_out_nid && mout->share_spdif &&
4913 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004915 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4916 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004917 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004919 setup_dig_out_stream(codec, mout->dig_out_nid,
4920 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } else {
4922 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004923 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 }
4925 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004926 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
4928 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004929 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4930 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004931 if (!mout->no_share_stream &&
4932 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4935 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004936 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004937 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4938 if (!mout->no_share_stream && mout->hp_out_nid[i])
4939 snd_hda_codec_setup_stream(codec,
4940 mout->hp_out_nid[i],
4941 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004942 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004943 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004944 snd_hda_codec_setup_stream(codec,
4945 mout->extra_out_nid[i],
4946 stream_tag, 0, format);
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 /* surrounds */
4949 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004950 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004951 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4952 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004953 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004954 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4955 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957 return 0;
4958}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004959EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Takashi Iwaid5191e52009-11-16 14:58:17 +01004961/**
4962 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004964int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4965 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Takashi Iwaidda14412011-05-02 11:29:30 +02004967 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 int i;
4969
4970 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004971 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004973 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004974 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4975 if (mout->hp_out_nid[i])
4976 snd_hda_codec_cleanup_stream(codec,
4977 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004978 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4979 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004980 snd_hda_codec_cleanup_stream(codec,
4981 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004982 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004984 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 mout->dig_out_used = 0;
4986 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004987 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 return 0;
4989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004990EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Takashi Iwai47408602012-04-20 13:06:53 +02004992/**
4993 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4994 *
4995 * Guess the suitable VREF pin bits to be set as the pin-control value.
4996 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4997 */
4998unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4999{
5000 unsigned int pincap;
5001 unsigned int oldval;
5002 oldval = snd_hda_codec_read(codec, pin, 0,
5003 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5004 pincap = snd_hda_query_pin_caps(codec, pin);
5005 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5006 /* Exception: if the default pin setup is vref50, we give it priority */
5007 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5008 return AC_PINCTL_VREF_80;
5009 else if (pincap & AC_PINCAP_VREF_50)
5010 return AC_PINCTL_VREF_50;
5011 else if (pincap & AC_PINCAP_VREF_100)
5012 return AC_PINCTL_VREF_100;
5013 else if (pincap & AC_PINCAP_VREF_GRD)
5014 return AC_PINCTL_VREF_GRD;
5015 return AC_PINCTL_VREF_HIZ;
5016}
5017EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5018
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005019int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5020 unsigned int val, bool cached)
5021{
5022 if (val) {
5023 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005024 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005025 if (!(cap & AC_PINCAP_OUT))
5026 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5027 else if ((val & AC_PINCTL_HP_EN) &&
5028 !(cap & AC_PINCAP_HP_DRV))
5029 val &= ~AC_PINCTL_HP_EN;
5030 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005031 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005032 if (!(cap & AC_PINCAP_IN))
5033 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5034 }
5035 }
5036 if (cached)
5037 return snd_hda_codec_update_cache(codec, pin, 0,
5038 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5039 else
5040 return snd_hda_codec_write(codec, pin, 0,
5041 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5042}
5043EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5044
Takashi Iwai990061c2010-09-09 22:08:44 +02005045/**
5046 * snd_hda_add_imux_item - Add an item to input_mux
5047 *
5048 * When the same label is used already in the existing items, the number
5049 * suffix is appended to the label. This label index number is stored
5050 * to type_idx when non-NULL pointer is given.
5051 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005052int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5053 int index, int *type_idx)
5054{
5055 int i, label_idx = 0;
5056 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5057 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5058 return -EINVAL;
5059 }
5060 for (i = 0; i < imux->num_items; i++) {
5061 if (!strncmp(label, imux->items[i].label, strlen(label)))
5062 label_idx++;
5063 }
5064 if (type_idx)
5065 *type_idx = label_idx;
5066 if (label_idx > 0)
5067 snprintf(imux->items[imux->num_items].label,
5068 sizeof(imux->items[imux->num_items].label),
5069 "%s %d", label, label_idx);
5070 else
5071 strlcpy(imux->items[imux->num_items].label, label,
5072 sizeof(imux->items[imux->num_items].label));
5073 imux->items[imux->num_items].index = index;
5074 imux->num_items++;
5075 return 0;
5076}
5077EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005078
Takashi Iwai4a471b72005-12-07 13:56:29 +01005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080#ifdef CONFIG_PM
5081/*
5082 * power management
5083 */
5084
5085/**
5086 * snd_hda_suspend - suspend the codecs
5087 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 *
5089 * Returns 0 if successful.
5090 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005091int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005093 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
Takashi Iwai0ba21762007-04-16 11:29:14 +02005095 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005096 if (hda_codec_is_power_on(codec))
5097 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 }
5099 return 0;
5100}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005101EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
5103/**
5104 * snd_hda_resume - resume the codecs
5105 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 *
5107 * Returns 0 if successful.
5108 */
5109int snd_hda_resume(struct hda_bus *bus)
5110{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005111 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Takashi Iwai0ba21762007-04-16 11:29:14 +02005113 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005114 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return 0;
5117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005118EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005119#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005120
5121/*
5122 * generic arrays
5123 */
5124
Takashi Iwaid5191e52009-11-16 14:58:17 +01005125/**
5126 * snd_array_new - get a new element from the given array
5127 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005128 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005129 * Get a new element from the given array. If it exceeds the
5130 * pre-allocated array size, re-allocate the array.
5131 *
5132 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005133 */
5134void *snd_array_new(struct snd_array *array)
5135{
5136 if (array->used >= array->alloced) {
5137 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005138 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005139 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005140 void *nlist;
5141 if (snd_BUG_ON(num >= 4096))
5142 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005143 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005144 if (!nlist)
5145 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005146 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005147 array->list = nlist;
5148 array->alloced = num;
5149 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005150 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005152EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005153
Takashi Iwaid5191e52009-11-16 14:58:17 +01005154/**
5155 * snd_array_free - free the given array elements
5156 * @array: the array object
5157 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005158void snd_array_free(struct snd_array *array)
5159{
5160 kfree(array->list);
5161 array->used = 0;
5162 array->alloced = 0;
5163 array->list = NULL;
5164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005165EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005166
Takashi Iwaid5191e52009-11-16 14:58:17 +01005167/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005168 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5169 * @pcm: PCM caps bits
5170 * @buf: the string buffer to write
5171 * @buflen: the max buffer length
5172 *
5173 * used by hda_proc.c and hda_eld.c
5174 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005175void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5176{
5177 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5178 int i, j;
5179
5180 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5181 if (pcm & (AC_SUPPCM_BITS_8 << i))
5182 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5183
5184 buf[j] = '\0'; /* necessary when j == 0 */
5185}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005186EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005187
5188MODULE_DESCRIPTION("HDA codec core");
5189MODULE_LICENSE("GPL");