blob: de1a7670ba0dfe5f165277170a19aae9004a5c4b [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 Iwai63e51fd72013-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 Iwai63e51fd72013-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 Iwai82467612007-07-27 19:15:54 +0200948 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100949 return NULL; /* use the generic parser */
950
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100951 again:
952 mutex_lock(&preset_mutex);
953 list_for_each_entry(tbl, &hda_preset_tables, list) {
954 if (!try_module_get(tbl->owner)) {
955 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
956 continue;
957 }
958 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100960 if (preset->afg && preset->afg != codec->afg)
961 continue;
962 if (preset->mfg && preset->mfg != codec->mfg)
963 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200964 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200966 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200967 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100968 preset->rev == codec->revision_id)) {
969 mutex_unlock(&preset_mutex);
970 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100974 module_put(tbl->owner);
975 }
976 mutex_unlock(&preset_mutex);
977
978 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
979 char name[32];
980 if (!mod_requested)
981 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
982 codec->vendor_id);
983 else
984 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
985 (codec->vendor_id >> 16) & 0xffff);
986 request_module(name);
987 mod_requested++;
988 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 return NULL;
991}
992
993/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200994 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200996static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 const struct hda_vendor_id *c;
999 const char *vendor = NULL;
1000 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001001 char tmp[16];
1002
1003 if (codec->vendor_name)
1004 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 for (c = hda_vendor_ids; c->id; c++) {
1007 if (c->id == vendor_id) {
1008 vendor = c->name;
1009 break;
1010 }
1011 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sprintf(tmp, "Generic %04x", vendor_id);
1014 vendor = tmp;
1015 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001016 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1017 if (!codec->vendor_name)
1018 return -ENOMEM;
1019
1020 get_chip_name:
1021 if (codec->chip_name)
1022 return 0;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001025 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1026 else {
1027 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1028 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1029 }
1030 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001031 return -ENOMEM;
1032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001036 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001038static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001040 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 hda_nid_t nid;
1042
1043 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1044 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001045 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001046 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001047 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001048 case AC_GRP_AUDIO_FUNCTION:
1049 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001050 codec->afg_function_id = function_id & 0xff;
1051 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001052 break;
1053 case AC_GRP_MODEM_FUNCTION:
1054 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001055 codec->mfg_function_id = function_id & 0xff;
1056 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001057 break;
1058 default:
1059 break;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001065 * read widget caps for each widget and store in cache
1066 */
1067static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1068{
1069 int i;
1070 hda_nid_t nid;
1071
1072 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1073 &codec->start_nid);
1074 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001075 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001076 return -ENOMEM;
1077 nid = codec->start_nid;
1078 for (i = 0; i < codec->num_nodes; i++, nid++)
1079 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1080 AC_PAR_AUDIO_WIDGET_CAP);
1081 return 0;
1082}
1083
Takashi Iwai3be14142009-02-20 14:11:16 +01001084/* read all pin default configurations and save codec->init_pins */
1085static int read_pin_defaults(struct hda_codec *codec)
1086{
1087 int i;
1088 hda_nid_t nid = codec->start_nid;
1089
1090 for (i = 0; i < codec->num_nodes; i++, nid++) {
1091 struct hda_pincfg *pin;
1092 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001093 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001094 if (wid_type != AC_WID_PIN)
1095 continue;
1096 pin = snd_array_new(&codec->init_pins);
1097 if (!pin)
1098 return -ENOMEM;
1099 pin->nid = nid;
1100 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1101 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001102 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1103 AC_VERB_GET_PIN_WIDGET_CONTROL,
1104 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001105 }
1106 return 0;
1107}
1108
1109/* look up the given pin config list and return the item matching with NID */
1110static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1111 struct snd_array *array,
1112 hda_nid_t nid)
1113{
1114 int i;
1115 for (i = 0; i < array->used; i++) {
1116 struct hda_pincfg *pin = snd_array_elem(array, i);
1117 if (pin->nid == nid)
1118 return pin;
1119 }
1120 return NULL;
1121}
1122
Takashi Iwai3be14142009-02-20 14:11:16 +01001123/* set the current pin config value for the given NID.
1124 * the value is cached, and read via snd_hda_codec_get_pincfg()
1125 */
1126int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1127 hda_nid_t nid, unsigned int cfg)
1128{
1129 struct hda_pincfg *pin;
1130
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001131 /* the check below may be invalid when pins are added by a fixup
1132 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1133 * for now
1134 */
1135 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001136 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1137 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001138 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001139
Takashi Iwai3be14142009-02-20 14:11:16 +01001140 pin = look_up_pincfg(codec, list, nid);
1141 if (!pin) {
1142 pin = snd_array_new(list);
1143 if (!pin)
1144 return -ENOMEM;
1145 pin->nid = nid;
1146 }
1147 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001148 return 0;
1149}
1150
Takashi Iwaid5191e52009-11-16 14:58:17 +01001151/**
1152 * snd_hda_codec_set_pincfg - Override a pin default configuration
1153 * @codec: the HDA codec
1154 * @nid: NID to set the pin config
1155 * @cfg: the pin default config value
1156 *
1157 * Override a pin default configuration value in the cache.
1158 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1159 * priority than the real hardware value.
1160 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001161int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1162 hda_nid_t nid, unsigned int cfg)
1163{
Takashi Iwai346ff702009-02-23 09:42:57 +01001164 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001165}
1166EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1167
Takashi Iwaid5191e52009-11-16 14:58:17 +01001168/**
1169 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1170 * @codec: the HDA codec
1171 * @nid: NID to get the pin config
1172 *
1173 * Get the current pin config value of the given pin NID.
1174 * If the pincfg value is cached or overridden via sysfs or driver,
1175 * returns the cached value.
1176 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001177unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1178{
1179 struct hda_pincfg *pin;
1180
Takashi Iwai3be14142009-02-20 14:11:16 +01001181#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001182 {
1183 unsigned int cfg = 0;
1184 mutex_lock(&codec->user_mutex);
1185 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1186 if (pin)
1187 cfg = pin->cfg;
1188 mutex_unlock(&codec->user_mutex);
1189 if (cfg)
1190 return cfg;
1191 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001192#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001193 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1194 if (pin)
1195 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001196 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1197 if (pin)
1198 return pin->cfg;
1199 return 0;
1200}
1201EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1202
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001203/* remember the current pinctl target value */
1204int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1205 unsigned int val)
1206{
1207 struct hda_pincfg *pin;
1208
1209 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1210 if (!pin)
1211 return -EINVAL;
1212 pin->target = val;
1213 return 0;
1214}
1215EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1216
1217/* return the current pinctl target value */
1218int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1219{
1220 struct hda_pincfg *pin;
1221
1222 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1223 if (!pin)
1224 return 0;
1225 return pin->target;
1226}
1227EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1228
Takashi Iwai92ee6162009-12-27 11:18:59 +01001229/**
1230 * snd_hda_shutup_pins - Shut up all pins
1231 * @codec: the HDA codec
1232 *
1233 * Clear all pin controls to shup up before suspend for avoiding click noise.
1234 * The controls aren't cached so that they can be resumed properly.
1235 */
1236void snd_hda_shutup_pins(struct hda_codec *codec)
1237{
1238 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001239 /* don't shut up pins when unloading the driver; otherwise it breaks
1240 * the default pin setup at the next load of the driver
1241 */
1242 if (codec->bus->shutdown)
1243 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001244 for (i = 0; i < codec->init_pins.used; i++) {
1245 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1246 /* use read here for syncing after issuing each verb */
1247 snd_hda_codec_read(codec, pin->nid, 0,
1248 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1249 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001250 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001251}
1252EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1253
Takashi Iwai2a439522011-07-26 09:52:50 +02001254#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001255/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1256static void restore_shutup_pins(struct hda_codec *codec)
1257{
1258 int i;
1259 if (!codec->pins_shutup)
1260 return;
1261 if (codec->bus->shutdown)
1262 return;
1263 for (i = 0; i < codec->init_pins.used; i++) {
1264 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1265 snd_hda_codec_write(codec, pin->nid, 0,
1266 AC_VERB_SET_PIN_WIDGET_CONTROL,
1267 pin->ctrl);
1268 }
1269 codec->pins_shutup = 0;
1270}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001271#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001272
David Henningsson26a6cb62012-10-09 15:04:21 +02001273static void hda_jackpoll_work(struct work_struct *work)
1274{
1275 struct hda_codec *codec =
1276 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001277
1278 snd_hda_jack_set_dirty_all(codec);
1279 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001280
1281 if (!codec->jackpoll_interval)
1282 return;
1283
David Henningsson26a6cb62012-10-09 15:04:21 +02001284 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1285 codec->jackpoll_interval);
1286}
1287
Takashi Iwai01751f52007-08-10 16:59:39 +02001288static void init_hda_cache(struct hda_cache_rec *cache,
1289 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001290static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001291
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001292/* release all pincfg lists */
1293static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001294{
Takashi Iwai346ff702009-02-23 09:42:57 +01001295 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001296#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001297 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001298#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001299 snd_array_free(&codec->init_pins);
1300}
1301
Takashi Iwai54d17402005-11-21 16:33:22 +01001302/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001303 * audio-converter setup caches
1304 */
1305struct hda_cvt_setup {
1306 hda_nid_t nid;
1307 u8 stream_tag;
1308 u8 channel_id;
1309 u16 format_id;
1310 unsigned char active; /* cvt is currently used */
1311 unsigned char dirty; /* setups should be cleared */
1312};
1313
1314/* get or create a cache entry for the given audio converter NID */
1315static struct hda_cvt_setup *
1316get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1317{
1318 struct hda_cvt_setup *p;
1319 int i;
1320
1321 for (i = 0; i < codec->cvt_setups.used; i++) {
1322 p = snd_array_elem(&codec->cvt_setups, i);
1323 if (p->nid == nid)
1324 return p;
1325 }
1326 p = snd_array_new(&codec->cvt_setups);
1327 if (p)
1328 p->nid = nid;
1329 return p;
1330}
1331
1332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * codec destructor
1334 */
1335static void snd_hda_codec_free(struct hda_codec *codec)
1336{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001337 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001339 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001340 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001341 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001342#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001343 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001344 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001347 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001348 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001349 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001350 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001351 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 codec->bus->caddr_tbl[codec->addr] = NULL;
1353 if (codec->patch_ops.free)
1354 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001355#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001356 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001357 hda_call_pm_notify(codec->bus, false);
1358#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001359 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001360 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001361 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 kfree(codec->vendor_name);
1363 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001364 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001365 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 kfree(codec);
1367}
1368
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001369static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1370 hda_nid_t fg, unsigned int power_state);
1371
Takashi Iwaid8193872012-08-31 07:54:38 -07001372static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001373 unsigned int power_state);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/**
1376 * snd_hda_codec_new - create a HDA codec
1377 * @bus: the bus to assign
1378 * @codec_addr: the codec address
1379 * @codecp: the pointer to store the generated codec
1380 *
1381 * Returns 0 if successful, or a negative error code.
1382 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001383int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001384 unsigned int codec_addr,
1385 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001388 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001389 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int err;
1391
Takashi Iwaida3cec32008-08-08 17:12:14 +02001392 if (snd_BUG_ON(!bus))
1393 return -EINVAL;
1394 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 snd_printk(KERN_ERR "hda_codec: "
1399 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -EBUSY;
1401 }
1402
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001403 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (codec == NULL) {
1405 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1406 return -ENOMEM;
1407 }
1408
1409 codec->bus = bus;
1410 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001411 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001412 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001413 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001414 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001415 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001416 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1417 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001418 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001419 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001420 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001421 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001422 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001423 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001424 INIT_LIST_HEAD(&codec->conn_list);
1425
David Henningsson26a6cb62012-10-09 15:04:21 +02001426 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Takashi Iwai83012a72012-08-24 18:38:08 +02001428#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001429 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001430 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1431 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1432 * the caller has to power down appropriatley after initialization
1433 * phase.
1434 */
1435 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001436 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001437#endif
1438
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001439 if (codec->bus->modelname) {
1440 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1441 if (!codec->modelname) {
1442 snd_hda_codec_free(codec);
1443 return -ENODEV;
1444 }
1445 }
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 list_add_tail(&codec->list, &bus->codec_list);
1448 bus->caddr_tbl[codec_addr] = codec;
1449
Takashi Iwai0ba21762007-04-16 11:29:14 +02001450 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1451 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001452 if (codec->vendor_id == -1)
1453 /* read again, hopefully the access method was corrected
1454 * in the last read...
1455 */
1456 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1457 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1459 AC_PAR_SUBSYSTEM_ID);
1460 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1461 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001463 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001464 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001465 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001466 err = -ENODEV;
1467 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
Takashi Iwaid8193872012-08-31 07:54:38 -07001470 fg = codec->afg ? codec->afg : codec->mfg;
1471 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001472 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001473 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001474 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001475 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001476 err = read_pin_defaults(codec);
1477 if (err < 0)
1478 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001479
Takashi Iwai0ba21762007-04-16 11:29:14 +02001480 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001481 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001482 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001483 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001484 }
1485
Takashi Iwai83012a72012-08-24 18:38:08 +02001486#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001487 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001488 AC_PWRST_CLKSTOP);
1489 if (!codec->d3_stop_clk)
1490 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001491#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001492 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001493 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001494
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001495 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001496 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001497
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001498 snd_hda_codec_proc_new(codec);
1499
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001500 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001501
1502 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1503 codec->subsystem_id, codec->revision_id);
1504 snd_component_add(codec->bus->card, component);
1505
1506 if (codecp)
1507 *codecp = codec;
1508 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001509
1510 error:
1511 snd_hda_codec_free(codec);
1512 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001513}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001514EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001515
Mengdong Lina15d05d2013-02-08 17:09:31 -05001516int snd_hda_codec_update_widgets(struct hda_codec *codec)
1517{
1518 hda_nid_t fg;
1519 int err;
1520
1521 /* Assume the function group node does not change,
1522 * only the widget nodes may change.
1523 */
1524 kfree(codec->wcaps);
1525 fg = codec->afg ? codec->afg : codec->mfg;
1526 err = read_widget_caps(codec, fg);
1527 if (err < 0) {
1528 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1529 return err;
1530 }
1531
1532 snd_array_free(&codec->init_pins);
1533 err = read_pin_defaults(codec);
1534
1535 return err;
1536}
1537EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1538
1539
Takashi Iwaid5191e52009-11-16 14:58:17 +01001540/**
1541 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1542 * @codec: the HDA codec
1543 *
1544 * Start parsing of the given codec tree and (re-)initialize the whole
1545 * patch instance.
1546 *
1547 * Returns 0 if successful or a negative error code.
1548 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001549int snd_hda_codec_configure(struct hda_codec *codec)
1550{
1551 int err;
1552
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001553 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001554 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001555 err = get_codec_name(codec);
1556 if (err < 0)
1557 return err;
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Takashi Iwai82467612007-07-27 19:15:54 +02001560 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001562 goto patched;
1563 }
Takashi Iwai82467612007-07-27 19:15:54 +02001564 if (codec->preset && codec->preset->patch) {
1565 err = codec->preset->patch(codec);
1566 goto patched;
1567 }
1568
1569 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001570 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001571 if (err < 0)
1572 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001573
1574 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001575 if (!err && codec->patch_ops.unsol_event)
1576 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001577 /* audio codec should override the mixer name */
1578 if (!err && (codec->afg || !*codec->bus->card->mixername))
1579 snprintf(codec->bus->card->mixername,
1580 sizeof(codec->bus->card->mixername),
1581 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001584EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Takashi Iwaied360812012-08-08 17:12:52 +02001586/* update the stream-id if changed */
1587static void update_pcm_stream_id(struct hda_codec *codec,
1588 struct hda_cvt_setup *p, hda_nid_t nid,
1589 u32 stream_tag, int channel_id)
1590{
1591 unsigned int oldval, newval;
1592
1593 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1594 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1595 newval = (stream_tag << 4) | channel_id;
1596 if (oldval != newval)
1597 snd_hda_codec_write(codec, nid, 0,
1598 AC_VERB_SET_CHANNEL_STREAMID,
1599 newval);
1600 p->stream_tag = stream_tag;
1601 p->channel_id = channel_id;
1602 }
1603}
1604
1605/* update the format-id if changed */
1606static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1607 hda_nid_t nid, int format)
1608{
1609 unsigned int oldval;
1610
1611 if (p->format_id != format) {
1612 oldval = snd_hda_codec_read(codec, nid, 0,
1613 AC_VERB_GET_STREAM_FORMAT, 0);
1614 if (oldval != format) {
1615 msleep(1);
1616 snd_hda_codec_write(codec, nid, 0,
1617 AC_VERB_SET_STREAM_FORMAT,
1618 format);
1619 }
1620 p->format_id = format;
1621 }
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624/**
1625 * snd_hda_codec_setup_stream - set up the codec for streaming
1626 * @codec: the CODEC to set up
1627 * @nid: the NID to set up
1628 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1629 * @channel_id: channel id to pass, zero based.
1630 * @format: stream format.
1631 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001632void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1633 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int channel_id, int format)
1635{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001636 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001637 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001638 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001639 int i;
1640
Takashi Iwai0ba21762007-04-16 11:29:14 +02001641 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001642 return;
1643
Takashi Iwai0ba21762007-04-16 11:29:14 +02001644 snd_printdd("hda_codec_setup_stream: "
1645 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001647 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001648 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001649 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001650
1651 if (codec->pcm_format_first)
1652 update_pcm_format(codec, p, nid, format);
1653 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1654 if (!codec->pcm_format_first)
1655 update_pcm_format(codec, p, nid, format);
1656
Takashi Iwaieb541332010-08-06 13:48:11 +02001657 p->active = 1;
1658 p->dirty = 0;
1659
1660 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001661 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001662 list_for_each_entry(c, &codec->bus->codec_list, list) {
1663 for (i = 0; i < c->cvt_setups.used; i++) {
1664 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001665 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001666 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001667 p->dirty = 1;
1668 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001671EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Takashi Iwaif0cea792010-08-13 11:56:53 +02001673static void really_cleanup_stream(struct hda_codec *codec,
1674 struct hda_cvt_setup *q);
1675
Takashi Iwaid5191e52009-11-16 14:58:17 +01001676/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001677 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001678 * @codec: the CODEC to clean up
1679 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001680 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001681 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001682void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1683 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001684{
Takashi Iwaieb541332010-08-06 13:48:11 +02001685 struct hda_cvt_setup *p;
1686
Takashi Iwai888afa12008-03-18 09:57:50 +01001687 if (!nid)
1688 return;
1689
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001690 if (codec->no_sticky_stream)
1691 do_now = 1;
1692
Takashi Iwai888afa12008-03-18 09:57:50 +01001693 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001694 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001695 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001696 /* here we just clear the active flag when do_now isn't set;
1697 * actual clean-ups will be done later in
1698 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1699 */
1700 if (do_now)
1701 really_cleanup_stream(codec, p);
1702 else
1703 p->active = 0;
1704 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001705}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001706EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001707
Takashi Iwaieb541332010-08-06 13:48:11 +02001708static void really_cleanup_stream(struct hda_codec *codec,
1709 struct hda_cvt_setup *q)
1710{
1711 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001712 if (q->stream_tag || q->channel_id)
1713 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1714 if (q->format_id)
1715 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1716);
Takashi Iwaieb541332010-08-06 13:48:11 +02001717 memset(q, 0, sizeof(*q));
1718 q->nid = nid;
1719}
1720
1721/* clean up the all conflicting obsolete streams */
1722static void purify_inactive_streams(struct hda_codec *codec)
1723{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001724 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001725 int i;
1726
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001727 list_for_each_entry(c, &codec->bus->codec_list, list) {
1728 for (i = 0; i < c->cvt_setups.used; i++) {
1729 struct hda_cvt_setup *p;
1730 p = snd_array_elem(&c->cvt_setups, i);
1731 if (p->dirty)
1732 really_cleanup_stream(c, p);
1733 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001734 }
1735}
1736
Takashi Iwai2a439522011-07-26 09:52:50 +02001737#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001738/* clean up all streams; called from suspend */
1739static void hda_cleanup_all_streams(struct hda_codec *codec)
1740{
1741 int i;
1742
1743 for (i = 0; i < codec->cvt_setups.used; i++) {
1744 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1745 if (p->stream_tag)
1746 really_cleanup_stream(codec, p);
1747 }
1748}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001749#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/*
1752 * amp access functions
1753 */
1754
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001755/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001756#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001757#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001758#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1759#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001761#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001764static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001765 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Takashi Iwai01751f52007-08-10 16:59:39 +02001767 memset(cache, 0, sizeof(*cache));
1768 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001769 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001770}
1771
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001772static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001773{
Takashi Iwai603c4012008-07-30 15:01:44 +02001774 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001778static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwai01751f52007-08-10 16:59:39 +02001780 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1781 u16 cur = cache->hash[idx];
1782 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001785 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (info->key == key)
1787 return info;
1788 cur = info->next;
1789 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001790 return NULL;
1791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001793/* query the hash. allocate an entry if not found. */
1794static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1795 u32 key)
1796{
1797 struct hda_cache_head *info = get_hash(cache, key);
1798 if (!info) {
1799 u16 idx, cur;
1800 /* add a new hash entry */
1801 info = snd_array_new(&cache->buf);
1802 if (!info)
1803 return NULL;
1804 cur = snd_array_index(&cache->buf, info);
1805 info->key = key;
1806 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001807 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001808 idx = key % (u16)ARRAY_SIZE(cache->hash);
1809 info->next = cache->hash[idx];
1810 cache->hash[idx] = cur;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return info;
1813}
1814
Takashi Iwai01751f52007-08-10 16:59:39 +02001815/* query and allocate an amp hash entry */
1816static inline struct hda_amp_info *
1817get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1818{
1819 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1820}
1821
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001822/* overwrite the value with the key in the caps hash */
1823static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1824{
1825 struct hda_amp_info *info;
1826
1827 mutex_lock(&codec->hash_mutex);
1828 info = get_alloc_amp_hash(codec, key);
1829 if (!info) {
1830 mutex_unlock(&codec->hash_mutex);
1831 return -EINVAL;
1832 }
1833 info->amp_caps = val;
1834 info->head.val |= INFO_AMP_CAPS;
1835 mutex_unlock(&codec->hash_mutex);
1836 return 0;
1837}
1838
1839/* query the value from the caps hash; if not found, fetch the current
1840 * value from the given function and store in the hash
1841 */
1842static unsigned int
1843query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1844 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1845{
1846 struct hda_amp_info *info;
1847 unsigned int val;
1848
1849 mutex_lock(&codec->hash_mutex);
1850 info = get_alloc_amp_hash(codec, key);
1851 if (!info) {
1852 mutex_unlock(&codec->hash_mutex);
1853 return 0;
1854 }
1855 if (!(info->head.val & INFO_AMP_CAPS)) {
1856 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1857 val = func(codec, nid, dir);
1858 write_caps_hash(codec, key, val);
1859 } else {
1860 val = info->amp_caps;
1861 mutex_unlock(&codec->hash_mutex);
1862 }
1863 return val;
1864}
1865
1866static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1867 int direction)
1868{
1869 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1870 nid = codec->afg;
1871 return snd_hda_param_read(codec, nid,
1872 direction == HDA_OUTPUT ?
1873 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1874}
1875
Takashi Iwaid5191e52009-11-16 14:58:17 +01001876/**
1877 * query_amp_caps - query AMP capabilities
1878 * @codec: the HD-auio codec
1879 * @nid: the NID to query
1880 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1881 *
1882 * Query AMP capabilities for the given widget and direction.
1883 * Returns the obtained capability bits.
1884 *
1885 * When cap bits have been already read, this doesn't read again but
1886 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001888u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001890 return query_caps_hash(codec, nid, direction,
1891 HDA_HASH_KEY(nid, direction, 0),
1892 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001894EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_override_amp_caps - Override the AMP capabilities
1898 * @codec: the CODEC to clean up
1899 * @nid: the NID to clean up
1900 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1901 * @caps: the capability bits to set
1902 *
1903 * Override the cached AMP caps bits value by the given one.
1904 * This function is useful if the driver needs to adjust the AMP ranges,
1905 * e.g. limit to 0dB, etc.
1906 *
1907 * Returns zero if successful or a negative error code.
1908 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001909int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1910 unsigned int caps)
1911{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001912 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001913}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001914EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001915
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001916static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1917 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001918{
1919 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1920}
1921
Takashi Iwaid5191e52009-11-16 14:58:17 +01001922/**
1923 * snd_hda_query_pin_caps - Query PIN capabilities
1924 * @codec: the HD-auio codec
1925 * @nid: the NID to query
1926 *
1927 * Query PIN capabilities for the given widget.
1928 * Returns the obtained capability bits.
1929 *
1930 * When cap bits have been already read, this doesn't read again but
1931 * returns the cached value.
1932 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001933u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1934{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001935 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001936 read_pin_cap);
1937}
Takashi Iwai1327a322009-03-23 13:07:47 +01001938EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1939
Wu Fengguang864f92b2009-11-18 12:38:02 +08001940/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001941 * snd_hda_override_pin_caps - Override the pin capabilities
1942 * @codec: the CODEC
1943 * @nid: the NID to override
1944 * @caps: the capability bits to set
1945 *
1946 * Override the cached PIN capabilitiy bits value by the given one.
1947 *
1948 * Returns zero if successful or a negative error code.
1949 */
1950int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1951 unsigned int caps)
1952{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001953 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001954}
1955EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1956
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001957/* read or sync the hash value with the current value;
1958 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001960static struct hda_amp_info *
1961update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001962 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001964 struct hda_amp_info *info;
1965 unsigned int parm, val = 0;
1966 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001968 retry:
1969 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1970 if (!info)
1971 return NULL;
1972 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1973 if (!val_read) {
1974 mutex_unlock(&codec->hash_mutex);
1975 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1976 parm |= direction == HDA_OUTPUT ?
1977 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1978 parm |= index;
1979 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001980 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001981 val &= 0xff;
1982 val_read = true;
1983 mutex_lock(&codec->hash_mutex);
1984 goto retry;
1985 }
1986 info->vol[ch] = val;
1987 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001988 } else if (init_only)
1989 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001990 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001994 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001996static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001997 hda_nid_t nid, int ch, int direction, int index,
1998 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 u32 parm;
2001
2002 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2003 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2004 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002005 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2006 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002007 ; /* set the zero value as a fake mute */
2008 else
2009 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2011}
2012
Takashi Iwaid5191e52009-11-16 14:58:17 +01002013/**
2014 * snd_hda_codec_amp_read - Read AMP value
2015 * @codec: HD-audio codec
2016 * @nid: NID to read the AMP value
2017 * @ch: channel (left=0 or right=1)
2018 * @direction: #HDA_INPUT or #HDA_OUTPUT
2019 * @index: the index value (only for input direction)
2020 *
2021 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Takashi Iwai834be882006-03-01 14:16:17 +01002023int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2024 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002026 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002027 unsigned int val = 0;
2028
2029 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002030 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002031 if (info)
2032 val = info->vol[ch];
2033 mutex_unlock(&codec->hash_mutex);
2034 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002036EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Takashi Iwai280e57d2012-12-14 10:32:21 +01002038static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2039 int direction, int idx, int mask, int val,
2040 bool init_only)
2041{
2042 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002043 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002044 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002045
2046 if (snd_BUG_ON(mask & ~0xff))
2047 mask &= 0xff;
2048 val &= mask;
2049
2050 mutex_lock(&codec->hash_mutex);
2051 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2052 if (!info) {
2053 mutex_unlock(&codec->hash_mutex);
2054 return 0;
2055 }
2056 val |= info->vol[ch] & ~mask;
2057 if (info->vol[ch] == val) {
2058 mutex_unlock(&codec->hash_mutex);
2059 return 0;
2060 }
2061 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002062 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002063 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002064 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002065 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002066 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002067 return 1;
2068}
2069
Takashi Iwaid5191e52009-11-16 14:58:17 +01002070/**
2071 * snd_hda_codec_amp_update - update the AMP value
2072 * @codec: HD-audio codec
2073 * @nid: NID to read the AMP value
2074 * @ch: channel (left=0 or right=1)
2075 * @direction: #HDA_INPUT or #HDA_OUTPUT
2076 * @idx: the index value (only for input direction)
2077 * @mask: bit mask to set
2078 * @val: the bits value to set
2079 *
2080 * Update the AMP value with a bit mask.
2081 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002082 */
Takashi Iwai834be882006-03-01 14:16:17 +01002083int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2084 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002086 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002088EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Takashi Iwaid5191e52009-11-16 14:58:17 +01002090/**
2091 * snd_hda_codec_amp_stereo - update the AMP stereo values
2092 * @codec: HD-audio codec
2093 * @nid: NID to read the AMP value
2094 * @direction: #HDA_INPUT or #HDA_OUTPUT
2095 * @idx: the index value (only for input direction)
2096 * @mask: bit mask to set
2097 * @val: the bits value to set
2098 *
2099 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2100 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002101 */
2102int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2103 int direction, int idx, int mask, int val)
2104{
2105 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002106
2107 if (snd_BUG_ON(mask & ~0xff))
2108 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002109 for (ch = 0; ch < 2; ch++)
2110 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2111 idx, mask, val);
2112 return ret;
2113}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002114EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002115
Takashi Iwai280e57d2012-12-14 10:32:21 +01002116/* Works like snd_hda_codec_amp_update() but it writes the value only at
2117 * the first access. If the amp was already initialized / updated beforehand,
2118 * this does nothing.
2119 */
2120int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2121 int dir, int idx, int mask, int val)
2122{
2123 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2124}
2125EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2126
2127int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2128 int dir, int idx, int mask, int val)
2129{
2130 int ch, ret = 0;
2131
2132 if (snd_BUG_ON(mask & ~0xff))
2133 mask &= 0xff;
2134 for (ch = 0; ch < 2; ch++)
2135 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2136 idx, mask, val);
2137 return ret;
2138}
2139EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2140
Takashi Iwaid5191e52009-11-16 14:58:17 +01002141/**
2142 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2143 * @codec: HD-audio codec
2144 *
2145 * Resume the all amp commands from the cache.
2146 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002147void snd_hda_codec_resume_amp(struct hda_codec *codec)
2148{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002149 int i;
2150
Takashi Iwaic370dd62012-12-13 18:30:04 +01002151 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002152 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002153 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2154 struct hda_amp_info *buffer;
2155 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002156 hda_nid_t nid;
2157 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002158 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002159
2160 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002161 if (!buffer->head.dirty)
2162 continue;
2163 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002164 info = *buffer;
2165 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002166 if (!key)
2167 continue;
2168 nid = key & 0xff;
2169 idx = (key >> 16) & 0xff;
2170 dir = (key >> 24) & 0xff;
2171 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002172 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002173 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002174 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002175 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2176 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002177 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002178 }
2179 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002180 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002182EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002184static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2185 unsigned int ofs)
2186{
2187 u32 caps = query_amp_caps(codec, nid, dir);
2188 /* get num steps */
2189 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2190 if (ofs < caps)
2191 caps -= ofs;
2192 return caps;
2193}
2194
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195/**
2196 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2197 *
2198 * The control element is supposed to have the private_value field
2199 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2200 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002201int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 u16 nid = get_amp_nid(kcontrol);
2206 u8 chs = get_amp_channels(kcontrol);
2207 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002208 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002210 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2211 uinfo->count = chs == 3 ? 2 : 1;
2212 uinfo->value.integer.min = 0;
2213 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2214 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002215 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002216 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2217 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return -EINVAL;
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 0;
2221}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002222EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002224
2225static inline unsigned int
2226read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2227 int ch, int dir, int idx, unsigned int ofs)
2228{
2229 unsigned int val;
2230 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2231 val &= HDA_AMP_VOLMASK;
2232 if (val >= ofs)
2233 val -= ofs;
2234 else
2235 val = 0;
2236 return val;
2237}
2238
2239static inline int
2240update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2241 int ch, int dir, int idx, unsigned int ofs,
2242 unsigned int val)
2243{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002244 unsigned int maxval;
2245
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002246 if (val > 0)
2247 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002248 /* ofs = 0: raw max value */
2249 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002250 if (val > maxval)
2251 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002252 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2253 HDA_AMP_VOLMASK, val);
2254}
2255
Takashi Iwaid5191e52009-11-16 14:58:17 +01002256/**
2257 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2258 *
2259 * The control element is supposed to have the private_value field
2260 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2261 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002262int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2266 hda_nid_t nid = get_amp_nid(kcontrol);
2267 int chs = get_amp_channels(kcontrol);
2268 int dir = get_amp_direction(kcontrol);
2269 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002270 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 long *valp = ucontrol->value.integer.value;
2272
2273 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002274 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002276 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
2278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002279EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Takashi Iwaid5191e52009-11-16 14:58:17 +01002281/**
2282 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2283 *
2284 * The control element is supposed to have the private_value field
2285 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2286 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002287int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2291 hda_nid_t nid = get_amp_nid(kcontrol);
2292 int chs = get_amp_channels(kcontrol);
2293 int dir = get_amp_direction(kcontrol);
2294 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002295 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 long *valp = ucontrol->value.integer.value;
2297 int change = 0;
2298
Takashi Iwaicb53c622007-08-10 17:21:45 +02002299 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002300 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002301 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002302 valp++;
2303 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002304 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002305 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002306 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return change;
2308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002309EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Takashi Iwaid5191e52009-11-16 14:58:17 +01002311/**
2312 * snd_hda_mixer_amp_volume_put - TLV 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 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002317int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2318 unsigned int size, unsigned int __user *_tlv)
2319{
2320 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2321 hda_nid_t nid = get_amp_nid(kcontrol);
2322 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002323 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002324 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002325 u32 caps, val1, val2;
2326
2327 if (size < 4 * sizeof(unsigned int))
2328 return -ENOMEM;
2329 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002330 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2331 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002332 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002333 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002334 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002335 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002336 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002337 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2338 return -EFAULT;
2339 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2340 return -EFAULT;
2341 if (put_user(val1, _tlv + 2))
2342 return -EFAULT;
2343 if (put_user(val2, _tlv + 3))
2344 return -EFAULT;
2345 return 0;
2346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002347EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002348
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349/**
2350 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2351 * @codec: HD-audio codec
2352 * @nid: NID of a reference widget
2353 * @dir: #HDA_INPUT or #HDA_OUTPUT
2354 * @tlv: TLV data to be stored, at least 4 elements
2355 *
2356 * Set (static) TLV data for a virtual master volume using the AMP caps
2357 * obtained from the reference NID.
2358 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002359 */
2360void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2361 unsigned int *tlv)
2362{
2363 u32 caps;
2364 int nums, step;
2365
2366 caps = query_amp_caps(codec, nid, dir);
2367 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2368 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2369 step = (step + 1) * 25;
2370 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2371 tlv[1] = 2 * sizeof(unsigned int);
2372 tlv[2] = -nums * step;
2373 tlv[3] = step;
2374}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002375EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002376
2377/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002378static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002379find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002380{
2381 struct snd_ctl_elem_id id;
2382 memset(&id, 0, sizeof(id));
2383 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002384 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002385 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002386 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2387 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002388 strcpy(id.name, name);
2389 return snd_ctl_find_id(codec->bus->card, &id);
2390}
2391
Takashi Iwaid5191e52009-11-16 14:58:17 +01002392/**
2393 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2394 * @codec: HD-audio codec
2395 * @name: ctl id name string
2396 *
2397 * Get the control element with the given id string and IFACE_MIXER.
2398 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002399struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2400 const char *name)
2401{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002402 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002404EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002405
Takashi Iwaidcda5802012-10-12 17:24:51 +02002406static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002407 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002408{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002409 int i, idx;
2410 /* 16 ctlrs should be large enough */
2411 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2412 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002413 return idx;
2414 }
2415 return -EBUSY;
2416}
2417
Takashi Iwaid5191e52009-11-16 14:58:17 +01002418/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002419 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002420 * @codec: HD-audio codec
2421 * @nid: corresponding NID (optional)
2422 * @kctl: the control element to assign
2423 *
2424 * Add the given control element to an array inside the codec instance.
2425 * All control elements belonging to a codec are supposed to be added
2426 * by this function so that a proper clean-up works at the free or
2427 * reconfiguration time.
2428 *
2429 * If non-zero @nid is passed, the NID is assigned to the control element.
2430 * The assignment is shown in the codec proc file.
2431 *
2432 * snd_hda_ctl_add() checks the control subdev id field whether
2433 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002434 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2435 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002436 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002437int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2438 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002439{
2440 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002441 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002442 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002443
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002444 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002445 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002446 if (nid == 0)
2447 nid = get_amp_nid_(kctl->private_value);
2448 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002449 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2450 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002451 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002452 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002453 err = snd_ctl_add(codec->bus->card, kctl);
2454 if (err < 0)
2455 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002456 item = snd_array_new(&codec->mixers);
2457 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002458 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002459 item->kctl = kctl;
2460 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002461 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002462 return 0;
2463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002464EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002465
Takashi Iwaid5191e52009-11-16 14:58:17 +01002466/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002467 * snd_hda_add_nid - Assign a NID to a control element
2468 * @codec: HD-audio codec
2469 * @nid: corresponding NID (optional)
2470 * @kctl: the control element to assign
2471 * @index: index to kctl
2472 *
2473 * Add the given control element to an array inside the codec instance.
2474 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2475 * NID:KCTL mapping - for example "Capture Source" selector.
2476 */
2477int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2478 unsigned int index, hda_nid_t nid)
2479{
2480 struct hda_nid_item *item;
2481
2482 if (nid > 0) {
2483 item = snd_array_new(&codec->nids);
2484 if (!item)
2485 return -ENOMEM;
2486 item->kctl = kctl;
2487 item->index = index;
2488 item->nid = nid;
2489 return 0;
2490 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002491 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2492 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002493 return -EINVAL;
2494}
2495EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2496
2497/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002498 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2499 * @codec: HD-audio codec
2500 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002501void snd_hda_ctls_clear(struct hda_codec *codec)
2502{
2503 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002504 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002505 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002506 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002507 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002508 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002509}
2510
Takashi Iwaia65d6292009-02-23 16:57:04 +01002511/* pseudo device locking
2512 * toggle card->shutdown to allow/disallow the device access (as a hack)
2513 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002514int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002515{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002516 struct snd_card *card = bus->card;
2517 struct hda_codec *codec;
2518
Takashi Iwaia65d6292009-02-23 16:57:04 +01002519 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002520 if (card->shutdown)
2521 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002522 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002523 if (!list_empty(&card->ctl_files))
2524 goto err_clear;
2525
2526 list_for_each_entry(codec, &bus->codec_list, list) {
2527 int pcm;
2528 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2529 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2530 if (!cpcm->pcm)
2531 continue;
2532 if (cpcm->pcm->streams[0].substream_opened ||
2533 cpcm->pcm->streams[1].substream_opened)
2534 goto err_clear;
2535 }
2536 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002537 spin_unlock(&card->files_lock);
2538 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002539
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002540 err_clear:
2541 card->shutdown = 0;
2542 err_unlock:
2543 spin_unlock(&card->files_lock);
2544 return -EINVAL;
2545}
2546EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2547
2548void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002549{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002550 struct snd_card *card = bus->card;
2551
2552 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002553 spin_lock(&card->files_lock);
2554 card->shutdown = 0;
2555 spin_unlock(&card->files_lock);
2556}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002557EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002558
Takashi Iwaid5191e52009-11-16 14:58:17 +01002559/**
2560 * snd_hda_codec_reset - Clear all objects assigned to the codec
2561 * @codec: HD-audio codec
2562 *
2563 * This frees the all PCM and control elements assigned to the codec, and
2564 * clears the caches and restores the pin default configurations.
2565 *
2566 * When a device is being used, it returns -EBSY. If successfully freed,
2567 * returns zero.
2568 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002569int snd_hda_codec_reset(struct hda_codec *codec)
2570{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002571 struct hda_bus *bus = codec->bus;
2572 struct snd_card *card = bus->card;
2573 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002574
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002575 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002576 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002577
2578 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002579 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002580#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002581 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002582 codec->power_on = 0;
2583 codec->power_transition = 0;
2584 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002585 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002586#endif
2587 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002588 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002589 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002590 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002591 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002592 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002593 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002594 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002595 }
2596 if (codec->patch_ops.free)
2597 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002598 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002599 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002600 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002601 codec->spec = NULL;
2602 free_hda_cache(&codec->amp_cache);
2603 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002604 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2605 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002606 /* free only driver_pins so that init_pins + user_pins are restored */
2607 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002608 snd_array_free(&codec->cvt_setups);
2609 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002610 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002611 codec->num_pcms = 0;
2612 codec->pcm_info = NULL;
2613 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002614 codec->slave_dig_outs = NULL;
2615 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002616 module_put(codec->owner);
2617 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002618
2619 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002620 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002621 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002622}
2623
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002624typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2625
2626/* apply the function to all matching slave ctls in the mixer list */
2627static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002628 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002629{
2630 struct hda_nid_item *items;
2631 const char * const *s;
2632 int i, err;
2633
2634 items = codec->mixers.list;
2635 for (i = 0; i < codec->mixers.used; i++) {
2636 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002637 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002638 continue;
2639 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002640 char tmpname[sizeof(sctl->id.name)];
2641 const char *name = *s;
2642 if (suffix) {
2643 snprintf(tmpname, sizeof(tmpname), "%s %s",
2644 name, suffix);
2645 name = tmpname;
2646 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002647 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002648 err = func(data, sctl);
2649 if (err)
2650 return err;
2651 break;
2652 }
2653 }
2654 }
2655 return 0;
2656}
2657
2658static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2659{
2660 return 1;
2661}
2662
Takashi Iwai18478e82012-03-09 17:51:10 +01002663/* guess the value corresponding to 0dB */
2664static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2665{
2666 int _tlv[4];
2667 const int *tlv = NULL;
2668 int val = -1;
2669
2670 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2671 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2672 mm_segment_t fs = get_fs();
2673 set_fs(get_ds());
2674 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2675 tlv = _tlv;
2676 set_fs(fs);
2677 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2678 tlv = kctl->tlv.p;
2679 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2680 val = -tlv[2] / tlv[3];
2681 return val;
2682}
2683
2684/* call kctl->put with the given value(s) */
2685static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2686{
2687 struct snd_ctl_elem_value *ucontrol;
2688 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2689 if (!ucontrol)
2690 return -ENOMEM;
2691 ucontrol->value.integer.value[0] = val;
2692 ucontrol->value.integer.value[1] = val;
2693 kctl->put(kctl, ucontrol);
2694 kfree(ucontrol);
2695 return 0;
2696}
2697
2698/* initialize the slave volume with 0dB */
2699static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2700{
2701 int offset = get_kctl_0dB_offset(slave);
2702 if (offset > 0)
2703 put_kctl_with_value(slave, offset);
2704 return 0;
2705}
2706
2707/* unmute the slave */
2708static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2709{
2710 return put_kctl_with_value(slave, 1);
2711}
2712
Takashi Iwaid5191e52009-11-16 14:58:17 +01002713/**
2714 * snd_hda_add_vmaster - create a virtual master control and add slaves
2715 * @codec: HD-audio codec
2716 * @name: vmaster control name
2717 * @tlv: TLV data (optional)
2718 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002719 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002720 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002721 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002722 *
2723 * Create a virtual master control with the given name. The TLV data
2724 * must be either NULL or a valid data.
2725 *
2726 * @slaves is a NULL-terminated array of strings, each of which is a
2727 * slave control name. All controls with these names are assigned to
2728 * the new virtual master control.
2729 *
2730 * This function returns zero if successful or a negative error code.
2731 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002732int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002733 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002734 const char *suffix, bool init_slave_vol,
2735 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002736{
2737 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002738 int err;
2739
Takashi Iwai29e58532012-03-12 12:25:03 +01002740 if (ctl_ret)
2741 *ctl_ret = NULL;
2742
Takashi Iwai9322ca52012-02-03 14:28:01 +01002743 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002744 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002745 snd_printdd("No slave found for %s\n", name);
2746 return 0;
2747 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002748 kctl = snd_ctl_make_virtual_master(name, tlv);
2749 if (!kctl)
2750 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002751 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002752 if (err < 0)
2753 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002754
Takashi Iwai9322ca52012-02-03 14:28:01 +01002755 err = map_slaves(codec, slaves, suffix,
2756 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002757 if (err < 0)
2758 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002759
2760 /* init with master mute & zero volume */
2761 put_kctl_with_value(kctl, 0);
2762 if (init_slave_vol)
2763 map_slaves(codec, slaves, suffix,
2764 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2765
Takashi Iwai29e58532012-03-12 12:25:03 +01002766 if (ctl_ret)
2767 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002768 return 0;
2769}
Takashi Iwai18478e82012-03-09 17:51:10 +01002770EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002771
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002772/*
2773 * mute-LED control using vmaster
2774 */
2775static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_info *uinfo)
2777{
2778 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002779 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002780 };
2781 unsigned int index;
2782
2783 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2784 uinfo->count = 1;
2785 uinfo->value.enumerated.items = 3;
2786 index = uinfo->value.enumerated.item;
2787 if (index >= 3)
2788 index = 2;
2789 strcpy(uinfo->value.enumerated.name, texts[index]);
2790 return 0;
2791}
2792
2793static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2794 struct snd_ctl_elem_value *ucontrol)
2795{
2796 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2797 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2798 return 0;
2799}
2800
2801static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2803{
2804 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2805 unsigned int old_mode = hook->mute_mode;
2806
2807 hook->mute_mode = ucontrol->value.enumerated.item[0];
2808 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2809 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2810 if (old_mode == hook->mute_mode)
2811 return 0;
2812 snd_hda_sync_vmaster_hook(hook);
2813 return 1;
2814}
2815
2816static struct snd_kcontrol_new vmaster_mute_mode = {
2817 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2818 .name = "Mute-LED Mode",
2819 .info = vmaster_mute_mode_info,
2820 .get = vmaster_mute_mode_get,
2821 .put = vmaster_mute_mode_put,
2822};
2823
2824/*
2825 * Add a mute-LED hook with the given vmaster switch kctl
2826 * "Mute-LED Mode" control is automatically created and associated with
2827 * the given hook.
2828 */
2829int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002830 struct hda_vmaster_mute_hook *hook,
2831 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002832{
2833 struct snd_kcontrol *kctl;
2834
2835 if (!hook->hook || !hook->sw_kctl)
2836 return 0;
2837 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2838 hook->codec = codec;
2839 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002840 if (!expose_enum_ctl)
2841 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002842 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2843 if (!kctl)
2844 return -ENOMEM;
2845 return snd_hda_ctl_add(codec, 0, kctl);
2846}
2847EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2848
2849/*
2850 * Call the hook with the current value for synchronization
2851 * Should be called in init callback
2852 */
2853void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2854{
2855 if (!hook->hook || !hook->codec)
2856 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002857 /* don't call vmaster hook in the destructor since it might have
2858 * been already destroyed
2859 */
2860 if (hook->codec->bus->shutdown)
2861 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002862 switch (hook->mute_mode) {
2863 case HDA_VMUTE_FOLLOW_MASTER:
2864 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2865 break;
2866 default:
2867 hook->hook(hook->codec, hook->mute_mode);
2868 break;
2869 }
2870}
2871EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2872
2873
Takashi Iwaid5191e52009-11-16 14:58:17 +01002874/**
2875 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2876 *
2877 * The control element is supposed to have the private_value field
2878 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2879 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 int chs = get_amp_channels(kcontrol);
2884
2885 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2886 uinfo->count = chs == 3 ? 2 : 1;
2887 uinfo->value.integer.min = 0;
2888 uinfo->value.integer.max = 1;
2889 return 0;
2890}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002891EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Takashi Iwaid5191e52009-11-16 14:58:17 +01002893/**
2894 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2895 *
2896 * The control element is supposed to have the private_value field
2897 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2898 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002899int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2903 hda_nid_t nid = get_amp_nid(kcontrol);
2904 int chs = get_amp_channels(kcontrol);
2905 int dir = get_amp_direction(kcontrol);
2906 int idx = get_amp_index(kcontrol);
2907 long *valp = ucontrol->value.integer.value;
2908
2909 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002910 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002911 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002914 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return 0;
2916}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002917EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Takashi Iwaid5191e52009-11-16 14:58:17 +01002919/**
2920 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2921 *
2922 * The control element is supposed to have the private_value field
2923 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2924 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002925int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
2928 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2929 hda_nid_t nid = get_amp_nid(kcontrol);
2930 int chs = get_amp_channels(kcontrol);
2931 int dir = get_amp_direction(kcontrol);
2932 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 long *valp = ucontrol->value.integer.value;
2934 int change = 0;
2935
Takashi Iwaicb53c622007-08-10 17:21:45 +02002936 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002937 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002938 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002939 HDA_AMP_MUTE,
2940 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002941 valp++;
2942 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002943 if (chs & 2)
2944 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002945 HDA_AMP_MUTE,
2946 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002947 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002948 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return change;
2950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002951EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953/*
Takashi Iwai985be542005-11-02 18:26:49 +01002954 * bound volume controls
2955 *
2956 * bind multiple volumes (# indices, from 0)
2957 */
2958
2959#define AMP_VAL_IDX_SHIFT 19
2960#define AMP_VAL_IDX_MASK (0x0f<<19)
2961
Takashi Iwaid5191e52009-11-16 14:58:17 +01002962/**
2963 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2964 *
2965 * The control element is supposed to have the private_value field
2966 * set up via HDA_BIND_MUTE*() macros.
2967 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002968int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002970{
2971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2972 unsigned long pval;
2973 int err;
2974
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002975 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002976 pval = kcontrol->private_value;
2977 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2978 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2979 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002980 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002981 return err;
2982}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002983EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002984
Takashi Iwaid5191e52009-11-16 14:58:17 +01002985/**
2986 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2987 *
2988 * The control element is supposed to have the private_value field
2989 * set up via HDA_BIND_MUTE*() macros.
2990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2992 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002993{
2994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2995 unsigned long pval;
2996 int i, indices, err = 0, change = 0;
2997
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002998 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002999 pval = kcontrol->private_value;
3000 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3001 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003002 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3003 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003004 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3005 if (err < 0)
3006 break;
3007 change |= err;
3008 }
3009 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003010 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003011 return err < 0 ? err : change;
3012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003013EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003014
Takashi Iwaid5191e52009-11-16 14:58:17 +01003015/**
3016 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3017 *
3018 * The control element is supposed to have the private_value field
3019 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003020 */
3021int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3022 struct snd_ctl_elem_info *uinfo)
3023{
3024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3025 struct hda_bind_ctls *c;
3026 int err;
3027
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003028 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003029 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003030 kcontrol->private_value = *c->values;
3031 err = c->ops->info(kcontrol, uinfo);
3032 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003033 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003034 return err;
3035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003036EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003037
Takashi Iwaid5191e52009-11-16 14:58:17 +01003038/**
3039 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3040 *
3041 * The control element is supposed to have the private_value field
3042 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3043 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003044int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3046{
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3048 struct hda_bind_ctls *c;
3049 int err;
3050
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003051 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003052 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003053 kcontrol->private_value = *c->values;
3054 err = c->ops->get(kcontrol, ucontrol);
3055 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003056 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003057 return err;
3058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003059EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003060
Takashi Iwaid5191e52009-11-16 14:58:17 +01003061/**
3062 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3063 *
3064 * The control element is supposed to have the private_value field
3065 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3066 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003067int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_value *ucontrol)
3069{
3070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3071 struct hda_bind_ctls *c;
3072 unsigned long *vals;
3073 int err = 0, change = 0;
3074
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003075 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003076 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003077 for (vals = c->values; *vals; vals++) {
3078 kcontrol->private_value = *vals;
3079 err = c->ops->put(kcontrol, ucontrol);
3080 if (err < 0)
3081 break;
3082 change |= err;
3083 }
3084 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003085 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003086 return err < 0 ? err : change;
3087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003088EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003089
Takashi Iwaid5191e52009-11-16 14:58:17 +01003090/**
3091 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3092 *
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_BIND_VOL() macro.
3095 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003096int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3097 unsigned int size, unsigned int __user *tlv)
3098{
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 struct hda_bind_ctls *c;
3101 int err;
3102
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003103 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003104 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003105 kcontrol->private_value = *c->values;
3106 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3107 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003108 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003109 return err;
3110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003111EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003112
3113struct hda_ctl_ops snd_hda_bind_vol = {
3114 .info = snd_hda_mixer_amp_volume_info,
3115 .get = snd_hda_mixer_amp_volume_get,
3116 .put = snd_hda_mixer_amp_volume_put,
3117 .tlv = snd_hda_mixer_amp_tlv
3118};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003119EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003120
3121struct hda_ctl_ops snd_hda_bind_sw = {
3122 .info = snd_hda_mixer_amp_switch_info,
3123 .get = snd_hda_mixer_amp_switch_get,
3124 .put = snd_hda_mixer_amp_switch_put,
3125 .tlv = snd_hda_mixer_amp_tlv
3126};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003127EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003128
3129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 * SPDIF out controls
3131 */
3132
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3137 uinfo->count = 1;
3138 return 0;
3139}
3140
Takashi Iwai0ba21762007-04-16 11:29:14 +02003141static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3145 IEC958_AES0_NONAUDIO |
3146 IEC958_AES0_CON_EMPHASIS_5015 |
3147 IEC958_AES0_CON_NOT_COPYRIGHT;
3148 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3149 IEC958_AES1_CON_ORIGINAL;
3150 return 0;
3151}
3152
Takashi Iwai0ba21762007-04-16 11:29:14 +02003153static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3154 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3157 IEC958_AES0_NONAUDIO |
3158 IEC958_AES0_PRO_EMPHASIS_5015;
3159 return 0;
3160}
3161
Takashi Iwai0ba21762007-04-16 11:29:14 +02003162static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003166 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003167 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003169 mutex_lock(&codec->spdif_mutex);
3170 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003171 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3172 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3173 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3174 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003175 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 return 0;
3178}
3179
3180/* convert from SPDIF status bits to HDA SPDIF bits
3181 * bit 0 (DigEn) is always set zero (to be filled later)
3182 */
3183static unsigned short convert_from_spdif_status(unsigned int sbits)
3184{
3185 unsigned short val = 0;
3186
3187 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003188 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003190 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003192 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3193 IEC958_AES0_PRO_EMPHASIS_5015)
3194 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003196 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3197 IEC958_AES0_CON_EMPHASIS_5015)
3198 val |= AC_DIG1_EMPHASIS;
3199 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3200 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003202 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3204 }
3205 return val;
3206}
3207
3208/* convert to SPDIF status bits from HDA SPDIF bits
3209 */
3210static unsigned int convert_to_spdif_status(unsigned short val)
3211{
3212 unsigned int sbits = 0;
3213
Takashi Iwai0ba21762007-04-16 11:29:14 +02003214 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003216 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 sbits |= IEC958_AES0_PROFESSIONAL;
3218 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003219 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3221 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003222 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003224 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3228 sbits |= val & (0x7f << 8);
3229 }
3230 return sbits;
3231}
3232
Takashi Iwai2f728532008-09-25 16:32:41 +02003233/* set digital convert verbs both for the given NID and its slaves */
3234static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3235 int verb, int val)
3236{
Takashi Iwaidda14412011-05-02 11:29:30 +02003237 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003238
Takashi Iwai9e976972008-11-25 08:17:20 +01003239 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003240 d = codec->slave_dig_outs;
3241 if (!d)
3242 return;
3243 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003244 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003245}
3246
3247static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3248 int dig1, int dig2)
3249{
3250 if (dig1 != -1)
3251 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3252 if (dig2 != -1)
3253 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3254}
3255
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3257 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003260 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003261 struct hda_spdif_out *spdif;
3262 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 unsigned short val;
3264 int change;
3265
Ingo Molnar62932df2006-01-16 16:34:20 +01003266 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003267 spdif = snd_array_elem(&codec->spdif_out, idx);
3268 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003269 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3271 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3272 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003273 val = convert_from_spdif_status(spdif->status);
3274 val |= spdif->ctls & 1;
3275 change = spdif->ctls != val;
3276 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003277 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003278 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003279 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return change;
3281}
3282
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003283#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Takashi Iwai0ba21762007-04-16 11:29:14 +02003285static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3286 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003289 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003290 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003292 mutex_lock(&codec->spdif_mutex);
3293 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003294 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003295 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return 0;
3297}
3298
Stephen Warren74b654c2011-06-01 11:14:18 -06003299static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3300 int dig1, int dig2)
3301{
3302 set_dig_out_convert(codec, nid, dig1, dig2);
3303 /* unmute amp switch (if any) */
3304 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3305 (dig1 & AC_DIG1_ENABLE))
3306 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3307 HDA_AMP_MUTE, 0);
3308}
3309
Takashi Iwai0ba21762007-04-16 11:29:14 +02003310static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003314 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003315 struct hda_spdif_out *spdif;
3316 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 unsigned short val;
3318 int change;
3319
Ingo Molnar62932df2006-01-16 16:34:20 +01003320 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003321 spdif = snd_array_elem(&codec->spdif_out, idx);
3322 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003323 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003325 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003326 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003327 spdif->ctls = val;
3328 if (change && nid != (u16)-1)
3329 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003330 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 return change;
3332}
3333
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003334static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 {
3336 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3337 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003338 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 .info = snd_hda_spdif_mask_info,
3340 .get = snd_hda_spdif_cmask_get,
3341 },
3342 {
3343 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3344 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003345 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 .info = snd_hda_spdif_mask_info,
3347 .get = snd_hda_spdif_pmask_get,
3348 },
3349 {
3350 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003351 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 .info = snd_hda_spdif_mask_info,
3353 .get = snd_hda_spdif_default_get,
3354 .put = snd_hda_spdif_default_put,
3355 },
3356 {
3357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003358 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 .info = snd_hda_spdif_out_switch_info,
3360 .get = snd_hda_spdif_out_switch_get,
3361 .put = snd_hda_spdif_out_switch_put,
3362 },
3363 { } /* end */
3364};
3365
3366/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003367 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003369 * @associated_nid: NID that new ctls associated with
3370 * @cvt_nid: converter NID
3371 * @type: HDA_PCM_TYPE_*
3372 * Creates controls related with the digital output.
3373 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 *
3375 * Returns 0 if successful, or a negative error code.
3376 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003377int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3378 hda_nid_t associated_nid,
3379 hda_nid_t cvt_nid,
3380 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003383 struct snd_kcontrol *kctl;
3384 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003385 int idx = 0;
3386 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003387 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003388 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003390 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003391 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003392 idx = spdif_index;
3393 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003394 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003395 /* suppose a single SPDIF device */
3396 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3397 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3398 if (!kctl)
3399 break;
3400 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003401 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003402 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003403 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003404 if (!bus->primary_dig_out_type)
3405 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003406
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003407 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003408 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003409 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3410 return -EBUSY;
3411 }
Stephen Warren7c935972011-06-01 11:14:17 -06003412 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003413 if (!spdif)
3414 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3416 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003417 if (!kctl)
3418 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003419 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003420 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003421 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003422 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return err;
3424 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003425 spdif->nid = cvt_nid;
3426 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003427 AC_VERB_GET_DIGI_CONVERT_1, 0);
3428 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003431EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003433/* get the hda_spdif_out entry from the given NID
3434 * call within spdif_mutex lock
3435 */
Stephen Warren7c935972011-06-01 11:14:17 -06003436struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3437 hda_nid_t nid)
3438{
3439 int i;
3440 for (i = 0; i < codec->spdif_out.used; i++) {
3441 struct hda_spdif_out *spdif =
3442 snd_array_elem(&codec->spdif_out, i);
3443 if (spdif->nid == nid)
3444 return spdif;
3445 }
3446 return NULL;
3447}
3448EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3449
Stephen Warren74b654c2011-06-01 11:14:18 -06003450void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3451{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003452 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003453
3454 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003455 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003456 spdif->nid = (u16)-1;
3457 mutex_unlock(&codec->spdif_mutex);
3458}
3459EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3460
3461void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3462{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003463 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003464 unsigned short val;
3465
3466 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003467 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003468 if (spdif->nid != nid) {
3469 spdif->nid = nid;
3470 val = spdif->ctls;
3471 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3472 }
3473 mutex_unlock(&codec->spdif_mutex);
3474}
3475EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003478 * SPDIF sharing with analog output
3479 */
3480static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3481 struct snd_ctl_elem_value *ucontrol)
3482{
3483 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3484 ucontrol->value.integer.value[0] = mout->share_spdif;
3485 return 0;
3486}
3487
3488static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3489 struct snd_ctl_elem_value *ucontrol)
3490{
3491 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3492 mout->share_spdif = !!ucontrol->value.integer.value[0];
3493 return 0;
3494}
3495
3496static struct snd_kcontrol_new spdif_share_sw = {
3497 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3498 .name = "IEC958 Default PCM Playback Switch",
3499 .info = snd_ctl_boolean_mono_info,
3500 .get = spdif_share_sw_get,
3501 .put = spdif_share_sw_put,
3502};
3503
Takashi Iwaid5191e52009-11-16 14:58:17 +01003504/**
3505 * snd_hda_create_spdif_share_sw - create Default PCM switch
3506 * @codec: the HDA codec
3507 * @mout: multi-out instance
3508 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003509int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3510 struct hda_multi_out *mout)
3511{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003512 struct snd_kcontrol *kctl;
3513
Takashi Iwai9a081602008-02-12 18:37:26 +01003514 if (!mout->dig_out_nid)
3515 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003516
3517 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3518 if (!kctl)
3519 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003520 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003521 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003522}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003523EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003524
3525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 * SPDIF input
3527 */
3528
3529#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3530
Takashi Iwai0ba21762007-04-16 11:29:14 +02003531static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3532 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
3534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3535
3536 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3537 return 0;
3538}
3539
Takashi Iwai0ba21762007-04-16 11:29:14 +02003540static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3541 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3544 hda_nid_t nid = kcontrol->private_value;
3545 unsigned int val = !!ucontrol->value.integer.value[0];
3546 int change;
3547
Ingo Molnar62932df2006-01-16 16:34:20 +01003548 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003550 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003552 snd_hda_codec_write_cache(codec, nid, 0,
3553 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003555 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return change;
3557}
3558
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3560 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
3562 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3563 hda_nid_t nid = kcontrol->private_value;
3564 unsigned short val;
3565 unsigned int sbits;
3566
Andrew Paprocki3982d172007-12-19 12:13:44 +01003567 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 sbits = convert_to_spdif_status(val);
3569 ucontrol->value.iec958.status[0] = sbits;
3570 ucontrol->value.iec958.status[1] = sbits >> 8;
3571 ucontrol->value.iec958.status[2] = sbits >> 16;
3572 ucontrol->value.iec958.status[3] = sbits >> 24;
3573 return 0;
3574}
3575
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003576static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 {
3578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003579 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 .info = snd_hda_spdif_in_switch_info,
3581 .get = snd_hda_spdif_in_switch_get,
3582 .put = snd_hda_spdif_in_switch_put,
3583 },
3584 {
3585 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003587 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 .info = snd_hda_spdif_mask_info,
3589 .get = snd_hda_spdif_in_status_get,
3590 },
3591 { } /* end */
3592};
3593
3594/**
3595 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3596 * @codec: the HDA codec
3597 * @nid: audio in widget NID
3598 *
3599 * Creates controls related with the SPDIF input.
3600 * Called from each patch supporting the SPDIF in.
3601 *
3602 * Returns 0 if successful, or a negative error code.
3603 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003604int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003607 struct snd_kcontrol *kctl;
3608 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003609 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Takashi Iwaidcda5802012-10-12 17:24:51 +02003611 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003612 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003613 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3614 return -EBUSY;
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3617 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003618 if (!kctl)
3619 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003621 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003622 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return err;
3624 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003625 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003626 snd_hda_codec_read(codec, nid, 0,
3627 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003628 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return 0;
3630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003631EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003633/*
3634 * command cache
3635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Takashi Iwaic370dd62012-12-13 18:30:04 +01003637/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003638#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3639#define get_cmd_cache_nid(key) ((key) & 0xff)
3640#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3641
3642/**
3643 * snd_hda_codec_write_cache - send a single command with caching
3644 * @codec: the HDA codec
3645 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003646 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003647 * @verb: the verb to send
3648 * @parm: the parameter for the verb
3649 *
3650 * Send a single command without waiting for response.
3651 *
3652 * Returns 0 if successful, or a negative error code.
3653 */
3654int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003655 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003656{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003657 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003658 struct hda_cache_head *c;
3659 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003660 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003661
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003662 cache_only = codec->cached_write;
3663 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003664 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003665 if (err < 0)
3666 return err;
3667 }
3668
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003669 /* parm may contain the verb stuff for get/set amp */
3670 verb = verb | (parm >> 8);
3671 parm &= 0xff;
3672 key = build_cmd_cache_key(nid, verb);
3673 mutex_lock(&codec->bus->cmd_mutex);
3674 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003675 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003676 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003677 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003678 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003679 mutex_unlock(&codec->bus->cmd_mutex);
3680 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003681}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003682EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003683
Takashi Iwaid5191e52009-11-16 14:58:17 +01003684/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003685 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3686 * @codec: the HDA codec
3687 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003688 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003689 * @verb: the verb to send
3690 * @parm: the parameter for the verb
3691 *
3692 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3693 * command if the parameter is already identical with the cached value.
3694 * If not, it sends the command and refreshes the cache.
3695 *
3696 * Returns 0 if successful, or a negative error code.
3697 */
3698int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003699 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003700{
3701 struct hda_cache_head *c;
3702 u32 key;
3703
3704 /* parm may contain the verb stuff for get/set amp */
3705 verb = verb | (parm >> 8);
3706 parm &= 0xff;
3707 key = build_cmd_cache_key(nid, verb);
3708 mutex_lock(&codec->bus->cmd_mutex);
3709 c = get_hash(&codec->cmd_cache, key);
3710 if (c && c->val == parm) {
3711 mutex_unlock(&codec->bus->cmd_mutex);
3712 return 0;
3713 }
3714 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003715 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003716}
3717EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3718
3719/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003720 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3721 * @codec: HD-audio codec
3722 *
3723 * Execute all verbs recorded in the command caches to resume.
3724 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003725void snd_hda_codec_resume_cache(struct hda_codec *codec)
3726{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003727 int i;
3728
Takashi Iwaic370dd62012-12-13 18:30:04 +01003729 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003730 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003731 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3732 struct hda_cache_head *buffer;
3733 u32 key;
3734
3735 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3736 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003737 if (!key)
3738 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003739 if (!buffer->dirty)
3740 continue;
3741 buffer->dirty = 0;
3742 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003743 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3744 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003745 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003746 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003747 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003748}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003749EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003750
3751/**
3752 * snd_hda_sequence_write_cache - sequence writes with caching
3753 * @codec: the HDA codec
3754 * @seq: VERB array to send
3755 *
3756 * Send the commands sequentially from the given array.
3757 * Thte commands are recorded on cache for power-save and resume.
3758 * The array must be terminated with NID=0.
3759 */
3760void snd_hda_sequence_write_cache(struct hda_codec *codec,
3761 const struct hda_verb *seq)
3762{
3763 for (; seq->nid; seq++)
3764 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3765 seq->param);
3766}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003767EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003768
Takashi Iwaidc870f32013-01-22 15:24:30 +01003769/**
3770 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3771 * @codec: HD-audio codec
3772 */
3773void snd_hda_codec_flush_cache(struct hda_codec *codec)
3774{
3775 snd_hda_codec_resume_amp(codec);
3776 snd_hda_codec_resume_cache(codec);
3777}
3778EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3779
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003780void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003781 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003782{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003783 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003784 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003785
Takashi Iwaicb53c622007-08-10 17:21:45 +02003786 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003787 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003788 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003789 if (!(wcaps & AC_WCAP_POWER))
3790 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003791 if (codec->power_filter) {
3792 state = codec->power_filter(codec, nid, power_state);
3793 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003794 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003795 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003796 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003797 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003798 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003799}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003800EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3801
3802/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003803 * supported power states check
3804 */
3805static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3806 unsigned int power_state)
3807{
3808 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3809
Mengdong Line037cb42012-08-10 14:11:58 +02003810 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003811 return false;
3812 if (sup & power_state)
3813 return true;
3814 else
3815 return false;
3816}
3817
3818/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003819 * wait until the state is reached, returns the current state
3820 */
3821static unsigned int hda_sync_power_state(struct hda_codec *codec,
3822 hda_nid_t fg,
3823 unsigned int power_state)
3824{
3825 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3826 unsigned int state, actual_state;
3827
3828 for (;;) {
3829 state = snd_hda_codec_read(codec, fg, 0,
3830 AC_VERB_GET_POWER_STATE, 0);
3831 if (state & AC_PWRST_ERROR)
3832 break;
3833 actual_state = (state >> 4) & 0x0f;
3834 if (actual_state == power_state)
3835 break;
3836 if (time_after_eq(jiffies, end_time))
3837 break;
3838 /* wait until the codec reachs to the target state */
3839 msleep(1);
3840 }
3841 return state;
3842}
3843
Takashi Iwai9419ab62013-01-24 17:23:35 +01003844/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003845unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3846 hda_nid_t nid,
3847 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003848{
3849 if (power_state == AC_PWRST_D3 &&
3850 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3851 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3852 int eapd = snd_hda_codec_read(codec, nid, 0,
3853 AC_VERB_GET_EAPD_BTLENABLE, 0);
3854 if (eapd & 0x02)
3855 return AC_PWRST_D0;
3856 }
3857 return power_state;
3858}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003859EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003860
Takashi Iwai432c6412012-08-28 09:59:20 -07003861/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003862 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003863 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003864static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003865 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003866{
Takashi Iwaid8193872012-08-31 07:54:38 -07003867 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003868 int count;
3869 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003870 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003871
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003872 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003873 if (power_state == AC_PWRST_D3) {
3874 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003875 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003876 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003877 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003878
3879 /* repeat power states setting at most 10 times*/
3880 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003881 if (codec->patch_ops.set_power_state)
3882 codec->patch_ops.set_power_state(codec, fg,
3883 power_state);
3884 else {
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003885 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003886 AC_VERB_SET_POWER_STATE,
3887 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003888 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003889 }
3890 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003891 if (!(state & AC_PWRST_ERROR))
3892 break;
3893 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003894
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003895 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003896}
Takashi Iwai54d17402005-11-21 16:33:22 +01003897
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003898/* sync power states of all widgets;
3899 * this is called at the end of codec parsing
3900 */
3901static void sync_power_up_states(struct hda_codec *codec)
3902{
3903 hda_nid_t nid = codec->start_nid;
3904 int i;
3905
Takashi Iwaiba615b82013-03-13 14:47:21 +01003906 /* don't care if no filter is used */
3907 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003908 return;
3909
3910 for (i = 0; i < codec->num_nodes; i++, nid++) {
3911 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003912 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003913 if (!(wcaps & AC_WCAP_POWER))
3914 continue;
3915 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3916 if (target == AC_PWRST_D0)
3917 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003918 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003919 snd_hda_codec_write(codec, nid, 0,
3920 AC_VERB_SET_POWER_STATE, target);
3921 }
3922}
3923
Takashi Iwai11aeff02008-07-30 15:01:46 +02003924#ifdef CONFIG_SND_HDA_HWDEP
3925/* execute additional init verbs */
3926static void hda_exec_init_verbs(struct hda_codec *codec)
3927{
3928 if (codec->init_verbs.list)
3929 snd_hda_sequence_write(codec, codec->init_verbs.list);
3930}
3931#else
3932static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3933#endif
3934
Takashi Iwai2a439522011-07-26 09:52:50 +02003935#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003936/*
3937 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003938 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003939 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003940static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003941{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003942 unsigned int state;
3943
Takashi Iwai989c3182012-11-19 14:14:58 +01003944 codec->in_pm = 1;
3945
Takashi Iwaicb53c622007-08-10 17:21:45 +02003946 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003947 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003948 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003949 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003950 /* Cancel delayed work if we aren't currently running from it. */
3951 if (!in_wq)
3952 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003953 spin_lock(&codec->power_lock);
3954 snd_hda_update_power_acct(codec);
3955 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003956 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003957 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003958 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003959 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003960 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003961 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003962}
3963
Takashi Iwaic370dd62012-12-13 18:30:04 +01003964/* mark all entries of cmd and amp caches dirty */
3965static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3966{
3967 int i;
3968 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3969 struct hda_cache_head *cmd;
3970 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3971 cmd->dirty = 1;
3972 }
3973 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3974 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003975 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003976 amp->head.dirty = 1;
3977 }
3978}
3979
Takashi Iwaicb53c622007-08-10 17:21:45 +02003980/*
3981 * kick up codec; used both from PM and power-save
3982 */
3983static void hda_call_codec_resume(struct hda_codec *codec)
3984{
Takashi Iwai989c3182012-11-19 14:14:58 +01003985 codec->in_pm = 1;
3986
Takashi Iwaic370dd62012-12-13 18:30:04 +01003987 hda_mark_cmd_cache_dirty(codec);
3988
Takashi Iwai7f308302012-05-08 16:52:23 +02003989 /* set as if powered on for avoiding re-entering the resume
3990 * in the resume / power-save sequence
3991 */
3992 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003993 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003994 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003995 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003996 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003997 if (codec->patch_ops.resume)
3998 codec->patch_ops.resume(codec);
3999 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004000 if (codec->patch_ops.init)
4001 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004002 snd_hda_codec_resume_amp(codec);
4003 snd_hda_codec_resume_cache(codec);
4004 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004005
4006 if (codec->jackpoll_interval)
4007 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004008 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004009 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004010
4011 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004012 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004013}
Takashi Iwai2a439522011-07-26 09:52:50 +02004014#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004015
Takashi Iwai54d17402005-11-21 16:33:22 +01004016
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017/**
4018 * snd_hda_build_controls - build mixer controls
4019 * @bus: the BUS
4020 *
4021 * Creates mixer controls for each codec included in the bus.
4022 *
4023 * Returns 0 if successful, otherwise a negative error code.
4024 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004025int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004027 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Takashi Iwai0ba21762007-04-16 11:29:14 +02004029 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004030 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004031 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004032 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004033 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004034 err = snd_hda_codec_reset(codec);
4035 if (err < 0) {
4036 printk(KERN_ERR
4037 "hda_codec: cannot revert codec\n");
4038 return err;
4039 }
4040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004042 return 0;
4043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004044EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004046/*
4047 * add standard channel maps if not specified
4048 */
4049static int add_std_chmaps(struct hda_codec *codec)
4050{
4051 int i, str, err;
4052
4053 for (i = 0; i < codec->num_pcms; i++) {
4054 for (str = 0; str < 2; str++) {
4055 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4056 struct hda_pcm_stream *hinfo =
4057 &codec->pcm_info[i].stream[str];
4058 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004059 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004060
4061 if (codec->pcm_info[i].own_chmap)
4062 continue;
4063 if (!pcm || !hinfo->substreams)
4064 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004065 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4066 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004067 hinfo->channels_max,
4068 0, &chmap);
4069 if (err < 0)
4070 return err;
4071 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4072 }
4073 }
4074 return 0;
4075}
4076
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004077/* default channel maps for 2.1 speakers;
4078 * since HD-audio supports only stereo, odd number channels are omitted
4079 */
4080const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4081 { .channels = 2,
4082 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4083 { .channels = 4,
4084 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4085 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4086 { }
4087};
4088EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4089
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004090int snd_hda_codec_build_controls(struct hda_codec *codec)
4091{
4092 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004093 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004094 /* continue to initialize... */
4095 if (codec->patch_ops.init)
4096 err = codec->patch_ops.init(codec);
4097 if (!err && codec->patch_ops.build_controls)
4098 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004099 if (err < 0)
4100 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004101
4102 /* we create chmaps here instead of build_pcms */
4103 err = add_std_chmaps(codec);
4104 if (err < 0)
4105 return err;
4106
David Henningsson26a6cb62012-10-09 15:04:21 +02004107 if (codec->jackpoll_interval)
4108 hda_jackpoll_work(&codec->jackpoll_work.work);
4109 else
4110 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004111 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return 0;
4113}
4114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115/*
4116 * stream formats
4117 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004118struct hda_rate_tbl {
4119 unsigned int hz;
4120 unsigned int alsa_bits;
4121 unsigned int hda_fmt;
4122};
4123
Takashi Iwai92f10b32010-08-03 14:21:00 +02004124/* rate = base * mult / div */
4125#define HDA_RATE(base, mult, div) \
4126 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4127 (((div) - 1) << AC_FMT_DIV_SHIFT))
4128
Takashi Iwaibefdf312005-08-22 13:57:55 +02004129static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004131
4132 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004133 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4134 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4135 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4136 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4137 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4138 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4139 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4140 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4141 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4142 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4143 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004144#define AC_PAR_PCM_RATE_BITS 11
4145 /* up to bits 10, 384kHZ isn't supported properly */
4146
4147 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004148 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004149
Takashi Iwaibefdf312005-08-22 13:57:55 +02004150 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151};
4152
4153/**
4154 * snd_hda_calc_stream_format - calculate format bitset
4155 * @rate: the sample rate
4156 * @channels: the number of channels
4157 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4158 * @maxbps: the max. bps
4159 *
4160 * Calculate the format bitset from the given rate, channels and th PCM format.
4161 *
4162 * Return zero if invalid.
4163 */
4164unsigned int snd_hda_calc_stream_format(unsigned int rate,
4165 unsigned int channels,
4166 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004167 unsigned int maxbps,
4168 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 int i;
4171 unsigned int val = 0;
4172
Takashi Iwaibefdf312005-08-22 13:57:55 +02004173 for (i = 0; rate_bits[i].hz; i++)
4174 if (rate_bits[i].hz == rate) {
4175 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 break;
4177 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004178 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 snd_printdd("invalid rate %d\n", rate);
4180 return 0;
4181 }
4182
4183 if (channels == 0 || channels > 8) {
4184 snd_printdd("invalid channels %d\n", channels);
4185 return 0;
4186 }
4187 val |= channels - 1;
4188
4189 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004190 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004191 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004192 break;
4193 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004194 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 case 20:
4197 case 24:
4198 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004199 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004200 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004202 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004204 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 break;
4206 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207 snd_printdd("invalid format width %d\n",
4208 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return 0;
4210 }
4211
Anssi Hannula32c168c2010-08-03 13:28:57 +03004212 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004213 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return val;
4216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004217EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004219static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4220 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004221{
4222 unsigned int val = 0;
4223 if (nid != codec->afg &&
4224 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4225 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4226 if (!val || val == -1)
4227 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4228 if (!val || val == -1)
4229 return 0;
4230 return val;
4231}
4232
4233static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4234{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004235 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004236 get_pcm_param);
4237}
4238
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004239static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4240 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004241{
4242 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4243 if (!streams || streams == -1)
4244 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4245 if (!streams || streams == -1)
4246 return 0;
4247 return streams;
4248}
4249
4250static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4251{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004252 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004253 get_stream_param);
4254}
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256/**
4257 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4258 * @codec: the HDA codec
4259 * @nid: NID to query
4260 * @ratesp: the pointer to store the detected rate bitflags
4261 * @formatsp: the pointer to store the detected formats
4262 * @bpsp: the pointer to store the detected format widths
4263 *
4264 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4265 * or @bsps argument is ignored.
4266 *
4267 * Returns 0 if successful, otherwise a negative error code.
4268 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004269int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4271{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004272 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004274 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004275 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 if (ratesp) {
4278 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004279 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004281 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004283 if (rates == 0) {
4284 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4285 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4286 nid, val,
4287 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4288 return -EIO;
4289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 *ratesp = rates;
4291 }
4292
4293 if (formatsp || bpsp) {
4294 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004295 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004297 streams = query_stream_param(codec, nid);
4298 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 bps = 0;
4302 if (streams & AC_SUPFMT_PCM) {
4303 if (val & AC_SUPPCM_BITS_8) {
4304 formats |= SNDRV_PCM_FMTBIT_U8;
4305 bps = 8;
4306 }
4307 if (val & AC_SUPPCM_BITS_16) {
4308 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4309 bps = 16;
4310 }
4311 if (wcaps & AC_WCAP_DIGITAL) {
4312 if (val & AC_SUPPCM_BITS_32)
4313 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4314 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4315 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4316 if (val & AC_SUPPCM_BITS_24)
4317 bps = 24;
4318 else if (val & AC_SUPPCM_BITS_20)
4319 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004320 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4321 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4323 if (val & AC_SUPPCM_BITS_32)
4324 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 else if (val & AC_SUPPCM_BITS_24)
4326 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004327 else if (val & AC_SUPPCM_BITS_20)
4328 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
4330 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004331#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004332 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004334 if (!bps)
4335 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004336 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004337#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004338 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004339 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 /* temporary hack: we have still no proper support
4341 * for the direct AC3 stream...
4342 */
4343 formats |= SNDRV_PCM_FMTBIT_U8;
4344 bps = 8;
4345 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004346 if (formats == 0) {
4347 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4348 "(nid=0x%x, val=0x%x, ovrd=%i, "
4349 "streams=0x%x)\n",
4350 nid, val,
4351 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4352 streams);
4353 return -EIO;
4354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (formatsp)
4356 *formatsp = formats;
4357 if (bpsp)
4358 *bpsp = bps;
4359 }
4360
4361 return 0;
4362}
Stephen Warren384a48d2011-06-01 11:14:21 -06004363EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004366 * snd_hda_is_supported_format - Check the validity of the format
4367 * @codec: HD-audio codec
4368 * @nid: NID to check
4369 * @format: the HD-audio format value to check
4370 *
4371 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 *
4373 * Returns 1 if supported, 0 if not.
4374 */
4375int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4376 unsigned int format)
4377{
4378 int i;
4379 unsigned int val = 0, rate, stream;
4380
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004381 val = query_pcm_param(codec, nid);
4382 if (!val)
4383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004386 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004387 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 if (val & (1 << i))
4389 break;
4390 return 0;
4391 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004392 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004395 stream = query_stream_param(codec, nid);
4396 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return 0;
4398
4399 if (stream & AC_SUPFMT_PCM) {
4400 switch (format & 0xf0) {
4401 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004402 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return 0;
4404 break;
4405 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return 0;
4408 break;
4409 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004410 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return 0;
4412 break;
4413 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004414 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 return 0;
4416 break;
4417 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004418 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return 0;
4420 break;
4421 default:
4422 return 0;
4423 }
4424 } else {
4425 /* FIXME: check for float32 and AC3? */
4426 }
4427
4428 return 1;
4429}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004430EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432/*
4433 * PCM stuff
4434 */
4435static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4436 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004437 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
4439 return 0;
4440}
4441
4442static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4443 struct hda_codec *codec,
4444 unsigned int stream_tag,
4445 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004446 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
4448 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4449 return 0;
4450}
4451
4452static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4453 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004454 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Takashi Iwai888afa12008-03-18 09:57:50 +01004456 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return 0;
4458}
4459
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004460static int set_pcm_default_values(struct hda_codec *codec,
4461 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004463 int err;
4464
Takashi Iwai0ba21762007-04-16 11:29:14 +02004465 /* query support PCM information from the given NID */
4466 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004467 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004468 info->rates ? NULL : &info->rates,
4469 info->formats ? NULL : &info->formats,
4470 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004471 if (err < 0)
4472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
4474 if (info->ops.open == NULL)
4475 info->ops.open = hda_pcm_default_open_close;
4476 if (info->ops.close == NULL)
4477 info->ops.close = hda_pcm_default_open_close;
4478 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004479 if (snd_BUG_ON(!info->nid))
4480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 info->ops.prepare = hda_pcm_default_prepare;
4482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004484 if (snd_BUG_ON(!info->nid))
4485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 info->ops.cleanup = hda_pcm_default_cleanup;
4487 }
4488 return 0;
4489}
4490
Takashi Iwaieb541332010-08-06 13:48:11 +02004491/*
4492 * codec prepare/cleanup entries
4493 */
4494int snd_hda_codec_prepare(struct hda_codec *codec,
4495 struct hda_pcm_stream *hinfo,
4496 unsigned int stream,
4497 unsigned int format,
4498 struct snd_pcm_substream *substream)
4499{
4500 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004501 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004502 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4503 if (ret >= 0)
4504 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004505 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004506 return ret;
4507}
4508EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4509
4510void snd_hda_codec_cleanup(struct hda_codec *codec,
4511 struct hda_pcm_stream *hinfo,
4512 struct snd_pcm_substream *substream)
4513{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004514 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004515 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004516 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004517}
4518EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4519
Takashi Iwaid5191e52009-11-16 14:58:17 +01004520/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004521const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4522 "Audio", "SPDIF", "HDMI", "Modem"
4523};
4524
Takashi Iwai176d5332008-07-30 15:01:44 +02004525/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004526 * get the empty PCM device number to assign
4527 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004528static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004529{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004530 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004531 /* assigned to static slots up to dev#10; if more needed, assign
4532 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4533 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004534 static int audio_idx[HDA_PCM_NTYPES][5] = {
4535 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4536 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004537 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004538 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004539 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004540 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004541
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004542 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004543 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4544 return -EINVAL;
4545 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004546
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004547 for (i = 0; audio_idx[type][i] >= 0; i++) {
4548#ifndef CONFIG_SND_DYNAMIC_MINORS
4549 if (audio_idx[type][i] >= 8)
4550 break;
4551#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004552 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4553 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004554 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004555
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004556#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004557 /* non-fixed slots starting from 10 */
4558 for (i = 10; i < 32; i++) {
4559 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4560 return i;
4561 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004562#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004563
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004564 snd_printk(KERN_WARNING "Too many %s devices\n",
4565 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004566#ifndef CONFIG_SND_DYNAMIC_MINORS
4567 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4568#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004569 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004570}
4571
4572/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004573 * attach a new PCM stream
4574 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004575static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004576{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004577 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004578 struct hda_pcm_stream *info;
4579 int stream, err;
4580
Takashi Iwaib91f0802008-11-04 08:43:08 +01004581 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004582 return -EINVAL;
4583 for (stream = 0; stream < 2; stream++) {
4584 info = &pcm->stream[stream];
4585 if (info->substreams) {
4586 err = set_pcm_default_values(codec, info);
4587 if (err < 0)
4588 return err;
4589 }
4590 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004591 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004592}
4593
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004594/* assign all PCMs of the given codec */
4595int snd_hda_codec_build_pcms(struct hda_codec *codec)
4596{
4597 unsigned int pcm;
4598 int err;
4599
4600 if (!codec->num_pcms) {
4601 if (!codec->patch_ops.build_pcms)
4602 return 0;
4603 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004604 if (err < 0) {
4605 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004606 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004607 err = snd_hda_codec_reset(codec);
4608 if (err < 0) {
4609 printk(KERN_ERR
4610 "hda_codec: cannot revert codec\n");
4611 return err;
4612 }
4613 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004614 }
4615 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4616 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4617 int dev;
4618
4619 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004620 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004621
4622 if (!cpcm->pcm) {
4623 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4624 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004625 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004626 cpcm->device = dev;
4627 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004628 if (err < 0) {
4629 printk(KERN_ERR "hda_codec: cannot attach "
4630 "PCM stream %d for codec #%d\n",
4631 dev, codec->addr);
4632 continue; /* no fatal error */
4633 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004634 }
4635 }
4636 return 0;
4637}
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639/**
4640 * snd_hda_build_pcms - build PCM information
4641 * @bus: the BUS
4642 *
4643 * Create PCM information for each codec included in the bus.
4644 *
4645 * The build_pcms codec patch is requested to set up codec->num_pcms and
4646 * codec->pcm_info properly. The array is referred by the top-level driver
4647 * to create its PCM instances.
4648 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4649 * callback.
4650 *
4651 * At least, substreams, channels_min and channels_max must be filled for
4652 * each stream. substreams = 0 indicates that the stream doesn't exist.
4653 * When rates and/or formats are zero, the supported values are queried
4654 * from the given nid. The nid is used also by the default ops.prepare
4655 * and ops.cleanup callbacks.
4656 *
4657 * The driver needs to call ops.open in its open callback. Similarly,
4658 * ops.close is supposed to be called in the close callback.
4659 * ops.prepare should be called in the prepare or hw_params callback
4660 * with the proper parameters for set up.
4661 * ops.cleanup should be called in hw_free for clean up of streams.
4662 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004663 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004665int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004667 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Takashi Iwai0ba21762007-04-16 11:29:14 +02004669 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004670 int err = snd_hda_codec_build_pcms(codec);
4671 if (err < 0)
4672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
4674 return 0;
4675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004676EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/**
4679 * snd_hda_check_board_config - compare the current codec with the config table
4680 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004681 * @num_configs: number of config enums
4682 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 * @tbl: configuration table, terminated by null entries
4684 *
4685 * Compares the modelname or PCI subsystem id of the current codec with the
4686 * given configuration table. If a matching entry is found, returns its
4687 * config value (supposed to be 0 or positive).
4688 *
4689 * If no entries are matching, the function returns a negative value.
4690 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004691int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004692 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004693 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004695 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004696 int i;
4697 for (i = 0; i < num_configs; i++) {
4698 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004699 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004700 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4701 "selected\n", models[i]);
4702 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704 }
4705 }
4706
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004707 if (!codec->bus->pci || !tbl)
4708 return -1;
4709
4710 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4711 if (!tbl)
4712 return -1;
4713 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004714#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004715 char tmp[10];
4716 const char *model = NULL;
4717 if (models)
4718 model = models[tbl->value];
4719 if (!model) {
4720 sprintf(tmp, "#%d", tbl->value);
4721 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004723 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4724 "for config %x:%x (%s)\n",
4725 model, tbl->subvendor, tbl->subdevice,
4726 (tbl->name ? tbl->name : "Unknown device"));
4727#endif
4728 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730 return -1;
4731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004732EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004735 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004736 subsystem ID with the
4737 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004738
4739 This is important for Gateway notebooks with SB450 HDA Audio
4740 where the vendor ID of the PCI device is:
4741 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4742 and the vendor/subvendor are found only at the codec.
4743
4744 * @codec: the HDA codec
4745 * @num_configs: number of config enums
4746 * @models: array of model name strings
4747 * @tbl: configuration table, terminated by null entries
4748 *
4749 * Compares the modelname or PCI subsystem id of the current codec with the
4750 * given configuration table. If a matching entry is found, returns its
4751 * config value (supposed to be 0 or positive).
4752 *
4753 * If no entries are matching, the function returns a negative value.
4754 */
4755int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004756 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004757 const struct snd_pci_quirk *tbl)
4758{
4759 const struct snd_pci_quirk *q;
4760
4761 /* Search for codec ID */
4762 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004763 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4764 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4765 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004766 break;
4767 }
4768
4769 if (!q->subvendor)
4770 return -1;
4771
4772 tbl = q;
4773
4774 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004775#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004776 char tmp[10];
4777 const char *model = NULL;
4778 if (models)
4779 model = models[tbl->value];
4780 if (!model) {
4781 sprintf(tmp, "#%d", tbl->value);
4782 model = tmp;
4783 }
4784 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4785 "for config %x:%x (%s)\n",
4786 model, tbl->subvendor, tbl->subdevice,
4787 (tbl->name ? tbl->name : "Unknown device"));
4788#endif
4789 return tbl->value;
4790 }
4791 return -1;
4792}
4793EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4794
4795/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 * snd_hda_add_new_ctls - create controls from the array
4797 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004798 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 *
4800 * This helper function creates and add new controls in the given array.
4801 * The array must be terminated with an empty entry as terminator.
4802 *
4803 * Returns 0 if successful, or a negative error code.
4804 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004805int snd_hda_add_new_ctls(struct hda_codec *codec,
4806 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004811 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004812 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004813 if (knew->iface == -1) /* skip this codec private value */
4814 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004815 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004816 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004817 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004818 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004819 if (addr > 0)
4820 kctl->id.device = addr;
4821 if (idx > 0)
4822 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004823 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004824 if (!err)
4825 break;
4826 /* try first with another device index corresponding to
4827 * the codec addr; if it still fails (or it's the
4828 * primary codec), then try another control index
4829 */
4830 if (!addr && codec->addr)
4831 addr = codec->addr;
4832 else if (!idx && !knew->index) {
4833 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004834 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004835 if (idx <= 0)
4836 return err;
4837 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004838 return err;
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
4841 return 0;
4842}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004843EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Takashi Iwai83012a72012-08-24 18:38:08 +02004845#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004846static void hda_power_work(struct work_struct *work)
4847{
4848 struct hda_codec *codec =
4849 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004850 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004851 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004852
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004853 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004854 if (codec->power_transition > 0) { /* during power-up sequence? */
4855 spin_unlock(&codec->power_lock);
4856 return;
4857 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004858 if (!codec->power_on || codec->power_count) {
4859 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004860 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004861 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004862 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004863 spin_unlock(&codec->power_lock);
4864
Dylan Reidd17344b2012-09-28 15:57:01 -07004865 state = hda_call_codec_suspend(codec, true);
Takashi Iwaie6bbe662013-10-24 01:20:24 +02004866 if (!codec->pm_down_notified &&
4867 !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004868 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004869 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004870 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004871}
4872
4873static void hda_keep_power_on(struct hda_codec *codec)
4874{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004875 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004876 codec->power_count++;
4877 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004878 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004879 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004880}
4881
Takashi Iwaid5191e52009-11-16 14:58:17 +01004882/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004883void snd_hda_update_power_acct(struct hda_codec *codec)
4884{
4885 unsigned long delta = jiffies - codec->power_jiffies;
4886 if (codec->power_on)
4887 codec->power_on_acct += delta;
4888 else
4889 codec->power_off_acct += delta;
4890 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004891}
4892
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004893/* Transition to powered up, if wait_power_down then wait for a pending
4894 * transition to D3 to complete. A pending D3 transition is indicated
4895 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004896/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004897static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004898{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004899 struct hda_bus *bus = codec->bus;
4900
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004901 /* Return if power_on or transitioning to power_on, unless currently
4902 * powering down. */
4903 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004904 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004905 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004906 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004907
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004908 cancel_delayed_work_sync(&codec->power_work);
4909
4910 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004911 /* If the power down delayed work was cancelled above before starting,
4912 * then there is no need to go through power up here.
4913 */
4914 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004915 if (codec->power_transition < 0)
4916 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004917 return;
4918 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004919
Takashi Iwaid66fee52011-08-02 15:39:31 +02004920 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004921 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004922 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004923 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004924 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004925 spin_unlock(&codec->power_lock);
4926
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004927 if (codec->pm_down_notified) {
4928 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004929 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004930 }
4931
Takashi Iwaicb53c622007-08-10 17:21:45 +02004932 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004933
4934 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004935 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004936}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004937
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004938#define power_save(codec) \
4939 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004940
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004941/* Transition to powered down */
4942static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004943{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004944 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004945 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004946
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004947 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004948 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004949 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004950 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004951 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004952}
4953
4954/**
4955 * snd_hda_power_save - Power-up/down/sync the codec
4956 * @codec: HD-audio codec
4957 * @delta: the counter delta to change
4958 *
4959 * Change the power-up counter via @delta, and power up or down the hardware
4960 * appropriately. For the power-down, queue to the delayed action.
4961 * Passing zero to @delta means to synchronize the power state.
4962 */
4963void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4964{
4965 spin_lock(&codec->power_lock);
4966 codec->power_count += delta;
4967 trace_hda_power_count(codec);
4968 if (delta > 0)
4969 __snd_hda_power_up(codec, d3wait);
4970 else
4971 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004972 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004973}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004974EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004975
Takashi Iwaid5191e52009-11-16 14:58:17 +01004976/**
4977 * snd_hda_check_amp_list_power - Check the amp list and update the power
4978 * @codec: HD-audio codec
4979 * @check: the object containing an AMP list and the status
4980 * @nid: NID to check / update
4981 *
4982 * Check whether the given NID is in the amp list. If it's in the list,
4983 * check the current AMP status, and update the the power-status according
4984 * to the mute status.
4985 *
4986 * This function is supposed to be set or called from the check_power_status
4987 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004988 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004989int snd_hda_check_amp_list_power(struct hda_codec *codec,
4990 struct hda_loopback_check *check,
4991 hda_nid_t nid)
4992{
Takashi Iwai031024e2011-05-02 11:29:30 +02004993 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004994 int ch, v;
4995
4996 if (!check->amplist)
4997 return 0;
4998 for (p = check->amplist; p->nid; p++) {
4999 if (p->nid == nid)
5000 break;
5001 }
5002 if (!p->nid)
5003 return 0; /* nothing changed */
5004
5005 for (p = check->amplist; p->nid; p++) {
5006 for (ch = 0; ch < 2; ch++) {
5007 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5008 p->idx);
5009 if (!(v & HDA_AMP_MUTE) && v > 0) {
5010 if (!check->power_on) {
5011 check->power_on = 1;
5012 snd_hda_power_up(codec);
5013 }
5014 return 1;
5015 }
5016 }
5017 }
5018 if (check->power_on) {
5019 check->power_on = 0;
5020 snd_hda_power_down(codec);
5021 }
5022 return 0;
5023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005024EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005027/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005028 * Channel mode helper
5029 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005030
5031/**
5032 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5033 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005034int snd_hda_ch_mode_info(struct hda_codec *codec,
5035 struct snd_ctl_elem_info *uinfo,
5036 const struct hda_channel_mode *chmode,
5037 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005038{
5039 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5040 uinfo->count = 1;
5041 uinfo->value.enumerated.items = num_chmodes;
5042 if (uinfo->value.enumerated.item >= num_chmodes)
5043 uinfo->value.enumerated.item = num_chmodes - 1;
5044 sprintf(uinfo->value.enumerated.name, "%dch",
5045 chmode[uinfo->value.enumerated.item].channels);
5046 return 0;
5047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005048EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005049
Takashi Iwaid5191e52009-11-16 14:58:17 +01005050/**
5051 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5052 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005053int snd_hda_ch_mode_get(struct hda_codec *codec,
5054 struct snd_ctl_elem_value *ucontrol,
5055 const struct hda_channel_mode *chmode,
5056 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005057 int max_channels)
5058{
5059 int i;
5060
5061 for (i = 0; i < num_chmodes; i++) {
5062 if (max_channels == chmode[i].channels) {
5063 ucontrol->value.enumerated.item[0] = i;
5064 break;
5065 }
5066 }
5067 return 0;
5068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005069EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005070
Takashi Iwaid5191e52009-11-16 14:58:17 +01005071/**
5072 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5073 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005074int snd_hda_ch_mode_put(struct hda_codec *codec,
5075 struct snd_ctl_elem_value *ucontrol,
5076 const struct hda_channel_mode *chmode,
5077 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005078 int *max_channelsp)
5079{
5080 unsigned int mode;
5081
5082 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005083 if (mode >= num_chmodes)
5084 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005085 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005086 return 0;
5087 /* change the current channel setting */
5088 *max_channelsp = chmode[mode].channels;
5089 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005090 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005091 return 1;
5092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005093EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095/*
5096 * input MUX helper
5097 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005098
5099/**
5100 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5101 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005102int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5103 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
5105 unsigned int index;
5106
5107 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5108 uinfo->count = 1;
5109 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005110 if (!imux->num_items)
5111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 index = uinfo->value.enumerated.item;
5113 if (index >= imux->num_items)
5114 index = imux->num_items - 1;
5115 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5116 return 0;
5117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005118EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Takashi Iwaid5191e52009-11-16 14:58:17 +01005120/**
5121 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5122 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005123int snd_hda_input_mux_put(struct hda_codec *codec,
5124 const struct hda_input_mux *imux,
5125 struct snd_ctl_elem_value *ucontrol,
5126 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 unsigned int *cur_val)
5128{
5129 unsigned int idx;
5130
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005131 if (!imux->num_items)
5132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 idx = ucontrol->value.enumerated.item[0];
5134 if (idx >= imux->num_items)
5135 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005136 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005138 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5139 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 *cur_val = idx;
5141 return 1;
5142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005143EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145
5146/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005147 * process kcontrol info callback of a simple string enum array
5148 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5149 */
5150int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5151 struct snd_ctl_elem_info *uinfo,
5152 int num_items, const char * const *texts)
5153{
5154 static const char * const texts_default[] = {
5155 "Disabled", "Enabled"
5156 };
5157
5158 if (!texts || !num_items) {
5159 num_items = 2;
5160 texts = texts_default;
5161 }
5162
5163 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5164 uinfo->count = 1;
5165 uinfo->value.enumerated.items = num_items;
5166 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5167 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5168 strcpy(uinfo->value.enumerated.name,
5169 texts[uinfo->value.enumerated.item]);
5170 return 0;
5171}
5172EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5173
5174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 * Multi-channel / digital-out PCM helper functions
5176 */
5177
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005178/* setup SPDIF output stream */
5179static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5180 unsigned int stream_tag, unsigned int format)
5181{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005182 struct hda_spdif_out *spdif;
5183 unsigned int curr_fmt;
5184 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005185
Laurence Darby3bef1c32012-11-03 17:00:06 +00005186 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5187 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5188 AC_VERB_GET_STREAM_FORMAT, 0);
5189 reset = codec->spdif_status_reset &&
5190 (spdif->ctls & AC_DIG1_ENABLE) &&
5191 curr_fmt != format;
5192
5193 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5194 updated */
5195 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005196 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005197 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005198 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005199 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005200 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005201 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005202 for (d = codec->slave_dig_outs; *d; d++)
5203 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5204 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005205 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005206 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005207 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005208 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005209 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005210}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005211
Takashi Iwai2f728532008-09-25 16:32:41 +02005212static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5213{
5214 snd_hda_codec_cleanup_stream(codec, nid);
5215 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005216 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005217 for (d = codec->slave_dig_outs; *d; d++)
5218 snd_hda_codec_cleanup_stream(codec, *d);
5219 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005220}
5221
Takashi Iwaid5191e52009-11-16 14:58:17 +01005222/**
5223 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5224 * @bus: HD-audio bus
5225 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005226void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5227{
5228 struct hda_codec *codec;
5229
5230 if (!bus)
5231 return;
5232 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005233 if (hda_codec_is_power_on(codec) &&
5234 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005235 codec->patch_ops.reboot_notify(codec);
5236 }
5237}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005238EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005239
Takashi Iwaid5191e52009-11-16 14:58:17 +01005240/**
5241 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005243int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5244 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
Ingo Molnar62932df2006-01-16 16:34:20 +01005246 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005247 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5248 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005249 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005251 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 return 0;
5253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005254EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Takashi Iwaid5191e52009-11-16 14:58:17 +01005256/**
5257 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5258 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005259int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5260 struct hda_multi_out *mout,
5261 unsigned int stream_tag,
5262 unsigned int format,
5263 struct snd_pcm_substream *substream)
5264{
5265 mutex_lock(&codec->spdif_mutex);
5266 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5267 mutex_unlock(&codec->spdif_mutex);
5268 return 0;
5269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005270EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005271
Takashi Iwaid5191e52009-11-16 14:58:17 +01005272/**
5273 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5274 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005275int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5276 struct hda_multi_out *mout)
5277{
5278 mutex_lock(&codec->spdif_mutex);
5279 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5280 mutex_unlock(&codec->spdif_mutex);
5281 return 0;
5282}
5283EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5284
Takashi Iwaid5191e52009-11-16 14:58:17 +01005285/**
5286 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005288int snd_hda_multi_out_dig_close(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005293 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 return 0;
5295}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005296EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
Takashi Iwaid5191e52009-11-16 14:58:17 +01005298/**
5299 * snd_hda_multi_out_analog_open - open analog outputs
5300 *
5301 * Open analog outputs and set up the hw-constraints.
5302 * If the digital outputs can be opened as slave, open the digital
5303 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005305int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5306 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005307 struct snd_pcm_substream *substream,
5308 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
Takashi Iwai9a081602008-02-12 18:37:26 +01005310 struct snd_pcm_runtime *runtime = substream->runtime;
5311 runtime->hw.channels_max = mout->max_channels;
5312 if (mout->dig_out_nid) {
5313 if (!mout->analog_rates) {
5314 mout->analog_rates = hinfo->rates;
5315 mout->analog_formats = hinfo->formats;
5316 mout->analog_maxbps = hinfo->maxbps;
5317 } else {
5318 runtime->hw.rates = mout->analog_rates;
5319 runtime->hw.formats = mout->analog_formats;
5320 hinfo->maxbps = mout->analog_maxbps;
5321 }
5322 if (!mout->spdif_rates) {
5323 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5324 &mout->spdif_rates,
5325 &mout->spdif_formats,
5326 &mout->spdif_maxbps);
5327 }
5328 mutex_lock(&codec->spdif_mutex);
5329 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005330 if ((runtime->hw.rates & mout->spdif_rates) &&
5331 (runtime->hw.formats & mout->spdif_formats)) {
5332 runtime->hw.rates &= mout->spdif_rates;
5333 runtime->hw.formats &= mout->spdif_formats;
5334 if (mout->spdif_maxbps < hinfo->maxbps)
5335 hinfo->maxbps = mout->spdif_maxbps;
5336 } else {
5337 mout->share_spdif = 0;
5338 /* FIXME: need notify? */
5339 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005340 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005341 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5344 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005346EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
Takashi Iwaid5191e52009-11-16 14:58:17 +01005348/**
5349 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5350 *
5351 * Set up the i/o for analog out.
5352 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005354int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5355 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 unsigned int stream_tag,
5357 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005358 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
Takashi Iwaidda14412011-05-02 11:29:30 +02005360 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005362 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 int i;
5364
Ingo Molnar62932df2006-01-16 16:34:20 +01005365 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005366 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005367 if (mout->dig_out_nid && mout->share_spdif &&
5368 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005370 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5371 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005372 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005374 setup_dig_out_stream(codec, mout->dig_out_nid,
5375 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 } else {
5377 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005378 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 }
5380 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005381 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
5383 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005384 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5385 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005386 if (!mout->no_share_stream &&
5387 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005389 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5390 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005391 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005392 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5393 if (!mout->no_share_stream && mout->hp_out_nid[i])
5394 snd_hda_codec_setup_stream(codec,
5395 mout->hp_out_nid[i],
5396 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 /* surrounds */
5399 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005400 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005401 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5402 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005403 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005404 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5405 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005407
5408 /* extra surrounds */
5409 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5410 int ch = 0;
5411 if (!mout->extra_out_nid[i])
5412 break;
5413 if (chs >= (i + 1) * 2)
5414 ch = i * 2;
5415 else if (!mout->no_share_stream)
5416 break;
5417 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5418 stream_tag, ch, format);
5419 }
5420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return 0;
5422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005423EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
Takashi Iwaid5191e52009-11-16 14:58:17 +01005425/**
5426 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005428int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5429 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430{
Takashi Iwaidda14412011-05-02 11:29:30 +02005431 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 int i;
5433
5434 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005435 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005437 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005438 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5439 if (mout->hp_out_nid[i])
5440 snd_hda_codec_cleanup_stream(codec,
5441 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005442 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5443 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005444 snd_hda_codec_cleanup_stream(codec,
5445 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005446 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005448 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 mout->dig_out_used = 0;
5450 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005451 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 return 0;
5453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005454EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Takashi Iwai47408602012-04-20 13:06:53 +02005456/**
5457 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5458 *
5459 * Guess the suitable VREF pin bits to be set as the pin-control value.
5460 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5461 */
5462unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5463{
5464 unsigned int pincap;
5465 unsigned int oldval;
5466 oldval = snd_hda_codec_read(codec, pin, 0,
5467 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5468 pincap = snd_hda_query_pin_caps(codec, pin);
5469 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5470 /* Exception: if the default pin setup is vref50, we give it priority */
5471 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5472 return AC_PINCTL_VREF_80;
5473 else if (pincap & AC_PINCAP_VREF_50)
5474 return AC_PINCTL_VREF_50;
5475 else if (pincap & AC_PINCAP_VREF_100)
5476 return AC_PINCTL_VREF_100;
5477 else if (pincap & AC_PINCAP_VREF_GRD)
5478 return AC_PINCTL_VREF_GRD;
5479 return AC_PINCTL_VREF_HIZ;
5480}
5481EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5482
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005483/* correct the pin ctl value for matching with the pin cap */
5484unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5485 hda_nid_t pin, unsigned int val)
5486{
5487 static unsigned int cap_lists[][2] = {
5488 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5489 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5490 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5491 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5492 };
5493 unsigned int cap;
5494
5495 if (!val)
5496 return 0;
5497 cap = snd_hda_query_pin_caps(codec, pin);
5498 if (!cap)
5499 return val; /* don't know what to do... */
5500
5501 if (val & AC_PINCTL_OUT_EN) {
5502 if (!(cap & AC_PINCAP_OUT))
5503 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5504 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5505 val &= ~AC_PINCTL_HP_EN;
5506 }
5507
5508 if (val & AC_PINCTL_IN_EN) {
5509 if (!(cap & AC_PINCAP_IN))
5510 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5511 else {
5512 unsigned int vcap, vref;
5513 int i;
5514 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5515 vref = val & AC_PINCTL_VREFEN;
5516 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5517 if (vref == cap_lists[i][0] &&
5518 !(vcap & cap_lists[i][1])) {
5519 if (i == ARRAY_SIZE(cap_lists) - 1)
5520 vref = AC_PINCTL_VREF_HIZ;
5521 else
5522 vref = cap_lists[i + 1][0];
5523 }
5524 }
5525 val &= ~AC_PINCTL_VREFEN;
5526 val |= vref;
5527 }
5528 }
5529
5530 return val;
5531}
5532EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5533
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005534int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5535 unsigned int val, bool cached)
5536{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005537 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005538 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005539 if (cached)
5540 return snd_hda_codec_update_cache(codec, pin, 0,
5541 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5542 else
5543 return snd_hda_codec_write(codec, pin, 0,
5544 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5545}
5546EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5547
Takashi Iwai990061c2010-09-09 22:08:44 +02005548/**
5549 * snd_hda_add_imux_item - Add an item to input_mux
5550 *
5551 * When the same label is used already in the existing items, the number
5552 * suffix is appended to the label. This label index number is stored
5553 * to type_idx when non-NULL pointer is given.
5554 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005555int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5556 int index, int *type_idx)
5557{
5558 int i, label_idx = 0;
5559 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5560 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5561 return -EINVAL;
5562 }
5563 for (i = 0; i < imux->num_items; i++) {
5564 if (!strncmp(label, imux->items[i].label, strlen(label)))
5565 label_idx++;
5566 }
5567 if (type_idx)
5568 *type_idx = label_idx;
5569 if (label_idx > 0)
5570 snprintf(imux->items[imux->num_items].label,
5571 sizeof(imux->items[imux->num_items].label),
5572 "%s %d", label, label_idx);
5573 else
5574 strlcpy(imux->items[imux->num_items].label, label,
5575 sizeof(imux->items[imux->num_items].label));
5576 imux->items[imux->num_items].index = index;
5577 imux->num_items++;
5578 return 0;
5579}
5580EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005581
Takashi Iwai4a471b72005-12-07 13:56:29 +01005582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583#ifdef CONFIG_PM
5584/*
5585 * power management
5586 */
5587
5588/**
5589 * snd_hda_suspend - suspend the codecs
5590 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 *
5592 * Returns 0 if successful.
5593 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005594int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005596 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Takashi Iwai0ba21762007-04-16 11:29:14 +02005598 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005599 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005600 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005601 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603 return 0;
5604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005605EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607/**
5608 * snd_hda_resume - resume the codecs
5609 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 *
5611 * Returns 0 if successful.
5612 */
5613int snd_hda_resume(struct hda_bus *bus)
5614{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005615 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Takashi Iwai0ba21762007-04-16 11:29:14 +02005617 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005618 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 return 0;
5621}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005622EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005623#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005624
5625/*
5626 * generic arrays
5627 */
5628
Takashi Iwaid5191e52009-11-16 14:58:17 +01005629/**
5630 * snd_array_new - get a new element from the given array
5631 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005632 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005633 * Get a new element from the given array. If it exceeds the
5634 * pre-allocated array size, re-allocate the array.
5635 *
5636 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005637 */
5638void *snd_array_new(struct snd_array *array)
5639{
Takashi Iwai12f17712012-10-10 08:59:14 +02005640 if (snd_BUG_ON(!array->elem_size))
5641 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005642 if (array->used >= array->alloced) {
5643 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005644 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005645 void *nlist;
5646 if (snd_BUG_ON(num >= 4096))
5647 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005648 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005649 if (!nlist)
5650 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005651 array->list = nlist;
5652 array->alloced = num;
5653 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005654 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005655}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005656EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005657
Takashi Iwaid5191e52009-11-16 14:58:17 +01005658/**
5659 * snd_array_free - free the given array elements
5660 * @array: the array object
5661 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005662void snd_array_free(struct snd_array *array)
5663{
5664 kfree(array->list);
5665 array->used = 0;
5666 array->alloced = 0;
5667 array->list = NULL;
5668}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005669EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005670
Takashi Iwaid5191e52009-11-16 14:58:17 +01005671/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005672 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5673 * @pcm: PCM caps bits
5674 * @buf: the string buffer to write
5675 * @buflen: the max buffer length
5676 *
5677 * used by hda_proc.c and hda_eld.c
5678 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005679void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5680{
5681 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5682 int i, j;
5683
5684 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5685 if (pcm & (AC_SUPPCM_BITS_8 << i))
5686 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5687
5688 buf[j] = '\0'; /* necessary when j == 0 */
5689}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005690EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005691
5692MODULE_DESCRIPTION("HDA codec core");
5693MODULE_LICENSE("GPL");