blob: bada677df8a7d24d5a83c26741e5d04235453faa [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 Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200193 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200194 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)nid << 20;
202 val |= verb << 8;
203 val |= parm;
204 return val;
205}
206
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207/*
208 * Send and receive a verb
209 */
210static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200211 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212{
213 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215
Wu Fengguang6430aee2009-07-17 16:49:19 +0800216 if (cmd == ~0)
217 return -1;
218
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200219 if (res)
220 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200221 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200222 snd_hda_power_up(codec);
223 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200224 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
225 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100226 for (;;) {
227 trace_hda_send_cmd(codec, cmd);
228 err = bus->ops.command(bus, cmd);
229 if (err != -EAGAIN)
230 break;
231 /* process pending verbs */
232 bus->ops.get_response(bus, codec->addr);
233 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200234 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800235 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200236 trace_hda_get_response(codec, *res);
237 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200238 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200239 mutex_unlock(&bus->cmd_mutex);
240 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100241 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200242 if (bus->response_reset) {
243 snd_printd("hda_codec: resetting BUS due to "
244 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200245 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200246 bus->ops.bus_reset(bus);
247 }
248 goto again;
249 }
250 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100251 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200252 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200253 return err;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
257 * snd_hda_codec_read - send a command and get the response
258 * @codec: the HDA codec
259 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200260 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * @verb: the verb to send
262 * @parm: the parameter for the verb
263 *
264 * Send a single command and read the corresponding response.
265 *
266 * Returns the obtained response value, or -1 for an error.
267 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200268unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200269 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int verb, unsigned int parm)
271{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200272 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200273 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200274 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700275 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return res;
277}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100278EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/**
281 * snd_hda_codec_write - send a single command without waiting for response
282 * @codec: the HDA codec
283 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200284 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @verb: the verb to send
286 * @parm: the parameter for the verb
287 *
288 * Send a single command without waiting for response.
289 *
290 * Returns 0 if successful, or a negative error code.
291 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200292int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
293 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200295 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100296 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200297 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200298 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100300EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302/**
303 * snd_hda_sequence_write - sequence writes
304 * @codec: the HDA codec
305 * @seq: VERB array to send
306 *
307 * Send the commands sequentially from the given array.
308 * The array must be terminated with NID=0.
309 */
310void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
311{
312 for (; seq->nid; seq++)
313 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100315EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/**
318 * snd_hda_get_sub_nodes - get the range of sub nodes
319 * @codec: the HDA codec
320 * @nid: NID to parse
321 * @start_id: the pointer to store the start NID
322 *
323 * Parse the NID and store the start NID of its sub-nodes.
324 * Returns the number of sub-nodes.
325 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200326int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
327 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned int parm;
330
331 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200332 if (parm == -1)
333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *start_id = (parm >> 16) & 0x7fff;
335 return (int)(parm & 0x7fff);
336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100337EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100339/* connection list element */
340struct hda_conn_list {
341 struct list_head list;
342 int len;
343 hda_nid_t nid;
344 hda_nid_t conns[0];
345};
346
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200347/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348static struct hda_conn_list *
349lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200350{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100351 struct hda_conn_list *p;
352 list_for_each_entry(p, &codec->conn_list, list) {
353 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 }
356 return NULL;
357}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
360 const hda_nid_t *list)
361{
362 struct hda_conn_list *p;
363
364 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
365 if (!p)
366 return -ENOMEM;
367 p->len = len;
368 p->nid = nid;
369 memcpy(p->conns, list, len * sizeof(hda_nid_t));
370 list_add(&p->list, &codec->conn_list);
371 return 0;
372}
373
374static void remove_conn_list(struct hda_codec *codec)
375{
376 while (!list_empty(&codec->conn_list)) {
377 struct hda_conn_list *p;
378 p = list_first_entry(&codec->conn_list, typeof(*p), list);
379 list_del(&p->list);
380 kfree(p);
381 }
382}
383
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200384/* read the connection and add to the cache */
385static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200386{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100387 hda_nid_t list[32];
388 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200391 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100392 if (len == -ENOSPC) {
393 len = snd_hda_get_num_raw_conns(codec, nid);
394 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
395 if (!result)
396 return -ENOMEM;
397 len = snd_hda_get_raw_connections(codec, nid, result, len);
398 }
399 if (len >= 0)
400 len = snd_hda_override_conn_list(codec, nid, len, result);
401 if (result != list)
402 kfree(result);
403 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404}
Takashi Iwaidce20792011-06-24 14:10:28 +0200405
406/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
412 *
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
415 *
416 * Returns the number of connections, or a negative error code.
417 *
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
421 */
422int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
423 const hda_nid_t **listp)
424{
425 bool added = false;
426
427 for (;;) {
428 int err;
429 const struct hda_conn_list *p;
430
431 /* if the connection-list is already cached, read it */
432 p = lookup_conn_list(codec, nid);
433 if (p) {
434 if (listp)
435 *listp = p->conns;
436 return p->len;
437 }
438 if (snd_BUG_ON(added))
439 return -EINVAL;
440
441 err = read_and_add_raw_conns(codec, nid);
442 if (err < 0)
443 return err;
444 added = true;
445 }
446}
447EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
448
449/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200450 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @codec: the HDA codec
452 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200453 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * @max_conns: max. number of connections to store
455 *
456 * Parses the connection list of the given widget and stores the list
457 * of NIDs.
458 *
459 * Returns the number of connections, or a negative error code.
460 */
461int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200462 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200463{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464 const hda_nid_t *list;
465 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200466
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100467 if (len > 0 && conn_list) {
468 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 snd_printk(KERN_ERR "hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
471 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200472 return -EINVAL;
473 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100474 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200475 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200476
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100477 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200478}
479EXPORT_SYMBOL_HDA(snd_hda_get_connections);
480
Takashi Iwai4eea3092013-02-07 18:18:19 +0100481/* return CONNLIST_LEN parameter of the given widget */
482static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
483{
484 unsigned int wcaps = get_wcaps(codec, nid);
485 unsigned int parm;
486
487 if (!(wcaps & AC_WCAP_CONN_LIST) &&
488 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
489 return 0;
490
491 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
492 if (parm == -1)
493 parm = 0;
494 return parm;
495}
496
497int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
498{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100499 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100500}
501
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200502/**
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
505 * @nid: NID to parse
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
508 *
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
512 */
513int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
514 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100519 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100520 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai4eea3092013-02-07 18:18:19 +0100522 parm = get_num_conns(codec, nid);
523 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200524 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (parm & AC_CLIST_LONG) {
527 /* long form */
528 shift = 16;
529 num_elems = 2;
530 } else {
531 /* short form */
532 shift = 8;
533 num_elems = 4;
534 }
535 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mask = (1 << (shift-1)) - 1;
537
Takashi Iwai0ba21762007-04-16 11:29:14 +0200538 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0; /* no connection */
540
541 if (conn_len == 1) {
542 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200543 parm = snd_hda_codec_read(codec, nid, 0,
544 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200545 if (parm == -1 && codec->bus->rirb_error)
546 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100547 if (conn_list)
548 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 1;
550 }
551
552 /* multi connection */
553 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
558
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100567 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4758fed2013-10-17 17:56:25 +0200568 snd_printdd("hda_codec: "
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
570 nid, i, parm);
571 return 0;
572 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100573 parm >>= shift;
574 if (range_val) {
575 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
579 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100582 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100583 if (conn_list) {
584 if (conns >= max_conns)
585 return -ENOSPC;
586 conn_list[conns] = n;
587 }
588 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100589 }
590 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100591 if (conn_list) {
592 if (conns >= max_conns)
593 return -ENOSPC;
594 conn_list[conns] = val;
595 }
596 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100598 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 return conns;
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200604 * snd_hda_override_conn_list - add/modify the connection-list to cache
605 * @codec: the HDA codec
606 * @nid: NID to parse
607 * @len: number of connection list entries
608 * @list: the list of connection entries
609 *
610 * Add or modify the given connection-list to the cache. If the corresponding
611 * cache already exists, invalidate it and append a new one.
612 *
613 * Returns zero or a negative error code.
614 */
615int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
616 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100620 p = lookup_conn_list(codec, nid);
621 if (p) {
622 list_del(&p->list);
623 kfree(p);
624 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200625
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100626 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200628EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
629
630/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200631 * snd_hda_get_conn_index - get the connection index of the given NID
632 * @codec: the HDA codec
633 * @mux: NID containing the list
634 * @nid: NID to select
635 * @recursive: 1 when searching NID recursively, otherwise 0
636 *
637 * Parses the connection list of the widget @mux and checks whether the
638 * widget @nid is present. If it is, return the connection index.
639 * Otherwise it returns -1.
640 */
641int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
642 hda_nid_t nid, int recursive)
643{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100644 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200645 int i, nums;
646
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100647 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 for (i = 0; i < nums; i++)
649 if (conn[i] == nid)
650 return i;
651 if (!recursive)
652 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100653 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200654 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
655 return -1;
656 }
657 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200658 for (i = 0; i < nums; i++) {
659 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
660 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
661 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
663 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200664 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200665 return -1;
666}
667EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Mengdong Linf1aa0682013-08-26 21:35:21 -0400669
670/* return DEVLIST_LEN parameter of the given widget */
671static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
672{
673 unsigned int wcaps = get_wcaps(codec, nid);
674 unsigned int parm;
675
676 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
677 get_wcaps_type(wcaps) != AC_WID_PIN)
678 return 0;
679
680 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
681 if (parm == -1 && codec->bus->rirb_error)
682 parm = 0;
683 return parm & AC_DEV_LIST_LEN_MASK;
684}
685
686/**
687 * snd_hda_get_devices - copy device list without cache
688 * @codec: the HDA codec
689 * @nid: NID of the pin to parse
690 * @dev_list: device list array
691 * @max_devices: max. number of devices to store
692 *
693 * Copy the device list. This info is dynamic and so not cached.
694 * Currently called only from hda_proc.c, so not exported.
695 */
696int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
697 u8 *dev_list, int max_devices)
698{
699 unsigned int parm;
700 int i, dev_len, devices;
701
702 parm = get_num_devices(codec, nid);
703 if (!parm) /* not multi-stream capable */
704 return 0;
705
706 dev_len = parm + 1;
707 dev_len = dev_len < max_devices ? dev_len : max_devices;
708
709 devices = 0;
710 while (devices < dev_len) {
711 parm = snd_hda_codec_read(codec, nid, 0,
712 AC_VERB_GET_DEVICE_LIST, devices);
713 if (parm == -1 && codec->bus->rirb_error)
714 break;
715
716 for (i = 0; i < 8; i++) {
717 dev_list[devices] = (u8)parm;
718 parm >>= 4;
719 devices++;
720 if (devices >= dev_len)
721 break;
722 }
723 }
724 return devices;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/**
728 * snd_hda_queue_unsol_event - add an unsolicited event to queue
729 * @bus: the BUS
730 * @res: unsolicited event (lower 32bit of RIRB entry)
731 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
732 *
733 * Adds the given event to the queue. The events are processed in
734 * the workqueue asynchronously. Call this function in the interrupt
735 * hanlder when RIRB receives an unsolicited event.
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
739int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
740{
741 struct hda_bus_unsolicited *unsol;
742 unsigned int wp;
743
Wang YanQing2195b062013-05-07 11:27:33 +0800744 if (!bus || !bus->workq)
745 return 0;
746
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200747 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200748 unsol = bus->unsol;
749 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751
752 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
753 unsol->wp = wp;
754
755 wp <<= 1;
756 unsol->queue[wp] = res;
757 unsol->queue[wp + 1] = res_ex;
758
Takashi Iwai6acaed32009-01-12 10:09:24 +0100759 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return 0;
762}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100763EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800766 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
David Howellsc4028952006-11-22 14:57:56 +0000768static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
David Howellsc4028952006-11-22 14:57:56 +0000770 struct hda_bus_unsolicited *unsol =
771 container_of(work, struct hda_bus_unsolicited, work);
772 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct hda_codec *codec;
774 unsigned int rp, caddr, res;
775
776 while (unsol->rp != unsol->wp) {
777 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
778 unsol->rp = rp;
779 rp <<= 1;
780 res = unsol->queue[rp];
781 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200782 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 continue;
784 codec = bus->caddr_tbl[caddr & 0x0f];
785 if (codec && codec->patch_ops.unsol_event)
786 codec->patch_ops.unsol_event(codec, res);
787 }
788}
789
790/*
791 * initialize unsolicited queue
792 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200793static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct hda_bus_unsolicited *unsol;
796
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100797 if (bus->unsol) /* already initialized */
798 return 0;
799
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200800 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200801 if (!unsol) {
802 snd_printk(KERN_ERR "hda_codec: "
803 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ENOMEM;
805 }
David Howellsc4028952006-11-22 14:57:56 +0000806 INIT_WORK(&unsol->work, process_unsol_events);
807 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 bus->unsol = unsol;
809 return 0;
810}
811
812/*
813 * destructor
814 */
815static void snd_hda_codec_free(struct hda_codec *codec);
816
817static int snd_hda_bus_free(struct hda_bus *bus)
818{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200819 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai0ba21762007-04-16 11:29:14 +0200821 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100823 if (bus->workq)
824 flush_workqueue(bus->workq);
825 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200827 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 snd_hda_codec_free(codec);
829 }
830 if (bus->ops.private_free)
831 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100832 if (bus->workq)
833 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree(bus);
835 return 0;
836}
837
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100838static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100841 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return snd_hda_bus_free(bus);
843}
844
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200845#ifdef CONFIG_SND_HDA_HWDEP
846static int snd_hda_bus_dev_register(struct snd_device *device)
847{
848 struct hda_bus *bus = device->device_data;
849 struct hda_codec *codec;
850 list_for_each_entry(codec, &bus->codec_list, list) {
851 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100852 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200853 }
854 return 0;
855}
856#else
857#define snd_hda_bus_dev_register NULL
858#endif
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/**
861 * snd_hda_bus_new - create a HDA bus
862 * @card: the card entry
863 * @temp: the template for hda_bus information
864 * @busp: the pointer to store the created bus instance
865 *
866 * Returns 0 if successful, or a negative error code.
867 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100868int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200869 const struct hda_bus_template *temp,
870 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct hda_bus *bus;
873 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100874 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200875 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .dev_free = snd_hda_bus_dev_free,
877 };
878
Takashi Iwaida3cec32008-08-08 17:12:14 +0200879 if (snd_BUG_ON(!temp))
880 return -EINVAL;
881 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (busp)
885 *busp = NULL;
886
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200887 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (bus == NULL) {
889 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
890 return -ENOMEM;
891 }
892
893 bus->card = card;
894 bus->private_data = temp->private_data;
895 bus->pci = temp->pci;
896 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100897 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 bus->ops = temp->ops;
899
Ingo Molnar62932df2006-01-16 16:34:20 +0100900 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200901 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 INIT_LIST_HEAD(&bus->codec_list);
903
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100904 snprintf(bus->workq_name, sizeof(bus->workq_name),
905 "hd-audio%d", card->number);
906 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100907 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100908 snd_printk(KERN_ERR "cannot create workqueue %s\n",
909 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100910 kfree(bus);
911 return -ENOMEM;
912 }
913
Takashi Iwai0ba21762007-04-16 11:29:14 +0200914 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
915 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 snd_hda_bus_free(bus);
917 return err;
918 }
919 if (busp)
920 *busp = bus;
921 return 0;
922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100923EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Takashi Iwai82467612007-07-27 19:15:54 +0200925#ifdef CONFIG_SND_HDA_GENERIC
926#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200927 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200928#else
929#define is_generic_config(codec) 0
930#endif
931
Takashi Iwai645f10c2008-11-28 15:07:37 +0100932#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100933#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
934#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100935#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100936#endif
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * find a matching codec preset
940 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200941static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200942find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100944 struct hda_codec_preset_list *tbl;
945 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200946 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100948 again:
949 mutex_lock(&preset_mutex);
950 list_for_each_entry(tbl, &hda_preset_tables, list) {
951 if (!try_module_get(tbl->owner)) {
952 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
953 continue;
954 }
955 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100957 if (preset->afg && preset->afg != codec->afg)
958 continue;
959 if (preset->mfg && preset->mfg != codec->mfg)
960 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200961 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200963 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200964 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100965 preset->rev == codec->revision_id)) {
966 mutex_unlock(&preset_mutex);
967 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100971 module_put(tbl->owner);
972 }
973 mutex_unlock(&preset_mutex);
974
975 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
976 char name[32];
977 if (!mod_requested)
978 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
979 codec->vendor_id);
980 else
981 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
982 (codec->vendor_id >> 16) & 0xffff);
983 request_module(name);
984 mod_requested++;
985 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 return NULL;
988}
989
990/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200991 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200993static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 const struct hda_vendor_id *c;
996 const char *vendor = NULL;
997 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200998 char tmp[16];
999
1000 if (codec->vendor_name)
1001 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 for (c = hda_vendor_ids; c->id; c++) {
1004 if (c->id == vendor_id) {
1005 vendor = c->name;
1006 break;
1007 }
1008 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001009 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sprintf(tmp, "Generic %04x", vendor_id);
1011 vendor = tmp;
1012 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001013 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1014 if (!codec->vendor_name)
1015 return -ENOMEM;
1016
1017 get_chip_name:
1018 if (codec->chip_name)
1019 return 0;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001022 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1023 else {
1024 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1025 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1026 }
1027 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001028 return -ENOMEM;
1029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001033 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001035static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001037 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 hda_nid_t nid;
1039
1040 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1041 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001042 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001043 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001044 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001045 case AC_GRP_AUDIO_FUNCTION:
1046 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001047 codec->afg_function_id = function_id & 0xff;
1048 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001049 break;
1050 case AC_GRP_MODEM_FUNCTION:
1051 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001052 codec->mfg_function_id = function_id & 0xff;
1053 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001054 break;
1055 default:
1056 break;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001062 * read widget caps for each widget and store in cache
1063 */
1064static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1065{
1066 int i;
1067 hda_nid_t nid;
1068
1069 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1070 &codec->start_nid);
1071 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001072 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001073 return -ENOMEM;
1074 nid = codec->start_nid;
1075 for (i = 0; i < codec->num_nodes; i++, nid++)
1076 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1077 AC_PAR_AUDIO_WIDGET_CAP);
1078 return 0;
1079}
1080
Takashi Iwai3be14142009-02-20 14:11:16 +01001081/* read all pin default configurations and save codec->init_pins */
1082static int read_pin_defaults(struct hda_codec *codec)
1083{
1084 int i;
1085 hda_nid_t nid = codec->start_nid;
1086
1087 for (i = 0; i < codec->num_nodes; i++, nid++) {
1088 struct hda_pincfg *pin;
1089 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001090 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001091 if (wid_type != AC_WID_PIN)
1092 continue;
1093 pin = snd_array_new(&codec->init_pins);
1094 if (!pin)
1095 return -ENOMEM;
1096 pin->nid = nid;
1097 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1098 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001099 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1100 AC_VERB_GET_PIN_WIDGET_CONTROL,
1101 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001102 }
1103 return 0;
1104}
1105
1106/* look up the given pin config list and return the item matching with NID */
1107static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1108 struct snd_array *array,
1109 hda_nid_t nid)
1110{
1111 int i;
1112 for (i = 0; i < array->used; i++) {
1113 struct hda_pincfg *pin = snd_array_elem(array, i);
1114 if (pin->nid == nid)
1115 return pin;
1116 }
1117 return NULL;
1118}
1119
Takashi Iwai3be14142009-02-20 14:11:16 +01001120/* set the current pin config value for the given NID.
1121 * the value is cached, and read via snd_hda_codec_get_pincfg()
1122 */
1123int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1124 hda_nid_t nid, unsigned int cfg)
1125{
1126 struct hda_pincfg *pin;
1127
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001128 /* the check below may be invalid when pins are added by a fixup
1129 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1130 * for now
1131 */
1132 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001133 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1134 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001135 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001136
Takashi Iwai3be14142009-02-20 14:11:16 +01001137 pin = look_up_pincfg(codec, list, nid);
1138 if (!pin) {
1139 pin = snd_array_new(list);
1140 if (!pin)
1141 return -ENOMEM;
1142 pin->nid = nid;
1143 }
1144 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001145 return 0;
1146}
1147
Takashi Iwaid5191e52009-11-16 14:58:17 +01001148/**
1149 * snd_hda_codec_set_pincfg - Override a pin default configuration
1150 * @codec: the HDA codec
1151 * @nid: NID to set the pin config
1152 * @cfg: the pin default config value
1153 *
1154 * Override a pin default configuration value in the cache.
1155 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1156 * priority than the real hardware value.
1157 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001158int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1159 hda_nid_t nid, unsigned int cfg)
1160{
Takashi Iwai346ff702009-02-23 09:42:57 +01001161 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001162}
1163EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1164
Takashi Iwaid5191e52009-11-16 14:58:17 +01001165/**
1166 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1167 * @codec: the HDA codec
1168 * @nid: NID to get the pin config
1169 *
1170 * Get the current pin config value of the given pin NID.
1171 * If the pincfg value is cached or overridden via sysfs or driver,
1172 * returns the cached value.
1173 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001174unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1175{
1176 struct hda_pincfg *pin;
1177
Takashi Iwai3be14142009-02-20 14:11:16 +01001178#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001179 {
1180 unsigned int cfg = 0;
1181 mutex_lock(&codec->user_mutex);
1182 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1183 if (pin)
1184 cfg = pin->cfg;
1185 mutex_unlock(&codec->user_mutex);
1186 if (cfg)
1187 return cfg;
1188 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001189#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001190 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1191 if (pin)
1192 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001193 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1194 if (pin)
1195 return pin->cfg;
1196 return 0;
1197}
1198EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1199
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001200/* remember the current pinctl target value */
1201int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1202 unsigned int val)
1203{
1204 struct hda_pincfg *pin;
1205
1206 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1207 if (!pin)
1208 return -EINVAL;
1209 pin->target = val;
1210 return 0;
1211}
1212EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1213
1214/* return the current pinctl target value */
1215int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1216{
1217 struct hda_pincfg *pin;
1218
1219 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1220 if (!pin)
1221 return 0;
1222 return pin->target;
1223}
1224EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1225
Takashi Iwai92ee6162009-12-27 11:18:59 +01001226/**
1227 * snd_hda_shutup_pins - Shut up all pins
1228 * @codec: the HDA codec
1229 *
1230 * Clear all pin controls to shup up before suspend for avoiding click noise.
1231 * The controls aren't cached so that they can be resumed properly.
1232 */
1233void snd_hda_shutup_pins(struct hda_codec *codec)
1234{
1235 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001236 /* don't shut up pins when unloading the driver; otherwise it breaks
1237 * the default pin setup at the next load of the driver
1238 */
1239 if (codec->bus->shutdown)
1240 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001241 for (i = 0; i < codec->init_pins.used; i++) {
1242 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1243 /* use read here for syncing after issuing each verb */
1244 snd_hda_codec_read(codec, pin->nid, 0,
1245 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1246 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001247 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001248}
1249EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1250
Takashi Iwai2a439522011-07-26 09:52:50 +02001251#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001252/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1253static void restore_shutup_pins(struct hda_codec *codec)
1254{
1255 int i;
1256 if (!codec->pins_shutup)
1257 return;
1258 if (codec->bus->shutdown)
1259 return;
1260 for (i = 0; i < codec->init_pins.used; i++) {
1261 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1262 snd_hda_codec_write(codec, pin->nid, 0,
1263 AC_VERB_SET_PIN_WIDGET_CONTROL,
1264 pin->ctrl);
1265 }
1266 codec->pins_shutup = 0;
1267}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001268#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001269
David Henningsson26a6cb62012-10-09 15:04:21 +02001270static void hda_jackpoll_work(struct work_struct *work)
1271{
1272 struct hda_codec *codec =
1273 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001274
1275 snd_hda_jack_set_dirty_all(codec);
1276 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001277
1278 if (!codec->jackpoll_interval)
1279 return;
1280
David Henningsson26a6cb62012-10-09 15:04:21 +02001281 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1282 codec->jackpoll_interval);
1283}
1284
Takashi Iwai01751f52007-08-10 16:59:39 +02001285static void init_hda_cache(struct hda_cache_rec *cache,
1286 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001287static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001288
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001289/* release all pincfg lists */
1290static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001291{
Takashi Iwai346ff702009-02-23 09:42:57 +01001292 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001293#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001294 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001295#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001296 snd_array_free(&codec->init_pins);
1297}
1298
Takashi Iwai54d17402005-11-21 16:33:22 +01001299/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001300 * audio-converter setup caches
1301 */
1302struct hda_cvt_setup {
1303 hda_nid_t nid;
1304 u8 stream_tag;
1305 u8 channel_id;
1306 u16 format_id;
1307 unsigned char active; /* cvt is currently used */
1308 unsigned char dirty; /* setups should be cleared */
1309};
1310
1311/* get or create a cache entry for the given audio converter NID */
1312static struct hda_cvt_setup *
1313get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1314{
1315 struct hda_cvt_setup *p;
1316 int i;
1317
1318 for (i = 0; i < codec->cvt_setups.used; i++) {
1319 p = snd_array_elem(&codec->cvt_setups, i);
1320 if (p->nid == nid)
1321 return p;
1322 }
1323 p = snd_array_new(&codec->cvt_setups);
1324 if (p)
1325 p->nid = nid;
1326 return p;
1327}
1328
1329/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001330 * Dynamic symbol binding for the codec parsers
1331 */
1332#ifdef MODULE
1333#define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym))
1334#define unload_parser_addr(addr) symbol_put_addr(addr)
1335#else
1336#define load_parser_sym(sym) (sym)
1337#define unload_parser_addr(addr) do {} while (0)
1338#endif
1339
1340#define load_parser(codec, sym) \
1341 ((codec)->parser = load_parser_sym(sym))
1342
1343static void unload_parser(struct hda_codec *codec)
1344{
1345 if (codec->parser) {
1346 unload_parser_addr(codec->parser);
1347 codec->parser = NULL;
1348 }
1349}
1350
1351/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * codec destructor
1353 */
1354static void snd_hda_codec_free(struct hda_codec *codec)
1355{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001356 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001358 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001359 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001360 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001361#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001362 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001363 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001366 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001367 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001368 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001369 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001370 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 codec->bus->caddr_tbl[codec->addr] = NULL;
1372 if (codec->patch_ops.free)
1373 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001374#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001375 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001376 hda_call_pm_notify(codec->bus, false);
1377#endif
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001378 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001379 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001380 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001381 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001382 kfree(codec->vendor_name);
1383 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001384 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001385 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 kfree(codec);
1387}
1388
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001389static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1390 hda_nid_t fg, unsigned int power_state);
1391
Takashi Iwaid8193872012-08-31 07:54:38 -07001392static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001393 unsigned int power_state);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/**
1396 * snd_hda_codec_new - create a HDA codec
1397 * @bus: the bus to assign
1398 * @codec_addr: the codec address
1399 * @codecp: the pointer to store the generated codec
1400 *
1401 * Returns 0 if successful, or a negative error code.
1402 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001403int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001404 unsigned int codec_addr,
1405 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001408 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001409 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int err;
1411
Takashi Iwaida3cec32008-08-08 17:12:14 +02001412 if (snd_BUG_ON(!bus))
1413 return -EINVAL;
1414 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 snd_printk(KERN_ERR "hda_codec: "
1419 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EBUSY;
1421 }
1422
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001423 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (codec == NULL) {
1425 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1426 return -ENOMEM;
1427 }
1428
1429 codec->bus = bus;
1430 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001431 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001432 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001433 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001434 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001435 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001436 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1437 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001438 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001439 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001440 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001441 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001442 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001443 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001444 INIT_LIST_HEAD(&codec->conn_list);
1445
David Henningsson26a6cb62012-10-09 15:04:21 +02001446 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwai83012a72012-08-24 18:38:08 +02001448#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001449 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001450 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1451 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1452 * the caller has to power down appropriatley after initialization
1453 * phase.
1454 */
1455 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001456 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001457#endif
1458
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001459 if (codec->bus->modelname) {
1460 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1461 if (!codec->modelname) {
1462 snd_hda_codec_free(codec);
1463 return -ENODEV;
1464 }
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 list_add_tail(&codec->list, &bus->codec_list);
1468 bus->caddr_tbl[codec_addr] = codec;
1469
Takashi Iwai0ba21762007-04-16 11:29:14 +02001470 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1471 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001472 if (codec->vendor_id == -1)
1473 /* read again, hopefully the access method was corrected
1474 * in the last read...
1475 */
1476 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1477 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001478 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1479 AC_PAR_SUBSYSTEM_ID);
1480 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1481 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001483 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001484 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001485 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001486 err = -ENODEV;
1487 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Takashi Iwaid8193872012-08-31 07:54:38 -07001490 fg = codec->afg ? codec->afg : codec->mfg;
1491 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001492 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001493 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001494 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001495 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001496 err = read_pin_defaults(codec);
1497 if (err < 0)
1498 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001499
Takashi Iwai0ba21762007-04-16 11:29:14 +02001500 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001502 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001503 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001504 }
1505
Takashi Iwai83012a72012-08-24 18:38:08 +02001506#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001507 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001508 AC_PWRST_CLKSTOP);
1509 if (!codec->d3_stop_clk)
1510 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001511#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001512 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001513 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001514
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001515 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001516 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001517
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001518 snd_hda_codec_proc_new(codec);
1519
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001520 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001521
1522 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1523 codec->subsystem_id, codec->revision_id);
1524 snd_component_add(codec->bus->card, component);
1525
1526 if (codecp)
1527 *codecp = codec;
1528 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001529
1530 error:
1531 snd_hda_codec_free(codec);
1532 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001534EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001535
Mengdong Lina15d05d2013-02-08 17:09:31 -05001536int snd_hda_codec_update_widgets(struct hda_codec *codec)
1537{
1538 hda_nid_t fg;
1539 int err;
1540
1541 /* Assume the function group node does not change,
1542 * only the widget nodes may change.
1543 */
1544 kfree(codec->wcaps);
1545 fg = codec->afg ? codec->afg : codec->mfg;
1546 err = read_widget_caps(codec, fg);
1547 if (err < 0) {
1548 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1549 return err;
1550 }
1551
1552 snd_array_free(&codec->init_pins);
1553 err = read_pin_defaults(codec);
1554
1555 return err;
1556}
1557EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1558
1559
Takashi Iwaif0639272013-11-18 12:07:29 +01001560#ifdef CONFIG_SND_HDA_CODEC_HDMI
1561/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1562static bool is_likely_hdmi_codec(struct hda_codec *codec)
1563{
1564 hda_nid_t nid = codec->start_nid;
1565 int i;
1566
1567 for (i = 0; i < codec->num_nodes; i++, nid++) {
1568 unsigned int wcaps = get_wcaps(codec, nid);
1569 switch (get_wcaps_type(wcaps)) {
1570 case AC_WID_AUD_IN:
1571 return false; /* HDMI parser supports only HDMI out */
1572 case AC_WID_AUD_OUT:
1573 if (!(wcaps & AC_WCAP_DIGITAL))
1574 return false;
1575 break;
1576 }
1577 }
1578 return true;
1579}
1580#else
1581/* no HDMI codec parser support */
1582#define is_likely_hdmi_codec(codec) false
1583#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1584
Takashi Iwaid5191e52009-11-16 14:58:17 +01001585/**
1586 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1587 * @codec: the HDA codec
1588 *
1589 * Start parsing of the given codec tree and (re-)initialize the whole
1590 * patch instance.
1591 *
1592 * Returns 0 if successful or a negative error code.
1593 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001594int snd_hda_codec_configure(struct hda_codec *codec)
1595{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001596 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001597 int err;
1598
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001599 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001600 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001601 err = get_codec_name(codec);
1602 if (err < 0)
1603 return err;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001606 if (!is_generic_config(codec) && codec->preset)
1607 patch = codec->preset->patch;
1608 if (!patch) {
1609 unload_parser(codec); /* to be sure */
Takashi Iwaif0639272013-11-18 12:07:29 +01001610 if (is_likely_hdmi_codec(codec))
1611 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001612#ifdef CONFIG_SND_HDA_GENERIC
1613 if (!patch)
1614 patch = load_parser(codec, snd_hda_parse_generic_codec);
1615#endif
1616 if (!patch) {
1617 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1618 return -ENODEV;
1619 }
Takashi Iwai82467612007-07-27 19:15:54 +02001620 }
1621
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001622 err = patch(codec);
1623 if (err < 0) {
1624 unload_parser(codec);
1625 return err;
1626 }
Takashi Iwai82467612007-07-27 19:15:54 +02001627
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001628 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001629 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001630 if (err < 0)
1631 return err;
1632 }
1633
Takashi Iwaif62faed2009-12-23 09:27:51 +01001634 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001635 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001636 snprintf(codec->bus->card->mixername,
1637 sizeof(codec->bus->card->mixername),
1638 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001641EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Takashi Iwaied360812012-08-08 17:12:52 +02001643/* update the stream-id if changed */
1644static void update_pcm_stream_id(struct hda_codec *codec,
1645 struct hda_cvt_setup *p, hda_nid_t nid,
1646 u32 stream_tag, int channel_id)
1647{
1648 unsigned int oldval, newval;
1649
1650 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1651 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1652 newval = (stream_tag << 4) | channel_id;
1653 if (oldval != newval)
1654 snd_hda_codec_write(codec, nid, 0,
1655 AC_VERB_SET_CHANNEL_STREAMID,
1656 newval);
1657 p->stream_tag = stream_tag;
1658 p->channel_id = channel_id;
1659 }
1660}
1661
1662/* update the format-id if changed */
1663static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1664 hda_nid_t nid, int format)
1665{
1666 unsigned int oldval;
1667
1668 if (p->format_id != format) {
1669 oldval = snd_hda_codec_read(codec, nid, 0,
1670 AC_VERB_GET_STREAM_FORMAT, 0);
1671 if (oldval != format) {
1672 msleep(1);
1673 snd_hda_codec_write(codec, nid, 0,
1674 AC_VERB_SET_STREAM_FORMAT,
1675 format);
1676 }
1677 p->format_id = format;
1678 }
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/**
1682 * snd_hda_codec_setup_stream - set up the codec for streaming
1683 * @codec: the CODEC to set up
1684 * @nid: the NID to set up
1685 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1686 * @channel_id: channel id to pass, zero based.
1687 * @format: stream format.
1688 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001689void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1690 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int channel_id, int format)
1692{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001693 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001694 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001695 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001696 int i;
1697
Takashi Iwai0ba21762007-04-16 11:29:14 +02001698 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001699 return;
1700
Takashi Iwai0ba21762007-04-16 11:29:14 +02001701 snd_printdd("hda_codec_setup_stream: "
1702 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001704 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001705 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001706 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001707
1708 if (codec->pcm_format_first)
1709 update_pcm_format(codec, p, nid, format);
1710 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1711 if (!codec->pcm_format_first)
1712 update_pcm_format(codec, p, nid, format);
1713
Takashi Iwaieb541332010-08-06 13:48:11 +02001714 p->active = 1;
1715 p->dirty = 0;
1716
1717 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001718 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001719 list_for_each_entry(c, &codec->bus->codec_list, list) {
1720 for (i = 0; i < c->cvt_setups.used; i++) {
1721 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001722 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001723 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001724 p->dirty = 1;
1725 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001728EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Takashi Iwaif0cea792010-08-13 11:56:53 +02001730static void really_cleanup_stream(struct hda_codec *codec,
1731 struct hda_cvt_setup *q);
1732
Takashi Iwaid5191e52009-11-16 14:58:17 +01001733/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001734 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735 * @codec: the CODEC to clean up
1736 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001737 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001738 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001739void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1740 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001741{
Takashi Iwaieb541332010-08-06 13:48:11 +02001742 struct hda_cvt_setup *p;
1743
Takashi Iwai888afa12008-03-18 09:57:50 +01001744 if (!nid)
1745 return;
1746
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001747 if (codec->no_sticky_stream)
1748 do_now = 1;
1749
Takashi Iwai888afa12008-03-18 09:57:50 +01001750 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001751 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001752 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001753 /* here we just clear the active flag when do_now isn't set;
1754 * actual clean-ups will be done later in
1755 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1756 */
1757 if (do_now)
1758 really_cleanup_stream(codec, p);
1759 else
1760 p->active = 0;
1761 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001762}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001763EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001764
Takashi Iwaieb541332010-08-06 13:48:11 +02001765static void really_cleanup_stream(struct hda_codec *codec,
1766 struct hda_cvt_setup *q)
1767{
1768 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001769 if (q->stream_tag || q->channel_id)
1770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1771 if (q->format_id)
1772 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1773);
Takashi Iwaieb541332010-08-06 13:48:11 +02001774 memset(q, 0, sizeof(*q));
1775 q->nid = nid;
1776}
1777
1778/* clean up the all conflicting obsolete streams */
1779static void purify_inactive_streams(struct hda_codec *codec)
1780{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001781 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001782 int i;
1783
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001784 list_for_each_entry(c, &codec->bus->codec_list, list) {
1785 for (i = 0; i < c->cvt_setups.used; i++) {
1786 struct hda_cvt_setup *p;
1787 p = snd_array_elem(&c->cvt_setups, i);
1788 if (p->dirty)
1789 really_cleanup_stream(c, p);
1790 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001791 }
1792}
1793
Takashi Iwai2a439522011-07-26 09:52:50 +02001794#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001795/* clean up all streams; called from suspend */
1796static void hda_cleanup_all_streams(struct hda_codec *codec)
1797{
1798 int i;
1799
1800 for (i = 0; i < codec->cvt_setups.used; i++) {
1801 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1802 if (p->stream_tag)
1803 really_cleanup_stream(codec, p);
1804 }
1805}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001806#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*
1809 * amp access functions
1810 */
1811
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001812/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001813#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001814#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001815#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1816#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001818#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001821static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001822 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Takashi Iwai01751f52007-08-10 16:59:39 +02001824 memset(cache, 0, sizeof(*cache));
1825 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001826 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001827}
1828
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001829static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001830{
Takashi Iwai603c4012008-07-30 15:01:44 +02001831 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001835static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai01751f52007-08-10 16:59:39 +02001837 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1838 u16 cur = cache->hash[idx];
1839 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001842 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (info->key == key)
1844 return info;
1845 cur = info->next;
1846 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001847 return NULL;
1848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001850/* query the hash. allocate an entry if not found. */
1851static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1852 u32 key)
1853{
1854 struct hda_cache_head *info = get_hash(cache, key);
1855 if (!info) {
1856 u16 idx, cur;
1857 /* add a new hash entry */
1858 info = snd_array_new(&cache->buf);
1859 if (!info)
1860 return NULL;
1861 cur = snd_array_index(&cache->buf, info);
1862 info->key = key;
1863 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001864 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001865 idx = key % (u16)ARRAY_SIZE(cache->hash);
1866 info->next = cache->hash[idx];
1867 cache->hash[idx] = cur;
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return info;
1870}
1871
Takashi Iwai01751f52007-08-10 16:59:39 +02001872/* query and allocate an amp hash entry */
1873static inline struct hda_amp_info *
1874get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1875{
1876 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1877}
1878
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001879/* overwrite the value with the key in the caps hash */
1880static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1881{
1882 struct hda_amp_info *info;
1883
1884 mutex_lock(&codec->hash_mutex);
1885 info = get_alloc_amp_hash(codec, key);
1886 if (!info) {
1887 mutex_unlock(&codec->hash_mutex);
1888 return -EINVAL;
1889 }
1890 info->amp_caps = val;
1891 info->head.val |= INFO_AMP_CAPS;
1892 mutex_unlock(&codec->hash_mutex);
1893 return 0;
1894}
1895
1896/* query the value from the caps hash; if not found, fetch the current
1897 * value from the given function and store in the hash
1898 */
1899static unsigned int
1900query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1901 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1902{
1903 struct hda_amp_info *info;
1904 unsigned int val;
1905
1906 mutex_lock(&codec->hash_mutex);
1907 info = get_alloc_amp_hash(codec, key);
1908 if (!info) {
1909 mutex_unlock(&codec->hash_mutex);
1910 return 0;
1911 }
1912 if (!(info->head.val & INFO_AMP_CAPS)) {
1913 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1914 val = func(codec, nid, dir);
1915 write_caps_hash(codec, key, val);
1916 } else {
1917 val = info->amp_caps;
1918 mutex_unlock(&codec->hash_mutex);
1919 }
1920 return val;
1921}
1922
1923static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1924 int direction)
1925{
1926 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1927 nid = codec->afg;
1928 return snd_hda_param_read(codec, nid,
1929 direction == HDA_OUTPUT ?
1930 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1931}
1932
Takashi Iwaid5191e52009-11-16 14:58:17 +01001933/**
1934 * query_amp_caps - query AMP capabilities
1935 * @codec: the HD-auio codec
1936 * @nid: the NID to query
1937 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1938 *
1939 * Query AMP capabilities for the given widget and direction.
1940 * Returns the obtained capability bits.
1941 *
1942 * When cap bits have been already read, this doesn't read again but
1943 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001945u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001947 return query_caps_hash(codec, nid, direction,
1948 HDA_HASH_KEY(nid, direction, 0),
1949 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001951EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Takashi Iwaid5191e52009-11-16 14:58:17 +01001953/**
1954 * snd_hda_override_amp_caps - Override the AMP capabilities
1955 * @codec: the CODEC to clean up
1956 * @nid: the NID to clean up
1957 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1958 * @caps: the capability bits to set
1959 *
1960 * Override the cached AMP caps bits value by the given one.
1961 * This function is useful if the driver needs to adjust the AMP ranges,
1962 * e.g. limit to 0dB, etc.
1963 *
1964 * Returns zero if successful or a negative error code.
1965 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001966int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1967 unsigned int caps)
1968{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001969 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001971EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001972
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001973static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1974 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001975{
1976 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1977}
1978
Takashi Iwaid5191e52009-11-16 14:58:17 +01001979/**
1980 * snd_hda_query_pin_caps - Query PIN capabilities
1981 * @codec: the HD-auio codec
1982 * @nid: the NID to query
1983 *
1984 * Query PIN capabilities for the given widget.
1985 * Returns the obtained capability bits.
1986 *
1987 * When cap bits have been already read, this doesn't read again but
1988 * returns the cached value.
1989 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001990u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1991{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001992 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001993 read_pin_cap);
1994}
Takashi Iwai1327a322009-03-23 13:07:47 +01001995EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1996
Wu Fengguang864f92b2009-11-18 12:38:02 +08001997/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001998 * snd_hda_override_pin_caps - Override the pin capabilities
1999 * @codec: the CODEC
2000 * @nid: the NID to override
2001 * @caps: the capability bits to set
2002 *
2003 * Override the cached PIN capabilitiy bits value by the given one.
2004 *
2005 * Returns zero if successful or a negative error code.
2006 */
2007int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2008 unsigned int caps)
2009{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002010 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002011}
2012EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2013
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002014/* read or sync the hash value with the current value;
2015 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002017static struct hda_amp_info *
2018update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002019 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002021 struct hda_amp_info *info;
2022 unsigned int parm, val = 0;
2023 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002025 retry:
2026 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2027 if (!info)
2028 return NULL;
2029 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2030 if (!val_read) {
2031 mutex_unlock(&codec->hash_mutex);
2032 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2033 parm |= direction == HDA_OUTPUT ?
2034 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2035 parm |= index;
2036 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002037 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002038 val &= 0xff;
2039 val_read = true;
2040 mutex_lock(&codec->hash_mutex);
2041 goto retry;
2042 }
2043 info->vol[ch] = val;
2044 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002045 } else if (init_only)
2046 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002047 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002051 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002053static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002054 hda_nid_t nid, int ch, int direction, int index,
2055 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 u32 parm;
2058
2059 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2060 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2061 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002062 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2063 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002064 ; /* set the zero value as a fake mute */
2065 else
2066 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2068}
2069
Takashi Iwaid5191e52009-11-16 14:58:17 +01002070/**
2071 * snd_hda_codec_amp_read - Read AMP value
2072 * @codec: HD-audio codec
2073 * @nid: NID to read the AMP value
2074 * @ch: channel (left=0 or right=1)
2075 * @direction: #HDA_INPUT or #HDA_OUTPUT
2076 * @index: the index value (only for input direction)
2077 *
2078 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
Takashi Iwai834be882006-03-01 14:16:17 +01002080int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2081 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002083 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002084 unsigned int val = 0;
2085
2086 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002087 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088 if (info)
2089 val = info->vol[ch];
2090 mutex_unlock(&codec->hash_mutex);
2091 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002093EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Takashi Iwai280e57d2012-12-14 10:32:21 +01002095static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2096 int direction, int idx, int mask, int val,
2097 bool init_only)
2098{
2099 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002100 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002101 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002102
2103 if (snd_BUG_ON(mask & ~0xff))
2104 mask &= 0xff;
2105 val &= mask;
2106
2107 mutex_lock(&codec->hash_mutex);
2108 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2109 if (!info) {
2110 mutex_unlock(&codec->hash_mutex);
2111 return 0;
2112 }
2113 val |= info->vol[ch] & ~mask;
2114 if (info->vol[ch] == val) {
2115 mutex_unlock(&codec->hash_mutex);
2116 return 0;
2117 }
2118 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002119 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002120 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002121 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002122 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002123 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002124 return 1;
2125}
2126
Takashi Iwaid5191e52009-11-16 14:58:17 +01002127/**
2128 * snd_hda_codec_amp_update - update the AMP value
2129 * @codec: HD-audio codec
2130 * @nid: NID to read the AMP value
2131 * @ch: channel (left=0 or right=1)
2132 * @direction: #HDA_INPUT or #HDA_OUTPUT
2133 * @idx: the index value (only for input direction)
2134 * @mask: bit mask to set
2135 * @val: the bits value to set
2136 *
2137 * Update the AMP value with a bit mask.
2138 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002139 */
Takashi Iwai834be882006-03-01 14:16:17 +01002140int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2141 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002143 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002145EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Takashi Iwaid5191e52009-11-16 14:58:17 +01002147/**
2148 * snd_hda_codec_amp_stereo - update the AMP stereo values
2149 * @codec: HD-audio codec
2150 * @nid: NID to read the AMP value
2151 * @direction: #HDA_INPUT or #HDA_OUTPUT
2152 * @idx: the index value (only for input direction)
2153 * @mask: bit mask to set
2154 * @val: the bits value to set
2155 *
2156 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2157 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002158 */
2159int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2160 int direction, int idx, int mask, int val)
2161{
2162 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002163
2164 if (snd_BUG_ON(mask & ~0xff))
2165 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002166 for (ch = 0; ch < 2; ch++)
2167 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2168 idx, mask, val);
2169 return ret;
2170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002171EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002172
Takashi Iwai280e57d2012-12-14 10:32:21 +01002173/* Works like snd_hda_codec_amp_update() but it writes the value only at
2174 * the first access. If the amp was already initialized / updated beforehand,
2175 * this does nothing.
2176 */
2177int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2178 int dir, int idx, int mask, int val)
2179{
2180 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2181}
2182EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2183
2184int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2185 int dir, int idx, int mask, int val)
2186{
2187 int ch, ret = 0;
2188
2189 if (snd_BUG_ON(mask & ~0xff))
2190 mask &= 0xff;
2191 for (ch = 0; ch < 2; ch++)
2192 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2193 idx, mask, val);
2194 return ret;
2195}
2196EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2197
Takashi Iwaid5191e52009-11-16 14:58:17 +01002198/**
2199 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2200 * @codec: HD-audio codec
2201 *
2202 * Resume the all amp commands from the cache.
2203 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002204void snd_hda_codec_resume_amp(struct hda_codec *codec)
2205{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002206 int i;
2207
Takashi Iwaic370dd62012-12-13 18:30:04 +01002208 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002209 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002210 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2211 struct hda_amp_info *buffer;
2212 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002213 hda_nid_t nid;
2214 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002215 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002216
2217 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002218 if (!buffer->head.dirty)
2219 continue;
2220 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002221 info = *buffer;
2222 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002223 if (!key)
2224 continue;
2225 nid = key & 0xff;
2226 idx = (key >> 16) & 0xff;
2227 dir = (key >> 24) & 0xff;
2228 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002229 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002230 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002231 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002232 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2233 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002234 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002235 }
2236 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002237 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002238}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002239EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002241static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2242 unsigned int ofs)
2243{
2244 u32 caps = query_amp_caps(codec, nid, dir);
2245 /* get num steps */
2246 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2247 if (ofs < caps)
2248 caps -= ofs;
2249 return caps;
2250}
2251
Takashi Iwaid5191e52009-11-16 14:58:17 +01002252/**
2253 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2254 *
2255 * The control element is supposed to have the private_value field
2256 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002258int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2262 u16 nid = get_amp_nid(kcontrol);
2263 u8 chs = get_amp_channels(kcontrol);
2264 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002265 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002267 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268 uinfo->count = chs == 3 ? 2 : 1;
2269 uinfo->value.integer.min = 0;
2270 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2271 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002272 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002273 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2274 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return -EINVAL;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
2278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002279EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002281
2282static inline unsigned int
2283read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2284 int ch, int dir, int idx, unsigned int ofs)
2285{
2286 unsigned int val;
2287 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2288 val &= HDA_AMP_VOLMASK;
2289 if (val >= ofs)
2290 val -= ofs;
2291 else
2292 val = 0;
2293 return val;
2294}
2295
2296static inline int
2297update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2298 int ch, int dir, int idx, unsigned int ofs,
2299 unsigned int val)
2300{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002301 unsigned int maxval;
2302
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002303 if (val > 0)
2304 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002305 /* ofs = 0: raw max value */
2306 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002307 if (val > maxval)
2308 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002309 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2310 HDA_AMP_VOLMASK, val);
2311}
2312
Takashi Iwaid5191e52009-11-16 14:58:17 +01002313/**
2314 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2315 *
2316 * The control element is supposed to have the private_value field
2317 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2318 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002319int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2323 hda_nid_t nid = get_amp_nid(kcontrol);
2324 int chs = get_amp_channels(kcontrol);
2325 int dir = get_amp_direction(kcontrol);
2326 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002327 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 long *valp = ucontrol->value.integer.value;
2329
2330 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002331 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002333 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return 0;
2335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002336EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Takashi Iwaid5191e52009-11-16 14:58:17 +01002338/**
2339 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2340 *
2341 * The control element is supposed to have the private_value field
2342 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2343 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002344int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2348 hda_nid_t nid = get_amp_nid(kcontrol);
2349 int chs = get_amp_channels(kcontrol);
2350 int dir = get_amp_direction(kcontrol);
2351 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002352 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 long *valp = ucontrol->value.integer.value;
2354 int change = 0;
2355
Takashi Iwaicb53c622007-08-10 17:21:45 +02002356 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002357 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002358 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002359 valp++;
2360 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002361 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002362 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002363 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return change;
2365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002366EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Takashi Iwaid5191e52009-11-16 14:58:17 +01002368/**
2369 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2370 *
2371 * The control element is supposed to have the private_value field
2372 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2373 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002374int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2375 unsigned int size, unsigned int __user *_tlv)
2376{
2377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2378 hda_nid_t nid = get_amp_nid(kcontrol);
2379 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002380 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002381 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002382 u32 caps, val1, val2;
2383
2384 if (size < 4 * sizeof(unsigned int))
2385 return -ENOMEM;
2386 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002387 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2388 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002389 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002390 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002391 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002392 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002393 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002394 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2395 return -EFAULT;
2396 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2397 return -EFAULT;
2398 if (put_user(val1, _tlv + 2))
2399 return -EFAULT;
2400 if (put_user(val2, _tlv + 3))
2401 return -EFAULT;
2402 return 0;
2403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002404EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002405
Takashi Iwaid5191e52009-11-16 14:58:17 +01002406/**
2407 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2408 * @codec: HD-audio codec
2409 * @nid: NID of a reference widget
2410 * @dir: #HDA_INPUT or #HDA_OUTPUT
2411 * @tlv: TLV data to be stored, at least 4 elements
2412 *
2413 * Set (static) TLV data for a virtual master volume using the AMP caps
2414 * obtained from the reference NID.
2415 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002416 */
2417void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2418 unsigned int *tlv)
2419{
2420 u32 caps;
2421 int nums, step;
2422
2423 caps = query_amp_caps(codec, nid, dir);
2424 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2425 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2426 step = (step + 1) * 25;
2427 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2428 tlv[1] = 2 * sizeof(unsigned int);
2429 tlv[2] = -nums * step;
2430 tlv[3] = step;
2431}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002432EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002433
2434/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002435static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002436find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002437{
2438 struct snd_ctl_elem_id id;
2439 memset(&id, 0, sizeof(id));
2440 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002441 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002442 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002443 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2444 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002445 strcpy(id.name, name);
2446 return snd_ctl_find_id(codec->bus->card, &id);
2447}
2448
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449/**
2450 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2451 * @codec: HD-audio codec
2452 * @name: ctl id name string
2453 *
2454 * Get the control element with the given id string and IFACE_MIXER.
2455 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002456struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2457 const char *name)
2458{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002459 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002461EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002462
Takashi Iwaidcda5802012-10-12 17:24:51 +02002463static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002464 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002465{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002466 int i, idx;
2467 /* 16 ctlrs should be large enough */
2468 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2469 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002470 return idx;
2471 }
2472 return -EBUSY;
2473}
2474
Takashi Iwaid5191e52009-11-16 14:58:17 +01002475/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002476 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002477 * @codec: HD-audio codec
2478 * @nid: corresponding NID (optional)
2479 * @kctl: the control element to assign
2480 *
2481 * Add the given control element to an array inside the codec instance.
2482 * All control elements belonging to a codec are supposed to be added
2483 * by this function so that a proper clean-up works at the free or
2484 * reconfiguration time.
2485 *
2486 * If non-zero @nid is passed, the NID is assigned to the control element.
2487 * The assignment is shown in the codec proc file.
2488 *
2489 * snd_hda_ctl_add() checks the control subdev id field whether
2490 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002491 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2492 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002493 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002494int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2495 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002496{
2497 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002498 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002499 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002500
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002501 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002502 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002503 if (nid == 0)
2504 nid = get_amp_nid_(kctl->private_value);
2505 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002506 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2507 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002508 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002509 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002510 err = snd_ctl_add(codec->bus->card, kctl);
2511 if (err < 0)
2512 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002513 item = snd_array_new(&codec->mixers);
2514 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002515 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002516 item->kctl = kctl;
2517 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002518 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002519 return 0;
2520}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002521EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002522
Takashi Iwaid5191e52009-11-16 14:58:17 +01002523/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002524 * snd_hda_add_nid - Assign a NID to a control element
2525 * @codec: HD-audio codec
2526 * @nid: corresponding NID (optional)
2527 * @kctl: the control element to assign
2528 * @index: index to kctl
2529 *
2530 * Add the given control element to an array inside the codec instance.
2531 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2532 * NID:KCTL mapping - for example "Capture Source" selector.
2533 */
2534int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2535 unsigned int index, hda_nid_t nid)
2536{
2537 struct hda_nid_item *item;
2538
2539 if (nid > 0) {
2540 item = snd_array_new(&codec->nids);
2541 if (!item)
2542 return -ENOMEM;
2543 item->kctl = kctl;
2544 item->index = index;
2545 item->nid = nid;
2546 return 0;
2547 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002548 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2549 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002550 return -EINVAL;
2551}
2552EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2553
2554/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002555 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2556 * @codec: HD-audio codec
2557 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002558void snd_hda_ctls_clear(struct hda_codec *codec)
2559{
2560 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002561 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002562 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002563 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002564 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002565 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002566}
2567
Takashi Iwaia65d6292009-02-23 16:57:04 +01002568/* pseudo device locking
2569 * toggle card->shutdown to allow/disallow the device access (as a hack)
2570 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002571int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002572{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002573 struct snd_card *card = bus->card;
2574 struct hda_codec *codec;
2575
Takashi Iwaia65d6292009-02-23 16:57:04 +01002576 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002577 if (card->shutdown)
2578 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002579 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002580 if (!list_empty(&card->ctl_files))
2581 goto err_clear;
2582
2583 list_for_each_entry(codec, &bus->codec_list, list) {
2584 int pcm;
2585 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2586 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2587 if (!cpcm->pcm)
2588 continue;
2589 if (cpcm->pcm->streams[0].substream_opened ||
2590 cpcm->pcm->streams[1].substream_opened)
2591 goto err_clear;
2592 }
2593 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002594 spin_unlock(&card->files_lock);
2595 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002596
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002597 err_clear:
2598 card->shutdown = 0;
2599 err_unlock:
2600 spin_unlock(&card->files_lock);
2601 return -EINVAL;
2602}
2603EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2604
2605void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002606{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002607 struct snd_card *card = bus->card;
2608
2609 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002610 spin_lock(&card->files_lock);
2611 card->shutdown = 0;
2612 spin_unlock(&card->files_lock);
2613}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002614EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002615
Takashi Iwaid5191e52009-11-16 14:58:17 +01002616/**
2617 * snd_hda_codec_reset - Clear all objects assigned to the codec
2618 * @codec: HD-audio codec
2619 *
2620 * This frees the all PCM and control elements assigned to the codec, and
2621 * clears the caches and restores the pin default configurations.
2622 *
2623 * When a device is being used, it returns -EBSY. If successfully freed,
2624 * returns zero.
2625 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002626int snd_hda_codec_reset(struct hda_codec *codec)
2627{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002628 struct hda_bus *bus = codec->bus;
2629 struct snd_card *card = bus->card;
2630 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002631
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002632 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002633 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002634
2635 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002636 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002637#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002638 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002639 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002640#endif
2641 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002642 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002643 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002644 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002645 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002646 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002647 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002648 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002649 }
2650 if (codec->patch_ops.free)
2651 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002652 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002653 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002654 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002655 codec->spec = NULL;
2656 free_hda_cache(&codec->amp_cache);
2657 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002658 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2659 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002660 /* free only driver_pins so that init_pins + user_pins are restored */
2661 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002662 snd_array_free(&codec->cvt_setups);
2663 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002664 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002665 codec->num_pcms = 0;
2666 codec->pcm_info = NULL;
2667 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002668 codec->slave_dig_outs = NULL;
2669 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002670 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002671 module_put(codec->owner);
2672 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002673
2674 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002675 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002676 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002677}
2678
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002679typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2680
2681/* apply the function to all matching slave ctls in the mixer list */
2682static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002683 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002684{
2685 struct hda_nid_item *items;
2686 const char * const *s;
2687 int i, err;
2688
2689 items = codec->mixers.list;
2690 for (i = 0; i < codec->mixers.used; i++) {
2691 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002692 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002693 continue;
2694 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002695 char tmpname[sizeof(sctl->id.name)];
2696 const char *name = *s;
2697 if (suffix) {
2698 snprintf(tmpname, sizeof(tmpname), "%s %s",
2699 name, suffix);
2700 name = tmpname;
2701 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002702 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002703 err = func(data, sctl);
2704 if (err)
2705 return err;
2706 break;
2707 }
2708 }
2709 }
2710 return 0;
2711}
2712
2713static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2714{
2715 return 1;
2716}
2717
Takashi Iwai18478e82012-03-09 17:51:10 +01002718/* guess the value corresponding to 0dB */
Takashi Iwai485e3e02013-11-04 15:51:00 +01002719static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002720{
2721 int _tlv[4];
2722 const int *tlv = NULL;
2723 int val = -1;
2724
2725 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2726 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2727 mm_segment_t fs = get_fs();
2728 set_fs(get_ds());
2729 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2730 tlv = _tlv;
2731 set_fs(fs);
2732 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2733 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002734 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2735 int step = tlv[3];
2736 step &= ~TLV_DB_SCALE_MUTE;
2737 if (!step)
2738 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002739 if (*step_to_check && *step_to_check != step) {
2740 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2741 *step_to_check, step);
2742 return -1;
2743 }
2744 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002745 val = -tlv[2] / step;
2746 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002747 return val;
2748}
2749
2750/* call kctl->put with the given value(s) */
2751static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2752{
2753 struct snd_ctl_elem_value *ucontrol;
2754 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2755 if (!ucontrol)
2756 return -ENOMEM;
2757 ucontrol->value.integer.value[0] = val;
2758 ucontrol->value.integer.value[1] = val;
2759 kctl->put(kctl, ucontrol);
2760 kfree(ucontrol);
2761 return 0;
2762}
2763
2764/* initialize the slave volume with 0dB */
2765static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2766{
Takashi Iwai485e3e02013-11-04 15:51:00 +01002767 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002768 if (offset > 0)
2769 put_kctl_with_value(slave, offset);
2770 return 0;
2771}
2772
2773/* unmute the slave */
2774static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2775{
2776 return put_kctl_with_value(slave, 1);
2777}
2778
Takashi Iwaid5191e52009-11-16 14:58:17 +01002779/**
2780 * snd_hda_add_vmaster - create a virtual master control and add slaves
2781 * @codec: HD-audio codec
2782 * @name: vmaster control name
2783 * @tlv: TLV data (optional)
2784 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002785 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002786 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002787 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002788 *
2789 * Create a virtual master control with the given name. The TLV data
2790 * must be either NULL or a valid data.
2791 *
2792 * @slaves is a NULL-terminated array of strings, each of which is a
2793 * slave control name. All controls with these names are assigned to
2794 * the new virtual master control.
2795 *
2796 * This function returns zero if successful or a negative error code.
2797 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002798int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002799 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002800 const char *suffix, bool init_slave_vol,
2801 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002802{
2803 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002804 int err;
2805
Takashi Iwai29e58532012-03-12 12:25:03 +01002806 if (ctl_ret)
2807 *ctl_ret = NULL;
2808
Takashi Iwai9322ca52012-02-03 14:28:01 +01002809 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002810 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002811 snd_printdd("No slave found for %s\n", name);
2812 return 0;
2813 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002814 kctl = snd_ctl_make_virtual_master(name, tlv);
2815 if (!kctl)
2816 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002817 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002818 if (err < 0)
2819 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002820
Takashi Iwai9322ca52012-02-03 14:28:01 +01002821 err = map_slaves(codec, slaves, suffix,
2822 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002823 if (err < 0)
2824 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002825
2826 /* init with master mute & zero volume */
2827 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002828 if (init_slave_vol) {
2829 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002830 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002831 tlv ? init_slave_0dB : init_slave_unmute, &step);
2832 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002833
Takashi Iwai29e58532012-03-12 12:25:03 +01002834 if (ctl_ret)
2835 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002836 return 0;
2837}
Takashi Iwai18478e82012-03-09 17:51:10 +01002838EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002839
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002840/*
2841 * mute-LED control using vmaster
2842 */
2843static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_info *uinfo)
2845{
2846 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002847 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002848 };
2849 unsigned int index;
2850
2851 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2852 uinfo->count = 1;
2853 uinfo->value.enumerated.items = 3;
2854 index = uinfo->value.enumerated.item;
2855 if (index >= 3)
2856 index = 2;
2857 strcpy(uinfo->value.enumerated.name, texts[index]);
2858 return 0;
2859}
2860
2861static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
2863{
2864 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2865 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2866 return 0;
2867}
2868
2869static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_value *ucontrol)
2871{
2872 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2873 unsigned int old_mode = hook->mute_mode;
2874
2875 hook->mute_mode = ucontrol->value.enumerated.item[0];
2876 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2877 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2878 if (old_mode == hook->mute_mode)
2879 return 0;
2880 snd_hda_sync_vmaster_hook(hook);
2881 return 1;
2882}
2883
2884static struct snd_kcontrol_new vmaster_mute_mode = {
2885 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2886 .name = "Mute-LED Mode",
2887 .info = vmaster_mute_mode_info,
2888 .get = vmaster_mute_mode_get,
2889 .put = vmaster_mute_mode_put,
2890};
2891
2892/*
2893 * Add a mute-LED hook with the given vmaster switch kctl
2894 * "Mute-LED Mode" control is automatically created and associated with
2895 * the given hook.
2896 */
2897int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002898 struct hda_vmaster_mute_hook *hook,
2899 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002900{
2901 struct snd_kcontrol *kctl;
2902
2903 if (!hook->hook || !hook->sw_kctl)
2904 return 0;
2905 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2906 hook->codec = codec;
2907 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002908 if (!expose_enum_ctl)
2909 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002910 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2911 if (!kctl)
2912 return -ENOMEM;
2913 return snd_hda_ctl_add(codec, 0, kctl);
2914}
2915EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2916
2917/*
2918 * Call the hook with the current value for synchronization
2919 * Should be called in init callback
2920 */
2921void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2922{
2923 if (!hook->hook || !hook->codec)
2924 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002925 /* don't call vmaster hook in the destructor since it might have
2926 * been already destroyed
2927 */
2928 if (hook->codec->bus->shutdown)
2929 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002930 switch (hook->mute_mode) {
2931 case HDA_VMUTE_FOLLOW_MASTER:
2932 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2933 break;
2934 default:
2935 hook->hook(hook->codec, hook->mute_mode);
2936 break;
2937 }
2938}
2939EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2940
2941
Takashi Iwaid5191e52009-11-16 14:58:17 +01002942/**
2943 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2944 *
2945 * The control element is supposed to have the private_value field
2946 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2947 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002948int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 int chs = get_amp_channels(kcontrol);
2952
2953 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2954 uinfo->count = chs == 3 ? 2 : 1;
2955 uinfo->value.integer.min = 0;
2956 uinfo->value.integer.max = 1;
2957 return 0;
2958}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002959EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Takashi Iwaid5191e52009-11-16 14:58:17 +01002961/**
2962 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2963 *
2964 * The control element is supposed to have the private_value field
2965 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2966 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002967int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
2970 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2971 hda_nid_t nid = get_amp_nid(kcontrol);
2972 int chs = get_amp_channels(kcontrol);
2973 int dir = get_amp_direction(kcontrol);
2974 int idx = get_amp_index(kcontrol);
2975 long *valp = ucontrol->value.integer.value;
2976
2977 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002978 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002979 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002981 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002982 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return 0;
2984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002985EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Takashi Iwaid5191e52009-11-16 14:58:17 +01002987/**
2988 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2989 *
2990 * The control element is supposed to have the private_value field
2991 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2992 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2997 hda_nid_t nid = get_amp_nid(kcontrol);
2998 int chs = get_amp_channels(kcontrol);
2999 int dir = get_amp_direction(kcontrol);
3000 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 long *valp = ucontrol->value.integer.value;
3002 int change = 0;
3003
Takashi Iwaicb53c622007-08-10 17:21:45 +02003004 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003005 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003006 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003007 HDA_AMP_MUTE,
3008 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003009 valp++;
3010 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003011 if (chs & 2)
3012 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003013 HDA_AMP_MUTE,
3014 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003015 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003016 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return change;
3018}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003019EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021/*
Takashi Iwai985be542005-11-02 18:26:49 +01003022 * bound volume controls
3023 *
3024 * bind multiple volumes (# indices, from 0)
3025 */
3026
3027#define AMP_VAL_IDX_SHIFT 19
3028#define AMP_VAL_IDX_MASK (0x0f<<19)
3029
Takashi Iwaid5191e52009-11-16 14:58:17 +01003030/**
3031 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3032 *
3033 * The control element is supposed to have the private_value field
3034 * set up via HDA_BIND_MUTE*() macros.
3035 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003036int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3037 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003038{
3039 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3040 unsigned long pval;
3041 int err;
3042
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003043 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003044 pval = kcontrol->private_value;
3045 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3046 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3047 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003048 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003049 return err;
3050}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003051EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003052
Takashi Iwaid5191e52009-11-16 14:58:17 +01003053/**
3054 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3055 *
3056 * The control element is supposed to have the private_value field
3057 * set up via HDA_BIND_MUTE*() macros.
3058 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003059int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3060 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003061{
3062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3063 unsigned long pval;
3064 int i, indices, err = 0, change = 0;
3065
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003066 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003067 pval = kcontrol->private_value;
3068 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3069 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003070 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3071 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003072 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3073 if (err < 0)
3074 break;
3075 change |= err;
3076 }
3077 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003078 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003079 return err < 0 ? err : change;
3080}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003081EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003082
Takashi Iwaid5191e52009-11-16 14:58:17 +01003083/**
3084 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3085 *
3086 * The control element is supposed to have the private_value field
3087 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003088 */
3089int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3090 struct snd_ctl_elem_info *uinfo)
3091{
3092 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3093 struct hda_bind_ctls *c;
3094 int err;
3095
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003096 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003097 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003098 kcontrol->private_value = *c->values;
3099 err = c->ops->info(kcontrol, uinfo);
3100 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003101 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003102 return err;
3103}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003104EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003105
Takashi Iwaid5191e52009-11-16 14:58:17 +01003106/**
3107 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3108 *
3109 * The control element is supposed to have the private_value field
3110 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3111 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003112int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3114{
3115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3116 struct hda_bind_ctls *c;
3117 int err;
3118
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003119 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003120 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003121 kcontrol->private_value = *c->values;
3122 err = c->ops->get(kcontrol, ucontrol);
3123 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003124 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003125 return err;
3126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003127EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003128
Takashi Iwaid5191e52009-11-16 14:58:17 +01003129/**
3130 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3131 *
3132 * The control element is supposed to have the private_value field
3133 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3134 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003135int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol)
3137{
3138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3139 struct hda_bind_ctls *c;
3140 unsigned long *vals;
3141 int err = 0, change = 0;
3142
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003143 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003144 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003145 for (vals = c->values; *vals; vals++) {
3146 kcontrol->private_value = *vals;
3147 err = c->ops->put(kcontrol, ucontrol);
3148 if (err < 0)
3149 break;
3150 change |= err;
3151 }
3152 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003153 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003154 return err < 0 ? err : change;
3155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003156EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003157
Takashi Iwaid5191e52009-11-16 14:58:17 +01003158/**
3159 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3160 *
3161 * The control element is supposed to have the private_value field
3162 * set up via HDA_BIND_VOL() macro.
3163 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003164int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3165 unsigned int size, unsigned int __user *tlv)
3166{
3167 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3168 struct hda_bind_ctls *c;
3169 int err;
3170
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003171 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003172 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003173 kcontrol->private_value = *c->values;
3174 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3175 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003176 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003177 return err;
3178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003179EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003180
3181struct hda_ctl_ops snd_hda_bind_vol = {
3182 .info = snd_hda_mixer_amp_volume_info,
3183 .get = snd_hda_mixer_amp_volume_get,
3184 .put = snd_hda_mixer_amp_volume_put,
3185 .tlv = snd_hda_mixer_amp_tlv
3186};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003187EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003188
3189struct hda_ctl_ops snd_hda_bind_sw = {
3190 .info = snd_hda_mixer_amp_switch_info,
3191 .get = snd_hda_mixer_amp_switch_get,
3192 .put = snd_hda_mixer_amp_switch_put,
3193 .tlv = snd_hda_mixer_amp_tlv
3194};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003195EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003196
3197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * SPDIF out controls
3199 */
3200
Takashi Iwai0ba21762007-04-16 11:29:14 +02003201static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3202 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3205 uinfo->count = 1;
3206 return 0;
3207}
3208
Takashi Iwai0ba21762007-04-16 11:29:14 +02003209static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
3212 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3213 IEC958_AES0_NONAUDIO |
3214 IEC958_AES0_CON_EMPHASIS_5015 |
3215 IEC958_AES0_CON_NOT_COPYRIGHT;
3216 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3217 IEC958_AES1_CON_ORIGINAL;
3218 return 0;
3219}
3220
Takashi Iwai0ba21762007-04-16 11:29:14 +02003221static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3225 IEC958_AES0_NONAUDIO |
3226 IEC958_AES0_PRO_EMPHASIS_5015;
3227 return 0;
3228}
3229
Takashi Iwai0ba21762007-04-16 11:29:14 +02003230static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3231 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003234 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003235 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003237 mutex_lock(&codec->spdif_mutex);
3238 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003239 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3240 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3241 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3242 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003243 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 return 0;
3246}
3247
3248/* convert from SPDIF status bits to HDA SPDIF bits
3249 * bit 0 (DigEn) is always set zero (to be filled later)
3250 */
3251static unsigned short convert_from_spdif_status(unsigned int sbits)
3252{
3253 unsigned short val = 0;
3254
3255 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003258 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003260 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3261 IEC958_AES0_PRO_EMPHASIS_5015)
3262 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003264 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3265 IEC958_AES0_CON_EMPHASIS_5015)
3266 val |= AC_DIG1_EMPHASIS;
3267 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3268 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003270 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3272 }
3273 return val;
3274}
3275
3276/* convert to SPDIF status bits from HDA SPDIF bits
3277 */
3278static unsigned int convert_to_spdif_status(unsigned short val)
3279{
3280 unsigned int sbits = 0;
3281
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003284 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 sbits |= IEC958_AES0_PROFESSIONAL;
3286 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003287 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3289 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003290 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003292 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003294 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3296 sbits |= val & (0x7f << 8);
3297 }
3298 return sbits;
3299}
3300
Takashi Iwai2f728532008-09-25 16:32:41 +02003301/* set digital convert verbs both for the given NID and its slaves */
3302static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3303 int verb, int val)
3304{
Takashi Iwaidda14412011-05-02 11:29:30 +02003305 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003306
Takashi Iwai9e976972008-11-25 08:17:20 +01003307 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003308 d = codec->slave_dig_outs;
3309 if (!d)
3310 return;
3311 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003312 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003313}
3314
3315static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3316 int dig1, int dig2)
3317{
3318 if (dig1 != -1)
3319 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3320 if (dig2 != -1)
3321 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3322}
3323
Takashi Iwai0ba21762007-04-16 11:29:14 +02003324static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003328 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003329 struct hda_spdif_out *spdif;
3330 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 unsigned short val;
3332 int change;
3333
Ingo Molnar62932df2006-01-16 16:34:20 +01003334 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003335 spdif = snd_array_elem(&codec->spdif_out, idx);
3336 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003337 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3339 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3340 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003341 val = convert_from_spdif_status(spdif->status);
3342 val |= spdif->ctls & 1;
3343 change = spdif->ctls != val;
3344 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003345 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003346 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003347 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return change;
3349}
3350
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003351#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Takashi Iwai0ba21762007-04-16 11:29:14 +02003353static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3354 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003357 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003358 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003360 mutex_lock(&codec->spdif_mutex);
3361 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003362 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003363 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return 0;
3365}
3366
Stephen Warren74b654c2011-06-01 11:14:18 -06003367static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3368 int dig1, int dig2)
3369{
3370 set_dig_out_convert(codec, nid, dig1, dig2);
3371 /* unmute amp switch (if any) */
3372 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3373 (dig1 & AC_DIG1_ENABLE))
3374 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3375 HDA_AMP_MUTE, 0);
3376}
3377
Takashi Iwai0ba21762007-04-16 11:29:14 +02003378static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3379 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003382 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003383 struct hda_spdif_out *spdif;
3384 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 unsigned short val;
3386 int change;
3387
Ingo Molnar62932df2006-01-16 16:34:20 +01003388 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003389 spdif = snd_array_elem(&codec->spdif_out, idx);
3390 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003391 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003393 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003394 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003395 spdif->ctls = val;
3396 if (change && nid != (u16)-1)
3397 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003398 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return change;
3400}
3401
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003402static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 {
3404 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003406 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 .info = snd_hda_spdif_mask_info,
3408 .get = snd_hda_spdif_cmask_get,
3409 },
3410 {
3411 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3412 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003413 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 .info = snd_hda_spdif_mask_info,
3415 .get = snd_hda_spdif_pmask_get,
3416 },
3417 {
3418 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003419 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 .info = snd_hda_spdif_mask_info,
3421 .get = snd_hda_spdif_default_get,
3422 .put = snd_hda_spdif_default_put,
3423 },
3424 {
3425 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003426 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 .info = snd_hda_spdif_out_switch_info,
3428 .get = snd_hda_spdif_out_switch_get,
3429 .put = snd_hda_spdif_out_switch_put,
3430 },
3431 { } /* end */
3432};
3433
3434/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003435 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003437 * @associated_nid: NID that new ctls associated with
3438 * @cvt_nid: converter NID
3439 * @type: HDA_PCM_TYPE_*
3440 * Creates controls related with the digital output.
3441 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 *
3443 * Returns 0 if successful, or a negative error code.
3444 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003445int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3446 hda_nid_t associated_nid,
3447 hda_nid_t cvt_nid,
3448 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
3450 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003451 struct snd_kcontrol *kctl;
3452 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003453 int idx = 0;
3454 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003455 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003456 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003458 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003459 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003460 idx = spdif_index;
3461 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003462 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003463 /* suppose a single SPDIF device */
3464 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3465 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3466 if (!kctl)
3467 break;
3468 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003469 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003470 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003471 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003472 if (!bus->primary_dig_out_type)
3473 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003474
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003475 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003476 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003477 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3478 return -EBUSY;
3479 }
Stephen Warren7c935972011-06-01 11:14:17 -06003480 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003481 if (!spdif)
3482 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3484 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003485 if (!kctl)
3486 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003487 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003488 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003489 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003490 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return err;
3492 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003493 spdif->nid = cvt_nid;
3494 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003495 AC_VERB_GET_DIGI_CONVERT_1, 0);
3496 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return 0;
3498}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003499EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003501/* get the hda_spdif_out entry from the given NID
3502 * call within spdif_mutex lock
3503 */
Stephen Warren7c935972011-06-01 11:14:17 -06003504struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3505 hda_nid_t nid)
3506{
3507 int i;
3508 for (i = 0; i < codec->spdif_out.used; i++) {
3509 struct hda_spdif_out *spdif =
3510 snd_array_elem(&codec->spdif_out, i);
3511 if (spdif->nid == nid)
3512 return spdif;
3513 }
3514 return NULL;
3515}
3516EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3517
Stephen Warren74b654c2011-06-01 11:14:18 -06003518void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3519{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003520 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003521
3522 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003523 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003524 spdif->nid = (u16)-1;
3525 mutex_unlock(&codec->spdif_mutex);
3526}
3527EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3528
3529void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3530{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003531 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003532 unsigned short val;
3533
3534 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003535 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003536 if (spdif->nid != nid) {
3537 spdif->nid = nid;
3538 val = spdif->ctls;
3539 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3540 }
3541 mutex_unlock(&codec->spdif_mutex);
3542}
3543EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003546 * SPDIF sharing with analog output
3547 */
3548static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3549 struct snd_ctl_elem_value *ucontrol)
3550{
3551 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3552 ucontrol->value.integer.value[0] = mout->share_spdif;
3553 return 0;
3554}
3555
3556static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3557 struct snd_ctl_elem_value *ucontrol)
3558{
3559 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3560 mout->share_spdif = !!ucontrol->value.integer.value[0];
3561 return 0;
3562}
3563
3564static struct snd_kcontrol_new spdif_share_sw = {
3565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3566 .name = "IEC958 Default PCM Playback Switch",
3567 .info = snd_ctl_boolean_mono_info,
3568 .get = spdif_share_sw_get,
3569 .put = spdif_share_sw_put,
3570};
3571
Takashi Iwaid5191e52009-11-16 14:58:17 +01003572/**
3573 * snd_hda_create_spdif_share_sw - create Default PCM switch
3574 * @codec: the HDA codec
3575 * @mout: multi-out instance
3576 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003577int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3578 struct hda_multi_out *mout)
3579{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003580 struct snd_kcontrol *kctl;
3581
Takashi Iwai9a081602008-02-12 18:37:26 +01003582 if (!mout->dig_out_nid)
3583 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003584
3585 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3586 if (!kctl)
3587 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003588 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003589 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003590}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003591EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003592
3593/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 * SPDIF input
3595 */
3596
3597#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3598
Takashi Iwai0ba21762007-04-16 11:29:14 +02003599static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3600 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3603
3604 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3605 return 0;
3606}
3607
Takashi Iwai0ba21762007-04-16 11:29:14 +02003608static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3609 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3612 hda_nid_t nid = kcontrol->private_value;
3613 unsigned int val = !!ucontrol->value.integer.value[0];
3614 int change;
3615
Ingo Molnar62932df2006-01-16 16:34:20 +01003616 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003618 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003620 snd_hda_codec_write_cache(codec, nid, 0,
3621 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003623 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return change;
3625}
3626
Takashi Iwai0ba21762007-04-16 11:29:14 +02003627static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3628 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3631 hda_nid_t nid = kcontrol->private_value;
3632 unsigned short val;
3633 unsigned int sbits;
3634
Andrew Paprocki3982d172007-12-19 12:13:44 +01003635 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 sbits = convert_to_spdif_status(val);
3637 ucontrol->value.iec958.status[0] = sbits;
3638 ucontrol->value.iec958.status[1] = sbits >> 8;
3639 ucontrol->value.iec958.status[2] = sbits >> 16;
3640 ucontrol->value.iec958.status[3] = sbits >> 24;
3641 return 0;
3642}
3643
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003644static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 {
3646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003647 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 .info = snd_hda_spdif_in_switch_info,
3649 .get = snd_hda_spdif_in_switch_get,
3650 .put = snd_hda_spdif_in_switch_put,
3651 },
3652 {
3653 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003655 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 .info = snd_hda_spdif_mask_info,
3657 .get = snd_hda_spdif_in_status_get,
3658 },
3659 { } /* end */
3660};
3661
3662/**
3663 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3664 * @codec: the HDA codec
3665 * @nid: audio in widget NID
3666 *
3667 * Creates controls related with the SPDIF input.
3668 * Called from each patch supporting the SPDIF in.
3669 *
3670 * Returns 0 if successful, or a negative error code.
3671 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003672int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003675 struct snd_kcontrol *kctl;
3676 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003677 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Takashi Iwaidcda5802012-10-12 17:24:51 +02003679 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003680 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003681 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3682 return -EBUSY;
3683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3685 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003686 if (!kctl)
3687 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003689 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003690 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return err;
3692 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003693 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003694 snd_hda_codec_read(codec, nid, 0,
3695 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003696 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return 0;
3698}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003699EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003701/*
3702 * command cache
3703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Takashi Iwaic370dd62012-12-13 18:30:04 +01003705/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003706#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3707#define get_cmd_cache_nid(key) ((key) & 0xff)
3708#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3709
3710/**
3711 * snd_hda_codec_write_cache - send a single command with caching
3712 * @codec: the HDA codec
3713 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003714 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003715 * @verb: the verb to send
3716 * @parm: the parameter for the verb
3717 *
3718 * Send a single command without waiting for response.
3719 *
3720 * Returns 0 if successful, or a negative error code.
3721 */
3722int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003723 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003724{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003725 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003726 struct hda_cache_head *c;
3727 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003728 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003729
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003730 cache_only = codec->cached_write;
3731 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003732 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003733 if (err < 0)
3734 return err;
3735 }
3736
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003737 /* parm may contain the verb stuff for get/set amp */
3738 verb = verb | (parm >> 8);
3739 parm &= 0xff;
3740 key = build_cmd_cache_key(nid, verb);
3741 mutex_lock(&codec->bus->cmd_mutex);
3742 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003743 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003744 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003745 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003746 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003747 mutex_unlock(&codec->bus->cmd_mutex);
3748 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003750EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003751
Takashi Iwaid5191e52009-11-16 14:58:17 +01003752/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003753 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3754 * @codec: the HDA codec
3755 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003756 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003757 * @verb: the verb to send
3758 * @parm: the parameter for the verb
3759 *
3760 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3761 * command if the parameter is already identical with the cached value.
3762 * If not, it sends the command and refreshes the cache.
3763 *
3764 * Returns 0 if successful, or a negative error code.
3765 */
3766int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003767 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003768{
3769 struct hda_cache_head *c;
3770 u32 key;
3771
3772 /* parm may contain the verb stuff for get/set amp */
3773 verb = verb | (parm >> 8);
3774 parm &= 0xff;
3775 key = build_cmd_cache_key(nid, verb);
3776 mutex_lock(&codec->bus->cmd_mutex);
3777 c = get_hash(&codec->cmd_cache, key);
3778 if (c && c->val == parm) {
3779 mutex_unlock(&codec->bus->cmd_mutex);
3780 return 0;
3781 }
3782 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003783 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003784}
3785EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3786
3787/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003788 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3789 * @codec: HD-audio codec
3790 *
3791 * Execute all verbs recorded in the command caches to resume.
3792 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003793void snd_hda_codec_resume_cache(struct hda_codec *codec)
3794{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003795 int i;
3796
Takashi Iwaic370dd62012-12-13 18:30:04 +01003797 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003798 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003799 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3800 struct hda_cache_head *buffer;
3801 u32 key;
3802
3803 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3804 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003805 if (!key)
3806 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003807 if (!buffer->dirty)
3808 continue;
3809 buffer->dirty = 0;
3810 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003811 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3812 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003813 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003814 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003815 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003816}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003817EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003818
3819/**
3820 * snd_hda_sequence_write_cache - sequence writes with caching
3821 * @codec: the HDA codec
3822 * @seq: VERB array to send
3823 *
3824 * Send the commands sequentially from the given array.
3825 * Thte commands are recorded on cache for power-save and resume.
3826 * The array must be terminated with NID=0.
3827 */
3828void snd_hda_sequence_write_cache(struct hda_codec *codec,
3829 const struct hda_verb *seq)
3830{
3831 for (; seq->nid; seq++)
3832 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3833 seq->param);
3834}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003835EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003836
Takashi Iwaidc870f32013-01-22 15:24:30 +01003837/**
3838 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3839 * @codec: HD-audio codec
3840 */
3841void snd_hda_codec_flush_cache(struct hda_codec *codec)
3842{
3843 snd_hda_codec_resume_amp(codec);
3844 snd_hda_codec_resume_cache(codec);
3845}
3846EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3847
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003848void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003849 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003850{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003851 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003852 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003853
Takashi Iwaicb53c622007-08-10 17:21:45 +02003854 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003855 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003856 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003857 if (!(wcaps & AC_WCAP_POWER))
3858 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003859 if (codec->power_filter) {
3860 state = codec->power_filter(codec, nid, power_state);
3861 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003862 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003863 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003864 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003865 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003866 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003867}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003868EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3869
3870/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003871 * supported power states check
3872 */
3873static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3874 unsigned int power_state)
3875{
3876 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3877
Mengdong Line037cb42012-08-10 14:11:58 +02003878 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003879 return false;
3880 if (sup & power_state)
3881 return true;
3882 else
3883 return false;
3884}
3885
3886/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003887 * wait until the state is reached, returns the current state
3888 */
3889static unsigned int hda_sync_power_state(struct hda_codec *codec,
3890 hda_nid_t fg,
3891 unsigned int power_state)
3892{
3893 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3894 unsigned int state, actual_state;
3895
3896 for (;;) {
3897 state = snd_hda_codec_read(codec, fg, 0,
3898 AC_VERB_GET_POWER_STATE, 0);
3899 if (state & AC_PWRST_ERROR)
3900 break;
3901 actual_state = (state >> 4) & 0x0f;
3902 if (actual_state == power_state)
3903 break;
3904 if (time_after_eq(jiffies, end_time))
3905 break;
3906 /* wait until the codec reachs to the target state */
3907 msleep(1);
3908 }
3909 return state;
3910}
3911
Takashi Iwai9419ab62013-01-24 17:23:35 +01003912/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003913unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3914 hda_nid_t nid,
3915 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003916{
3917 if (power_state == AC_PWRST_D3 &&
3918 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3919 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3920 int eapd = snd_hda_codec_read(codec, nid, 0,
3921 AC_VERB_GET_EAPD_BTLENABLE, 0);
3922 if (eapd & 0x02)
3923 return AC_PWRST_D0;
3924 }
3925 return power_state;
3926}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003927EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003928
Takashi Iwai432c6412012-08-28 09:59:20 -07003929/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003930 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003931 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003932static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003933 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003934{
Takashi Iwaid8193872012-08-31 07:54:38 -07003935 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003936 int count;
3937 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003938 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003939
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003940 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003941 if (power_state == AC_PWRST_D3) {
3942 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003943 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003944 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003945 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003946
3947 /* repeat power states setting at most 10 times*/
3948 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003949 if (codec->patch_ops.set_power_state)
3950 codec->patch_ops.set_power_state(codec, fg,
3951 power_state);
3952 else {
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003953 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003954 AC_VERB_SET_POWER_STATE,
3955 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003956 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003957 }
3958 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003959 if (!(state & AC_PWRST_ERROR))
3960 break;
3961 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003962
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003963 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003964}
Takashi Iwai54d17402005-11-21 16:33:22 +01003965
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003966/* sync power states of all widgets;
3967 * this is called at the end of codec parsing
3968 */
3969static void sync_power_up_states(struct hda_codec *codec)
3970{
3971 hda_nid_t nid = codec->start_nid;
3972 int i;
3973
Takashi Iwaiba615b82013-03-13 14:47:21 +01003974 /* don't care if no filter is used */
3975 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003976 return;
3977
3978 for (i = 0; i < codec->num_nodes; i++, nid++) {
3979 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003980 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003981 if (!(wcaps & AC_WCAP_POWER))
3982 continue;
3983 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3984 if (target == AC_PWRST_D0)
3985 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003986 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003987 snd_hda_codec_write(codec, nid, 0,
3988 AC_VERB_SET_POWER_STATE, target);
3989 }
3990}
3991
Takashi Iwai11aeff02008-07-30 15:01:46 +02003992#ifdef CONFIG_SND_HDA_HWDEP
3993/* execute additional init verbs */
3994static void hda_exec_init_verbs(struct hda_codec *codec)
3995{
3996 if (codec->init_verbs.list)
3997 snd_hda_sequence_write(codec, codec->init_verbs.list);
3998}
3999#else
4000static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4001#endif
4002
Takashi Iwai2a439522011-07-26 09:52:50 +02004003#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004004/*
4005 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004006 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004007 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004008static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004009{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004010 unsigned int state;
4011
Takashi Iwai989c3182012-11-19 14:14:58 +01004012 codec->in_pm = 1;
4013
Takashi Iwaicb53c622007-08-10 17:21:45 +02004014 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004015 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004016 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004017 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004018 /* Cancel delayed work if we aren't currently running from it. */
4019 if (!in_wq)
4020 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004021 spin_lock(&codec->power_lock);
4022 snd_hda_update_power_acct(codec);
4023 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004024 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004025 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004026 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004027 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004028 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004029 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004030}
4031
Takashi Iwaic370dd62012-12-13 18:30:04 +01004032/* mark all entries of cmd and amp caches dirty */
4033static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4034{
4035 int i;
4036 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4037 struct hda_cache_head *cmd;
4038 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4039 cmd->dirty = 1;
4040 }
4041 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4042 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004043 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004044 amp->head.dirty = 1;
4045 }
4046}
4047
Takashi Iwaicb53c622007-08-10 17:21:45 +02004048/*
4049 * kick up codec; used both from PM and power-save
4050 */
4051static void hda_call_codec_resume(struct hda_codec *codec)
4052{
Takashi Iwai989c3182012-11-19 14:14:58 +01004053 codec->in_pm = 1;
4054
Takashi Iwaic370dd62012-12-13 18:30:04 +01004055 hda_mark_cmd_cache_dirty(codec);
4056
Takashi Iwai7f308302012-05-08 16:52:23 +02004057 /* set as if powered on for avoiding re-entering the resume
4058 * in the resume / power-save sequence
4059 */
4060 hda_keep_power_on(codec);
Takashi Iwai0fc28fc2013-11-20 12:15:07 +01004061 if (codec->pm_down_notified) {
4062 codec->pm_down_notified = 0;
4063 hda_call_pm_notify(codec->bus, true);
4064 }
Takashi Iwaid8193872012-08-31 07:54:38 -07004065 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004066 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004067 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004068 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004069 if (codec->patch_ops.resume)
4070 codec->patch_ops.resume(codec);
4071 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004072 if (codec->patch_ops.init)
4073 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004074 snd_hda_codec_resume_amp(codec);
4075 snd_hda_codec_resume_cache(codec);
4076 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004077
4078 if (codec->jackpoll_interval)
4079 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004080 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004081 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004082
4083 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004084 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004085}
Takashi Iwai2a439522011-07-26 09:52:50 +02004086#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087
Takashi Iwai54d17402005-11-21 16:33:22 +01004088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089/**
4090 * snd_hda_build_controls - build mixer controls
4091 * @bus: the BUS
4092 *
4093 * Creates mixer controls for each codec included in the bus.
4094 *
4095 * Returns 0 if successful, otherwise a negative error code.
4096 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004097int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004099 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Takashi Iwai0ba21762007-04-16 11:29:14 +02004101 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004102 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004103 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004104 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004105 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004106 err = snd_hda_codec_reset(codec);
4107 if (err < 0) {
4108 printk(KERN_ERR
4109 "hda_codec: cannot revert codec\n");
4110 return err;
4111 }
4112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004114 return 0;
4115}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004116EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004118/*
4119 * add standard channel maps if not specified
4120 */
4121static int add_std_chmaps(struct hda_codec *codec)
4122{
4123 int i, str, err;
4124
4125 for (i = 0; i < codec->num_pcms; i++) {
4126 for (str = 0; str < 2; str++) {
4127 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4128 struct hda_pcm_stream *hinfo =
4129 &codec->pcm_info[i].stream[str];
4130 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004131 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004132
4133 if (codec->pcm_info[i].own_chmap)
4134 continue;
4135 if (!pcm || !hinfo->substreams)
4136 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004137 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4138 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004139 hinfo->channels_max,
4140 0, &chmap);
4141 if (err < 0)
4142 return err;
4143 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4144 }
4145 }
4146 return 0;
4147}
4148
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004149/* default channel maps for 2.1 speakers;
4150 * since HD-audio supports only stereo, odd number channels are omitted
4151 */
4152const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4153 { .channels = 2,
4154 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4155 { .channels = 4,
4156 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4157 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4158 { }
4159};
4160EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4161
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004162int snd_hda_codec_build_controls(struct hda_codec *codec)
4163{
4164 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004165 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004166 /* continue to initialize... */
4167 if (codec->patch_ops.init)
4168 err = codec->patch_ops.init(codec);
4169 if (!err && codec->patch_ops.build_controls)
4170 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004171 if (err < 0)
4172 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004173
4174 /* we create chmaps here instead of build_pcms */
4175 err = add_std_chmaps(codec);
4176 if (err < 0)
4177 return err;
4178
David Henningsson26a6cb62012-10-09 15:04:21 +02004179 if (codec->jackpoll_interval)
4180 hda_jackpoll_work(&codec->jackpoll_work.work);
4181 else
4182 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004183 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return 0;
4185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/*
4188 * stream formats
4189 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004190struct hda_rate_tbl {
4191 unsigned int hz;
4192 unsigned int alsa_bits;
4193 unsigned int hda_fmt;
4194};
4195
Takashi Iwai92f10b32010-08-03 14:21:00 +02004196/* rate = base * mult / div */
4197#define HDA_RATE(base, mult, div) \
4198 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4199 (((div) - 1) << AC_FMT_DIV_SHIFT))
4200
Takashi Iwaibefdf312005-08-22 13:57:55 +02004201static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004203
4204 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004205 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4206 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4207 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4208 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4209 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4210 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4211 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4212 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4213 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4214 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4215 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004216#define AC_PAR_PCM_RATE_BITS 11
4217 /* up to bits 10, 384kHZ isn't supported properly */
4218
4219 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004220 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004221
Takashi Iwaibefdf312005-08-22 13:57:55 +02004222 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223};
4224
4225/**
4226 * snd_hda_calc_stream_format - calculate format bitset
4227 * @rate: the sample rate
4228 * @channels: the number of channels
4229 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4230 * @maxbps: the max. bps
4231 *
4232 * Calculate the format bitset from the given rate, channels and th PCM format.
4233 *
4234 * Return zero if invalid.
4235 */
4236unsigned int snd_hda_calc_stream_format(unsigned int rate,
4237 unsigned int channels,
4238 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004239 unsigned int maxbps,
4240 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
4242 int i;
4243 unsigned int val = 0;
4244
Takashi Iwaibefdf312005-08-22 13:57:55 +02004245 for (i = 0; rate_bits[i].hz; i++)
4246 if (rate_bits[i].hz == rate) {
4247 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 break;
4249 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004250 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 snd_printdd("invalid rate %d\n", rate);
4252 return 0;
4253 }
4254
4255 if (channels == 0 || channels > 8) {
4256 snd_printdd("invalid channels %d\n", channels);
4257 return 0;
4258 }
4259 val |= channels - 1;
4260
4261 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004262 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004263 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004264 break;
4265 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004266 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 case 20:
4269 case 24:
4270 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004271 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004272 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004274 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004276 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 break;
4278 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004279 snd_printdd("invalid format width %d\n",
4280 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return 0;
4282 }
4283
Anssi Hannula32c168c2010-08-03 13:28:57 +03004284 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004285 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return val;
4288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004289EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004291static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4292 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004293{
4294 unsigned int val = 0;
4295 if (nid != codec->afg &&
4296 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4297 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4298 if (!val || val == -1)
4299 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4300 if (!val || val == -1)
4301 return 0;
4302 return val;
4303}
4304
4305static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4306{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004307 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004308 get_pcm_param);
4309}
4310
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004311static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4312 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004313{
4314 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4315 if (!streams || streams == -1)
4316 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4317 if (!streams || streams == -1)
4318 return 0;
4319 return streams;
4320}
4321
4322static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4323{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004324 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004325 get_stream_param);
4326}
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328/**
4329 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4330 * @codec: the HDA codec
4331 * @nid: NID to query
4332 * @ratesp: the pointer to store the detected rate bitflags
4333 * @formatsp: the pointer to store the detected formats
4334 * @bpsp: the pointer to store the detected format widths
4335 *
4336 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4337 * or @bsps argument is ignored.
4338 *
4339 * Returns 0 if successful, otherwise a negative error code.
4340 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004341int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4343{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004344 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004346 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004347 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 if (ratesp) {
4350 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004351 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004353 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004355 if (rates == 0) {
4356 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4357 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4358 nid, val,
4359 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4360 return -EIO;
4361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 *ratesp = rates;
4363 }
4364
4365 if (formatsp || bpsp) {
4366 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004367 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004369 streams = query_stream_param(codec, nid);
4370 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 bps = 0;
4374 if (streams & AC_SUPFMT_PCM) {
4375 if (val & AC_SUPPCM_BITS_8) {
4376 formats |= SNDRV_PCM_FMTBIT_U8;
4377 bps = 8;
4378 }
4379 if (val & AC_SUPPCM_BITS_16) {
4380 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4381 bps = 16;
4382 }
4383 if (wcaps & AC_WCAP_DIGITAL) {
4384 if (val & AC_SUPPCM_BITS_32)
4385 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4386 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4387 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4388 if (val & AC_SUPPCM_BITS_24)
4389 bps = 24;
4390 else if (val & AC_SUPPCM_BITS_20)
4391 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004392 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4393 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4395 if (val & AC_SUPPCM_BITS_32)
4396 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 else if (val & AC_SUPPCM_BITS_24)
4398 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004399 else if (val & AC_SUPPCM_BITS_20)
4400 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004403#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004404 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004406 if (!bps)
4407 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004408 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004409#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004410 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004411 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 /* temporary hack: we have still no proper support
4413 * for the direct AC3 stream...
4414 */
4415 formats |= SNDRV_PCM_FMTBIT_U8;
4416 bps = 8;
4417 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004418 if (formats == 0) {
4419 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4420 "(nid=0x%x, val=0x%x, ovrd=%i, "
4421 "streams=0x%x)\n",
4422 nid, val,
4423 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4424 streams);
4425 return -EIO;
4426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (formatsp)
4428 *formatsp = formats;
4429 if (bpsp)
4430 *bpsp = bps;
4431 }
4432
4433 return 0;
4434}
Stephen Warren384a48d2011-06-01 11:14:21 -06004435EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004438 * snd_hda_is_supported_format - Check the validity of the format
4439 * @codec: HD-audio codec
4440 * @nid: NID to check
4441 * @format: the HD-audio format value to check
4442 *
4443 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 *
4445 * Returns 1 if supported, 0 if not.
4446 */
4447int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4448 unsigned int format)
4449{
4450 int i;
4451 unsigned int val = 0, rate, stream;
4452
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004453 val = query_pcm_param(codec, nid);
4454 if (!val)
4455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004458 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004459 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (val & (1 << i))
4461 break;
4462 return 0;
4463 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004464 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return 0;
4466
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004467 stream = query_stream_param(codec, nid);
4468 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return 0;
4470
4471 if (stream & AC_SUPFMT_PCM) {
4472 switch (format & 0xf0) {
4473 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004474 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return 0;
4476 break;
4477 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004478 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return 0;
4480 break;
4481 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004482 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return 0;
4484 break;
4485 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004486 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return 0;
4488 break;
4489 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004490 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return 0;
4492 break;
4493 default:
4494 return 0;
4495 }
4496 } else {
4497 /* FIXME: check for float32 and AC3? */
4498 }
4499
4500 return 1;
4501}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004502EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504/*
4505 * PCM stuff
4506 */
4507static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4508 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004509 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 return 0;
4512}
4513
4514static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4515 struct hda_codec *codec,
4516 unsigned int stream_tag,
4517 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004518 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4521 return 0;
4522}
4523
4524static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4525 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004526 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Takashi Iwai888afa12008-03-18 09:57:50 +01004528 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return 0;
4530}
4531
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004532static int set_pcm_default_values(struct hda_codec *codec,
4533 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004535 int err;
4536
Takashi Iwai0ba21762007-04-16 11:29:14 +02004537 /* query support PCM information from the given NID */
4538 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004539 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004540 info->rates ? NULL : &info->rates,
4541 info->formats ? NULL : &info->formats,
4542 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004543 if (err < 0)
4544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 }
4546 if (info->ops.open == NULL)
4547 info->ops.open = hda_pcm_default_open_close;
4548 if (info->ops.close == NULL)
4549 info->ops.close = hda_pcm_default_open_close;
4550 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004551 if (snd_BUG_ON(!info->nid))
4552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 info->ops.prepare = hda_pcm_default_prepare;
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004556 if (snd_BUG_ON(!info->nid))
4557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 info->ops.cleanup = hda_pcm_default_cleanup;
4559 }
4560 return 0;
4561}
4562
Takashi Iwaieb541332010-08-06 13:48:11 +02004563/*
4564 * codec prepare/cleanup entries
4565 */
4566int snd_hda_codec_prepare(struct hda_codec *codec,
4567 struct hda_pcm_stream *hinfo,
4568 unsigned int stream,
4569 unsigned int format,
4570 struct snd_pcm_substream *substream)
4571{
4572 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004573 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004574 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4575 if (ret >= 0)
4576 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004577 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004578 return ret;
4579}
4580EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4581
4582void snd_hda_codec_cleanup(struct hda_codec *codec,
4583 struct hda_pcm_stream *hinfo,
4584 struct snd_pcm_substream *substream)
4585{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004586 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004587 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004588 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004589}
4590EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4591
Takashi Iwaid5191e52009-11-16 14:58:17 +01004592/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004593const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4594 "Audio", "SPDIF", "HDMI", "Modem"
4595};
4596
Takashi Iwai176d5332008-07-30 15:01:44 +02004597/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004598 * get the empty PCM device number to assign
4599 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004600static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004601{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004602 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004603 /* assigned to static slots up to dev#10; if more needed, assign
4604 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4605 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004606 static int audio_idx[HDA_PCM_NTYPES][5] = {
4607 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4608 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004609 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004610 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004611 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004612 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004613
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004614 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004615 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4616 return -EINVAL;
4617 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004618
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004619 for (i = 0; audio_idx[type][i] >= 0; i++) {
4620#ifndef CONFIG_SND_DYNAMIC_MINORS
4621 if (audio_idx[type][i] >= 8)
4622 break;
4623#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004624 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4625 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004626 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004627
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004628#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004629 /* non-fixed slots starting from 10 */
4630 for (i = 10; i < 32; i++) {
4631 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4632 return i;
4633 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004634#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004635
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004636 snd_printk(KERN_WARNING "Too many %s devices\n",
4637 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004638#ifndef CONFIG_SND_DYNAMIC_MINORS
4639 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4640#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004641 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004642}
4643
4644/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004645 * attach a new PCM stream
4646 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004647static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004648{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004649 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004650 struct hda_pcm_stream *info;
4651 int stream, err;
4652
Takashi Iwaib91f0802008-11-04 08:43:08 +01004653 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004654 return -EINVAL;
4655 for (stream = 0; stream < 2; stream++) {
4656 info = &pcm->stream[stream];
4657 if (info->substreams) {
4658 err = set_pcm_default_values(codec, info);
4659 if (err < 0)
4660 return err;
4661 }
4662 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004663 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004664}
4665
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004666/* assign all PCMs of the given codec */
4667int snd_hda_codec_build_pcms(struct hda_codec *codec)
4668{
4669 unsigned int pcm;
4670 int err;
4671
4672 if (!codec->num_pcms) {
4673 if (!codec->patch_ops.build_pcms)
4674 return 0;
4675 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004676 if (err < 0) {
4677 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004678 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004679 err = snd_hda_codec_reset(codec);
4680 if (err < 0) {
4681 printk(KERN_ERR
4682 "hda_codec: cannot revert codec\n");
4683 return err;
4684 }
4685 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004686 }
4687 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4688 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4689 int dev;
4690
4691 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004692 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004693
4694 if (!cpcm->pcm) {
4695 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4696 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004697 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004698 cpcm->device = dev;
4699 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004700 if (err < 0) {
4701 printk(KERN_ERR "hda_codec: cannot attach "
4702 "PCM stream %d for codec #%d\n",
4703 dev, codec->addr);
4704 continue; /* no fatal error */
4705 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004706 }
4707 }
4708 return 0;
4709}
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711/**
4712 * snd_hda_build_pcms - build PCM information
4713 * @bus: the BUS
4714 *
4715 * Create PCM information for each codec included in the bus.
4716 *
4717 * The build_pcms codec patch is requested to set up codec->num_pcms and
4718 * codec->pcm_info properly. The array is referred by the top-level driver
4719 * to create its PCM instances.
4720 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4721 * callback.
4722 *
4723 * At least, substreams, channels_min and channels_max must be filled for
4724 * each stream. substreams = 0 indicates that the stream doesn't exist.
4725 * When rates and/or formats are zero, the supported values are queried
4726 * from the given nid. The nid is used also by the default ops.prepare
4727 * and ops.cleanup callbacks.
4728 *
4729 * The driver needs to call ops.open in its open callback. Similarly,
4730 * ops.close is supposed to be called in the close callback.
4731 * ops.prepare should be called in the prepare or hw_params callback
4732 * with the proper parameters for set up.
4733 * ops.cleanup should be called in hw_free for clean up of streams.
4734 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004735 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004737int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004739 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Takashi Iwai0ba21762007-04-16 11:29:14 +02004741 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004742 int err = snd_hda_codec_build_pcms(codec);
4743 if (err < 0)
4744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 }
4746 return 0;
4747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004748EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750/**
4751 * snd_hda_check_board_config - compare the current codec with the config table
4752 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004753 * @num_configs: number of config enums
4754 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 * @tbl: configuration table, terminated by null entries
4756 *
4757 * Compares the modelname or PCI subsystem id of the current codec with the
4758 * given configuration table. If a matching entry is found, returns its
4759 * config value (supposed to be 0 or positive).
4760 *
4761 * If no entries are matching, the function returns a negative value.
4762 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004763int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004764 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004765 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004767 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004768 int i;
4769 for (i = 0; i < num_configs; i++) {
4770 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004771 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004772 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4773 "selected\n", models[i]);
4774 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
4776 }
4777 }
4778
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004779 if (!codec->bus->pci || !tbl)
4780 return -1;
4781
4782 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4783 if (!tbl)
4784 return -1;
4785 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004786#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004787 char tmp[10];
4788 const char *model = NULL;
4789 if (models)
4790 model = models[tbl->value];
4791 if (!model) {
4792 sprintf(tmp, "#%d", tbl->value);
4793 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004795 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4796 "for config %x:%x (%s)\n",
4797 model, tbl->subvendor, tbl->subdevice,
4798 (tbl->name ? tbl->name : "Unknown device"));
4799#endif
4800 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 }
4802 return -1;
4803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004804EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004807 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004808 subsystem ID with the
4809 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004810
4811 This is important for Gateway notebooks with SB450 HDA Audio
4812 where the vendor ID of the PCI device is:
4813 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4814 and the vendor/subvendor are found only at the codec.
4815
4816 * @codec: the HDA codec
4817 * @num_configs: number of config enums
4818 * @models: array of model name strings
4819 * @tbl: configuration table, terminated by null entries
4820 *
4821 * Compares the modelname or PCI subsystem id of the current codec with the
4822 * given configuration table. If a matching entry is found, returns its
4823 * config value (supposed to be 0 or positive).
4824 *
4825 * If no entries are matching, the function returns a negative value.
4826 */
4827int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004828 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004829 const struct snd_pci_quirk *tbl)
4830{
4831 const struct snd_pci_quirk *q;
4832
4833 /* Search for codec ID */
4834 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004835 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4836 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4837 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004838 break;
4839 }
4840
4841 if (!q->subvendor)
4842 return -1;
4843
4844 tbl = q;
4845
4846 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004847#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004848 char tmp[10];
4849 const char *model = NULL;
4850 if (models)
4851 model = models[tbl->value];
4852 if (!model) {
4853 sprintf(tmp, "#%d", tbl->value);
4854 model = tmp;
4855 }
4856 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4857 "for config %x:%x (%s)\n",
4858 model, tbl->subvendor, tbl->subdevice,
4859 (tbl->name ? tbl->name : "Unknown device"));
4860#endif
4861 return tbl->value;
4862 }
4863 return -1;
4864}
4865EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4866
4867/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 * snd_hda_add_new_ctls - create controls from the array
4869 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004870 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 *
4872 * This helper function creates and add new controls in the given array.
4873 * The array must be terminated with an empty entry as terminator.
4874 *
4875 * Returns 0 if successful, or a negative error code.
4876 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004877int snd_hda_add_new_ctls(struct hda_codec *codec,
4878 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004880 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004883 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004884 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004885 if (knew->iface == -1) /* skip this codec private value */
4886 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004887 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004888 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004889 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004890 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004891 if (addr > 0)
4892 kctl->id.device = addr;
4893 if (idx > 0)
4894 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004895 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004896 if (!err)
4897 break;
4898 /* try first with another device index corresponding to
4899 * the codec addr; if it still fails (or it's the
4900 * primary codec), then try another control index
4901 */
4902 if (!addr && codec->addr)
4903 addr = codec->addr;
4904 else if (!idx && !knew->index) {
4905 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004906 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004907 if (idx <= 0)
4908 return err;
4909 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004910 return err;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913 return 0;
4914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004915EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Takashi Iwai83012a72012-08-24 18:38:08 +02004917#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004918static void hda_power_work(struct work_struct *work)
4919{
4920 struct hda_codec *codec =
4921 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004922 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004923 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004924
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004925 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004926 if (codec->power_transition > 0) { /* during power-up sequence? */
4927 spin_unlock(&codec->power_lock);
4928 return;
4929 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004930 if (!codec->power_on || codec->power_count) {
4931 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004932 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004933 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004934 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004935 spin_unlock(&codec->power_lock);
4936
Dylan Reidd17344b2012-09-28 15:57:01 -07004937 state = hda_call_codec_suspend(codec, true);
Takashi Iwaie6bbe662013-10-24 01:20:24 +02004938 if (!codec->pm_down_notified &&
4939 !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004940 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004941 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004942 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004943}
4944
4945static void hda_keep_power_on(struct hda_codec *codec)
4946{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004947 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004948 codec->power_count++;
4949 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004950 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004951 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004952}
4953
Takashi Iwaid5191e52009-11-16 14:58:17 +01004954/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004955void snd_hda_update_power_acct(struct hda_codec *codec)
4956{
4957 unsigned long delta = jiffies - codec->power_jiffies;
4958 if (codec->power_on)
4959 codec->power_on_acct += delta;
4960 else
4961 codec->power_off_acct += delta;
4962 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004963}
4964
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004965/* Transition to powered up, if wait_power_down then wait for a pending
4966 * transition to D3 to complete. A pending D3 transition is indicated
4967 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004968/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004969static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004970{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004971 struct hda_bus *bus = codec->bus;
4972
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004973 /* Return if power_on or transitioning to power_on, unless currently
4974 * powering down. */
4975 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004976 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004977 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004978 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004979
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004980 cancel_delayed_work_sync(&codec->power_work);
4981
4982 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004983 /* If the power down delayed work was cancelled above before starting,
4984 * then there is no need to go through power up here.
4985 */
4986 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004987 if (codec->power_transition < 0)
4988 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004989 return;
4990 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004991
Takashi Iwaid66fee52011-08-02 15:39:31 +02004992 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004993 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004994 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004995 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004996 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004997 spin_unlock(&codec->power_lock);
4998
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004999 if (codec->pm_down_notified) {
5000 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07005001 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07005002 }
5003
Takashi Iwaicb53c622007-08-10 17:21:45 +02005004 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005005
5006 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005007 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005008}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005009
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005010#define power_save(codec) \
5011 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005012
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005013/* Transition to powered down */
5014static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005015{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005016 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005017 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005018
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005019 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005020 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005021 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005022 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005023 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005024}
5025
5026/**
5027 * snd_hda_power_save - Power-up/down/sync the codec
5028 * @codec: HD-audio codec
5029 * @delta: the counter delta to change
5030 *
5031 * Change the power-up counter via @delta, and power up or down the hardware
5032 * appropriately. For the power-down, queue to the delayed action.
5033 * Passing zero to @delta means to synchronize the power state.
5034 */
5035void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5036{
5037 spin_lock(&codec->power_lock);
5038 codec->power_count += delta;
5039 trace_hda_power_count(codec);
5040 if (delta > 0)
5041 __snd_hda_power_up(codec, d3wait);
5042 else
5043 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005044 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005045}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005046EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005047
Takashi Iwaid5191e52009-11-16 14:58:17 +01005048/**
5049 * snd_hda_check_amp_list_power - Check the amp list and update the power
5050 * @codec: HD-audio codec
5051 * @check: the object containing an AMP list and the status
5052 * @nid: NID to check / update
5053 *
5054 * Check whether the given NID is in the amp list. If it's in the list,
5055 * check the current AMP status, and update the the power-status according
5056 * to the mute status.
5057 *
5058 * This function is supposed to be set or called from the check_power_status
5059 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005060 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005061int snd_hda_check_amp_list_power(struct hda_codec *codec,
5062 struct hda_loopback_check *check,
5063 hda_nid_t nid)
5064{
Takashi Iwai031024e2011-05-02 11:29:30 +02005065 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005066 int ch, v;
5067
5068 if (!check->amplist)
5069 return 0;
5070 for (p = check->amplist; p->nid; p++) {
5071 if (p->nid == nid)
5072 break;
5073 }
5074 if (!p->nid)
5075 return 0; /* nothing changed */
5076
5077 for (p = check->amplist; p->nid; p++) {
5078 for (ch = 0; ch < 2; ch++) {
5079 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5080 p->idx);
5081 if (!(v & HDA_AMP_MUTE) && v > 0) {
5082 if (!check->power_on) {
5083 check->power_on = 1;
5084 snd_hda_power_up(codec);
5085 }
5086 return 1;
5087 }
5088 }
5089 }
5090 if (check->power_on) {
5091 check->power_on = 0;
5092 snd_hda_power_down(codec);
5093 }
5094 return 0;
5095}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005096EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005099/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005100 * Channel mode helper
5101 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005102
5103/**
5104 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5105 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005106int snd_hda_ch_mode_info(struct hda_codec *codec,
5107 struct snd_ctl_elem_info *uinfo,
5108 const struct hda_channel_mode *chmode,
5109 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005110{
5111 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5112 uinfo->count = 1;
5113 uinfo->value.enumerated.items = num_chmodes;
5114 if (uinfo->value.enumerated.item >= num_chmodes)
5115 uinfo->value.enumerated.item = num_chmodes - 1;
5116 sprintf(uinfo->value.enumerated.name, "%dch",
5117 chmode[uinfo->value.enumerated.item].channels);
5118 return 0;
5119}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005120EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005121
Takashi Iwaid5191e52009-11-16 14:58:17 +01005122/**
5123 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5124 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005125int snd_hda_ch_mode_get(struct hda_codec *codec,
5126 struct snd_ctl_elem_value *ucontrol,
5127 const struct hda_channel_mode *chmode,
5128 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005129 int max_channels)
5130{
5131 int i;
5132
5133 for (i = 0; i < num_chmodes; i++) {
5134 if (max_channels == chmode[i].channels) {
5135 ucontrol->value.enumerated.item[0] = i;
5136 break;
5137 }
5138 }
5139 return 0;
5140}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005141EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005142
Takashi Iwaid5191e52009-11-16 14:58:17 +01005143/**
5144 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5145 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005146int snd_hda_ch_mode_put(struct hda_codec *codec,
5147 struct snd_ctl_elem_value *ucontrol,
5148 const struct hda_channel_mode *chmode,
5149 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005150 int *max_channelsp)
5151{
5152 unsigned int mode;
5153
5154 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005155 if (mode >= num_chmodes)
5156 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005157 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005158 return 0;
5159 /* change the current channel setting */
5160 *max_channelsp = chmode[mode].channels;
5161 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005162 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005163 return 1;
5164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005165EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167/*
5168 * input MUX helper
5169 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005170
5171/**
5172 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5173 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005174int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5175 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
5177 unsigned int index;
5178
5179 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5180 uinfo->count = 1;
5181 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005182 if (!imux->num_items)
5183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 index = uinfo->value.enumerated.item;
5185 if (index >= imux->num_items)
5186 index = imux->num_items - 1;
5187 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5188 return 0;
5189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005190EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Takashi Iwaid5191e52009-11-16 14:58:17 +01005192/**
5193 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5194 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005195int snd_hda_input_mux_put(struct hda_codec *codec,
5196 const struct hda_input_mux *imux,
5197 struct snd_ctl_elem_value *ucontrol,
5198 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 unsigned int *cur_val)
5200{
5201 unsigned int idx;
5202
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005203 if (!imux->num_items)
5204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 idx = ucontrol->value.enumerated.item[0];
5206 if (idx >= imux->num_items)
5207 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005208 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005210 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5211 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 *cur_val = idx;
5213 return 1;
5214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005215EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
5217
5218/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005219 * process kcontrol info callback of a simple string enum array
5220 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5221 */
5222int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5223 struct snd_ctl_elem_info *uinfo,
5224 int num_items, const char * const *texts)
5225{
5226 static const char * const texts_default[] = {
5227 "Disabled", "Enabled"
5228 };
5229
5230 if (!texts || !num_items) {
5231 num_items = 2;
5232 texts = texts_default;
5233 }
5234
5235 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5236 uinfo->count = 1;
5237 uinfo->value.enumerated.items = num_items;
5238 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5239 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5240 strcpy(uinfo->value.enumerated.name,
5241 texts[uinfo->value.enumerated.item]);
5242 return 0;
5243}
5244EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5245
5246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 * Multi-channel / digital-out PCM helper functions
5248 */
5249
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005250/* setup SPDIF output stream */
5251static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5252 unsigned int stream_tag, unsigned int format)
5253{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005254 struct hda_spdif_out *spdif;
5255 unsigned int curr_fmt;
5256 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005257
Laurence Darby3bef1c32012-11-03 17:00:06 +00005258 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5259 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5260 AC_VERB_GET_STREAM_FORMAT, 0);
5261 reset = codec->spdif_status_reset &&
5262 (spdif->ctls & AC_DIG1_ENABLE) &&
5263 curr_fmt != format;
5264
5265 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5266 updated */
5267 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005268 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005269 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005270 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005271 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005272 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005273 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005274 for (d = codec->slave_dig_outs; *d; d++)
5275 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5276 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005277 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005278 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005279 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005280 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005281 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005282}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005283
Takashi Iwai2f728532008-09-25 16:32:41 +02005284static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5285{
5286 snd_hda_codec_cleanup_stream(codec, nid);
5287 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005288 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005289 for (d = codec->slave_dig_outs; *d; d++)
5290 snd_hda_codec_cleanup_stream(codec, *d);
5291 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005292}
5293
Takashi Iwaid5191e52009-11-16 14:58:17 +01005294/**
5295 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5296 * @bus: HD-audio bus
5297 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005298void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5299{
5300 struct hda_codec *codec;
5301
5302 if (!bus)
5303 return;
5304 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005305 if (hda_codec_is_power_on(codec) &&
5306 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005307 codec->patch_ops.reboot_notify(codec);
5308 }
5309}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005310EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005311
Takashi Iwaid5191e52009-11-16 14:58:17 +01005312/**
5313 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005315int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5316 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
Ingo Molnar62932df2006-01-16 16:34:20 +01005318 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005319 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5320 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005321 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005323 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 return 0;
5325}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005326EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Takashi Iwaid5191e52009-11-16 14:58:17 +01005328/**
5329 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5330 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005331int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5332 struct hda_multi_out *mout,
5333 unsigned int stream_tag,
5334 unsigned int format,
5335 struct snd_pcm_substream *substream)
5336{
5337 mutex_lock(&codec->spdif_mutex);
5338 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5339 mutex_unlock(&codec->spdif_mutex);
5340 return 0;
5341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005342EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005343
Takashi Iwaid5191e52009-11-16 14:58:17 +01005344/**
5345 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5346 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005347int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5348 struct hda_multi_out *mout)
5349{
5350 mutex_lock(&codec->spdif_mutex);
5351 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5352 mutex_unlock(&codec->spdif_mutex);
5353 return 0;
5354}
5355EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5356
Takashi Iwaid5191e52009-11-16 14:58:17 +01005357/**
5358 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005360int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5361 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Ingo Molnar62932df2006-01-16 16:34:20 +01005363 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005365 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return 0;
5367}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005368EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Takashi Iwaid5191e52009-11-16 14:58:17 +01005370/**
5371 * snd_hda_multi_out_analog_open - open analog outputs
5372 *
5373 * Open analog outputs and set up the hw-constraints.
5374 * If the digital outputs can be opened as slave, open the digital
5375 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005377int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5378 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005379 struct snd_pcm_substream *substream,
5380 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
Takashi Iwai9a081602008-02-12 18:37:26 +01005382 struct snd_pcm_runtime *runtime = substream->runtime;
5383 runtime->hw.channels_max = mout->max_channels;
5384 if (mout->dig_out_nid) {
5385 if (!mout->analog_rates) {
5386 mout->analog_rates = hinfo->rates;
5387 mout->analog_formats = hinfo->formats;
5388 mout->analog_maxbps = hinfo->maxbps;
5389 } else {
5390 runtime->hw.rates = mout->analog_rates;
5391 runtime->hw.formats = mout->analog_formats;
5392 hinfo->maxbps = mout->analog_maxbps;
5393 }
5394 if (!mout->spdif_rates) {
5395 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5396 &mout->spdif_rates,
5397 &mout->spdif_formats,
5398 &mout->spdif_maxbps);
5399 }
5400 mutex_lock(&codec->spdif_mutex);
5401 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005402 if ((runtime->hw.rates & mout->spdif_rates) &&
5403 (runtime->hw.formats & mout->spdif_formats)) {
5404 runtime->hw.rates &= mout->spdif_rates;
5405 runtime->hw.formats &= mout->spdif_formats;
5406 if (mout->spdif_maxbps < hinfo->maxbps)
5407 hinfo->maxbps = mout->spdif_maxbps;
5408 } else {
5409 mout->share_spdif = 0;
5410 /* FIXME: need notify? */
5411 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005412 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005413 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5416 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005418EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Takashi Iwaid5191e52009-11-16 14:58:17 +01005420/**
5421 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5422 *
5423 * Set up the i/o for analog out.
5424 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005426int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5427 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 unsigned int stream_tag,
5429 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005430 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
Takashi Iwaidda14412011-05-02 11:29:30 +02005432 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005434 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 int i;
5436
Ingo Molnar62932df2006-01-16 16:34:20 +01005437 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005438 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005439 if (mout->dig_out_nid && mout->share_spdif &&
5440 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005442 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5443 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005444 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005446 setup_dig_out_stream(codec, mout->dig_out_nid,
5447 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 } else {
5449 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005450 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005453 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005456 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5457 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005458 if (!mout->no_share_stream &&
5459 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005461 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5462 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005463 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005464 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5465 if (!mout->no_share_stream && mout->hp_out_nid[i])
5466 snd_hda_codec_setup_stream(codec,
5467 mout->hp_out_nid[i],
5468 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005469
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 /* surrounds */
5471 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005472 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005473 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5474 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005475 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005476 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5477 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005479
5480 /* extra surrounds */
5481 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5482 int ch = 0;
5483 if (!mout->extra_out_nid[i])
5484 break;
5485 if (chs >= (i + 1) * 2)
5486 ch = i * 2;
5487 else if (!mout->no_share_stream)
5488 break;
5489 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5490 stream_tag, ch, format);
5491 }
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 return 0;
5494}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005495EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Takashi Iwaid5191e52009-11-16 14:58:17 +01005497/**
5498 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005500int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5501 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Takashi Iwaidda14412011-05-02 11:29:30 +02005503 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 int i;
5505
5506 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005507 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005509 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005510 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5511 if (mout->hp_out_nid[i])
5512 snd_hda_codec_cleanup_stream(codec,
5513 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005514 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5515 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005516 snd_hda_codec_cleanup_stream(codec,
5517 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005518 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005520 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 mout->dig_out_used = 0;
5522 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005523 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return 0;
5525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005526EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Takashi Iwai47408602012-04-20 13:06:53 +02005528/**
5529 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5530 *
5531 * Guess the suitable VREF pin bits to be set as the pin-control value.
5532 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5533 */
5534unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5535{
5536 unsigned int pincap;
5537 unsigned int oldval;
5538 oldval = snd_hda_codec_read(codec, pin, 0,
5539 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5540 pincap = snd_hda_query_pin_caps(codec, pin);
5541 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5542 /* Exception: if the default pin setup is vref50, we give it priority */
5543 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5544 return AC_PINCTL_VREF_80;
5545 else if (pincap & AC_PINCAP_VREF_50)
5546 return AC_PINCTL_VREF_50;
5547 else if (pincap & AC_PINCAP_VREF_100)
5548 return AC_PINCTL_VREF_100;
5549 else if (pincap & AC_PINCAP_VREF_GRD)
5550 return AC_PINCTL_VREF_GRD;
5551 return AC_PINCTL_VREF_HIZ;
5552}
5553EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5554
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005555/* correct the pin ctl value for matching with the pin cap */
5556unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5557 hda_nid_t pin, unsigned int val)
5558{
5559 static unsigned int cap_lists[][2] = {
5560 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5561 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5562 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5563 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5564 };
5565 unsigned int cap;
5566
5567 if (!val)
5568 return 0;
5569 cap = snd_hda_query_pin_caps(codec, pin);
5570 if (!cap)
5571 return val; /* don't know what to do... */
5572
5573 if (val & AC_PINCTL_OUT_EN) {
5574 if (!(cap & AC_PINCAP_OUT))
5575 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5576 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5577 val &= ~AC_PINCTL_HP_EN;
5578 }
5579
5580 if (val & AC_PINCTL_IN_EN) {
5581 if (!(cap & AC_PINCAP_IN))
5582 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5583 else {
5584 unsigned int vcap, vref;
5585 int i;
5586 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5587 vref = val & AC_PINCTL_VREFEN;
5588 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5589 if (vref == cap_lists[i][0] &&
5590 !(vcap & cap_lists[i][1])) {
5591 if (i == ARRAY_SIZE(cap_lists) - 1)
5592 vref = AC_PINCTL_VREF_HIZ;
5593 else
5594 vref = cap_lists[i + 1][0];
5595 }
5596 }
5597 val &= ~AC_PINCTL_VREFEN;
5598 val |= vref;
5599 }
5600 }
5601
5602 return val;
5603}
5604EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5605
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005606int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5607 unsigned int val, bool cached)
5608{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005609 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005610 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005611 if (cached)
5612 return snd_hda_codec_update_cache(codec, pin, 0,
5613 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5614 else
5615 return snd_hda_codec_write(codec, pin, 0,
5616 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5617}
5618EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5619
Takashi Iwai990061c2010-09-09 22:08:44 +02005620/**
5621 * snd_hda_add_imux_item - Add an item to input_mux
5622 *
5623 * When the same label is used already in the existing items, the number
5624 * suffix is appended to the label. This label index number is stored
5625 * to type_idx when non-NULL pointer is given.
5626 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005627int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5628 int index, int *type_idx)
5629{
5630 int i, label_idx = 0;
5631 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5632 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5633 return -EINVAL;
5634 }
5635 for (i = 0; i < imux->num_items; i++) {
5636 if (!strncmp(label, imux->items[i].label, strlen(label)))
5637 label_idx++;
5638 }
5639 if (type_idx)
5640 *type_idx = label_idx;
5641 if (label_idx > 0)
5642 snprintf(imux->items[imux->num_items].label,
5643 sizeof(imux->items[imux->num_items].label),
5644 "%s %d", label, label_idx);
5645 else
5646 strlcpy(imux->items[imux->num_items].label, label,
5647 sizeof(imux->items[imux->num_items].label));
5648 imux->items[imux->num_items].index = index;
5649 imux->num_items++;
5650 return 0;
5651}
5652EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005653
Takashi Iwai4a471b72005-12-07 13:56:29 +01005654
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655#ifdef CONFIG_PM
5656/*
5657 * power management
5658 */
5659
5660/**
5661 * snd_hda_suspend - suspend the codecs
5662 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 *
5664 * Returns 0 if successful.
5665 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005666int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005668 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Takashi Iwai0ba21762007-04-16 11:29:14 +02005670 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005671 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005672 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005673 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 }
5675 return 0;
5676}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005677EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
5679/**
5680 * snd_hda_resume - resume the codecs
5681 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 *
5683 * Returns 0 if successful.
5684 */
5685int snd_hda_resume(struct hda_bus *bus)
5686{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005687 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Takashi Iwai0ba21762007-04-16 11:29:14 +02005689 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005690 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 return 0;
5693}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005694EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005695#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005696
5697/*
5698 * generic arrays
5699 */
5700
Takashi Iwaid5191e52009-11-16 14:58:17 +01005701/**
5702 * snd_array_new - get a new element from the given array
5703 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005704 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005705 * Get a new element from the given array. If it exceeds the
5706 * pre-allocated array size, re-allocate the array.
5707 *
5708 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005709 */
5710void *snd_array_new(struct snd_array *array)
5711{
Takashi Iwai12f17712012-10-10 08:59:14 +02005712 if (snd_BUG_ON(!array->elem_size))
5713 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005714 if (array->used >= array->alloced) {
5715 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005716 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005717 void *nlist;
5718 if (snd_BUG_ON(num >= 4096))
5719 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005720 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005721 if (!nlist)
5722 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005723 array->list = nlist;
5724 array->alloced = num;
5725 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005726 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005727}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005728EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005729
Takashi Iwaid5191e52009-11-16 14:58:17 +01005730/**
5731 * snd_array_free - free the given array elements
5732 * @array: the array object
5733 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005734void snd_array_free(struct snd_array *array)
5735{
5736 kfree(array->list);
5737 array->used = 0;
5738 array->alloced = 0;
5739 array->list = NULL;
5740}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005741EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005742
Takashi Iwaid5191e52009-11-16 14:58:17 +01005743/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005744 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5745 * @pcm: PCM caps bits
5746 * @buf: the string buffer to write
5747 * @buflen: the max buffer length
5748 *
5749 * used by hda_proc.c and hda_eld.c
5750 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005751void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5752{
5753 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5754 int i, j;
5755
5756 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5757 if (pcm & (AC_SUPPCM_BITS_8 << i))
5758 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5759
5760 buf[j] = '\0'; /* necessary when j == 0 */
5761}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005762EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005763
5764MODULE_DESCRIPTION("HDA codec core");
5765MODULE_LICENSE("GPL");