blob: 4d3f46329eb515749c2b1a2a0bd08c1442728e15 [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 Iwaid5191e52009-11-16 14:58:17 +01001560/**
1561 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1562 * @codec: the HDA codec
1563 *
1564 * Start parsing of the given codec tree and (re-)initialize the whole
1565 * patch instance.
1566 *
1567 * Returns 0 if successful or a negative error code.
1568 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001569int snd_hda_codec_configure(struct hda_codec *codec)
1570{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001571 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001572 int err;
1573
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001574 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001575 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001576 err = get_codec_name(codec);
1577 if (err < 0)
1578 return err;
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001581 if (!is_generic_config(codec) && codec->preset)
1582 patch = codec->preset->patch;
1583 if (!patch) {
1584 unload_parser(codec); /* to be sure */
1585#ifdef CONFIG_SND_HDA_GENERIC
1586 if (!patch)
1587 patch = load_parser(codec, snd_hda_parse_generic_codec);
1588#endif
1589 if (!patch) {
1590 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1591 return -ENODEV;
1592 }
Takashi Iwai82467612007-07-27 19:15:54 +02001593 }
1594
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001595 err = patch(codec);
1596 if (err < 0) {
1597 unload_parser(codec);
1598 return err;
1599 }
Takashi Iwai82467612007-07-27 19:15:54 +02001600
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001601 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001602 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001603 if (err < 0)
1604 return err;
1605 }
1606
Takashi Iwaif62faed2009-12-23 09:27:51 +01001607 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001608 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001609 snprintf(codec->bus->card->mixername,
1610 sizeof(codec->bus->card->mixername),
1611 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001614EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Takashi Iwaied360812012-08-08 17:12:52 +02001616/* update the stream-id if changed */
1617static void update_pcm_stream_id(struct hda_codec *codec,
1618 struct hda_cvt_setup *p, hda_nid_t nid,
1619 u32 stream_tag, int channel_id)
1620{
1621 unsigned int oldval, newval;
1622
1623 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1624 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1625 newval = (stream_tag << 4) | channel_id;
1626 if (oldval != newval)
1627 snd_hda_codec_write(codec, nid, 0,
1628 AC_VERB_SET_CHANNEL_STREAMID,
1629 newval);
1630 p->stream_tag = stream_tag;
1631 p->channel_id = channel_id;
1632 }
1633}
1634
1635/* update the format-id if changed */
1636static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1637 hda_nid_t nid, int format)
1638{
1639 unsigned int oldval;
1640
1641 if (p->format_id != format) {
1642 oldval = snd_hda_codec_read(codec, nid, 0,
1643 AC_VERB_GET_STREAM_FORMAT, 0);
1644 if (oldval != format) {
1645 msleep(1);
1646 snd_hda_codec_write(codec, nid, 0,
1647 AC_VERB_SET_STREAM_FORMAT,
1648 format);
1649 }
1650 p->format_id = format;
1651 }
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654/**
1655 * snd_hda_codec_setup_stream - set up the codec for streaming
1656 * @codec: the CODEC to set up
1657 * @nid: the NID to set up
1658 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1659 * @channel_id: channel id to pass, zero based.
1660 * @format: stream format.
1661 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001662void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1663 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int channel_id, int format)
1665{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001666 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001667 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001668 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001669 int i;
1670
Takashi Iwai0ba21762007-04-16 11:29:14 +02001671 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001672 return;
1673
Takashi Iwai0ba21762007-04-16 11:29:14 +02001674 snd_printdd("hda_codec_setup_stream: "
1675 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001677 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001678 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001679 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001680
1681 if (codec->pcm_format_first)
1682 update_pcm_format(codec, p, nid, format);
1683 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1684 if (!codec->pcm_format_first)
1685 update_pcm_format(codec, p, nid, format);
1686
Takashi Iwaieb541332010-08-06 13:48:11 +02001687 p->active = 1;
1688 p->dirty = 0;
1689
1690 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001691 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001692 list_for_each_entry(c, &codec->bus->codec_list, list) {
1693 for (i = 0; i < c->cvt_setups.used; i++) {
1694 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001695 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001696 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001697 p->dirty = 1;
1698 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001701EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Takashi Iwaif0cea792010-08-13 11:56:53 +02001703static void really_cleanup_stream(struct hda_codec *codec,
1704 struct hda_cvt_setup *q);
1705
Takashi Iwaid5191e52009-11-16 14:58:17 +01001706/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001707 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001708 * @codec: the CODEC to clean up
1709 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001710 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001711 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001712void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1713 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001714{
Takashi Iwaieb541332010-08-06 13:48:11 +02001715 struct hda_cvt_setup *p;
1716
Takashi Iwai888afa12008-03-18 09:57:50 +01001717 if (!nid)
1718 return;
1719
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001720 if (codec->no_sticky_stream)
1721 do_now = 1;
1722
Takashi Iwai888afa12008-03-18 09:57:50 +01001723 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001724 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001725 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001726 /* here we just clear the active flag when do_now isn't set;
1727 * actual clean-ups will be done later in
1728 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1729 */
1730 if (do_now)
1731 really_cleanup_stream(codec, p);
1732 else
1733 p->active = 0;
1734 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001735}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001736EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001737
Takashi Iwaieb541332010-08-06 13:48:11 +02001738static void really_cleanup_stream(struct hda_codec *codec,
1739 struct hda_cvt_setup *q)
1740{
1741 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001742 if (q->stream_tag || q->channel_id)
1743 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1744 if (q->format_id)
1745 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1746);
Takashi Iwaieb541332010-08-06 13:48:11 +02001747 memset(q, 0, sizeof(*q));
1748 q->nid = nid;
1749}
1750
1751/* clean up the all conflicting obsolete streams */
1752static void purify_inactive_streams(struct hda_codec *codec)
1753{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001754 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001755 int i;
1756
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001757 list_for_each_entry(c, &codec->bus->codec_list, list) {
1758 for (i = 0; i < c->cvt_setups.used; i++) {
1759 struct hda_cvt_setup *p;
1760 p = snd_array_elem(&c->cvt_setups, i);
1761 if (p->dirty)
1762 really_cleanup_stream(c, p);
1763 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001764 }
1765}
1766
Takashi Iwai2a439522011-07-26 09:52:50 +02001767#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001768/* clean up all streams; called from suspend */
1769static void hda_cleanup_all_streams(struct hda_codec *codec)
1770{
1771 int i;
1772
1773 for (i = 0; i < codec->cvt_setups.used; i++) {
1774 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1775 if (p->stream_tag)
1776 really_cleanup_stream(codec, p);
1777 }
1778}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001779#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781/*
1782 * amp access functions
1783 */
1784
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001785/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001786#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001787#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001788#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1789#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001791#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001794static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001795 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai01751f52007-08-10 16:59:39 +02001797 memset(cache, 0, sizeof(*cache));
1798 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001799 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001800}
1801
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001802static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001803{
Takashi Iwai603c4012008-07-30 15:01:44 +02001804 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001808static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Takashi Iwai01751f52007-08-10 16:59:39 +02001810 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1811 u16 cur = cache->hash[idx];
1812 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001815 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (info->key == key)
1817 return info;
1818 cur = info->next;
1819 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001820 return NULL;
1821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001823/* query the hash. allocate an entry if not found. */
1824static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1825 u32 key)
1826{
1827 struct hda_cache_head *info = get_hash(cache, key);
1828 if (!info) {
1829 u16 idx, cur;
1830 /* add a new hash entry */
1831 info = snd_array_new(&cache->buf);
1832 if (!info)
1833 return NULL;
1834 cur = snd_array_index(&cache->buf, info);
1835 info->key = key;
1836 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001837 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001838 idx = key % (u16)ARRAY_SIZE(cache->hash);
1839 info->next = cache->hash[idx];
1840 cache->hash[idx] = cur;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return info;
1843}
1844
Takashi Iwai01751f52007-08-10 16:59:39 +02001845/* query and allocate an amp hash entry */
1846static inline struct hda_amp_info *
1847get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1848{
1849 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1850}
1851
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001852/* overwrite the value with the key in the caps hash */
1853static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1854{
1855 struct hda_amp_info *info;
1856
1857 mutex_lock(&codec->hash_mutex);
1858 info = get_alloc_amp_hash(codec, key);
1859 if (!info) {
1860 mutex_unlock(&codec->hash_mutex);
1861 return -EINVAL;
1862 }
1863 info->amp_caps = val;
1864 info->head.val |= INFO_AMP_CAPS;
1865 mutex_unlock(&codec->hash_mutex);
1866 return 0;
1867}
1868
1869/* query the value from the caps hash; if not found, fetch the current
1870 * value from the given function and store in the hash
1871 */
1872static unsigned int
1873query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1874 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1875{
1876 struct hda_amp_info *info;
1877 unsigned int val;
1878
1879 mutex_lock(&codec->hash_mutex);
1880 info = get_alloc_amp_hash(codec, key);
1881 if (!info) {
1882 mutex_unlock(&codec->hash_mutex);
1883 return 0;
1884 }
1885 if (!(info->head.val & INFO_AMP_CAPS)) {
1886 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1887 val = func(codec, nid, dir);
1888 write_caps_hash(codec, key, val);
1889 } else {
1890 val = info->amp_caps;
1891 mutex_unlock(&codec->hash_mutex);
1892 }
1893 return val;
1894}
1895
1896static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1897 int direction)
1898{
1899 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1900 nid = codec->afg;
1901 return snd_hda_param_read(codec, nid,
1902 direction == HDA_OUTPUT ?
1903 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1904}
1905
Takashi Iwaid5191e52009-11-16 14:58:17 +01001906/**
1907 * query_amp_caps - query AMP capabilities
1908 * @codec: the HD-auio codec
1909 * @nid: the NID to query
1910 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1911 *
1912 * Query AMP capabilities for the given widget and direction.
1913 * Returns the obtained capability bits.
1914 *
1915 * When cap bits have been already read, this doesn't read again but
1916 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001918u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001920 return query_caps_hash(codec, nid, direction,
1921 HDA_HASH_KEY(nid, direction, 0),
1922 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001924EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Takashi Iwaid5191e52009-11-16 14:58:17 +01001926/**
1927 * snd_hda_override_amp_caps - Override the AMP capabilities
1928 * @codec: the CODEC to clean up
1929 * @nid: the NID to clean up
1930 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1931 * @caps: the capability bits to set
1932 *
1933 * Override the cached AMP caps bits value by the given one.
1934 * This function is useful if the driver needs to adjust the AMP ranges,
1935 * e.g. limit to 0dB, etc.
1936 *
1937 * Returns zero if successful or a negative error code.
1938 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001939int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1940 unsigned int caps)
1941{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001942 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001943}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001944EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001945
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001946static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1947 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001948{
1949 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1950}
1951
Takashi Iwaid5191e52009-11-16 14:58:17 +01001952/**
1953 * snd_hda_query_pin_caps - Query PIN capabilities
1954 * @codec: the HD-auio codec
1955 * @nid: the NID to query
1956 *
1957 * Query PIN capabilities for the given widget.
1958 * Returns the obtained capability bits.
1959 *
1960 * When cap bits have been already read, this doesn't read again but
1961 * returns the cached value.
1962 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001963u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1964{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001965 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001966 read_pin_cap);
1967}
Takashi Iwai1327a322009-03-23 13:07:47 +01001968EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1969
Wu Fengguang864f92b2009-11-18 12:38:02 +08001970/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001971 * snd_hda_override_pin_caps - Override the pin capabilities
1972 * @codec: the CODEC
1973 * @nid: the NID to override
1974 * @caps: the capability bits to set
1975 *
1976 * Override the cached PIN capabilitiy bits value by the given one.
1977 *
1978 * Returns zero if successful or a negative error code.
1979 */
1980int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1981 unsigned int caps)
1982{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001983 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001984}
1985EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1986
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001987/* read or sync the hash value with the current value;
1988 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001990static struct hda_amp_info *
1991update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001992 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001994 struct hda_amp_info *info;
1995 unsigned int parm, val = 0;
1996 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001998 retry:
1999 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2000 if (!info)
2001 return NULL;
2002 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2003 if (!val_read) {
2004 mutex_unlock(&codec->hash_mutex);
2005 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2006 parm |= direction == HDA_OUTPUT ?
2007 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2008 parm |= index;
2009 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002010 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002011 val &= 0xff;
2012 val_read = true;
2013 mutex_lock(&codec->hash_mutex);
2014 goto retry;
2015 }
2016 info->vol[ch] = val;
2017 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002018 } else if (init_only)
2019 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002020 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002024 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002026static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002027 hda_nid_t nid, int ch, int direction, int index,
2028 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 u32 parm;
2031
2032 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2033 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2034 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002035 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2036 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002037 ; /* set the zero value as a fake mute */
2038 else
2039 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2041}
2042
Takashi Iwaid5191e52009-11-16 14:58:17 +01002043/**
2044 * snd_hda_codec_amp_read - Read AMP value
2045 * @codec: HD-audio codec
2046 * @nid: NID to read the AMP value
2047 * @ch: channel (left=0 or right=1)
2048 * @direction: #HDA_INPUT or #HDA_OUTPUT
2049 * @index: the index value (only for input direction)
2050 *
2051 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 */
Takashi Iwai834be882006-03-01 14:16:17 +01002053int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2054 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002056 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002057 unsigned int val = 0;
2058
2059 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002060 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002061 if (info)
2062 val = info->vol[ch];
2063 mutex_unlock(&codec->hash_mutex);
2064 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002066EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Takashi Iwai280e57d2012-12-14 10:32:21 +01002068static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2069 int direction, int idx, int mask, int val,
2070 bool init_only)
2071{
2072 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002073 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002074 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002075
2076 if (snd_BUG_ON(mask & ~0xff))
2077 mask &= 0xff;
2078 val &= mask;
2079
2080 mutex_lock(&codec->hash_mutex);
2081 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2082 if (!info) {
2083 mutex_unlock(&codec->hash_mutex);
2084 return 0;
2085 }
2086 val |= info->vol[ch] & ~mask;
2087 if (info->vol[ch] == val) {
2088 mutex_unlock(&codec->hash_mutex);
2089 return 0;
2090 }
2091 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002092 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002093 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002094 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002095 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002096 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002097 return 1;
2098}
2099
Takashi Iwaid5191e52009-11-16 14:58:17 +01002100/**
2101 * snd_hda_codec_amp_update - update the AMP value
2102 * @codec: HD-audio codec
2103 * @nid: NID to read the AMP value
2104 * @ch: channel (left=0 or right=1)
2105 * @direction: #HDA_INPUT or #HDA_OUTPUT
2106 * @idx: the index value (only for input direction)
2107 * @mask: bit mask to set
2108 * @val: the bits value to set
2109 *
2110 * Update the AMP value with a bit mask.
2111 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002112 */
Takashi Iwai834be882006-03-01 14:16:17 +01002113int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2114 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002116 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002118EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_codec_amp_stereo - update the AMP stereo values
2122 * @codec: HD-audio codec
2123 * @nid: NID to read the AMP value
2124 * @direction: #HDA_INPUT or #HDA_OUTPUT
2125 * @idx: the index value (only for input direction)
2126 * @mask: bit mask to set
2127 * @val: the bits value to set
2128 *
2129 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2130 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002131 */
2132int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2133 int direction, int idx, int mask, int val)
2134{
2135 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002136
2137 if (snd_BUG_ON(mask & ~0xff))
2138 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002139 for (ch = 0; ch < 2; ch++)
2140 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2141 idx, mask, val);
2142 return ret;
2143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002144EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002145
Takashi Iwai280e57d2012-12-14 10:32:21 +01002146/* Works like snd_hda_codec_amp_update() but it writes the value only at
2147 * the first access. If the amp was already initialized / updated beforehand,
2148 * this does nothing.
2149 */
2150int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2151 int dir, int idx, int mask, int val)
2152{
2153 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2154}
2155EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2156
2157int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2158 int dir, int idx, int mask, int val)
2159{
2160 int ch, ret = 0;
2161
2162 if (snd_BUG_ON(mask & ~0xff))
2163 mask &= 0xff;
2164 for (ch = 0; ch < 2; ch++)
2165 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2166 idx, mask, val);
2167 return ret;
2168}
2169EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2170
Takashi Iwaid5191e52009-11-16 14:58:17 +01002171/**
2172 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2173 * @codec: HD-audio codec
2174 *
2175 * Resume the all amp commands from the cache.
2176 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002177void snd_hda_codec_resume_amp(struct hda_codec *codec)
2178{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002179 int i;
2180
Takashi Iwaic370dd62012-12-13 18:30:04 +01002181 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002182 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002183 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2184 struct hda_amp_info *buffer;
2185 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002186 hda_nid_t nid;
2187 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002188 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002189
2190 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002191 if (!buffer->head.dirty)
2192 continue;
2193 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002194 info = *buffer;
2195 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002196 if (!key)
2197 continue;
2198 nid = key & 0xff;
2199 idx = (key >> 16) & 0xff;
2200 dir = (key >> 24) & 0xff;
2201 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002202 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002203 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002204 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002205 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2206 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002207 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002208 }
2209 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002210 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002211}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002212EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002214static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2215 unsigned int ofs)
2216{
2217 u32 caps = query_amp_caps(codec, nid, dir);
2218 /* get num steps */
2219 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2220 if (ofs < caps)
2221 caps -= ofs;
2222 return caps;
2223}
2224
Takashi Iwaid5191e52009-11-16 14:58:17 +01002225/**
2226 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2227 *
2228 * The control element is supposed to have the private_value field
2229 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2230 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002231int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 u16 nid = get_amp_nid(kcontrol);
2236 u8 chs = get_amp_channels(kcontrol);
2237 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002238 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2241 uinfo->count = chs == 3 ? 2 : 1;
2242 uinfo->value.integer.min = 0;
2243 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2244 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002245 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002246 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2247 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return -EINVAL;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
2251}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002252EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002254
2255static inline unsigned int
2256read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2257 int ch, int dir, int idx, unsigned int ofs)
2258{
2259 unsigned int val;
2260 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2261 val &= HDA_AMP_VOLMASK;
2262 if (val >= ofs)
2263 val -= ofs;
2264 else
2265 val = 0;
2266 return val;
2267}
2268
2269static inline int
2270update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2271 int ch, int dir, int idx, unsigned int ofs,
2272 unsigned int val)
2273{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002274 unsigned int maxval;
2275
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002276 if (val > 0)
2277 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002278 /* ofs = 0: raw max value */
2279 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002280 if (val > maxval)
2281 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002282 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2283 HDA_AMP_VOLMASK, val);
2284}
2285
Takashi Iwaid5191e52009-11-16 14:58:17 +01002286/**
2287 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2288 *
2289 * The control element is supposed to have the private_value field
2290 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2291 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002292int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2296 hda_nid_t nid = get_amp_nid(kcontrol);
2297 int chs = get_amp_channels(kcontrol);
2298 int dir = get_amp_direction(kcontrol);
2299 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002300 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 long *valp = ucontrol->value.integer.value;
2302
2303 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002304 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002306 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002309EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Takashi Iwaid5191e52009-11-16 14:58:17 +01002311/**
2312 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2313 *
2314 * The control element is supposed to have the private_value field
2315 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2316 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002317int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2318 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2321 hda_nid_t nid = get_amp_nid(kcontrol);
2322 int chs = get_amp_channels(kcontrol);
2323 int dir = get_amp_direction(kcontrol);
2324 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002325 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 long *valp = ucontrol->value.integer.value;
2327 int change = 0;
2328
Takashi Iwaicb53c622007-08-10 17:21:45 +02002329 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002330 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002331 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002332 valp++;
2333 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002334 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002335 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002336 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return change;
2338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002339EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Takashi Iwaid5191e52009-11-16 14:58:17 +01002341/**
2342 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2343 *
2344 * The control element is supposed to have the private_value field
2345 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2346 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002347int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2348 unsigned int size, unsigned int __user *_tlv)
2349{
2350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2351 hda_nid_t nid = get_amp_nid(kcontrol);
2352 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002353 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002354 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002355 u32 caps, val1, val2;
2356
2357 if (size < 4 * sizeof(unsigned int))
2358 return -ENOMEM;
2359 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002360 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2361 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002362 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002363 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002364 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002365 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002366 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002367 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2368 return -EFAULT;
2369 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2370 return -EFAULT;
2371 if (put_user(val1, _tlv + 2))
2372 return -EFAULT;
2373 if (put_user(val2, _tlv + 3))
2374 return -EFAULT;
2375 return 0;
2376}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002377EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002378
Takashi Iwaid5191e52009-11-16 14:58:17 +01002379/**
2380 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2381 * @codec: HD-audio codec
2382 * @nid: NID of a reference widget
2383 * @dir: #HDA_INPUT or #HDA_OUTPUT
2384 * @tlv: TLV data to be stored, at least 4 elements
2385 *
2386 * Set (static) TLV data for a virtual master volume using the AMP caps
2387 * obtained from the reference NID.
2388 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002389 */
2390void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2391 unsigned int *tlv)
2392{
2393 u32 caps;
2394 int nums, step;
2395
2396 caps = query_amp_caps(codec, nid, dir);
2397 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2398 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2399 step = (step + 1) * 25;
2400 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2401 tlv[1] = 2 * sizeof(unsigned int);
2402 tlv[2] = -nums * step;
2403 tlv[3] = step;
2404}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002405EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002406
2407/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002408static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002409find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002410{
2411 struct snd_ctl_elem_id id;
2412 memset(&id, 0, sizeof(id));
2413 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002414 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002415 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002416 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2417 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002418 strcpy(id.name, name);
2419 return snd_ctl_find_id(codec->bus->card, &id);
2420}
2421
Takashi Iwaid5191e52009-11-16 14:58:17 +01002422/**
2423 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2424 * @codec: HD-audio codec
2425 * @name: ctl id name string
2426 *
2427 * Get the control element with the given id string and IFACE_MIXER.
2428 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002429struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2430 const char *name)
2431{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002432 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002433}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002434EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002435
Takashi Iwaidcda5802012-10-12 17:24:51 +02002436static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002437 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002438{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002439 int i, idx;
2440 /* 16 ctlrs should be large enough */
2441 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2442 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002443 return idx;
2444 }
2445 return -EBUSY;
2446}
2447
Takashi Iwaid5191e52009-11-16 14:58:17 +01002448/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002449 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002450 * @codec: HD-audio codec
2451 * @nid: corresponding NID (optional)
2452 * @kctl: the control element to assign
2453 *
2454 * Add the given control element to an array inside the codec instance.
2455 * All control elements belonging to a codec are supposed to be added
2456 * by this function so that a proper clean-up works at the free or
2457 * reconfiguration time.
2458 *
2459 * If non-zero @nid is passed, the NID is assigned to the control element.
2460 * The assignment is shown in the codec proc file.
2461 *
2462 * snd_hda_ctl_add() checks the control subdev id field whether
2463 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002464 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2465 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002466 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002467int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2468 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002469{
2470 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002471 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002472 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002473
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002474 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002475 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002476 if (nid == 0)
2477 nid = get_amp_nid_(kctl->private_value);
2478 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002479 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2480 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002481 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002482 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002483 err = snd_ctl_add(codec->bus->card, kctl);
2484 if (err < 0)
2485 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002486 item = snd_array_new(&codec->mixers);
2487 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002488 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002489 item->kctl = kctl;
2490 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002491 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002492 return 0;
2493}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002494EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002495
Takashi Iwaid5191e52009-11-16 14:58:17 +01002496/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002497 * snd_hda_add_nid - Assign a NID to a control element
2498 * @codec: HD-audio codec
2499 * @nid: corresponding NID (optional)
2500 * @kctl: the control element to assign
2501 * @index: index to kctl
2502 *
2503 * Add the given control element to an array inside the codec instance.
2504 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2505 * NID:KCTL mapping - for example "Capture Source" selector.
2506 */
2507int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2508 unsigned int index, hda_nid_t nid)
2509{
2510 struct hda_nid_item *item;
2511
2512 if (nid > 0) {
2513 item = snd_array_new(&codec->nids);
2514 if (!item)
2515 return -ENOMEM;
2516 item->kctl = kctl;
2517 item->index = index;
2518 item->nid = nid;
2519 return 0;
2520 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002521 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2522 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002523 return -EINVAL;
2524}
2525EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2526
2527/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002528 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2529 * @codec: HD-audio codec
2530 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002531void snd_hda_ctls_clear(struct hda_codec *codec)
2532{
2533 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002534 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002535 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002536 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002537 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002538 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002539}
2540
Takashi Iwaia65d6292009-02-23 16:57:04 +01002541/* pseudo device locking
2542 * toggle card->shutdown to allow/disallow the device access (as a hack)
2543 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002544int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002545{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002546 struct snd_card *card = bus->card;
2547 struct hda_codec *codec;
2548
Takashi Iwaia65d6292009-02-23 16:57:04 +01002549 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002550 if (card->shutdown)
2551 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002552 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002553 if (!list_empty(&card->ctl_files))
2554 goto err_clear;
2555
2556 list_for_each_entry(codec, &bus->codec_list, list) {
2557 int pcm;
2558 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2559 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2560 if (!cpcm->pcm)
2561 continue;
2562 if (cpcm->pcm->streams[0].substream_opened ||
2563 cpcm->pcm->streams[1].substream_opened)
2564 goto err_clear;
2565 }
2566 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002567 spin_unlock(&card->files_lock);
2568 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002569
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002570 err_clear:
2571 card->shutdown = 0;
2572 err_unlock:
2573 spin_unlock(&card->files_lock);
2574 return -EINVAL;
2575}
2576EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2577
2578void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002579{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002580 struct snd_card *card = bus->card;
2581
2582 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002583 spin_lock(&card->files_lock);
2584 card->shutdown = 0;
2585 spin_unlock(&card->files_lock);
2586}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002587EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002588
Takashi Iwaid5191e52009-11-16 14:58:17 +01002589/**
2590 * snd_hda_codec_reset - Clear all objects assigned to the codec
2591 * @codec: HD-audio codec
2592 *
2593 * This frees the all PCM and control elements assigned to the codec, and
2594 * clears the caches and restores the pin default configurations.
2595 *
2596 * When a device is being used, it returns -EBSY. If successfully freed,
2597 * returns zero.
2598 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002599int snd_hda_codec_reset(struct hda_codec *codec)
2600{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002601 struct hda_bus *bus = codec->bus;
2602 struct snd_card *card = bus->card;
2603 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002604
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002605 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002606 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002607
2608 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002609 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002610#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002611 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002612 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002613#endif
2614 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002615 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002616 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002617 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002618 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002619 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002620 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002621 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002622 }
2623 if (codec->patch_ops.free)
2624 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002625 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002626 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002627 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002628 codec->spec = NULL;
2629 free_hda_cache(&codec->amp_cache);
2630 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002631 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2632 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002633 /* free only driver_pins so that init_pins + user_pins are restored */
2634 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002635 snd_array_free(&codec->cvt_setups);
2636 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002637 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002638 codec->num_pcms = 0;
2639 codec->pcm_info = NULL;
2640 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002641 codec->slave_dig_outs = NULL;
2642 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002643 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002644 module_put(codec->owner);
2645 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002646
2647 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002648 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002649 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002650}
2651
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002652typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2653
2654/* apply the function to all matching slave ctls in the mixer list */
2655static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002656 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002657{
2658 struct hda_nid_item *items;
2659 const char * const *s;
2660 int i, err;
2661
2662 items = codec->mixers.list;
2663 for (i = 0; i < codec->mixers.used; i++) {
2664 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002665 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002666 continue;
2667 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002668 char tmpname[sizeof(sctl->id.name)];
2669 const char *name = *s;
2670 if (suffix) {
2671 snprintf(tmpname, sizeof(tmpname), "%s %s",
2672 name, suffix);
2673 name = tmpname;
2674 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002675 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002676 err = func(data, sctl);
2677 if (err)
2678 return err;
2679 break;
2680 }
2681 }
2682 }
2683 return 0;
2684}
2685
2686static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2687{
2688 return 1;
2689}
2690
Takashi Iwai18478e82012-03-09 17:51:10 +01002691/* guess the value corresponding to 0dB */
Takashi Iwai485e3e02013-11-04 15:51:00 +01002692static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002693{
2694 int _tlv[4];
2695 const int *tlv = NULL;
2696 int val = -1;
2697
2698 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2699 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2700 mm_segment_t fs = get_fs();
2701 set_fs(get_ds());
2702 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2703 tlv = _tlv;
2704 set_fs(fs);
2705 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2706 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002707 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2708 int step = tlv[3];
2709 step &= ~TLV_DB_SCALE_MUTE;
2710 if (!step)
2711 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002712 if (*step_to_check && *step_to_check != step) {
2713 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2714 *step_to_check, step);
2715 return -1;
2716 }
2717 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002718 val = -tlv[2] / step;
2719 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002720 return val;
2721}
2722
2723/* call kctl->put with the given value(s) */
2724static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2725{
2726 struct snd_ctl_elem_value *ucontrol;
2727 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2728 if (!ucontrol)
2729 return -ENOMEM;
2730 ucontrol->value.integer.value[0] = val;
2731 ucontrol->value.integer.value[1] = val;
2732 kctl->put(kctl, ucontrol);
2733 kfree(ucontrol);
2734 return 0;
2735}
2736
2737/* initialize the slave volume with 0dB */
2738static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2739{
Takashi Iwai485e3e02013-11-04 15:51:00 +01002740 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002741 if (offset > 0)
2742 put_kctl_with_value(slave, offset);
2743 return 0;
2744}
2745
2746/* unmute the slave */
2747static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2748{
2749 return put_kctl_with_value(slave, 1);
2750}
2751
Takashi Iwaid5191e52009-11-16 14:58:17 +01002752/**
2753 * snd_hda_add_vmaster - create a virtual master control and add slaves
2754 * @codec: HD-audio codec
2755 * @name: vmaster control name
2756 * @tlv: TLV data (optional)
2757 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002758 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002759 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002760 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761 *
2762 * Create a virtual master control with the given name. The TLV data
2763 * must be either NULL or a valid data.
2764 *
2765 * @slaves is a NULL-terminated array of strings, each of which is a
2766 * slave control name. All controls with these names are assigned to
2767 * the new virtual master control.
2768 *
2769 * This function returns zero if successful or a negative error code.
2770 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002771int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002772 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002773 const char *suffix, bool init_slave_vol,
2774 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002775{
2776 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002777 int err;
2778
Takashi Iwai29e58532012-03-12 12:25:03 +01002779 if (ctl_ret)
2780 *ctl_ret = NULL;
2781
Takashi Iwai9322ca52012-02-03 14:28:01 +01002782 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002783 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002784 snd_printdd("No slave found for %s\n", name);
2785 return 0;
2786 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002787 kctl = snd_ctl_make_virtual_master(name, tlv);
2788 if (!kctl)
2789 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002790 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002791 if (err < 0)
2792 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002793
Takashi Iwai9322ca52012-02-03 14:28:01 +01002794 err = map_slaves(codec, slaves, suffix,
2795 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002796 if (err < 0)
2797 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002798
2799 /* init with master mute & zero volume */
2800 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002801 if (init_slave_vol) {
2802 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002803 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002804 tlv ? init_slave_0dB : init_slave_unmute, &step);
2805 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002806
Takashi Iwai29e58532012-03-12 12:25:03 +01002807 if (ctl_ret)
2808 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002809 return 0;
2810}
Takashi Iwai18478e82012-03-09 17:51:10 +01002811EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002812
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002813/*
2814 * mute-LED control using vmaster
2815 */
2816static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
2818{
2819 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002820 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002821 };
2822 unsigned int index;
2823
2824 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2825 uinfo->count = 1;
2826 uinfo->value.enumerated.items = 3;
2827 index = uinfo->value.enumerated.item;
2828 if (index >= 3)
2829 index = 2;
2830 strcpy(uinfo->value.enumerated.name, texts[index]);
2831 return 0;
2832}
2833
2834static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
2836{
2837 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2838 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2839 return 0;
2840}
2841
2842static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2843 struct snd_ctl_elem_value *ucontrol)
2844{
2845 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2846 unsigned int old_mode = hook->mute_mode;
2847
2848 hook->mute_mode = ucontrol->value.enumerated.item[0];
2849 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2850 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2851 if (old_mode == hook->mute_mode)
2852 return 0;
2853 snd_hda_sync_vmaster_hook(hook);
2854 return 1;
2855}
2856
2857static struct snd_kcontrol_new vmaster_mute_mode = {
2858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2859 .name = "Mute-LED Mode",
2860 .info = vmaster_mute_mode_info,
2861 .get = vmaster_mute_mode_get,
2862 .put = vmaster_mute_mode_put,
2863};
2864
2865/*
2866 * Add a mute-LED hook with the given vmaster switch kctl
2867 * "Mute-LED Mode" control is automatically created and associated with
2868 * the given hook.
2869 */
2870int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002871 struct hda_vmaster_mute_hook *hook,
2872 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002873{
2874 struct snd_kcontrol *kctl;
2875
2876 if (!hook->hook || !hook->sw_kctl)
2877 return 0;
2878 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2879 hook->codec = codec;
2880 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002881 if (!expose_enum_ctl)
2882 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002883 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2884 if (!kctl)
2885 return -ENOMEM;
2886 return snd_hda_ctl_add(codec, 0, kctl);
2887}
2888EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2889
2890/*
2891 * Call the hook with the current value for synchronization
2892 * Should be called in init callback
2893 */
2894void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2895{
2896 if (!hook->hook || !hook->codec)
2897 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002898 /* don't call vmaster hook in the destructor since it might have
2899 * been already destroyed
2900 */
2901 if (hook->codec->bus->shutdown)
2902 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002903 switch (hook->mute_mode) {
2904 case HDA_VMUTE_FOLLOW_MASTER:
2905 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2906 break;
2907 default:
2908 hook->hook(hook->codec, hook->mute_mode);
2909 break;
2910 }
2911}
2912EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2913
2914
Takashi Iwaid5191e52009-11-16 14:58:17 +01002915/**
2916 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2917 *
2918 * The control element is supposed to have the private_value field
2919 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2920 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 int chs = get_amp_channels(kcontrol);
2925
2926 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2927 uinfo->count = chs == 3 ? 2 : 1;
2928 uinfo->value.integer.min = 0;
2929 uinfo->value.integer.max = 1;
2930 return 0;
2931}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002932EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Takashi Iwaid5191e52009-11-16 14:58:17 +01002934/**
2935 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2936 *
2937 * The control element is supposed to have the private_value field
2938 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2939 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002940int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2941 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2944 hda_nid_t nid = get_amp_nid(kcontrol);
2945 int chs = get_amp_channels(kcontrol);
2946 int dir = get_amp_direction(kcontrol);
2947 int idx = get_amp_index(kcontrol);
2948 long *valp = ucontrol->value.integer.value;
2949
2950 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002952 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002954 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002955 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return 0;
2957}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002958EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Takashi Iwaid5191e52009-11-16 14:58:17 +01002960/**
2961 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2962 *
2963 * The control element is supposed to have the private_value field
2964 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2965 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002966int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970 hda_nid_t nid = get_amp_nid(kcontrol);
2971 int chs = get_amp_channels(kcontrol);
2972 int dir = get_amp_direction(kcontrol);
2973 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 long *valp = ucontrol->value.integer.value;
2975 int change = 0;
2976
Takashi Iwaicb53c622007-08-10 17:21:45 +02002977 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002978 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002979 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002980 HDA_AMP_MUTE,
2981 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002982 valp++;
2983 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002984 if (chs & 2)
2985 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002986 HDA_AMP_MUTE,
2987 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002988 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002989 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return change;
2991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002992EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994/*
Takashi Iwai985be542005-11-02 18:26:49 +01002995 * bound volume controls
2996 *
2997 * bind multiple volumes (# indices, from 0)
2998 */
2999
3000#define AMP_VAL_IDX_SHIFT 19
3001#define AMP_VAL_IDX_MASK (0x0f<<19)
3002
Takashi Iwaid5191e52009-11-16 14:58:17 +01003003/**
3004 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3005 *
3006 * The control element is supposed to have the private_value field
3007 * set up via HDA_BIND_MUTE*() macros.
3008 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003009int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003011{
3012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3013 unsigned long pval;
3014 int err;
3015
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003016 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003017 pval = kcontrol->private_value;
3018 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3019 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3020 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003021 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003022 return err;
3023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003024EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003025
Takashi Iwaid5191e52009-11-16 14:58:17 +01003026/**
3027 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3028 *
3029 * The control element is supposed to have the private_value field
3030 * set up via HDA_BIND_MUTE*() macros.
3031 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3036 unsigned long pval;
3037 int i, indices, err = 0, change = 0;
3038
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003039 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003040 pval = kcontrol->private_value;
3041 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3042 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003043 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3044 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003045 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3046 if (err < 0)
3047 break;
3048 change |= err;
3049 }
3050 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003051 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003052 return err < 0 ? err : change;
3053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003054EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003055
Takashi Iwaid5191e52009-11-16 14:58:17 +01003056/**
3057 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3058 *
3059 * The control element is supposed to have the private_value field
3060 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003061 */
3062int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3063 struct snd_ctl_elem_info *uinfo)
3064{
3065 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3066 struct hda_bind_ctls *c;
3067 int err;
3068
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003069 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003070 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003071 kcontrol->private_value = *c->values;
3072 err = c->ops->info(kcontrol, uinfo);
3073 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003074 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003075 return err;
3076}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003077EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003078
Takashi Iwaid5191e52009-11-16 14:58:17 +01003079/**
3080 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3081 *
3082 * The control element is supposed to have the private_value field
3083 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3084 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003085int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3086 struct snd_ctl_elem_value *ucontrol)
3087{
3088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3089 struct hda_bind_ctls *c;
3090 int err;
3091
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003092 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003093 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003094 kcontrol->private_value = *c->values;
3095 err = c->ops->get(kcontrol, ucontrol);
3096 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003097 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003098 return err;
3099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003100EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003101
Takashi Iwaid5191e52009-11-16 14:58:17 +01003102/**
3103 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3104 *
3105 * The control element is supposed to have the private_value field
3106 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3107 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003108int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3109 struct snd_ctl_elem_value *ucontrol)
3110{
3111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3112 struct hda_bind_ctls *c;
3113 unsigned long *vals;
3114 int err = 0, change = 0;
3115
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003116 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003117 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003118 for (vals = c->values; *vals; vals++) {
3119 kcontrol->private_value = *vals;
3120 err = c->ops->put(kcontrol, ucontrol);
3121 if (err < 0)
3122 break;
3123 change |= err;
3124 }
3125 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003126 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003127 return err < 0 ? err : change;
3128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003129EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003130
Takashi Iwaid5191e52009-11-16 14:58:17 +01003131/**
3132 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3133 *
3134 * The control element is supposed to have the private_value field
3135 * set up via HDA_BIND_VOL() macro.
3136 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003137int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3138 unsigned int size, unsigned int __user *tlv)
3139{
3140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3141 struct hda_bind_ctls *c;
3142 int err;
3143
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003144 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003145 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003146 kcontrol->private_value = *c->values;
3147 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3148 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003149 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003150 return err;
3151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003152EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003153
3154struct hda_ctl_ops snd_hda_bind_vol = {
3155 .info = snd_hda_mixer_amp_volume_info,
3156 .get = snd_hda_mixer_amp_volume_get,
3157 .put = snd_hda_mixer_amp_volume_put,
3158 .tlv = snd_hda_mixer_amp_tlv
3159};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003160EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003161
3162struct hda_ctl_ops snd_hda_bind_sw = {
3163 .info = snd_hda_mixer_amp_switch_info,
3164 .get = snd_hda_mixer_amp_switch_get,
3165 .put = snd_hda_mixer_amp_switch_put,
3166 .tlv = snd_hda_mixer_amp_tlv
3167};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003168EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003169
3170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * SPDIF out controls
3172 */
3173
Takashi Iwai0ba21762007-04-16 11:29:14 +02003174static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
3177 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3178 uinfo->count = 1;
3179 return 0;
3180}
3181
Takashi Iwai0ba21762007-04-16 11:29:14 +02003182static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
3185 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3186 IEC958_AES0_NONAUDIO |
3187 IEC958_AES0_CON_EMPHASIS_5015 |
3188 IEC958_AES0_CON_NOT_COPYRIGHT;
3189 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3190 IEC958_AES1_CON_ORIGINAL;
3191 return 0;
3192}
3193
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3198 IEC958_AES0_NONAUDIO |
3199 IEC958_AES0_PRO_EMPHASIS_5015;
3200 return 0;
3201}
3202
Takashi Iwai0ba21762007-04-16 11:29:14 +02003203static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
3206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003207 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003208 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003210 mutex_lock(&codec->spdif_mutex);
3211 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003212 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3213 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3214 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3215 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003216 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 return 0;
3219}
3220
3221/* convert from SPDIF status bits to HDA SPDIF bits
3222 * bit 0 (DigEn) is always set zero (to be filled later)
3223 */
3224static unsigned short convert_from_spdif_status(unsigned int sbits)
3225{
3226 unsigned short val = 0;
3227
3228 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003229 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003233 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3234 IEC958_AES0_PRO_EMPHASIS_5015)
3235 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003237 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3238 IEC958_AES0_CON_EMPHASIS_5015)
3239 val |= AC_DIG1_EMPHASIS;
3240 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3241 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003243 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3245 }
3246 return val;
3247}
3248
3249/* convert to SPDIF status bits from HDA SPDIF bits
3250 */
3251static unsigned int convert_to_spdif_status(unsigned short val)
3252{
3253 unsigned int sbits = 0;
3254
Takashi Iwai0ba21762007-04-16 11:29:14 +02003255 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003257 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 sbits |= IEC958_AES0_PROFESSIONAL;
3259 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003260 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3262 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003263 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003265 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003267 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3269 sbits |= val & (0x7f << 8);
3270 }
3271 return sbits;
3272}
3273
Takashi Iwai2f728532008-09-25 16:32:41 +02003274/* set digital convert verbs both for the given NID and its slaves */
3275static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3276 int verb, int val)
3277{
Takashi Iwaidda14412011-05-02 11:29:30 +02003278 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003279
Takashi Iwai9e976972008-11-25 08:17:20 +01003280 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003281 d = codec->slave_dig_outs;
3282 if (!d)
3283 return;
3284 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003285 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003286}
3287
3288static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3289 int dig1, int dig2)
3290{
3291 if (dig1 != -1)
3292 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3293 if (dig2 != -1)
3294 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3295}
3296
Takashi Iwai0ba21762007-04-16 11:29:14 +02003297static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003301 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003302 struct hda_spdif_out *spdif;
3303 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 unsigned short val;
3305 int change;
3306
Ingo Molnar62932df2006-01-16 16:34:20 +01003307 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003308 spdif = snd_array_elem(&codec->spdif_out, idx);
3309 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003310 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3312 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3313 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003314 val = convert_from_spdif_status(spdif->status);
3315 val |= spdif->ctls & 1;
3316 change = spdif->ctls != val;
3317 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003318 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003319 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003320 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return change;
3322}
3323
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003324#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Takashi Iwai0ba21762007-04-16 11:29:14 +02003326static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
3329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003330 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003331 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003333 mutex_lock(&codec->spdif_mutex);
3334 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003335 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003336 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return 0;
3338}
3339
Stephen Warren74b654c2011-06-01 11:14:18 -06003340static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3341 int dig1, int dig2)
3342{
3343 set_dig_out_convert(codec, nid, dig1, dig2);
3344 /* unmute amp switch (if any) */
3345 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3346 (dig1 & AC_DIG1_ENABLE))
3347 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3348 HDA_AMP_MUTE, 0);
3349}
3350
Takashi Iwai0ba21762007-04-16 11:29:14 +02003351static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003355 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003356 struct hda_spdif_out *spdif;
3357 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 unsigned short val;
3359 int change;
3360
Ingo Molnar62932df2006-01-16 16:34:20 +01003361 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003362 spdif = snd_array_elem(&codec->spdif_out, idx);
3363 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003364 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003366 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003367 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003368 spdif->ctls = val;
3369 if (change && nid != (u16)-1)
3370 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003371 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return change;
3373}
3374
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003375static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 {
3377 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3378 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003379 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 .info = snd_hda_spdif_mask_info,
3381 .get = snd_hda_spdif_cmask_get,
3382 },
3383 {
3384 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3385 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003386 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 .info = snd_hda_spdif_mask_info,
3388 .get = snd_hda_spdif_pmask_get,
3389 },
3390 {
3391 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003392 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 .info = snd_hda_spdif_mask_info,
3394 .get = snd_hda_spdif_default_get,
3395 .put = snd_hda_spdif_default_put,
3396 },
3397 {
3398 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003399 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 .info = snd_hda_spdif_out_switch_info,
3401 .get = snd_hda_spdif_out_switch_get,
3402 .put = snd_hda_spdif_out_switch_put,
3403 },
3404 { } /* end */
3405};
3406
3407/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003408 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003410 * @associated_nid: NID that new ctls associated with
3411 * @cvt_nid: converter NID
3412 * @type: HDA_PCM_TYPE_*
3413 * Creates controls related with the digital output.
3414 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 *
3416 * Returns 0 if successful, or a negative error code.
3417 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003418int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3419 hda_nid_t associated_nid,
3420 hda_nid_t cvt_nid,
3421 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
3423 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003424 struct snd_kcontrol *kctl;
3425 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003426 int idx = 0;
3427 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003428 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003429 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003431 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003432 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003433 idx = spdif_index;
3434 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003435 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003436 /* suppose a single SPDIF device */
3437 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3438 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3439 if (!kctl)
3440 break;
3441 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003442 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003443 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003444 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003445 if (!bus->primary_dig_out_type)
3446 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003447
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003448 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003449 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003450 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3451 return -EBUSY;
3452 }
Stephen Warren7c935972011-06-01 11:14:17 -06003453 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003454 if (!spdif)
3455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3457 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003458 if (!kctl)
3459 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003460 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003461 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003462 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return err;
3465 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003466 spdif->nid = cvt_nid;
3467 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003468 AC_VERB_GET_DIGI_CONVERT_1, 0);
3469 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return 0;
3471}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003472EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003474/* get the hda_spdif_out entry from the given NID
3475 * call within spdif_mutex lock
3476 */
Stephen Warren7c935972011-06-01 11:14:17 -06003477struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3478 hda_nid_t nid)
3479{
3480 int i;
3481 for (i = 0; i < codec->spdif_out.used; i++) {
3482 struct hda_spdif_out *spdif =
3483 snd_array_elem(&codec->spdif_out, i);
3484 if (spdif->nid == nid)
3485 return spdif;
3486 }
3487 return NULL;
3488}
3489EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3490
Stephen Warren74b654c2011-06-01 11:14:18 -06003491void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3492{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003493 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003494
3495 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003496 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003497 spdif->nid = (u16)-1;
3498 mutex_unlock(&codec->spdif_mutex);
3499}
3500EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3501
3502void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3503{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003504 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003505 unsigned short val;
3506
3507 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003508 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003509 if (spdif->nid != nid) {
3510 spdif->nid = nid;
3511 val = spdif->ctls;
3512 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3513 }
3514 mutex_unlock(&codec->spdif_mutex);
3515}
3516EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003519 * SPDIF sharing with analog output
3520 */
3521static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3522 struct snd_ctl_elem_value *ucontrol)
3523{
3524 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3525 ucontrol->value.integer.value[0] = mout->share_spdif;
3526 return 0;
3527}
3528
3529static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3530 struct snd_ctl_elem_value *ucontrol)
3531{
3532 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3533 mout->share_spdif = !!ucontrol->value.integer.value[0];
3534 return 0;
3535}
3536
3537static struct snd_kcontrol_new spdif_share_sw = {
3538 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3539 .name = "IEC958 Default PCM Playback Switch",
3540 .info = snd_ctl_boolean_mono_info,
3541 .get = spdif_share_sw_get,
3542 .put = spdif_share_sw_put,
3543};
3544
Takashi Iwaid5191e52009-11-16 14:58:17 +01003545/**
3546 * snd_hda_create_spdif_share_sw - create Default PCM switch
3547 * @codec: the HDA codec
3548 * @mout: multi-out instance
3549 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003550int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3551 struct hda_multi_out *mout)
3552{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003553 struct snd_kcontrol *kctl;
3554
Takashi Iwai9a081602008-02-12 18:37:26 +01003555 if (!mout->dig_out_nid)
3556 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003557
3558 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3559 if (!kctl)
3560 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003561 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003562 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003564EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003565
3566/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 * SPDIF input
3568 */
3569
3570#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3571
Takashi Iwai0ba21762007-04-16 11:29:14 +02003572static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3573 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
3575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3576
3577 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3578 return 0;
3579}
3580
Takashi Iwai0ba21762007-04-16 11:29:14 +02003581static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3582 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3585 hda_nid_t nid = kcontrol->private_value;
3586 unsigned int val = !!ucontrol->value.integer.value[0];
3587 int change;
3588
Ingo Molnar62932df2006-01-16 16:34:20 +01003589 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003591 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003593 snd_hda_codec_write_cache(codec, nid, 0,
3594 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003596 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return change;
3598}
3599
Takashi Iwai0ba21762007-04-16 11:29:14 +02003600static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3601 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3604 hda_nid_t nid = kcontrol->private_value;
3605 unsigned short val;
3606 unsigned int sbits;
3607
Andrew Paprocki3982d172007-12-19 12:13:44 +01003608 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 sbits = convert_to_spdif_status(val);
3610 ucontrol->value.iec958.status[0] = sbits;
3611 ucontrol->value.iec958.status[1] = sbits >> 8;
3612 ucontrol->value.iec958.status[2] = sbits >> 16;
3613 ucontrol->value.iec958.status[3] = sbits >> 24;
3614 return 0;
3615}
3616
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003617static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 {
3619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003620 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 .info = snd_hda_spdif_in_switch_info,
3622 .get = snd_hda_spdif_in_switch_get,
3623 .put = snd_hda_spdif_in_switch_put,
3624 },
3625 {
3626 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3627 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003628 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 .info = snd_hda_spdif_mask_info,
3630 .get = snd_hda_spdif_in_status_get,
3631 },
3632 { } /* end */
3633};
3634
3635/**
3636 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3637 * @codec: the HDA codec
3638 * @nid: audio in widget NID
3639 *
3640 * Creates controls related with the SPDIF input.
3641 * Called from each patch supporting the SPDIF in.
3642 *
3643 * Returns 0 if successful, or a negative error code.
3644 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003645int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003648 struct snd_kcontrol *kctl;
3649 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003650 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Takashi Iwaidcda5802012-10-12 17:24:51 +02003652 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003653 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003654 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3655 return -EBUSY;
3656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3658 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003659 if (!kctl)
3660 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003662 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return err;
3665 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003666 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003667 snd_hda_codec_read(codec, nid, 0,
3668 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003669 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 return 0;
3671}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003672EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003674/*
3675 * command cache
3676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Takashi Iwaic370dd62012-12-13 18:30:04 +01003678/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003679#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3680#define get_cmd_cache_nid(key) ((key) & 0xff)
3681#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3682
3683/**
3684 * snd_hda_codec_write_cache - send a single command with caching
3685 * @codec: the HDA codec
3686 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003687 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003688 * @verb: the verb to send
3689 * @parm: the parameter for the verb
3690 *
3691 * Send a single command without waiting for response.
3692 *
3693 * Returns 0 if successful, or a negative error code.
3694 */
3695int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003696 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003697{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003698 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003699 struct hda_cache_head *c;
3700 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003701 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003702
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003703 cache_only = codec->cached_write;
3704 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003705 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003706 if (err < 0)
3707 return err;
3708 }
3709
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003710 /* parm may contain the verb stuff for get/set amp */
3711 verb = verb | (parm >> 8);
3712 parm &= 0xff;
3713 key = build_cmd_cache_key(nid, verb);
3714 mutex_lock(&codec->bus->cmd_mutex);
3715 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003716 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003717 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003718 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003719 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003720 mutex_unlock(&codec->bus->cmd_mutex);
3721 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003722}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003723EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003724
Takashi Iwaid5191e52009-11-16 14:58:17 +01003725/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003726 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3727 * @codec: the HDA codec
3728 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003729 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003730 * @verb: the verb to send
3731 * @parm: the parameter for the verb
3732 *
3733 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3734 * command if the parameter is already identical with the cached value.
3735 * If not, it sends the command and refreshes the cache.
3736 *
3737 * Returns 0 if successful, or a negative error code.
3738 */
3739int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003740 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003741{
3742 struct hda_cache_head *c;
3743 u32 key;
3744
3745 /* parm may contain the verb stuff for get/set amp */
3746 verb = verb | (parm >> 8);
3747 parm &= 0xff;
3748 key = build_cmd_cache_key(nid, verb);
3749 mutex_lock(&codec->bus->cmd_mutex);
3750 c = get_hash(&codec->cmd_cache, key);
3751 if (c && c->val == parm) {
3752 mutex_unlock(&codec->bus->cmd_mutex);
3753 return 0;
3754 }
3755 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003756 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003757}
3758EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3759
3760/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003761 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3762 * @codec: HD-audio codec
3763 *
3764 * Execute all verbs recorded in the command caches to resume.
3765 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003766void snd_hda_codec_resume_cache(struct hda_codec *codec)
3767{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003768 int i;
3769
Takashi Iwaic370dd62012-12-13 18:30:04 +01003770 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003771 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003772 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3773 struct hda_cache_head *buffer;
3774 u32 key;
3775
3776 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3777 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003778 if (!key)
3779 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003780 if (!buffer->dirty)
3781 continue;
3782 buffer->dirty = 0;
3783 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003784 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3785 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003786 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003787 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003788 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003790EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003791
3792/**
3793 * snd_hda_sequence_write_cache - sequence writes with caching
3794 * @codec: the HDA codec
3795 * @seq: VERB array to send
3796 *
3797 * Send the commands sequentially from the given array.
3798 * Thte commands are recorded on cache for power-save and resume.
3799 * The array must be terminated with NID=0.
3800 */
3801void snd_hda_sequence_write_cache(struct hda_codec *codec,
3802 const struct hda_verb *seq)
3803{
3804 for (; seq->nid; seq++)
3805 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3806 seq->param);
3807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003808EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003809
Takashi Iwaidc870f32013-01-22 15:24:30 +01003810/**
3811 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3812 * @codec: HD-audio codec
3813 */
3814void snd_hda_codec_flush_cache(struct hda_codec *codec)
3815{
3816 snd_hda_codec_resume_amp(codec);
3817 snd_hda_codec_resume_cache(codec);
3818}
3819EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3820
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003821void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003822 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003823{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003824 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003825 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003826
Takashi Iwaicb53c622007-08-10 17:21:45 +02003827 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003828 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003829 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003830 if (!(wcaps & AC_WCAP_POWER))
3831 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003832 if (codec->power_filter) {
3833 state = codec->power_filter(codec, nid, power_state);
3834 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003835 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003836 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003837 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003838 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003839 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003840}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003841EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3842
3843/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003844 * supported power states check
3845 */
3846static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3847 unsigned int power_state)
3848{
3849 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3850
Mengdong Line037cb42012-08-10 14:11:58 +02003851 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003852 return false;
3853 if (sup & power_state)
3854 return true;
3855 else
3856 return false;
3857}
3858
3859/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003860 * wait until the state is reached, returns the current state
3861 */
3862static unsigned int hda_sync_power_state(struct hda_codec *codec,
3863 hda_nid_t fg,
3864 unsigned int power_state)
3865{
3866 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3867 unsigned int state, actual_state;
3868
3869 for (;;) {
3870 state = snd_hda_codec_read(codec, fg, 0,
3871 AC_VERB_GET_POWER_STATE, 0);
3872 if (state & AC_PWRST_ERROR)
3873 break;
3874 actual_state = (state >> 4) & 0x0f;
3875 if (actual_state == power_state)
3876 break;
3877 if (time_after_eq(jiffies, end_time))
3878 break;
3879 /* wait until the codec reachs to the target state */
3880 msleep(1);
3881 }
3882 return state;
3883}
3884
Takashi Iwai9419ab62013-01-24 17:23:35 +01003885/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003886unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3887 hda_nid_t nid,
3888 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003889{
3890 if (power_state == AC_PWRST_D3 &&
3891 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3892 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3893 int eapd = snd_hda_codec_read(codec, nid, 0,
3894 AC_VERB_GET_EAPD_BTLENABLE, 0);
3895 if (eapd & 0x02)
3896 return AC_PWRST_D0;
3897 }
3898 return power_state;
3899}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003900EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003901
Takashi Iwai432c6412012-08-28 09:59:20 -07003902/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003903 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003904 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003905static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003906 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003907{
Takashi Iwaid8193872012-08-31 07:54:38 -07003908 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003909 int count;
3910 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003911 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003912
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003913 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003914 if (power_state == AC_PWRST_D3) {
3915 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003916 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003917 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003918 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003919
3920 /* repeat power states setting at most 10 times*/
3921 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003922 if (codec->patch_ops.set_power_state)
3923 codec->patch_ops.set_power_state(codec, fg,
3924 power_state);
3925 else {
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003926 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003927 AC_VERB_SET_POWER_STATE,
3928 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003929 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003930 }
3931 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003932 if (!(state & AC_PWRST_ERROR))
3933 break;
3934 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003935
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003936 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003937}
Takashi Iwai54d17402005-11-21 16:33:22 +01003938
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003939/* sync power states of all widgets;
3940 * this is called at the end of codec parsing
3941 */
3942static void sync_power_up_states(struct hda_codec *codec)
3943{
3944 hda_nid_t nid = codec->start_nid;
3945 int i;
3946
Takashi Iwaiba615b82013-03-13 14:47:21 +01003947 /* don't care if no filter is used */
3948 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003949 return;
3950
3951 for (i = 0; i < codec->num_nodes; i++, nid++) {
3952 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003953 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003954 if (!(wcaps & AC_WCAP_POWER))
3955 continue;
3956 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3957 if (target == AC_PWRST_D0)
3958 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003959 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003960 snd_hda_codec_write(codec, nid, 0,
3961 AC_VERB_SET_POWER_STATE, target);
3962 }
3963}
3964
Takashi Iwai11aeff02008-07-30 15:01:46 +02003965#ifdef CONFIG_SND_HDA_HWDEP
3966/* execute additional init verbs */
3967static void hda_exec_init_verbs(struct hda_codec *codec)
3968{
3969 if (codec->init_verbs.list)
3970 snd_hda_sequence_write(codec, codec->init_verbs.list);
3971}
3972#else
3973static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3974#endif
3975
Takashi Iwai2a439522011-07-26 09:52:50 +02003976#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003977/*
3978 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003979 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003980 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003981static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003982{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003983 unsigned int state;
3984
Takashi Iwai989c3182012-11-19 14:14:58 +01003985 codec->in_pm = 1;
3986
Takashi Iwaicb53c622007-08-10 17:21:45 +02003987 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003988 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003989 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003990 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003991 /* Cancel delayed work if we aren't currently running from it. */
3992 if (!in_wq)
3993 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003994 spin_lock(&codec->power_lock);
3995 snd_hda_update_power_acct(codec);
3996 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003997 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003998 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003999 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004000 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004001 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004002 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004003}
4004
Takashi Iwaic370dd62012-12-13 18:30:04 +01004005/* mark all entries of cmd and amp caches dirty */
4006static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4007{
4008 int i;
4009 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4010 struct hda_cache_head *cmd;
4011 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4012 cmd->dirty = 1;
4013 }
4014 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4015 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004016 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004017 amp->head.dirty = 1;
4018 }
4019}
4020
Takashi Iwaicb53c622007-08-10 17:21:45 +02004021/*
4022 * kick up codec; used both from PM and power-save
4023 */
4024static void hda_call_codec_resume(struct hda_codec *codec)
4025{
Takashi Iwai989c3182012-11-19 14:14:58 +01004026 codec->in_pm = 1;
4027
Takashi Iwaic370dd62012-12-13 18:30:04 +01004028 hda_mark_cmd_cache_dirty(codec);
4029
Takashi Iwai7f308302012-05-08 16:52:23 +02004030 /* set as if powered on for avoiding re-entering the resume
4031 * in the resume / power-save sequence
4032 */
4033 hda_keep_power_on(codec);
Takashi Iwai0fc28fc2013-11-20 12:15:07 +01004034 if (codec->pm_down_notified) {
4035 codec->pm_down_notified = 0;
4036 hda_call_pm_notify(codec->bus, true);
4037 }
Takashi Iwaid8193872012-08-31 07:54:38 -07004038 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004039 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004040 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004041 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004042 if (codec->patch_ops.resume)
4043 codec->patch_ops.resume(codec);
4044 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004045 if (codec->patch_ops.init)
4046 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004047 snd_hda_codec_resume_amp(codec);
4048 snd_hda_codec_resume_cache(codec);
4049 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004050
4051 if (codec->jackpoll_interval)
4052 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004053 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004054 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004055
4056 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004057 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004058}
Takashi Iwai2a439522011-07-26 09:52:50 +02004059#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004060
Takashi Iwai54d17402005-11-21 16:33:22 +01004061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062/**
4063 * snd_hda_build_controls - build mixer controls
4064 * @bus: the BUS
4065 *
4066 * Creates mixer controls for each codec included in the bus.
4067 *
4068 * Returns 0 if successful, otherwise a negative error code.
4069 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004070int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004072 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Takashi Iwai0ba21762007-04-16 11:29:14 +02004074 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004075 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004076 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004077 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004078 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004079 err = snd_hda_codec_reset(codec);
4080 if (err < 0) {
4081 printk(KERN_ERR
4082 "hda_codec: cannot revert codec\n");
4083 return err;
4084 }
4085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004087 return 0;
4088}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004089EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004091/*
4092 * add standard channel maps if not specified
4093 */
4094static int add_std_chmaps(struct hda_codec *codec)
4095{
4096 int i, str, err;
4097
4098 for (i = 0; i < codec->num_pcms; i++) {
4099 for (str = 0; str < 2; str++) {
4100 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4101 struct hda_pcm_stream *hinfo =
4102 &codec->pcm_info[i].stream[str];
4103 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004104 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004105
4106 if (codec->pcm_info[i].own_chmap)
4107 continue;
4108 if (!pcm || !hinfo->substreams)
4109 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004110 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4111 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004112 hinfo->channels_max,
4113 0, &chmap);
4114 if (err < 0)
4115 return err;
4116 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4117 }
4118 }
4119 return 0;
4120}
4121
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004122/* default channel maps for 2.1 speakers;
4123 * since HD-audio supports only stereo, odd number channels are omitted
4124 */
4125const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4126 { .channels = 2,
4127 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4128 { .channels = 4,
4129 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4130 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4131 { }
4132};
4133EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4134
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004135int snd_hda_codec_build_controls(struct hda_codec *codec)
4136{
4137 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004138 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004139 /* continue to initialize... */
4140 if (codec->patch_ops.init)
4141 err = codec->patch_ops.init(codec);
4142 if (!err && codec->patch_ops.build_controls)
4143 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004144 if (err < 0)
4145 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004146
4147 /* we create chmaps here instead of build_pcms */
4148 err = add_std_chmaps(codec);
4149 if (err < 0)
4150 return err;
4151
David Henningsson26a6cb62012-10-09 15:04:21 +02004152 if (codec->jackpoll_interval)
4153 hda_jackpoll_work(&codec->jackpoll_work.work);
4154 else
4155 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004156 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return 0;
4158}
4159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160/*
4161 * stream formats
4162 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004163struct hda_rate_tbl {
4164 unsigned int hz;
4165 unsigned int alsa_bits;
4166 unsigned int hda_fmt;
4167};
4168
Takashi Iwai92f10b32010-08-03 14:21:00 +02004169/* rate = base * mult / div */
4170#define HDA_RATE(base, mult, div) \
4171 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4172 (((div) - 1) << AC_FMT_DIV_SHIFT))
4173
Takashi Iwaibefdf312005-08-22 13:57:55 +02004174static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004176
4177 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004178 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4179 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4180 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4181 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4182 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4183 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4184 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4185 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4186 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4187 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4188 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004189#define AC_PAR_PCM_RATE_BITS 11
4190 /* up to bits 10, 384kHZ isn't supported properly */
4191
4192 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004193 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004194
Takashi Iwaibefdf312005-08-22 13:57:55 +02004195 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196};
4197
4198/**
4199 * snd_hda_calc_stream_format - calculate format bitset
4200 * @rate: the sample rate
4201 * @channels: the number of channels
4202 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4203 * @maxbps: the max. bps
4204 *
4205 * Calculate the format bitset from the given rate, channels and th PCM format.
4206 *
4207 * Return zero if invalid.
4208 */
4209unsigned int snd_hda_calc_stream_format(unsigned int rate,
4210 unsigned int channels,
4211 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004212 unsigned int maxbps,
4213 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 int i;
4216 unsigned int val = 0;
4217
Takashi Iwaibefdf312005-08-22 13:57:55 +02004218 for (i = 0; rate_bits[i].hz; i++)
4219 if (rate_bits[i].hz == rate) {
4220 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 break;
4222 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004223 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 snd_printdd("invalid rate %d\n", rate);
4225 return 0;
4226 }
4227
4228 if (channels == 0 || channels > 8) {
4229 snd_printdd("invalid channels %d\n", channels);
4230 return 0;
4231 }
4232 val |= channels - 1;
4233
4234 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004235 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004236 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004237 break;
4238 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004239 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 case 20:
4242 case 24:
4243 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004244 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004245 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004247 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004249 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 break;
4251 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004252 snd_printdd("invalid format width %d\n",
4253 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return 0;
4255 }
4256
Anssi Hannula32c168c2010-08-03 13:28:57 +03004257 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004258 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return val;
4261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004262EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004264static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4265 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004266{
4267 unsigned int val = 0;
4268 if (nid != codec->afg &&
4269 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4270 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4271 if (!val || val == -1)
4272 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4273 if (!val || val == -1)
4274 return 0;
4275 return val;
4276}
4277
4278static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4279{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004280 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004281 get_pcm_param);
4282}
4283
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004284static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4285 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004286{
4287 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4288 if (!streams || streams == -1)
4289 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4290 if (!streams || streams == -1)
4291 return 0;
4292 return streams;
4293}
4294
4295static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4296{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004297 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004298 get_stream_param);
4299}
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301/**
4302 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4303 * @codec: the HDA codec
4304 * @nid: NID to query
4305 * @ratesp: the pointer to store the detected rate bitflags
4306 * @formatsp: the pointer to store the detected formats
4307 * @bpsp: the pointer to store the detected format widths
4308 *
4309 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4310 * or @bsps argument is ignored.
4311 *
4312 * Returns 0 if successful, otherwise a negative error code.
4313 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004314int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4316{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004317 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004319 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004320 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 if (ratesp) {
4323 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004324 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004326 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004328 if (rates == 0) {
4329 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4330 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4331 nid, val,
4332 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4333 return -EIO;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 *ratesp = rates;
4336 }
4337
4338 if (formatsp || bpsp) {
4339 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004340 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004342 streams = query_stream_param(codec, nid);
4343 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 bps = 0;
4347 if (streams & AC_SUPFMT_PCM) {
4348 if (val & AC_SUPPCM_BITS_8) {
4349 formats |= SNDRV_PCM_FMTBIT_U8;
4350 bps = 8;
4351 }
4352 if (val & AC_SUPPCM_BITS_16) {
4353 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4354 bps = 16;
4355 }
4356 if (wcaps & AC_WCAP_DIGITAL) {
4357 if (val & AC_SUPPCM_BITS_32)
4358 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4359 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4360 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4361 if (val & AC_SUPPCM_BITS_24)
4362 bps = 24;
4363 else if (val & AC_SUPPCM_BITS_20)
4364 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004365 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4366 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4368 if (val & AC_SUPPCM_BITS_32)
4369 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 else if (val & AC_SUPPCM_BITS_24)
4371 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004372 else if (val & AC_SUPPCM_BITS_20)
4373 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
4375 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004376#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004377 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004379 if (!bps)
4380 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004381 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004382#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004383 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004384 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 /* temporary hack: we have still no proper support
4386 * for the direct AC3 stream...
4387 */
4388 formats |= SNDRV_PCM_FMTBIT_U8;
4389 bps = 8;
4390 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004391 if (formats == 0) {
4392 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4393 "(nid=0x%x, val=0x%x, ovrd=%i, "
4394 "streams=0x%x)\n",
4395 nid, val,
4396 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4397 streams);
4398 return -EIO;
4399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 if (formatsp)
4401 *formatsp = formats;
4402 if (bpsp)
4403 *bpsp = bps;
4404 }
4405
4406 return 0;
4407}
Stephen Warren384a48d2011-06-01 11:14:21 -06004408EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004411 * snd_hda_is_supported_format - Check the validity of the format
4412 * @codec: HD-audio codec
4413 * @nid: NID to check
4414 * @format: the HD-audio format value to check
4415 *
4416 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 *
4418 * Returns 1 if supported, 0 if not.
4419 */
4420int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4421 unsigned int format)
4422{
4423 int i;
4424 unsigned int val = 0, rate, stream;
4425
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004426 val = query_pcm_param(codec, nid);
4427 if (!val)
4428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004431 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004432 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (val & (1 << i))
4434 break;
4435 return 0;
4436 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004437 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return 0;
4439
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004440 stream = query_stream_param(codec, nid);
4441 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 return 0;
4443
4444 if (stream & AC_SUPFMT_PCM) {
4445 switch (format & 0xf0) {
4446 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004447 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return 0;
4449 break;
4450 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004451 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return 0;
4453 break;
4454 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004455 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 return 0;
4457 break;
4458 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004459 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 return 0;
4461 break;
4462 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004463 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return 0;
4465 break;
4466 default:
4467 return 0;
4468 }
4469 } else {
4470 /* FIXME: check for float32 and AC3? */
4471 }
4472
4473 return 1;
4474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004475EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477/*
4478 * PCM stuff
4479 */
4480static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4481 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004482 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 return 0;
4485}
4486
4487static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4488 struct hda_codec *codec,
4489 unsigned int stream_tag,
4490 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004491 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4494 return 0;
4495}
4496
4497static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4498 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004499 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Takashi Iwai888afa12008-03-18 09:57:50 +01004501 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return 0;
4503}
4504
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004505static int set_pcm_default_values(struct hda_codec *codec,
4506 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004508 int err;
4509
Takashi Iwai0ba21762007-04-16 11:29:14 +02004510 /* query support PCM information from the given NID */
4511 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004512 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004513 info->rates ? NULL : &info->rates,
4514 info->formats ? NULL : &info->formats,
4515 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004516 if (err < 0)
4517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 }
4519 if (info->ops.open == NULL)
4520 info->ops.open = hda_pcm_default_open_close;
4521 if (info->ops.close == NULL)
4522 info->ops.close = hda_pcm_default_open_close;
4523 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004524 if (snd_BUG_ON(!info->nid))
4525 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 info->ops.prepare = hda_pcm_default_prepare;
4527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004529 if (snd_BUG_ON(!info->nid))
4530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 info->ops.cleanup = hda_pcm_default_cleanup;
4532 }
4533 return 0;
4534}
4535
Takashi Iwaieb541332010-08-06 13:48:11 +02004536/*
4537 * codec prepare/cleanup entries
4538 */
4539int snd_hda_codec_prepare(struct hda_codec *codec,
4540 struct hda_pcm_stream *hinfo,
4541 unsigned int stream,
4542 unsigned int format,
4543 struct snd_pcm_substream *substream)
4544{
4545 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004546 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004547 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4548 if (ret >= 0)
4549 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004550 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004551 return ret;
4552}
4553EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4554
4555void snd_hda_codec_cleanup(struct hda_codec *codec,
4556 struct hda_pcm_stream *hinfo,
4557 struct snd_pcm_substream *substream)
4558{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004559 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004560 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004561 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004562}
4563EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4564
Takashi Iwaid5191e52009-11-16 14:58:17 +01004565/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004566const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4567 "Audio", "SPDIF", "HDMI", "Modem"
4568};
4569
Takashi Iwai176d5332008-07-30 15:01:44 +02004570/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004571 * get the empty PCM device number to assign
4572 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004573static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004574{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004575 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004576 /* assigned to static slots up to dev#10; if more needed, assign
4577 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4578 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004579 static int audio_idx[HDA_PCM_NTYPES][5] = {
4580 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4581 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004582 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004583 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004584 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004585 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004586
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004587 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004588 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4589 return -EINVAL;
4590 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004591
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004592 for (i = 0; audio_idx[type][i] >= 0; i++) {
4593#ifndef CONFIG_SND_DYNAMIC_MINORS
4594 if (audio_idx[type][i] >= 8)
4595 break;
4596#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004597 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4598 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004599 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004600
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004601#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004602 /* non-fixed slots starting from 10 */
4603 for (i = 10; i < 32; i++) {
4604 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4605 return i;
4606 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004607#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004608
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004609 snd_printk(KERN_WARNING "Too many %s devices\n",
4610 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004611#ifndef CONFIG_SND_DYNAMIC_MINORS
4612 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4613#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004614 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004615}
4616
4617/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004618 * attach a new PCM stream
4619 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004620static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004621{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004622 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004623 struct hda_pcm_stream *info;
4624 int stream, err;
4625
Takashi Iwaib91f0802008-11-04 08:43:08 +01004626 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004627 return -EINVAL;
4628 for (stream = 0; stream < 2; stream++) {
4629 info = &pcm->stream[stream];
4630 if (info->substreams) {
4631 err = set_pcm_default_values(codec, info);
4632 if (err < 0)
4633 return err;
4634 }
4635 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004636 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004637}
4638
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004639/* assign all PCMs of the given codec */
4640int snd_hda_codec_build_pcms(struct hda_codec *codec)
4641{
4642 unsigned int pcm;
4643 int err;
4644
4645 if (!codec->num_pcms) {
4646 if (!codec->patch_ops.build_pcms)
4647 return 0;
4648 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004649 if (err < 0) {
4650 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004651 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004652 err = snd_hda_codec_reset(codec);
4653 if (err < 0) {
4654 printk(KERN_ERR
4655 "hda_codec: cannot revert codec\n");
4656 return err;
4657 }
4658 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004659 }
4660 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4661 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4662 int dev;
4663
4664 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004665 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004666
4667 if (!cpcm->pcm) {
4668 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4669 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004670 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004671 cpcm->device = dev;
4672 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004673 if (err < 0) {
4674 printk(KERN_ERR "hda_codec: cannot attach "
4675 "PCM stream %d for codec #%d\n",
4676 dev, codec->addr);
4677 continue; /* no fatal error */
4678 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004679 }
4680 }
4681 return 0;
4682}
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684/**
4685 * snd_hda_build_pcms - build PCM information
4686 * @bus: the BUS
4687 *
4688 * Create PCM information for each codec included in the bus.
4689 *
4690 * The build_pcms codec patch is requested to set up codec->num_pcms and
4691 * codec->pcm_info properly. The array is referred by the top-level driver
4692 * to create its PCM instances.
4693 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4694 * callback.
4695 *
4696 * At least, substreams, channels_min and channels_max must be filled for
4697 * each stream. substreams = 0 indicates that the stream doesn't exist.
4698 * When rates and/or formats are zero, the supported values are queried
4699 * from the given nid. The nid is used also by the default ops.prepare
4700 * and ops.cleanup callbacks.
4701 *
4702 * The driver needs to call ops.open in its open callback. Similarly,
4703 * ops.close is supposed to be called in the close callback.
4704 * ops.prepare should be called in the prepare or hw_params callback
4705 * with the proper parameters for set up.
4706 * ops.cleanup should be called in hw_free for clean up of streams.
4707 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004708 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004710int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004712 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Takashi Iwai0ba21762007-04-16 11:29:14 +02004714 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004715 int err = snd_hda_codec_build_pcms(codec);
4716 if (err < 0)
4717 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
4719 return 0;
4720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004721EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723/**
4724 * snd_hda_check_board_config - compare the current codec with the config table
4725 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004726 * @num_configs: number of config enums
4727 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 * @tbl: configuration table, terminated by null entries
4729 *
4730 * Compares the modelname or PCI subsystem id of the current codec with the
4731 * given configuration table. If a matching entry is found, returns its
4732 * config value (supposed to be 0 or positive).
4733 *
4734 * If no entries are matching, the function returns a negative value.
4735 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004736int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004737 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004738 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004740 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004741 int i;
4742 for (i = 0; i < num_configs; i++) {
4743 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004744 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004745 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4746 "selected\n", models[i]);
4747 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749 }
4750 }
4751
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004752 if (!codec->bus->pci || !tbl)
4753 return -1;
4754
4755 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4756 if (!tbl)
4757 return -1;
4758 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004759#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004760 char tmp[10];
4761 const char *model = NULL;
4762 if (models)
4763 model = models[tbl->value];
4764 if (!model) {
4765 sprintf(tmp, "#%d", tbl->value);
4766 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004768 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4769 "for config %x:%x (%s)\n",
4770 model, tbl->subvendor, tbl->subdevice,
4771 (tbl->name ? tbl->name : "Unknown device"));
4772#endif
4773 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 }
4775 return -1;
4776}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004777EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004780 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004781 subsystem ID with the
4782 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004783
4784 This is important for Gateway notebooks with SB450 HDA Audio
4785 where the vendor ID of the PCI device is:
4786 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4787 and the vendor/subvendor are found only at the codec.
4788
4789 * @codec: the HDA codec
4790 * @num_configs: number of config enums
4791 * @models: array of model name strings
4792 * @tbl: configuration table, terminated by null entries
4793 *
4794 * Compares the modelname or PCI subsystem id of the current codec with the
4795 * given configuration table. If a matching entry is found, returns its
4796 * config value (supposed to be 0 or positive).
4797 *
4798 * If no entries are matching, the function returns a negative value.
4799 */
4800int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004801 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004802 const struct snd_pci_quirk *tbl)
4803{
4804 const struct snd_pci_quirk *q;
4805
4806 /* Search for codec ID */
4807 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004808 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4809 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4810 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004811 break;
4812 }
4813
4814 if (!q->subvendor)
4815 return -1;
4816
4817 tbl = q;
4818
4819 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004820#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004821 char tmp[10];
4822 const char *model = NULL;
4823 if (models)
4824 model = models[tbl->value];
4825 if (!model) {
4826 sprintf(tmp, "#%d", tbl->value);
4827 model = tmp;
4828 }
4829 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4830 "for config %x:%x (%s)\n",
4831 model, tbl->subvendor, tbl->subdevice,
4832 (tbl->name ? tbl->name : "Unknown device"));
4833#endif
4834 return tbl->value;
4835 }
4836 return -1;
4837}
4838EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4839
4840/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 * snd_hda_add_new_ctls - create controls from the array
4842 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004843 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 *
4845 * This helper function creates and add new controls in the given array.
4846 * The array must be terminated with an empty entry as terminator.
4847 *
4848 * Returns 0 if successful, or a negative error code.
4849 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004850int snd_hda_add_new_ctls(struct hda_codec *codec,
4851 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004853 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004856 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004857 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004858 if (knew->iface == -1) /* skip this codec private value */
4859 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004860 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004861 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004862 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004863 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004864 if (addr > 0)
4865 kctl->id.device = addr;
4866 if (idx > 0)
4867 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004868 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004869 if (!err)
4870 break;
4871 /* try first with another device index corresponding to
4872 * the codec addr; if it still fails (or it's the
4873 * primary codec), then try another control index
4874 */
4875 if (!addr && codec->addr)
4876 addr = codec->addr;
4877 else if (!idx && !knew->index) {
4878 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004879 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004880 if (idx <= 0)
4881 return err;
4882 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004883 return err;
4884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 }
4886 return 0;
4887}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004888EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Takashi Iwai83012a72012-08-24 18:38:08 +02004890#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004891static void hda_power_work(struct work_struct *work)
4892{
4893 struct hda_codec *codec =
4894 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004895 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004896 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004897
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004898 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004899 if (codec->power_transition > 0) { /* during power-up sequence? */
4900 spin_unlock(&codec->power_lock);
4901 return;
4902 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004903 if (!codec->power_on || codec->power_count) {
4904 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004905 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004906 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004907 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004908 spin_unlock(&codec->power_lock);
4909
Dylan Reidd17344b2012-09-28 15:57:01 -07004910 state = hda_call_codec_suspend(codec, true);
Takashi Iwaie6bbe662013-10-24 01:20:24 +02004911 if (!codec->pm_down_notified &&
4912 !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004913 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004914 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004915 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004916}
4917
4918static void hda_keep_power_on(struct hda_codec *codec)
4919{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004920 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004921 codec->power_count++;
4922 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004923 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004924 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004925}
4926
Takashi Iwaid5191e52009-11-16 14:58:17 +01004927/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004928void snd_hda_update_power_acct(struct hda_codec *codec)
4929{
4930 unsigned long delta = jiffies - codec->power_jiffies;
4931 if (codec->power_on)
4932 codec->power_on_acct += delta;
4933 else
4934 codec->power_off_acct += delta;
4935 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004936}
4937
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004938/* Transition to powered up, if wait_power_down then wait for a pending
4939 * transition to D3 to complete. A pending D3 transition is indicated
4940 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004941/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004942static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004943{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004944 struct hda_bus *bus = codec->bus;
4945
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004946 /* Return if power_on or transitioning to power_on, unless currently
4947 * powering down. */
4948 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004949 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004950 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004951 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004952
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004953 cancel_delayed_work_sync(&codec->power_work);
4954
4955 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004956 /* If the power down delayed work was cancelled above before starting,
4957 * then there is no need to go through power up here.
4958 */
4959 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004960 if (codec->power_transition < 0)
4961 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004962 return;
4963 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004964
Takashi Iwaid66fee52011-08-02 15:39:31 +02004965 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004966 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004967 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004968 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004969 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004970 spin_unlock(&codec->power_lock);
4971
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004972 if (codec->pm_down_notified) {
4973 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004974 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004975 }
4976
Takashi Iwaicb53c622007-08-10 17:21:45 +02004977 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004978
4979 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004980 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004981}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004982
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004983#define power_save(codec) \
4984 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004985
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004986/* Transition to powered down */
4987static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004988{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004989 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004990 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004991
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004992 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004993 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004994 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004995 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004996 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004997}
4998
4999/**
5000 * snd_hda_power_save - Power-up/down/sync the codec
5001 * @codec: HD-audio codec
5002 * @delta: the counter delta to change
5003 *
5004 * Change the power-up counter via @delta, and power up or down the hardware
5005 * appropriately. For the power-down, queue to the delayed action.
5006 * Passing zero to @delta means to synchronize the power state.
5007 */
5008void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5009{
5010 spin_lock(&codec->power_lock);
5011 codec->power_count += delta;
5012 trace_hda_power_count(codec);
5013 if (delta > 0)
5014 __snd_hda_power_up(codec, d3wait);
5015 else
5016 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005017 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005018}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005019EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005020
Takashi Iwaid5191e52009-11-16 14:58:17 +01005021/**
5022 * snd_hda_check_amp_list_power - Check the amp list and update the power
5023 * @codec: HD-audio codec
5024 * @check: the object containing an AMP list and the status
5025 * @nid: NID to check / update
5026 *
5027 * Check whether the given NID is in the amp list. If it's in the list,
5028 * check the current AMP status, and update the the power-status according
5029 * to the mute status.
5030 *
5031 * This function is supposed to be set or called from the check_power_status
5032 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005033 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005034int snd_hda_check_amp_list_power(struct hda_codec *codec,
5035 struct hda_loopback_check *check,
5036 hda_nid_t nid)
5037{
Takashi Iwai031024e2011-05-02 11:29:30 +02005038 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005039 int ch, v;
5040
5041 if (!check->amplist)
5042 return 0;
5043 for (p = check->amplist; p->nid; p++) {
5044 if (p->nid == nid)
5045 break;
5046 }
5047 if (!p->nid)
5048 return 0; /* nothing changed */
5049
5050 for (p = check->amplist; p->nid; p++) {
5051 for (ch = 0; ch < 2; ch++) {
5052 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5053 p->idx);
5054 if (!(v & HDA_AMP_MUTE) && v > 0) {
5055 if (!check->power_on) {
5056 check->power_on = 1;
5057 snd_hda_power_up(codec);
5058 }
5059 return 1;
5060 }
5061 }
5062 }
5063 if (check->power_on) {
5064 check->power_on = 0;
5065 snd_hda_power_down(codec);
5066 }
5067 return 0;
5068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005069EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005072/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005073 * Channel mode helper
5074 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005075
5076/**
5077 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5078 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005079int snd_hda_ch_mode_info(struct hda_codec *codec,
5080 struct snd_ctl_elem_info *uinfo,
5081 const struct hda_channel_mode *chmode,
5082 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005083{
5084 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5085 uinfo->count = 1;
5086 uinfo->value.enumerated.items = num_chmodes;
5087 if (uinfo->value.enumerated.item >= num_chmodes)
5088 uinfo->value.enumerated.item = num_chmodes - 1;
5089 sprintf(uinfo->value.enumerated.name, "%dch",
5090 chmode[uinfo->value.enumerated.item].channels);
5091 return 0;
5092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005093EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005094
Takashi Iwaid5191e52009-11-16 14:58:17 +01005095/**
5096 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5097 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005098int snd_hda_ch_mode_get(struct hda_codec *codec,
5099 struct snd_ctl_elem_value *ucontrol,
5100 const struct hda_channel_mode *chmode,
5101 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005102 int max_channels)
5103{
5104 int i;
5105
5106 for (i = 0; i < num_chmodes; i++) {
5107 if (max_channels == chmode[i].channels) {
5108 ucontrol->value.enumerated.item[0] = i;
5109 break;
5110 }
5111 }
5112 return 0;
5113}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005114EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005115
Takashi Iwaid5191e52009-11-16 14:58:17 +01005116/**
5117 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5118 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005119int snd_hda_ch_mode_put(struct hda_codec *codec,
5120 struct snd_ctl_elem_value *ucontrol,
5121 const struct hda_channel_mode *chmode,
5122 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005123 int *max_channelsp)
5124{
5125 unsigned int mode;
5126
5127 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005128 if (mode >= num_chmodes)
5129 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005130 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005131 return 0;
5132 /* change the current channel setting */
5133 *max_channelsp = chmode[mode].channels;
5134 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005135 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005136 return 1;
5137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005138EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140/*
5141 * input MUX helper
5142 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005143
5144/**
5145 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5146 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005147int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5148 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
5150 unsigned int index;
5151
5152 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5153 uinfo->count = 1;
5154 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005155 if (!imux->num_items)
5156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 index = uinfo->value.enumerated.item;
5158 if (index >= imux->num_items)
5159 index = imux->num_items - 1;
5160 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5161 return 0;
5162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005163EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Takashi Iwaid5191e52009-11-16 14:58:17 +01005165/**
5166 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5167 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005168int snd_hda_input_mux_put(struct hda_codec *codec,
5169 const struct hda_input_mux *imux,
5170 struct snd_ctl_elem_value *ucontrol,
5171 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 unsigned int *cur_val)
5173{
5174 unsigned int idx;
5175
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005176 if (!imux->num_items)
5177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 idx = ucontrol->value.enumerated.item[0];
5179 if (idx >= imux->num_items)
5180 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005181 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005183 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5184 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 *cur_val = idx;
5186 return 1;
5187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005188EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
5190
5191/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005192 * process kcontrol info callback of a simple string enum array
5193 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5194 */
5195int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5196 struct snd_ctl_elem_info *uinfo,
5197 int num_items, const char * const *texts)
5198{
5199 static const char * const texts_default[] = {
5200 "Disabled", "Enabled"
5201 };
5202
5203 if (!texts || !num_items) {
5204 num_items = 2;
5205 texts = texts_default;
5206 }
5207
5208 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5209 uinfo->count = 1;
5210 uinfo->value.enumerated.items = num_items;
5211 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5212 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5213 strcpy(uinfo->value.enumerated.name,
5214 texts[uinfo->value.enumerated.item]);
5215 return 0;
5216}
5217EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5218
5219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 * Multi-channel / digital-out PCM helper functions
5221 */
5222
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005223/* setup SPDIF output stream */
5224static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5225 unsigned int stream_tag, unsigned int format)
5226{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005227 struct hda_spdif_out *spdif;
5228 unsigned int curr_fmt;
5229 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005230
Laurence Darby3bef1c32012-11-03 17:00:06 +00005231 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5232 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5233 AC_VERB_GET_STREAM_FORMAT, 0);
5234 reset = codec->spdif_status_reset &&
5235 (spdif->ctls & AC_DIG1_ENABLE) &&
5236 curr_fmt != format;
5237
5238 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5239 updated */
5240 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005241 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005242 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005243 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005244 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005245 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005246 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005247 for (d = codec->slave_dig_outs; *d; d++)
5248 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5249 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005250 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005251 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005252 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005253 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005254 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005255}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005256
Takashi Iwai2f728532008-09-25 16:32:41 +02005257static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5258{
5259 snd_hda_codec_cleanup_stream(codec, nid);
5260 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005261 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005262 for (d = codec->slave_dig_outs; *d; d++)
5263 snd_hda_codec_cleanup_stream(codec, *d);
5264 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005265}
5266
Takashi Iwaid5191e52009-11-16 14:58:17 +01005267/**
5268 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5269 * @bus: HD-audio bus
5270 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005271void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5272{
5273 struct hda_codec *codec;
5274
5275 if (!bus)
5276 return;
5277 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005278 if (hda_codec_is_power_on(codec) &&
5279 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005280 codec->patch_ops.reboot_notify(codec);
5281 }
5282}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005283EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005284
Takashi Iwaid5191e52009-11-16 14:58:17 +01005285/**
5286 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005288int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5289 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Ingo Molnar62932df2006-01-16 16:34:20 +01005291 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005292 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5293 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005294 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005296 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 return 0;
5298}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005299EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Takashi Iwaid5191e52009-11-16 14:58:17 +01005301/**
5302 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5303 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005304int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5305 struct hda_multi_out *mout,
5306 unsigned int stream_tag,
5307 unsigned int format,
5308 struct snd_pcm_substream *substream)
5309{
5310 mutex_lock(&codec->spdif_mutex);
5311 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5312 mutex_unlock(&codec->spdif_mutex);
5313 return 0;
5314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005315EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005316
Takashi Iwaid5191e52009-11-16 14:58:17 +01005317/**
5318 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5319 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005320int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5321 struct hda_multi_out *mout)
5322{
5323 mutex_lock(&codec->spdif_mutex);
5324 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5325 mutex_unlock(&codec->spdif_mutex);
5326 return 0;
5327}
5328EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5329
Takashi Iwaid5191e52009-11-16 14:58:17 +01005330/**
5331 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005333int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5334 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Ingo Molnar62932df2006-01-16 16:34:20 +01005336 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005338 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 return 0;
5340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005341EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
Takashi Iwaid5191e52009-11-16 14:58:17 +01005343/**
5344 * snd_hda_multi_out_analog_open - open analog outputs
5345 *
5346 * Open analog outputs and set up the hw-constraints.
5347 * If the digital outputs can be opened as slave, open the digital
5348 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005350int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5351 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005352 struct snd_pcm_substream *substream,
5353 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
Takashi Iwai9a081602008-02-12 18:37:26 +01005355 struct snd_pcm_runtime *runtime = substream->runtime;
5356 runtime->hw.channels_max = mout->max_channels;
5357 if (mout->dig_out_nid) {
5358 if (!mout->analog_rates) {
5359 mout->analog_rates = hinfo->rates;
5360 mout->analog_formats = hinfo->formats;
5361 mout->analog_maxbps = hinfo->maxbps;
5362 } else {
5363 runtime->hw.rates = mout->analog_rates;
5364 runtime->hw.formats = mout->analog_formats;
5365 hinfo->maxbps = mout->analog_maxbps;
5366 }
5367 if (!mout->spdif_rates) {
5368 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5369 &mout->spdif_rates,
5370 &mout->spdif_formats,
5371 &mout->spdif_maxbps);
5372 }
5373 mutex_lock(&codec->spdif_mutex);
5374 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005375 if ((runtime->hw.rates & mout->spdif_rates) &&
5376 (runtime->hw.formats & mout->spdif_formats)) {
5377 runtime->hw.rates &= mout->spdif_rates;
5378 runtime->hw.formats &= mout->spdif_formats;
5379 if (mout->spdif_maxbps < hinfo->maxbps)
5380 hinfo->maxbps = mout->spdif_maxbps;
5381 } else {
5382 mout->share_spdif = 0;
5383 /* FIXME: need notify? */
5384 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005385 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005386 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5389 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5390}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005391EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Takashi Iwaid5191e52009-11-16 14:58:17 +01005393/**
5394 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5395 *
5396 * Set up the i/o for analog out.
5397 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005399int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5400 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 unsigned int stream_tag,
5402 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005403 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
Takashi Iwaidda14412011-05-02 11:29:30 +02005405 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005407 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 int i;
5409
Ingo Molnar62932df2006-01-16 16:34:20 +01005410 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005411 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005412 if (mout->dig_out_nid && mout->share_spdif &&
5413 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005415 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5416 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005417 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005419 setup_dig_out_stream(codec, mout->dig_out_nid,
5420 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 } else {
5422 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005423 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 }
5425 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005426 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005429 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5430 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005431 if (!mout->no_share_stream &&
5432 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005434 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5435 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005436 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005437 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5438 if (!mout->no_share_stream && mout->hp_out_nid[i])
5439 snd_hda_codec_setup_stream(codec,
5440 mout->hp_out_nid[i],
5441 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 /* surrounds */
5444 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005445 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005446 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5447 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005448 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005449 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5450 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005452
5453 /* extra surrounds */
5454 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5455 int ch = 0;
5456 if (!mout->extra_out_nid[i])
5457 break;
5458 if (chs >= (i + 1) * 2)
5459 ch = i * 2;
5460 else if (!mout->no_share_stream)
5461 break;
5462 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5463 stream_tag, ch, format);
5464 }
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 return 0;
5467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005468EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Takashi Iwaid5191e52009-11-16 14:58:17 +01005470/**
5471 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005473int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5474 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
Takashi Iwaidda14412011-05-02 11:29:30 +02005476 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 int i;
5478
5479 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005480 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005482 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005483 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5484 if (mout->hp_out_nid[i])
5485 snd_hda_codec_cleanup_stream(codec,
5486 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005487 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5488 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005489 snd_hda_codec_cleanup_stream(codec,
5490 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005491 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005493 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 mout->dig_out_used = 0;
5495 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005496 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 return 0;
5498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005499EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Takashi Iwai47408602012-04-20 13:06:53 +02005501/**
5502 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5503 *
5504 * Guess the suitable VREF pin bits to be set as the pin-control value.
5505 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5506 */
5507unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5508{
5509 unsigned int pincap;
5510 unsigned int oldval;
5511 oldval = snd_hda_codec_read(codec, pin, 0,
5512 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5513 pincap = snd_hda_query_pin_caps(codec, pin);
5514 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5515 /* Exception: if the default pin setup is vref50, we give it priority */
5516 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5517 return AC_PINCTL_VREF_80;
5518 else if (pincap & AC_PINCAP_VREF_50)
5519 return AC_PINCTL_VREF_50;
5520 else if (pincap & AC_PINCAP_VREF_100)
5521 return AC_PINCTL_VREF_100;
5522 else if (pincap & AC_PINCAP_VREF_GRD)
5523 return AC_PINCTL_VREF_GRD;
5524 return AC_PINCTL_VREF_HIZ;
5525}
5526EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5527
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005528/* correct the pin ctl value for matching with the pin cap */
5529unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5530 hda_nid_t pin, unsigned int val)
5531{
5532 static unsigned int cap_lists[][2] = {
5533 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5534 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5535 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5536 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5537 };
5538 unsigned int cap;
5539
5540 if (!val)
5541 return 0;
5542 cap = snd_hda_query_pin_caps(codec, pin);
5543 if (!cap)
5544 return val; /* don't know what to do... */
5545
5546 if (val & AC_PINCTL_OUT_EN) {
5547 if (!(cap & AC_PINCAP_OUT))
5548 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5549 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5550 val &= ~AC_PINCTL_HP_EN;
5551 }
5552
5553 if (val & AC_PINCTL_IN_EN) {
5554 if (!(cap & AC_PINCAP_IN))
5555 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5556 else {
5557 unsigned int vcap, vref;
5558 int i;
5559 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5560 vref = val & AC_PINCTL_VREFEN;
5561 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5562 if (vref == cap_lists[i][0] &&
5563 !(vcap & cap_lists[i][1])) {
5564 if (i == ARRAY_SIZE(cap_lists) - 1)
5565 vref = AC_PINCTL_VREF_HIZ;
5566 else
5567 vref = cap_lists[i + 1][0];
5568 }
5569 }
5570 val &= ~AC_PINCTL_VREFEN;
5571 val |= vref;
5572 }
5573 }
5574
5575 return val;
5576}
5577EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5578
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005579int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5580 unsigned int val, bool cached)
5581{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005582 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005583 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005584 if (cached)
5585 return snd_hda_codec_update_cache(codec, pin, 0,
5586 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5587 else
5588 return snd_hda_codec_write(codec, pin, 0,
5589 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5590}
5591EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5592
Takashi Iwai990061c2010-09-09 22:08:44 +02005593/**
5594 * snd_hda_add_imux_item - Add an item to input_mux
5595 *
5596 * When the same label is used already in the existing items, the number
5597 * suffix is appended to the label. This label index number is stored
5598 * to type_idx when non-NULL pointer is given.
5599 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005600int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5601 int index, int *type_idx)
5602{
5603 int i, label_idx = 0;
5604 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5605 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5606 return -EINVAL;
5607 }
5608 for (i = 0; i < imux->num_items; i++) {
5609 if (!strncmp(label, imux->items[i].label, strlen(label)))
5610 label_idx++;
5611 }
5612 if (type_idx)
5613 *type_idx = label_idx;
5614 if (label_idx > 0)
5615 snprintf(imux->items[imux->num_items].label,
5616 sizeof(imux->items[imux->num_items].label),
5617 "%s %d", label, label_idx);
5618 else
5619 strlcpy(imux->items[imux->num_items].label, label,
5620 sizeof(imux->items[imux->num_items].label));
5621 imux->items[imux->num_items].index = index;
5622 imux->num_items++;
5623 return 0;
5624}
5625EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005626
Takashi Iwai4a471b72005-12-07 13:56:29 +01005627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628#ifdef CONFIG_PM
5629/*
5630 * power management
5631 */
5632
5633/**
5634 * snd_hda_suspend - suspend the codecs
5635 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 *
5637 * Returns 0 if successful.
5638 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005639int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005641 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Takashi Iwai0ba21762007-04-16 11:29:14 +02005643 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005644 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005645 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005646 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648 return 0;
5649}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005650EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
5652/**
5653 * snd_hda_resume - resume the codecs
5654 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 *
5656 * Returns 0 if successful.
5657 */
5658int snd_hda_resume(struct hda_bus *bus)
5659{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005660 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
Takashi Iwai0ba21762007-04-16 11:29:14 +02005662 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005663 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 return 0;
5666}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005667EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005668#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005669
5670/*
5671 * generic arrays
5672 */
5673
Takashi Iwaid5191e52009-11-16 14:58:17 +01005674/**
5675 * snd_array_new - get a new element from the given array
5676 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005677 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005678 * Get a new element from the given array. If it exceeds the
5679 * pre-allocated array size, re-allocate the array.
5680 *
5681 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005682 */
5683void *snd_array_new(struct snd_array *array)
5684{
Takashi Iwai12f17712012-10-10 08:59:14 +02005685 if (snd_BUG_ON(!array->elem_size))
5686 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005687 if (array->used >= array->alloced) {
5688 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005689 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005690 void *nlist;
5691 if (snd_BUG_ON(num >= 4096))
5692 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005693 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005694 if (!nlist)
5695 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005696 array->list = nlist;
5697 array->alloced = num;
5698 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005699 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005701EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005702
Takashi Iwaid5191e52009-11-16 14:58:17 +01005703/**
5704 * snd_array_free - free the given array elements
5705 * @array: the array object
5706 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005707void snd_array_free(struct snd_array *array)
5708{
5709 kfree(array->list);
5710 array->used = 0;
5711 array->alloced = 0;
5712 array->list = NULL;
5713}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005714EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005715
Takashi Iwaid5191e52009-11-16 14:58:17 +01005716/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005717 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5718 * @pcm: PCM caps bits
5719 * @buf: the string buffer to write
5720 * @buflen: the max buffer length
5721 *
5722 * used by hda_proc.c and hda_eld.c
5723 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005724void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5725{
5726 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5727 int i, j;
5728
5729 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5730 if (pcm & (AC_SUPPCM_BITS_8 << i))
5731 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5732
5733 buf[j] = '\0'; /* necessary when j == 0 */
5734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005735EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005736
5737MODULE_DESCRIPTION("HDA codec core");
5738MODULE_LICENSE("GPL");