blob: 8a005f0e5ca484c842e2fe5f684275abafbde248 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200193 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200194 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)nid << 20;
202 val |= verb << 8;
203 val |= parm;
204 return val;
205}
206
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207/*
208 * Send and receive a verb
209 */
210static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200211 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212{
213 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215
Wu Fengguang6430aee2009-07-17 16:49:19 +0800216 if (cmd == ~0)
217 return -1;
218
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200219 if (res)
220 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200221 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200222 snd_hda_power_up(codec);
223 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200224 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
225 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100226 for (;;) {
227 trace_hda_send_cmd(codec, cmd);
228 err = bus->ops.command(bus, cmd);
229 if (err != -EAGAIN)
230 break;
231 /* process pending verbs */
232 bus->ops.get_response(bus, codec->addr);
233 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200234 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800235 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200236 trace_hda_get_response(codec, *res);
237 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200238 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200239 mutex_unlock(&bus->cmd_mutex);
240 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100241 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200242 if (bus->response_reset) {
243 snd_printd("hda_codec: resetting BUS due to "
244 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200245 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200246 bus->ops.bus_reset(bus);
247 }
248 goto again;
249 }
250 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100251 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200252 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200253 return err;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
257 * snd_hda_codec_read - send a command and get the response
258 * @codec: the HDA codec
259 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200260 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * @verb: the verb to send
262 * @parm: the parameter for the verb
263 *
264 * Send a single command and read the corresponding response.
265 *
266 * Returns the obtained response value, or -1 for an error.
267 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200268unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200269 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int verb, unsigned int parm)
271{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200272 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200273 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200274 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700275 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return res;
277}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100278EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/**
281 * snd_hda_codec_write - send a single command without waiting for response
282 * @codec: the HDA codec
283 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200284 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @verb: the verb to send
286 * @parm: the parameter for the verb
287 *
288 * Send a single command without waiting for response.
289 *
290 * Returns 0 if successful, or a negative error code.
291 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200292int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
293 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200295 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100296 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200297 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200298 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100300EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302/**
303 * snd_hda_sequence_write - sequence writes
304 * @codec: the HDA codec
305 * @seq: VERB array to send
306 *
307 * Send the commands sequentially from the given array.
308 * The array must be terminated with NID=0.
309 */
310void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
311{
312 for (; seq->nid; seq++)
313 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100315EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/**
318 * snd_hda_get_sub_nodes - get the range of sub nodes
319 * @codec: the HDA codec
320 * @nid: NID to parse
321 * @start_id: the pointer to store the start NID
322 *
323 * Parse the NID and store the start NID of its sub-nodes.
324 * Returns the number of sub-nodes.
325 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200326int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
327 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned int parm;
330
331 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200332 if (parm == -1)
333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *start_id = (parm >> 16) & 0x7fff;
335 return (int)(parm & 0x7fff);
336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100337EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100339/* connection list element */
340struct hda_conn_list {
341 struct list_head list;
342 int len;
343 hda_nid_t nid;
344 hda_nid_t conns[0];
345};
346
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200347/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348static struct hda_conn_list *
349lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200350{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100351 struct hda_conn_list *p;
352 list_for_each_entry(p, &codec->conn_list, list) {
353 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 }
356 return NULL;
357}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
360 const hda_nid_t *list)
361{
362 struct hda_conn_list *p;
363
364 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
365 if (!p)
366 return -ENOMEM;
367 p->len = len;
368 p->nid = nid;
369 memcpy(p->conns, list, len * sizeof(hda_nid_t));
370 list_add(&p->list, &codec->conn_list);
371 return 0;
372}
373
374static void remove_conn_list(struct hda_codec *codec)
375{
376 while (!list_empty(&codec->conn_list)) {
377 struct hda_conn_list *p;
378 p = list_first_entry(&codec->conn_list, typeof(*p), list);
379 list_del(&p->list);
380 kfree(p);
381 }
382}
383
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200384/* read the connection and add to the cache */
385static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200386{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100387 hda_nid_t list[32];
388 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200391 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100392 if (len == -ENOSPC) {
393 len = snd_hda_get_num_raw_conns(codec, nid);
394 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
395 if (!result)
396 return -ENOMEM;
397 len = snd_hda_get_raw_connections(codec, nid, result, len);
398 }
399 if (len >= 0)
400 len = snd_hda_override_conn_list(codec, nid, len, result);
401 if (result != list)
402 kfree(result);
403 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404}
Takashi Iwaidce20792011-06-24 14:10:28 +0200405
406/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
412 *
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
415 *
416 * Returns the number of connections, or a negative error code.
417 *
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
421 */
422int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
423 const hda_nid_t **listp)
424{
425 bool added = false;
426
427 for (;;) {
428 int err;
429 const struct hda_conn_list *p;
430
431 /* if the connection-list is already cached, read it */
432 p = lookup_conn_list(codec, nid);
433 if (p) {
434 if (listp)
435 *listp = p->conns;
436 return p->len;
437 }
438 if (snd_BUG_ON(added))
439 return -EINVAL;
440
441 err = read_and_add_raw_conns(codec, nid);
442 if (err < 0)
443 return err;
444 added = true;
445 }
446}
447EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
448
449/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200450 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @codec: the HDA codec
452 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200453 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * @max_conns: max. number of connections to store
455 *
456 * Parses the connection list of the given widget and stores the list
457 * of NIDs.
458 *
459 * Returns the number of connections, or a negative error code.
460 */
461int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200462 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200463{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464 const hda_nid_t *list;
465 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200466
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100467 if (len > 0 && conn_list) {
468 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 snd_printk(KERN_ERR "hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
471 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200472 return -EINVAL;
473 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100474 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200475 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200476
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100477 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200478}
479EXPORT_SYMBOL_HDA(snd_hda_get_connections);
480
Takashi Iwai4eea3092013-02-07 18:18:19 +0100481/* return CONNLIST_LEN parameter of the given widget */
482static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
483{
484 unsigned int wcaps = get_wcaps(codec, nid);
485 unsigned int parm;
486
487 if (!(wcaps & AC_WCAP_CONN_LIST) &&
488 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
489 return 0;
490
491 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
492 if (parm == -1)
493 parm = 0;
494 return parm;
495}
496
497int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
498{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100499 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100500}
501
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200502/**
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
505 * @nid: NID to parse
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
508 *
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
512 */
513int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
514 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100519 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100520 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai4eea3092013-02-07 18:18:19 +0100522 parm = get_num_conns(codec, nid);
523 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200524 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (parm & AC_CLIST_LONG) {
527 /* long form */
528 shift = 16;
529 num_elems = 2;
530 } else {
531 /* short form */
532 shift = 8;
533 num_elems = 4;
534 }
535 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mask = (1 << (shift-1)) - 1;
537
Takashi Iwai0ba21762007-04-16 11:29:14 +0200538 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0; /* no connection */
540
541 if (conn_len == 1) {
542 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200543 parm = snd_hda_codec_read(codec, nid, 0,
544 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200545 if (parm == -1 && codec->bus->rirb_error)
546 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100547 if (conn_list)
548 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 1;
550 }
551
552 /* multi connection */
553 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
558
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100567 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200568 snd_printk(KERN_WARNING "hda_codec: "
569 "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
669/**
670 * snd_hda_queue_unsol_event - add an unsolicited event to queue
671 * @bus: the BUS
672 * @res: unsolicited event (lower 32bit of RIRB entry)
673 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
674 *
675 * Adds the given event to the queue. The events are processed in
676 * the workqueue asynchronously. Call this function in the interrupt
677 * hanlder when RIRB receives an unsolicited event.
678 *
679 * Returns 0 if successful, or a negative error code.
680 */
681int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
682{
683 struct hda_bus_unsolicited *unsol;
684 unsigned int wp;
685
Wang YanQing2195b062013-05-07 11:27:33 +0800686 if (!bus || !bus->workq)
687 return 0;
688
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200689 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 unsol = bus->unsol;
691 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693
694 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
695 unsol->wp = wp;
696
697 wp <<= 1;
698 unsol->queue[wp] = res;
699 unsol->queue[wp + 1] = res_ex;
700
Takashi Iwai6acaed32009-01-12 10:09:24 +0100701 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return 0;
704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100705EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800708 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
David Howellsc4028952006-11-22 14:57:56 +0000710static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
David Howellsc4028952006-11-22 14:57:56 +0000712 struct hda_bus_unsolicited *unsol =
713 container_of(work, struct hda_bus_unsolicited, work);
714 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct hda_codec *codec;
716 unsigned int rp, caddr, res;
717
718 while (unsol->rp != unsol->wp) {
719 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
720 unsol->rp = rp;
721 rp <<= 1;
722 res = unsol->queue[rp];
723 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200724 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 continue;
726 codec = bus->caddr_tbl[caddr & 0x0f];
727 if (codec && codec->patch_ops.unsol_event)
728 codec->patch_ops.unsol_event(codec, res);
729 }
730}
731
732/*
733 * initialize unsolicited queue
734 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200735static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hda_bus_unsolicited *unsol;
738
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100739 if (bus->unsol) /* already initialized */
740 return 0;
741
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200742 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200743 if (!unsol) {
744 snd_printk(KERN_ERR "hda_codec: "
745 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -ENOMEM;
747 }
David Howellsc4028952006-11-22 14:57:56 +0000748 INIT_WORK(&unsol->work, process_unsol_events);
749 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 bus->unsol = unsol;
751 return 0;
752}
753
754/*
755 * destructor
756 */
757static void snd_hda_codec_free(struct hda_codec *codec);
758
759static int snd_hda_bus_free(struct hda_bus *bus)
760{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200761 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100765 if (bus->workq)
766 flush_workqueue(bus->workq);
767 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200769 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 snd_hda_codec_free(codec);
771 }
772 if (bus->ops.private_free)
773 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100774 if (bus->workq)
775 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(bus);
777 return 0;
778}
779
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100780static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d3532008-11-21 09:08:06 +0100783 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return snd_hda_bus_free(bus);
785}
786
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200787#ifdef CONFIG_SND_HDA_HWDEP
788static int snd_hda_bus_dev_register(struct snd_device *device)
789{
790 struct hda_bus *bus = device->device_data;
791 struct hda_codec *codec;
792 list_for_each_entry(codec, &bus->codec_list, list) {
793 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100794 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200795 }
796 return 0;
797}
798#else
799#define snd_hda_bus_dev_register NULL
800#endif
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/**
803 * snd_hda_bus_new - create a HDA bus
804 * @card: the card entry
805 * @temp: the template for hda_bus information
806 * @busp: the pointer to store the created bus instance
807 *
808 * Returns 0 if successful, or a negative error code.
809 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100810int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200811 const struct hda_bus_template *temp,
812 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct hda_bus *bus;
815 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100816 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200817 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .dev_free = snd_hda_bus_dev_free,
819 };
820
Takashi Iwaida3cec32008-08-08 17:12:14 +0200821 if (snd_BUG_ON(!temp))
822 return -EINVAL;
823 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (busp)
827 *busp = NULL;
828
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200829 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (bus == NULL) {
831 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
832 return -ENOMEM;
833 }
834
835 bus->card = card;
836 bus->private_data = temp->private_data;
837 bus->pci = temp->pci;
838 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100839 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 bus->ops = temp->ops;
841
Ingo Molnar62932df2006-01-16 16:34:20 +0100842 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200843 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 INIT_LIST_HEAD(&bus->codec_list);
845
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100846 snprintf(bus->workq_name, sizeof(bus->workq_name),
847 "hd-audio%d", card->number);
848 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100849 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100850 snd_printk(KERN_ERR "cannot create workqueue %s\n",
851 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100852 kfree(bus);
853 return -ENOMEM;
854 }
855
Takashi Iwai0ba21762007-04-16 11:29:14 +0200856 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
857 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 snd_hda_bus_free(bus);
859 return err;
860 }
861 if (busp)
862 *busp = bus;
863 return 0;
864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100865EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Takashi Iwai82467612007-07-27 19:15:54 +0200867#ifdef CONFIG_SND_HDA_GENERIC
868#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200870#else
871#define is_generic_config(codec) 0
872#endif
873
Takashi Iwai645f10c2008-11-28 15:07:37 +0100874#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100875#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
876#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100877#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100878#endif
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * find a matching codec preset
882 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200883static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200884find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100886 struct hda_codec_preset_list *tbl;
887 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200888 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Takashi Iwai82467612007-07-27 19:15:54 +0200890 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100891 return NULL; /* use the generic parser */
892
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100893 again:
894 mutex_lock(&preset_mutex);
895 list_for_each_entry(tbl, &hda_preset_tables, list) {
896 if (!try_module_get(tbl->owner)) {
897 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
898 continue;
899 }
900 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100902 if (preset->afg && preset->afg != codec->afg)
903 continue;
904 if (preset->mfg && preset->mfg != codec->mfg)
905 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200908 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200909 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100910 preset->rev == codec->revision_id)) {
911 mutex_unlock(&preset_mutex);
912 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100916 module_put(tbl->owner);
917 }
918 mutex_unlock(&preset_mutex);
919
920 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
921 char name[32];
922 if (!mod_requested)
923 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
924 codec->vendor_id);
925 else
926 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
927 (codec->vendor_id >> 16) & 0xffff);
928 request_module(name);
929 mod_requested++;
930 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 return NULL;
933}
934
935/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200936 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200938static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 const struct hda_vendor_id *c;
941 const char *vendor = NULL;
942 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200943 char tmp[16];
944
945 if (codec->vendor_name)
946 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 for (c = hda_vendor_ids; c->id; c++) {
949 if (c->id == vendor_id) {
950 vendor = c->name;
951 break;
952 }
953 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200954 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sprintf(tmp, "Generic %04x", vendor_id);
956 vendor = tmp;
957 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200958 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
959 if (!codec->vendor_name)
960 return -ENOMEM;
961
962 get_chip_name:
963 if (codec->chip_name)
964 return 0;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200967 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
968 else {
969 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
970 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
971 }
972 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200973 return -ENOMEM;
974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200978 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100980static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200982 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 hda_nid_t nid;
984
985 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
986 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200987 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200988 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200989 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200990 case AC_GRP_AUDIO_FUNCTION:
991 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200992 codec->afg_function_id = function_id & 0xff;
993 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 break;
995 case AC_GRP_MODEM_FUNCTION:
996 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200997 codec->mfg_function_id = function_id & 0xff;
998 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200999 break;
1000 default:
1001 break;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001007 * read widget caps for each widget and store in cache
1008 */
1009static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1010{
1011 int i;
1012 hda_nid_t nid;
1013
1014 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1015 &codec->start_nid);
1016 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001017 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001018 return -ENOMEM;
1019 nid = codec->start_nid;
1020 for (i = 0; i < codec->num_nodes; i++, nid++)
1021 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1022 AC_PAR_AUDIO_WIDGET_CAP);
1023 return 0;
1024}
1025
Takashi Iwai3be14142009-02-20 14:11:16 +01001026/* read all pin default configurations and save codec->init_pins */
1027static int read_pin_defaults(struct hda_codec *codec)
1028{
1029 int i;
1030 hda_nid_t nid = codec->start_nid;
1031
1032 for (i = 0; i < codec->num_nodes; i++, nid++) {
1033 struct hda_pincfg *pin;
1034 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001035 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001036 if (wid_type != AC_WID_PIN)
1037 continue;
1038 pin = snd_array_new(&codec->init_pins);
1039 if (!pin)
1040 return -ENOMEM;
1041 pin->nid = nid;
1042 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1043 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001044 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1045 AC_VERB_GET_PIN_WIDGET_CONTROL,
1046 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001047 }
1048 return 0;
1049}
1050
1051/* look up the given pin config list and return the item matching with NID */
1052static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1053 struct snd_array *array,
1054 hda_nid_t nid)
1055{
1056 int i;
1057 for (i = 0; i < array->used; i++) {
1058 struct hda_pincfg *pin = snd_array_elem(array, i);
1059 if (pin->nid == nid)
1060 return pin;
1061 }
1062 return NULL;
1063}
1064
Takashi Iwai3be14142009-02-20 14:11:16 +01001065/* set the current pin config value for the given NID.
1066 * the value is cached, and read via snd_hda_codec_get_pincfg()
1067 */
1068int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1069 hda_nid_t nid, unsigned int cfg)
1070{
1071 struct hda_pincfg *pin;
1072
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001073 /* the check below may be invalid when pins are added by a fixup
1074 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1075 * for now
1076 */
1077 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001078 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1079 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001080 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001081
Takashi Iwai3be14142009-02-20 14:11:16 +01001082 pin = look_up_pincfg(codec, list, nid);
1083 if (!pin) {
1084 pin = snd_array_new(list);
1085 if (!pin)
1086 return -ENOMEM;
1087 pin->nid = nid;
1088 }
1089 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 return 0;
1091}
1092
Takashi Iwaid5191e52009-11-16 14:58:17 +01001093/**
1094 * snd_hda_codec_set_pincfg - Override a pin default configuration
1095 * @codec: the HDA codec
1096 * @nid: NID to set the pin config
1097 * @cfg: the pin default config value
1098 *
1099 * Override a pin default configuration value in the cache.
1100 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1101 * priority than the real hardware value.
1102 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001103int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1104 hda_nid_t nid, unsigned int cfg)
1105{
Takashi Iwai346ff702009-02-23 09:42:57 +01001106 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001107}
1108EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1109
Takashi Iwaid5191e52009-11-16 14:58:17 +01001110/**
1111 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1112 * @codec: the HDA codec
1113 * @nid: NID to get the pin config
1114 *
1115 * Get the current pin config value of the given pin NID.
1116 * If the pincfg value is cached or overridden via sysfs or driver,
1117 * returns the cached value.
1118 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001119unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1120{
1121 struct hda_pincfg *pin;
1122
Takashi Iwai3be14142009-02-20 14:11:16 +01001123#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001124 {
1125 unsigned int cfg = 0;
1126 mutex_lock(&codec->user_mutex);
1127 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1128 if (pin)
1129 cfg = pin->cfg;
1130 mutex_unlock(&codec->user_mutex);
1131 if (cfg)
1132 return cfg;
1133 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001134#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001135 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1136 if (pin)
1137 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001138 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1139 if (pin)
1140 return pin->cfg;
1141 return 0;
1142}
1143EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1144
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001145/* remember the current pinctl target value */
1146int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1147 unsigned int val)
1148{
1149 struct hda_pincfg *pin;
1150
1151 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1152 if (!pin)
1153 return -EINVAL;
1154 pin->target = val;
1155 return 0;
1156}
1157EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1158
1159/* return the current pinctl target value */
1160int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1161{
1162 struct hda_pincfg *pin;
1163
1164 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1165 if (!pin)
1166 return 0;
1167 return pin->target;
1168}
1169EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1170
Takashi Iwai92ee6162009-12-27 11:18:59 +01001171/**
1172 * snd_hda_shutup_pins - Shut up all pins
1173 * @codec: the HDA codec
1174 *
1175 * Clear all pin controls to shup up before suspend for avoiding click noise.
1176 * The controls aren't cached so that they can be resumed properly.
1177 */
1178void snd_hda_shutup_pins(struct hda_codec *codec)
1179{
1180 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001181 /* don't shut up pins when unloading the driver; otherwise it breaks
1182 * the default pin setup at the next load of the driver
1183 */
1184 if (codec->bus->shutdown)
1185 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001186 for (i = 0; i < codec->init_pins.used; i++) {
1187 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1188 /* use read here for syncing after issuing each verb */
1189 snd_hda_codec_read(codec, pin->nid, 0,
1190 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1191 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001192 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001193}
1194EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1195
Takashi Iwai2a439522011-07-26 09:52:50 +02001196#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001197/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1198static void restore_shutup_pins(struct hda_codec *codec)
1199{
1200 int i;
1201 if (!codec->pins_shutup)
1202 return;
1203 if (codec->bus->shutdown)
1204 return;
1205 for (i = 0; i < codec->init_pins.used; i++) {
1206 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1207 snd_hda_codec_write(codec, pin->nid, 0,
1208 AC_VERB_SET_PIN_WIDGET_CONTROL,
1209 pin->ctrl);
1210 }
1211 codec->pins_shutup = 0;
1212}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001213#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001214
David Henningsson26a6cb62012-10-09 15:04:21 +02001215static void hda_jackpoll_work(struct work_struct *work)
1216{
1217 struct hda_codec *codec =
1218 container_of(work, struct hda_codec, jackpoll_work.work);
1219 if (!codec->jackpoll_interval)
1220 return;
1221
1222 snd_hda_jack_set_dirty_all(codec);
1223 snd_hda_jack_poll_all(codec);
1224 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1225 codec->jackpoll_interval);
1226}
1227
Takashi Iwai01751f52007-08-10 16:59:39 +02001228static void init_hda_cache(struct hda_cache_rec *cache,
1229 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001230static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001231
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001232/* release all pincfg lists */
1233static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001234{
Takashi Iwai346ff702009-02-23 09:42:57 +01001235 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001236#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001237 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001238#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001239 snd_array_free(&codec->init_pins);
1240}
1241
Takashi Iwai54d17402005-11-21 16:33:22 +01001242/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001243 * audio-converter setup caches
1244 */
1245struct hda_cvt_setup {
1246 hda_nid_t nid;
1247 u8 stream_tag;
1248 u8 channel_id;
1249 u16 format_id;
1250 unsigned char active; /* cvt is currently used */
1251 unsigned char dirty; /* setups should be cleared */
1252};
1253
1254/* get or create a cache entry for the given audio converter NID */
1255static struct hda_cvt_setup *
1256get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1257{
1258 struct hda_cvt_setup *p;
1259 int i;
1260
1261 for (i = 0; i < codec->cvt_setups.used; i++) {
1262 p = snd_array_elem(&codec->cvt_setups, i);
1263 if (p->nid == nid)
1264 return p;
1265 }
1266 p = snd_array_new(&codec->cvt_setups);
1267 if (p)
1268 p->nid = nid;
1269 return p;
1270}
1271
1272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * codec destructor
1274 */
1275static void snd_hda_codec_free(struct hda_codec *codec)
1276{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001279 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001280 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001281 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001282#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001283 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001284 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001287 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001288 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001289 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001290 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001291 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 codec->bus->caddr_tbl[codec->addr] = NULL;
1293 if (codec->patch_ops.free)
1294 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001295#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001296 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001297 hda_call_pm_notify(codec->bus, false);
1298#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001299 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001300 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001301 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001302 kfree(codec->vendor_name);
1303 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001304 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001305 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(codec);
1307}
1308
Mengdong Linb8dfc462012-08-23 17:32:30 +08001309static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1310 hda_nid_t fg, unsigned int power_state);
1311
Takashi Iwaid8193872012-08-31 07:54:38 -07001312static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001313 unsigned int power_state);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/**
1316 * snd_hda_codec_new - create a HDA codec
1317 * @bus: the bus to assign
1318 * @codec_addr: the codec address
1319 * @codecp: the pointer to store the generated codec
1320 *
1321 * Returns 0 if successful, or a negative error code.
1322 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001323int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001324 unsigned int codec_addr,
1325 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001328 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001329 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err;
1331
Takashi Iwaida3cec32008-08-08 17:12:14 +02001332 if (snd_BUG_ON(!bus))
1333 return -EINVAL;
1334 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001338 snd_printk(KERN_ERR "hda_codec: "
1339 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EBUSY;
1341 }
1342
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001343 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (codec == NULL) {
1345 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1346 return -ENOMEM;
1347 }
1348
1349 codec->bus = bus;
1350 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001351 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001352 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001353 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001354 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001355 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001356 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1357 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001358 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001359 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001360 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001361 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001362 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001363 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001364 INIT_LIST_HEAD(&codec->conn_list);
1365
David Henningsson26a6cb62012-10-09 15:04:21 +02001366 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai83012a72012-08-24 18:38:08 +02001368#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001369 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001370 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1371 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1372 * the caller has to power down appropriatley after initialization
1373 * phase.
1374 */
1375 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001376 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001377#endif
1378
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001379 if (codec->bus->modelname) {
1380 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1381 if (!codec->modelname) {
1382 snd_hda_codec_free(codec);
1383 return -ENODEV;
1384 }
1385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 list_add_tail(&codec->list, &bus->codec_list);
1388 bus->caddr_tbl[codec_addr] = codec;
1389
Takashi Iwai0ba21762007-04-16 11:29:14 +02001390 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1391 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001392 if (codec->vendor_id == -1)
1393 /* read again, hopefully the access method was corrected
1394 * in the last read...
1395 */
1396 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1397 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1399 AC_PAR_SUBSYSTEM_ID);
1400 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1401 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001403 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001404 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001405 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001406 err = -ENODEV;
1407 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Takashi Iwaid8193872012-08-31 07:54:38 -07001410 fg = codec->afg ? codec->afg : codec->mfg;
1411 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001412 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001413 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001414 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001415 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001416 err = read_pin_defaults(codec);
1417 if (err < 0)
1418 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001419
Takashi Iwai0ba21762007-04-16 11:29:14 +02001420 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001422 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001423 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001424 }
1425
Takashi Iwai83012a72012-08-24 18:38:08 +02001426#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001427 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc462012-08-23 17:32:30 +08001428 AC_PWRST_CLKSTOP);
1429 if (!codec->d3_stop_clk)
1430 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001431#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001432 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001433 AC_PWRST_EPSS);
Mengdong Linb8dfc462012-08-23 17:32:30 +08001434
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001435 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001436 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001437
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001438 snd_hda_codec_proc_new(codec);
1439
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001440 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001441
1442 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1443 codec->subsystem_id, codec->revision_id);
1444 snd_component_add(codec->bus->card, component);
1445
1446 if (codecp)
1447 *codecp = codec;
1448 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001449
1450 error:
1451 snd_hda_codec_free(codec);
1452 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001454EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001455
Mengdong Lina15d05d2013-02-08 17:09:31 -05001456int snd_hda_codec_update_widgets(struct hda_codec *codec)
1457{
1458 hda_nid_t fg;
1459 int err;
1460
1461 /* Assume the function group node does not change,
1462 * only the widget nodes may change.
1463 */
1464 kfree(codec->wcaps);
1465 fg = codec->afg ? codec->afg : codec->mfg;
1466 err = read_widget_caps(codec, fg);
1467 if (err < 0) {
1468 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1469 return err;
1470 }
1471
1472 snd_array_free(&codec->init_pins);
1473 err = read_pin_defaults(codec);
1474
1475 return err;
1476}
1477EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1478
1479
Takashi Iwaid5191e52009-11-16 14:58:17 +01001480/**
1481 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1482 * @codec: the HDA codec
1483 *
1484 * Start parsing of the given codec tree and (re-)initialize the whole
1485 * patch instance.
1486 *
1487 * Returns 0 if successful or a negative error code.
1488 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001489int snd_hda_codec_configure(struct hda_codec *codec)
1490{
1491 int err;
1492
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001493 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001494 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001495 err = get_codec_name(codec);
1496 if (err < 0)
1497 return err;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Takashi Iwai82467612007-07-27 19:15:54 +02001500 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001502 goto patched;
1503 }
Takashi Iwai82467612007-07-27 19:15:54 +02001504 if (codec->preset && codec->preset->patch) {
1505 err = codec->preset->patch(codec);
1506 goto patched;
1507 }
1508
1509 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001510 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001511 if (err < 0)
1512 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001513
1514 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001515 if (!err && codec->patch_ops.unsol_event)
1516 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001517 /* audio codec should override the mixer name */
1518 if (!err && (codec->afg || !*codec->bus->card->mixername))
1519 snprintf(codec->bus->card->mixername,
1520 sizeof(codec->bus->card->mixername),
1521 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001524EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Takashi Iwaied360812012-08-08 17:12:52 +02001526/* update the stream-id if changed */
1527static void update_pcm_stream_id(struct hda_codec *codec,
1528 struct hda_cvt_setup *p, hda_nid_t nid,
1529 u32 stream_tag, int channel_id)
1530{
1531 unsigned int oldval, newval;
1532
1533 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1534 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1535 newval = (stream_tag << 4) | channel_id;
1536 if (oldval != newval)
1537 snd_hda_codec_write(codec, nid, 0,
1538 AC_VERB_SET_CHANNEL_STREAMID,
1539 newval);
1540 p->stream_tag = stream_tag;
1541 p->channel_id = channel_id;
1542 }
1543}
1544
1545/* update the format-id if changed */
1546static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1547 hda_nid_t nid, int format)
1548{
1549 unsigned int oldval;
1550
1551 if (p->format_id != format) {
1552 oldval = snd_hda_codec_read(codec, nid, 0,
1553 AC_VERB_GET_STREAM_FORMAT, 0);
1554 if (oldval != format) {
1555 msleep(1);
1556 snd_hda_codec_write(codec, nid, 0,
1557 AC_VERB_SET_STREAM_FORMAT,
1558 format);
1559 }
1560 p->format_id = format;
1561 }
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/**
1565 * snd_hda_codec_setup_stream - set up the codec for streaming
1566 * @codec: the CODEC to set up
1567 * @nid: the NID to set up
1568 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1569 * @channel_id: channel id to pass, zero based.
1570 * @format: stream format.
1571 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001572void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1573 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int channel_id, int format)
1575{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001576 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001577 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001578 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001579 int i;
1580
Takashi Iwai0ba21762007-04-16 11:29:14 +02001581 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001582 return;
1583
Takashi Iwai0ba21762007-04-16 11:29:14 +02001584 snd_printdd("hda_codec_setup_stream: "
1585 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001587 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001588 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001589 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001590
1591 if (codec->pcm_format_first)
1592 update_pcm_format(codec, p, nid, format);
1593 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1594 if (!codec->pcm_format_first)
1595 update_pcm_format(codec, p, nid, format);
1596
Takashi Iwaieb541332010-08-06 13:48:11 +02001597 p->active = 1;
1598 p->dirty = 0;
1599
1600 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001601 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001602 list_for_each_entry(c, &codec->bus->codec_list, list) {
1603 for (i = 0; i < c->cvt_setups.used; i++) {
1604 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001605 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001606 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001607 p->dirty = 1;
1608 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001611EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Takashi Iwaif0cea792010-08-13 11:56:53 +02001613static void really_cleanup_stream(struct hda_codec *codec,
1614 struct hda_cvt_setup *q);
1615
Takashi Iwaid5191e52009-11-16 14:58:17 +01001616/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001617 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001618 * @codec: the CODEC to clean up
1619 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001620 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001621 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001622void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1623 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001624{
Takashi Iwaieb541332010-08-06 13:48:11 +02001625 struct hda_cvt_setup *p;
1626
Takashi Iwai888afa12008-03-18 09:57:50 +01001627 if (!nid)
1628 return;
1629
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001630 if (codec->no_sticky_stream)
1631 do_now = 1;
1632
Takashi Iwai888afa12008-03-18 09:57:50 +01001633 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001634 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001635 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001636 /* here we just clear the active flag when do_now isn't set;
1637 * actual clean-ups will be done later in
1638 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1639 */
1640 if (do_now)
1641 really_cleanup_stream(codec, p);
1642 else
1643 p->active = 0;
1644 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001645}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001646EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001647
Takashi Iwaieb541332010-08-06 13:48:11 +02001648static void really_cleanup_stream(struct hda_codec *codec,
1649 struct hda_cvt_setup *q)
1650{
1651 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001652 if (q->stream_tag || q->channel_id)
1653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1654 if (q->format_id)
1655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1656);
Takashi Iwaieb541332010-08-06 13:48:11 +02001657 memset(q, 0, sizeof(*q));
1658 q->nid = nid;
1659}
1660
1661/* clean up the all conflicting obsolete streams */
1662static void purify_inactive_streams(struct hda_codec *codec)
1663{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001664 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001665 int i;
1666
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001667 list_for_each_entry(c, &codec->bus->codec_list, list) {
1668 for (i = 0; i < c->cvt_setups.used; i++) {
1669 struct hda_cvt_setup *p;
1670 p = snd_array_elem(&c->cvt_setups, i);
1671 if (p->dirty)
1672 really_cleanup_stream(c, p);
1673 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001674 }
1675}
1676
Takashi Iwai2a439522011-07-26 09:52:50 +02001677#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001678/* clean up all streams; called from suspend */
1679static void hda_cleanup_all_streams(struct hda_codec *codec)
1680{
1681 int i;
1682
1683 for (i = 0; i < codec->cvt_setups.used; i++) {
1684 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1685 if (p->stream_tag)
1686 really_cleanup_stream(codec, p);
1687 }
1688}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001689#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * amp access functions
1693 */
1694
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001695/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001696#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001697#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001698#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1699#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001701#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001704static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001705 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwai01751f52007-08-10 16:59:39 +02001707 memset(cache, 0, sizeof(*cache));
1708 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001709 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001710}
1711
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001712static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001713{
Takashi Iwai603c4012008-07-30 15:01:44 +02001714 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001718static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai01751f52007-08-10 16:59:39 +02001720 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1721 u16 cur = cache->hash[idx];
1722 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001725 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (info->key == key)
1727 return info;
1728 cur = info->next;
1729 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001730 return NULL;
1731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001733/* query the hash. allocate an entry if not found. */
1734static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1735 u32 key)
1736{
1737 struct hda_cache_head *info = get_hash(cache, key);
1738 if (!info) {
1739 u16 idx, cur;
1740 /* add a new hash entry */
1741 info = snd_array_new(&cache->buf);
1742 if (!info)
1743 return NULL;
1744 cur = snd_array_index(&cache->buf, info);
1745 info->key = key;
1746 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001747 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001748 idx = key % (u16)ARRAY_SIZE(cache->hash);
1749 info->next = cache->hash[idx];
1750 cache->hash[idx] = cur;
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return info;
1753}
1754
Takashi Iwai01751f52007-08-10 16:59:39 +02001755/* query and allocate an amp hash entry */
1756static inline struct hda_amp_info *
1757get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1758{
1759 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1760}
1761
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762/* overwrite the value with the key in the caps hash */
1763static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1764{
1765 struct hda_amp_info *info;
1766
1767 mutex_lock(&codec->hash_mutex);
1768 info = get_alloc_amp_hash(codec, key);
1769 if (!info) {
1770 mutex_unlock(&codec->hash_mutex);
1771 return -EINVAL;
1772 }
1773 info->amp_caps = val;
1774 info->head.val |= INFO_AMP_CAPS;
1775 mutex_unlock(&codec->hash_mutex);
1776 return 0;
1777}
1778
1779/* query the value from the caps hash; if not found, fetch the current
1780 * value from the given function and store in the hash
1781 */
1782static unsigned int
1783query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1784 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1785{
1786 struct hda_amp_info *info;
1787 unsigned int val;
1788
1789 mutex_lock(&codec->hash_mutex);
1790 info = get_alloc_amp_hash(codec, key);
1791 if (!info) {
1792 mutex_unlock(&codec->hash_mutex);
1793 return 0;
1794 }
1795 if (!(info->head.val & INFO_AMP_CAPS)) {
1796 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1797 val = func(codec, nid, dir);
1798 write_caps_hash(codec, key, val);
1799 } else {
1800 val = info->amp_caps;
1801 mutex_unlock(&codec->hash_mutex);
1802 }
1803 return val;
1804}
1805
1806static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1807 int direction)
1808{
1809 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1810 nid = codec->afg;
1811 return snd_hda_param_read(codec, nid,
1812 direction == HDA_OUTPUT ?
1813 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1814}
1815
Takashi Iwaid5191e52009-11-16 14:58:17 +01001816/**
1817 * query_amp_caps - query AMP capabilities
1818 * @codec: the HD-auio codec
1819 * @nid: the NID to query
1820 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1821 *
1822 * Query AMP capabilities for the given widget and direction.
1823 * Returns the obtained capability bits.
1824 *
1825 * When cap bits have been already read, this doesn't read again but
1826 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001828u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001830 return query_caps_hash(codec, nid, direction,
1831 HDA_HASH_KEY(nid, direction, 0),
1832 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001834EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Takashi Iwaid5191e52009-11-16 14:58:17 +01001836/**
1837 * snd_hda_override_amp_caps - Override the AMP capabilities
1838 * @codec: the CODEC to clean up
1839 * @nid: the NID to clean up
1840 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1841 * @caps: the capability bits to set
1842 *
1843 * Override the cached AMP caps bits value by the given one.
1844 * This function is useful if the driver needs to adjust the AMP ranges,
1845 * e.g. limit to 0dB, etc.
1846 *
1847 * Returns zero if successful or a negative error code.
1848 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001849int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1850 unsigned int caps)
1851{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001852 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001855
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001856static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1857 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001858{
1859 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1860}
1861
Takashi Iwaid5191e52009-11-16 14:58:17 +01001862/**
1863 * snd_hda_query_pin_caps - Query PIN capabilities
1864 * @codec: the HD-auio codec
1865 * @nid: the NID to query
1866 *
1867 * Query PIN capabilities for the given widget.
1868 * Returns the obtained capability bits.
1869 *
1870 * When cap bits have been already read, this doesn't read again but
1871 * returns the cached value.
1872 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001873u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1874{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001875 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001876 read_pin_cap);
1877}
Takashi Iwai1327a322009-03-23 13:07:47 +01001878EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1879
Wu Fengguang864f92b2009-11-18 12:38:02 +08001880/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001881 * snd_hda_override_pin_caps - Override the pin capabilities
1882 * @codec: the CODEC
1883 * @nid: the NID to override
1884 * @caps: the capability bits to set
1885 *
1886 * Override the cached PIN capabilitiy bits value by the given one.
1887 *
1888 * Returns zero if successful or a negative error code.
1889 */
1890int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1891 unsigned int caps)
1892{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001893 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001894}
1895EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1896
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001897/* read or sync the hash value with the current value;
1898 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001900static struct hda_amp_info *
1901update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001902 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001904 struct hda_amp_info *info;
1905 unsigned int parm, val = 0;
1906 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001908 retry:
1909 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1910 if (!info)
1911 return NULL;
1912 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1913 if (!val_read) {
1914 mutex_unlock(&codec->hash_mutex);
1915 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1916 parm |= direction == HDA_OUTPUT ?
1917 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1918 parm |= index;
1919 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001920 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001921 val &= 0xff;
1922 val_read = true;
1923 mutex_lock(&codec->hash_mutex);
1924 goto retry;
1925 }
1926 info->vol[ch] = val;
1927 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001928 } else if (init_only)
1929 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001930 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001934 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001936static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001937 hda_nid_t nid, int ch, int direction, int index,
1938 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 u32 parm;
1941
1942 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1943 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1944 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001945 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1946 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001947 ; /* set the zero value as a fake mute */
1948 else
1949 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1951}
1952
Takashi Iwaid5191e52009-11-16 14:58:17 +01001953/**
1954 * snd_hda_codec_amp_read - Read AMP value
1955 * @codec: HD-audio codec
1956 * @nid: NID to read the AMP value
1957 * @ch: channel (left=0 or right=1)
1958 * @direction: #HDA_INPUT or #HDA_OUTPUT
1959 * @index: the index value (only for input direction)
1960 *
1961 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Takashi Iwai834be882006-03-01 14:16:17 +01001963int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1964 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001966 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001967 unsigned int val = 0;
1968
1969 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001971 if (info)
1972 val = info->vol[ch];
1973 mutex_unlock(&codec->hash_mutex);
1974 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001976EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Takashi Iwai280e57d2012-12-14 10:32:21 +01001978static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1979 int direction, int idx, int mask, int val,
1980 bool init_only)
1981{
1982 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001983 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001984 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001985
1986 if (snd_BUG_ON(mask & ~0xff))
1987 mask &= 0xff;
1988 val &= mask;
1989
1990 mutex_lock(&codec->hash_mutex);
1991 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1992 if (!info) {
1993 mutex_unlock(&codec->hash_mutex);
1994 return 0;
1995 }
1996 val |= info->vol[ch] & ~mask;
1997 if (info->vol[ch] == val) {
1998 mutex_unlock(&codec->hash_mutex);
1999 return 0;
2000 }
2001 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002002 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002003 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002004 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002005 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002006 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002007 return 1;
2008}
2009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * snd_hda_codec_amp_update - update the AMP value
2012 * @codec: HD-audio codec
2013 * @nid: NID to read the AMP value
2014 * @ch: channel (left=0 or right=1)
2015 * @direction: #HDA_INPUT or #HDA_OUTPUT
2016 * @idx: the index value (only for input direction)
2017 * @mask: bit mask to set
2018 * @val: the bits value to set
2019 *
2020 * Update the AMP value with a bit mask.
2021 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002022 */
Takashi Iwai834be882006-03-01 14:16:17 +01002023int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2024 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002026 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002028EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Takashi Iwaid5191e52009-11-16 14:58:17 +01002030/**
2031 * snd_hda_codec_amp_stereo - update the AMP stereo values
2032 * @codec: HD-audio codec
2033 * @nid: NID to read the AMP value
2034 * @direction: #HDA_INPUT or #HDA_OUTPUT
2035 * @idx: the index value (only for input direction)
2036 * @mask: bit mask to set
2037 * @val: the bits value to set
2038 *
2039 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2040 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002041 */
2042int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2043 int direction, int idx, int mask, int val)
2044{
2045 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02002046
2047 if (snd_BUG_ON(mask & ~0xff))
2048 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002049 for (ch = 0; ch < 2; ch++)
2050 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2051 idx, mask, val);
2052 return ret;
2053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002054EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002055
Takashi Iwai280e57d2012-12-14 10:32:21 +01002056/* Works like snd_hda_codec_amp_update() but it writes the value only at
2057 * the first access. If the amp was already initialized / updated beforehand,
2058 * this does nothing.
2059 */
2060int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2061 int dir, int idx, int mask, int val)
2062{
2063 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2064}
2065EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2066
2067int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2068 int dir, int idx, int mask, int val)
2069{
2070 int ch, ret = 0;
2071
2072 if (snd_BUG_ON(mask & ~0xff))
2073 mask &= 0xff;
2074 for (ch = 0; ch < 2; ch++)
2075 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2076 idx, mask, val);
2077 return ret;
2078}
2079EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2080
Takashi Iwaid5191e52009-11-16 14:58:17 +01002081/**
2082 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2083 * @codec: HD-audio codec
2084 *
2085 * Resume the all amp commands from the cache.
2086 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002087void snd_hda_codec_resume_amp(struct hda_codec *codec)
2088{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002089 int i;
2090
Takashi Iwaic370dd62012-12-13 18:30:04 +01002091 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002092 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002093 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2094 struct hda_amp_info *buffer;
2095 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002096 hda_nid_t nid;
2097 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002098 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002099
2100 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002101 if (!buffer->head.dirty)
2102 continue;
2103 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002104 info = *buffer;
2105 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002106 if (!key)
2107 continue;
2108 nid = key & 0xff;
2109 idx = (key >> 16) & 0xff;
2110 dir = (key >> 24) & 0xff;
2111 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002112 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002113 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002114 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002115 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2116 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002117 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002118 }
2119 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002120 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002122EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002124static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2125 unsigned int ofs)
2126{
2127 u32 caps = query_amp_caps(codec, nid, dir);
2128 /* get num steps */
2129 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2130 if (ofs < caps)
2131 caps -= ofs;
2132 return caps;
2133}
2134
Takashi Iwaid5191e52009-11-16 14:58:17 +01002135/**
2136 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2137 *
2138 * The control element is supposed to have the private_value field
2139 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2140 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002141int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 u16 nid = get_amp_nid(kcontrol);
2146 u8 chs = get_amp_channels(kcontrol);
2147 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002148 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002150 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2151 uinfo->count = chs == 3 ? 2 : 1;
2152 uinfo->value.integer.min = 0;
2153 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2154 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002156 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2157 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return -EINVAL;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002162EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002164
2165static inline unsigned int
2166read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2167 int ch, int dir, int idx, unsigned int ofs)
2168{
2169 unsigned int val;
2170 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2171 val &= HDA_AMP_VOLMASK;
2172 if (val >= ofs)
2173 val -= ofs;
2174 else
2175 val = 0;
2176 return val;
2177}
2178
2179static inline int
2180update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2181 int ch, int dir, int idx, unsigned int ofs,
2182 unsigned int val)
2183{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002184 unsigned int maxval;
2185
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002186 if (val > 0)
2187 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002188 /* ofs = 0: raw max value */
2189 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002190 if (val > maxval)
2191 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002192 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2193 HDA_AMP_VOLMASK, val);
2194}
2195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2198 *
2199 * The control element is supposed to have the private_value field
2200 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2201 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002202int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2203 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2206 hda_nid_t nid = get_amp_nid(kcontrol);
2207 int chs = get_amp_channels(kcontrol);
2208 int dir = get_amp_direction(kcontrol);
2209 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002210 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 long *valp = ucontrol->value.integer.value;
2212
2213 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002214 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002216 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002219EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Takashi Iwaid5191e52009-11-16 14:58:17 +01002221/**
2222 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2223 *
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2226 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002227int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 hda_nid_t nid = get_amp_nid(kcontrol);
2232 int chs = get_amp_channels(kcontrol);
2233 int dir = get_amp_direction(kcontrol);
2234 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002235 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 long *valp = ucontrol->value.integer.value;
2237 int change = 0;
2238
Takashi Iwaicb53c622007-08-10 17:21:45 +02002239 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002240 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002241 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002242 valp++;
2243 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002244 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002245 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002246 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return change;
2248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002249EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Takashi Iwaid5191e52009-11-16 14:58:17 +01002251/**
2252 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2253 *
2254 * The control element is supposed to have the private_value field
2255 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2256 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002257int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2258 unsigned int size, unsigned int __user *_tlv)
2259{
2260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2261 hda_nid_t nid = get_amp_nid(kcontrol);
2262 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002263 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002264 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002265 u32 caps, val1, val2;
2266
2267 if (size < 4 * sizeof(unsigned int))
2268 return -ENOMEM;
2269 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002270 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2271 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002272 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002273 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002274 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002275 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002276 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002277 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2278 return -EFAULT;
2279 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2280 return -EFAULT;
2281 if (put_user(val1, _tlv + 2))
2282 return -EFAULT;
2283 if (put_user(val2, _tlv + 3))
2284 return -EFAULT;
2285 return 0;
2286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002287EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002288
Takashi Iwaid5191e52009-11-16 14:58:17 +01002289/**
2290 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2291 * @codec: HD-audio codec
2292 * @nid: NID of a reference widget
2293 * @dir: #HDA_INPUT or #HDA_OUTPUT
2294 * @tlv: TLV data to be stored, at least 4 elements
2295 *
2296 * Set (static) TLV data for a virtual master volume using the AMP caps
2297 * obtained from the reference NID.
2298 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002299 */
2300void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2301 unsigned int *tlv)
2302{
2303 u32 caps;
2304 int nums, step;
2305
2306 caps = query_amp_caps(codec, nid, dir);
2307 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2308 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2309 step = (step + 1) * 25;
2310 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2311 tlv[1] = 2 * sizeof(unsigned int);
2312 tlv[2] = -nums * step;
2313 tlv[3] = step;
2314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002315EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002316
2317/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002318static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002319find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002320{
2321 struct snd_ctl_elem_id id;
2322 memset(&id, 0, sizeof(id));
2323 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002324 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002325 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002326 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2327 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002328 strcpy(id.name, name);
2329 return snd_ctl_find_id(codec->bus->card, &id);
2330}
2331
Takashi Iwaid5191e52009-11-16 14:58:17 +01002332/**
2333 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2334 * @codec: HD-audio codec
2335 * @name: ctl id name string
2336 *
2337 * Get the control element with the given id string and IFACE_MIXER.
2338 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002339struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2340 const char *name)
2341{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002342 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002344EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002345
Takashi Iwaidcda5802012-10-12 17:24:51 +02002346static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002347 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002348{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002349 int i, idx;
2350 /* 16 ctlrs should be large enough */
2351 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2352 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002353 return idx;
2354 }
2355 return -EBUSY;
2356}
2357
Takashi Iwaid5191e52009-11-16 14:58:17 +01002358/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002359 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002360 * @codec: HD-audio codec
2361 * @nid: corresponding NID (optional)
2362 * @kctl: the control element to assign
2363 *
2364 * Add the given control element to an array inside the codec instance.
2365 * All control elements belonging to a codec are supposed to be added
2366 * by this function so that a proper clean-up works at the free or
2367 * reconfiguration time.
2368 *
2369 * If non-zero @nid is passed, the NID is assigned to the control element.
2370 * The assignment is shown in the codec proc file.
2371 *
2372 * snd_hda_ctl_add() checks the control subdev id field whether
2373 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002374 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2375 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002376 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002377int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2378 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002379{
2380 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002381 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002382 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002383
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002384 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002385 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002386 if (nid == 0)
2387 nid = get_amp_nid_(kctl->private_value);
2388 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002389 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2390 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002391 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002392 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002393 err = snd_ctl_add(codec->bus->card, kctl);
2394 if (err < 0)
2395 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002396 item = snd_array_new(&codec->mixers);
2397 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002398 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002399 item->kctl = kctl;
2400 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002401 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002402 return 0;
2403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002404EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002405
Takashi Iwaid5191e52009-11-16 14:58:17 +01002406/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002407 * snd_hda_add_nid - Assign a NID to a control element
2408 * @codec: HD-audio codec
2409 * @nid: corresponding NID (optional)
2410 * @kctl: the control element to assign
2411 * @index: index to kctl
2412 *
2413 * Add the given control element to an array inside the codec instance.
2414 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2415 * NID:KCTL mapping - for example "Capture Source" selector.
2416 */
2417int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2418 unsigned int index, hda_nid_t nid)
2419{
2420 struct hda_nid_item *item;
2421
2422 if (nid > 0) {
2423 item = snd_array_new(&codec->nids);
2424 if (!item)
2425 return -ENOMEM;
2426 item->kctl = kctl;
2427 item->index = index;
2428 item->nid = nid;
2429 return 0;
2430 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002431 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2432 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002433 return -EINVAL;
2434}
2435EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2436
2437/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002438 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2439 * @codec: HD-audio codec
2440 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002441void snd_hda_ctls_clear(struct hda_codec *codec)
2442{
2443 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002444 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002445 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002446 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002447 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002448 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002449}
2450
Takashi Iwaia65d6292009-02-23 16:57:04 +01002451/* pseudo device locking
2452 * toggle card->shutdown to allow/disallow the device access (as a hack)
2453 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002454int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002455{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002456 struct snd_card *card = bus->card;
2457 struct hda_codec *codec;
2458
Takashi Iwaia65d6292009-02-23 16:57:04 +01002459 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002460 if (card->shutdown)
2461 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002462 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002463 if (!list_empty(&card->ctl_files))
2464 goto err_clear;
2465
2466 list_for_each_entry(codec, &bus->codec_list, list) {
2467 int pcm;
2468 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2469 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2470 if (!cpcm->pcm)
2471 continue;
2472 if (cpcm->pcm->streams[0].substream_opened ||
2473 cpcm->pcm->streams[1].substream_opened)
2474 goto err_clear;
2475 }
2476 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002477 spin_unlock(&card->files_lock);
2478 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002479
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002480 err_clear:
2481 card->shutdown = 0;
2482 err_unlock:
2483 spin_unlock(&card->files_lock);
2484 return -EINVAL;
2485}
2486EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2487
2488void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002489{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002490 struct snd_card *card = bus->card;
2491
2492 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002493 spin_lock(&card->files_lock);
2494 card->shutdown = 0;
2495 spin_unlock(&card->files_lock);
2496}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002497EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002498
Takashi Iwaid5191e52009-11-16 14:58:17 +01002499/**
2500 * snd_hda_codec_reset - Clear all objects assigned to the codec
2501 * @codec: HD-audio codec
2502 *
2503 * This frees the all PCM and control elements assigned to the codec, and
2504 * clears the caches and restores the pin default configurations.
2505 *
2506 * When a device is being used, it returns -EBSY. If successfully freed,
2507 * returns zero.
2508 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002509int snd_hda_codec_reset(struct hda_codec *codec)
2510{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002511 struct hda_bus *bus = codec->bus;
2512 struct snd_card *card = bus->card;
2513 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002514
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002515 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002516 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002517
2518 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002519 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002520#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002521 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002522 codec->power_on = 0;
2523 codec->power_transition = 0;
2524 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002525 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002526#endif
2527 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002528 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002529 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002530 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002531 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002532 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002533 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002534 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002535 }
2536 if (codec->patch_ops.free)
2537 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002538 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002539 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002540 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002541 codec->spec = NULL;
2542 free_hda_cache(&codec->amp_cache);
2543 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002544 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2545 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002546 /* free only driver_pins so that init_pins + user_pins are restored */
2547 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002548 snd_array_free(&codec->cvt_setups);
2549 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002550 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002551 codec->num_pcms = 0;
2552 codec->pcm_info = NULL;
2553 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002554 codec->slave_dig_outs = NULL;
2555 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002556 module_put(codec->owner);
2557 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002558
2559 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002560 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002561 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002562}
2563
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002564typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2565
2566/* apply the function to all matching slave ctls in the mixer list */
2567static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002568 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002569{
2570 struct hda_nid_item *items;
2571 const char * const *s;
2572 int i, err;
2573
2574 items = codec->mixers.list;
2575 for (i = 0; i < codec->mixers.used; i++) {
2576 struct snd_kcontrol *sctl = items[i].kctl;
2577 if (!sctl || !sctl->id.name ||
2578 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2579 continue;
2580 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002581 char tmpname[sizeof(sctl->id.name)];
2582 const char *name = *s;
2583 if (suffix) {
2584 snprintf(tmpname, sizeof(tmpname), "%s %s",
2585 name, suffix);
2586 name = tmpname;
2587 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002588 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002589 err = func(data, sctl);
2590 if (err)
2591 return err;
2592 break;
2593 }
2594 }
2595 }
2596 return 0;
2597}
2598
2599static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2600{
2601 return 1;
2602}
2603
Takashi Iwai18478e82012-03-09 17:51:10 +01002604/* guess the value corresponding to 0dB */
2605static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2606{
2607 int _tlv[4];
2608 const int *tlv = NULL;
2609 int val = -1;
2610
2611 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2612 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2613 mm_segment_t fs = get_fs();
2614 set_fs(get_ds());
2615 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2616 tlv = _tlv;
2617 set_fs(fs);
2618 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2619 tlv = kctl->tlv.p;
2620 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2621 val = -tlv[2] / tlv[3];
2622 return val;
2623}
2624
2625/* call kctl->put with the given value(s) */
2626static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2627{
2628 struct snd_ctl_elem_value *ucontrol;
2629 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2630 if (!ucontrol)
2631 return -ENOMEM;
2632 ucontrol->value.integer.value[0] = val;
2633 ucontrol->value.integer.value[1] = val;
2634 kctl->put(kctl, ucontrol);
2635 kfree(ucontrol);
2636 return 0;
2637}
2638
2639/* initialize the slave volume with 0dB */
2640static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2641{
2642 int offset = get_kctl_0dB_offset(slave);
2643 if (offset > 0)
2644 put_kctl_with_value(slave, offset);
2645 return 0;
2646}
2647
2648/* unmute the slave */
2649static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2650{
2651 return put_kctl_with_value(slave, 1);
2652}
2653
Takashi Iwaid5191e52009-11-16 14:58:17 +01002654/**
2655 * snd_hda_add_vmaster - create a virtual master control and add slaves
2656 * @codec: HD-audio codec
2657 * @name: vmaster control name
2658 * @tlv: TLV data (optional)
2659 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002660 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002661 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002662 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002663 *
2664 * Create a virtual master control with the given name. The TLV data
2665 * must be either NULL or a valid data.
2666 *
2667 * @slaves is a NULL-terminated array of strings, each of which is a
2668 * slave control name. All controls with these names are assigned to
2669 * the new virtual master control.
2670 *
2671 * This function returns zero if successful or a negative error code.
2672 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002673int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002674 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002675 const char *suffix, bool init_slave_vol,
2676 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002677{
2678 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002679 int err;
2680
Takashi Iwai29e58532012-03-12 12:25:03 +01002681 if (ctl_ret)
2682 *ctl_ret = NULL;
2683
Takashi Iwai9322ca52012-02-03 14:28:01 +01002684 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002685 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002686 snd_printdd("No slave found for %s\n", name);
2687 return 0;
2688 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002689 kctl = snd_ctl_make_virtual_master(name, tlv);
2690 if (!kctl)
2691 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002692 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002693 if (err < 0)
2694 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002695
Takashi Iwai9322ca52012-02-03 14:28:01 +01002696 err = map_slaves(codec, slaves, suffix,
2697 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002698 if (err < 0)
2699 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002700
2701 /* init with master mute & zero volume */
2702 put_kctl_with_value(kctl, 0);
2703 if (init_slave_vol)
2704 map_slaves(codec, slaves, suffix,
2705 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2706
Takashi Iwai29e58532012-03-12 12:25:03 +01002707 if (ctl_ret)
2708 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002709 return 0;
2710}
Takashi Iwai18478e82012-03-09 17:51:10 +01002711EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002712
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002713/*
2714 * mute-LED control using vmaster
2715 */
2716static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_info *uinfo)
2718{
2719 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002720 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002721 };
2722 unsigned int index;
2723
2724 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2725 uinfo->count = 1;
2726 uinfo->value.enumerated.items = 3;
2727 index = uinfo->value.enumerated.item;
2728 if (index >= 3)
2729 index = 2;
2730 strcpy(uinfo->value.enumerated.name, texts[index]);
2731 return 0;
2732}
2733
2734static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2736{
2737 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2738 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2739 return 0;
2740}
2741
2742static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
2744{
2745 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2746 unsigned int old_mode = hook->mute_mode;
2747
2748 hook->mute_mode = ucontrol->value.enumerated.item[0];
2749 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2750 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2751 if (old_mode == hook->mute_mode)
2752 return 0;
2753 snd_hda_sync_vmaster_hook(hook);
2754 return 1;
2755}
2756
2757static struct snd_kcontrol_new vmaster_mute_mode = {
2758 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2759 .name = "Mute-LED Mode",
2760 .info = vmaster_mute_mode_info,
2761 .get = vmaster_mute_mode_get,
2762 .put = vmaster_mute_mode_put,
2763};
2764
2765/*
2766 * Add a mute-LED hook with the given vmaster switch kctl
2767 * "Mute-LED Mode" control is automatically created and associated with
2768 * the given hook.
2769 */
2770int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002771 struct hda_vmaster_mute_hook *hook,
2772 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002773{
2774 struct snd_kcontrol *kctl;
2775
2776 if (!hook->hook || !hook->sw_kctl)
2777 return 0;
2778 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2779 hook->codec = codec;
2780 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002781 if (!expose_enum_ctl)
2782 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002783 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2784 if (!kctl)
2785 return -ENOMEM;
2786 return snd_hda_ctl_add(codec, 0, kctl);
2787}
2788EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2789
2790/*
2791 * Call the hook with the current value for synchronization
2792 * Should be called in init callback
2793 */
2794void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2795{
2796 if (!hook->hook || !hook->codec)
2797 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002798 /* don't call vmaster hook in the destructor since it might have
2799 * been already destroyed
2800 */
2801 if (hook->codec->bus->shutdown)
2802 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002803 switch (hook->mute_mode) {
2804 case HDA_VMUTE_FOLLOW_MASTER:
2805 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2806 break;
2807 default:
2808 hook->hook(hook->codec, hook->mute_mode);
2809 break;
2810 }
2811}
2812EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2813
2814
Takashi Iwaid5191e52009-11-16 14:58:17 +01002815/**
2816 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2817 *
2818 * The control element is supposed to have the private_value field
2819 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2820 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002821int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 int chs = get_amp_channels(kcontrol);
2825
2826 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2827 uinfo->count = chs == 3 ? 2 : 1;
2828 uinfo->value.integer.min = 0;
2829 uinfo->value.integer.max = 1;
2830 return 0;
2831}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002832EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Takashi Iwaid5191e52009-11-16 14:58:17 +01002834/**
2835 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2836 *
2837 * The control element is supposed to have the private_value field
2838 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2839 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002840int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2844 hda_nid_t nid = get_amp_nid(kcontrol);
2845 int chs = get_amp_channels(kcontrol);
2846 int dir = get_amp_direction(kcontrol);
2847 int idx = get_amp_index(kcontrol);
2848 long *valp = ucontrol->value.integer.value;
2849
2850 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002852 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002855 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002858EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Takashi Iwaid5191e52009-11-16 14:58:17 +01002860/**
2861 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2862 *
2863 * The control element is supposed to have the private_value field
2864 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2865 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002866int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2870 hda_nid_t nid = get_amp_nid(kcontrol);
2871 int chs = get_amp_channels(kcontrol);
2872 int dir = get_amp_direction(kcontrol);
2873 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 long *valp = ucontrol->value.integer.value;
2875 int change = 0;
2876
Takashi Iwaicb53c622007-08-10 17:21:45 +02002877 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002878 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002879 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002880 HDA_AMP_MUTE,
2881 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002882 valp++;
2883 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002884 if (chs & 2)
2885 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002886 HDA_AMP_MUTE,
2887 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002888 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002889 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return change;
2891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002892EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894/*
Takashi Iwai985be542005-11-02 18:26:49 +01002895 * bound volume controls
2896 *
2897 * bind multiple volumes (# indices, from 0)
2898 */
2899
2900#define AMP_VAL_IDX_SHIFT 19
2901#define AMP_VAL_IDX_MASK (0x0f<<19)
2902
Takashi Iwaid5191e52009-11-16 14:58:17 +01002903/**
2904 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2905 *
2906 * The control element is supposed to have the private_value field
2907 * set up via HDA_BIND_MUTE*() macros.
2908 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002911{
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 unsigned long pval;
2914 int err;
2915
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002916 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002917 pval = kcontrol->private_value;
2918 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2919 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2920 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002921 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002922 return err;
2923}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002924EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002925
Takashi Iwaid5191e52009-11-16 14:58:17 +01002926/**
2927 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2928 *
2929 * The control element is supposed to have the private_value field
2930 * set up via HDA_BIND_MUTE*() macros.
2931 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002934{
2935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2936 unsigned long pval;
2937 int i, indices, err = 0, change = 0;
2938
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002939 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002940 pval = kcontrol->private_value;
2941 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2942 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2944 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002945 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2946 if (err < 0)
2947 break;
2948 change |= err;
2949 }
2950 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002951 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002952 return err < 0 ? err : change;
2953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002954EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002955
Takashi Iwaid5191e52009-11-16 14:58:17 +01002956/**
2957 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2958 *
2959 * The control element is supposed to have the private_value field
2960 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002961 */
2962int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_info *uinfo)
2964{
2965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2966 struct hda_bind_ctls *c;
2967 int err;
2968
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002969 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002970 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002971 kcontrol->private_value = *c->values;
2972 err = c->ops->info(kcontrol, uinfo);
2973 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002974 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002975 return err;
2976}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002977EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002978
Takashi Iwaid5191e52009-11-16 14:58:17 +01002979/**
2980 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2981 *
2982 * The control element is supposed to have the private_value field
2983 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2984 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002985int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2987{
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2989 struct hda_bind_ctls *c;
2990 int err;
2991
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002992 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002993 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002994 kcontrol->private_value = *c->values;
2995 err = c->ops->get(kcontrol, ucontrol);
2996 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002997 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002998 return err;
2999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003000EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003001
Takashi Iwaid5191e52009-11-16 14:58:17 +01003002/**
3003 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3004 *
3005 * The control element is supposed to have the private_value field
3006 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3007 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003008int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
3010{
3011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3012 struct hda_bind_ctls *c;
3013 unsigned long *vals;
3014 int err = 0, change = 0;
3015
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003016 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003017 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003018 for (vals = c->values; *vals; vals++) {
3019 kcontrol->private_value = *vals;
3020 err = c->ops->put(kcontrol, ucontrol);
3021 if (err < 0)
3022 break;
3023 change |= err;
3024 }
3025 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003026 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003027 return err < 0 ? err : change;
3028}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003029EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003030
Takashi Iwaid5191e52009-11-16 14:58:17 +01003031/**
3032 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3033 *
3034 * The control element is supposed to have the private_value field
3035 * set up via HDA_BIND_VOL() macro.
3036 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003037int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3038 unsigned int size, unsigned int __user *tlv)
3039{
3040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3041 struct hda_bind_ctls *c;
3042 int err;
3043
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003044 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003045 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003046 kcontrol->private_value = *c->values;
3047 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3048 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003049 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003050 return err;
3051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003052EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003053
3054struct hda_ctl_ops snd_hda_bind_vol = {
3055 .info = snd_hda_mixer_amp_volume_info,
3056 .get = snd_hda_mixer_amp_volume_get,
3057 .put = snd_hda_mixer_amp_volume_put,
3058 .tlv = snd_hda_mixer_amp_tlv
3059};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003060EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003061
3062struct hda_ctl_ops snd_hda_bind_sw = {
3063 .info = snd_hda_mixer_amp_switch_info,
3064 .get = snd_hda_mixer_amp_switch_get,
3065 .put = snd_hda_mixer_amp_switch_put,
3066 .tlv = snd_hda_mixer_amp_tlv
3067};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003068EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003069
3070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 * SPDIF out controls
3072 */
3073
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3078 uinfo->count = 1;
3079 return 0;
3080}
3081
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3086 IEC958_AES0_NONAUDIO |
3087 IEC958_AES0_CON_EMPHASIS_5015 |
3088 IEC958_AES0_CON_NOT_COPYRIGHT;
3089 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3090 IEC958_AES1_CON_ORIGINAL;
3091 return 0;
3092}
3093
Takashi Iwai0ba21762007-04-16 11:29:14 +02003094static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3098 IEC958_AES0_NONAUDIO |
3099 IEC958_AES0_PRO_EMPHASIS_5015;
3100 return 0;
3101}
3102
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003107 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003108 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003110 mutex_lock(&codec->spdif_mutex);
3111 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003112 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3113 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3114 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3115 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003116 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 return 0;
3119}
3120
3121/* convert from SPDIF status bits to HDA SPDIF bits
3122 * bit 0 (DigEn) is always set zero (to be filled later)
3123 */
3124static unsigned short convert_from_spdif_status(unsigned int sbits)
3125{
3126 unsigned short val = 0;
3127
3128 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003129 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003131 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3134 IEC958_AES0_PRO_EMPHASIS_5015)
3135 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003137 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3138 IEC958_AES0_CON_EMPHASIS_5015)
3139 val |= AC_DIG1_EMPHASIS;
3140 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3141 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3145 }
3146 return val;
3147}
3148
3149/* convert to SPDIF status bits from HDA SPDIF bits
3150 */
3151static unsigned int convert_to_spdif_status(unsigned short val)
3152{
3153 unsigned int sbits = 0;
3154
Takashi Iwai0ba21762007-04-16 11:29:14 +02003155 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003157 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 sbits |= IEC958_AES0_PROFESSIONAL;
3159 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003160 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3162 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003163 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003165 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003167 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3169 sbits |= val & (0x7f << 8);
3170 }
3171 return sbits;
3172}
3173
Takashi Iwai2f728532008-09-25 16:32:41 +02003174/* set digital convert verbs both for the given NID and its slaves */
3175static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3176 int verb, int val)
3177{
Takashi Iwaidda14412011-05-02 11:29:30 +02003178 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003179
Takashi Iwai9e976972008-11-25 08:17:20 +01003180 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003181 d = codec->slave_dig_outs;
3182 if (!d)
3183 return;
3184 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003185 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003186}
3187
3188static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3189 int dig1, int dig2)
3190{
3191 if (dig1 != -1)
3192 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3193 if (dig2 != -1)
3194 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3195}
3196
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003201 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003202 struct hda_spdif_out *spdif;
3203 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 unsigned short val;
3205 int change;
3206
Ingo Molnar62932df2006-01-16 16:34:20 +01003207 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003208 spdif = snd_array_elem(&codec->spdif_out, idx);
3209 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003210 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3212 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3213 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003214 val = convert_from_spdif_status(spdif->status);
3215 val |= spdif->ctls & 1;
3216 change = spdif->ctls != val;
3217 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003218 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003219 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003220 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return change;
3222}
3223
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003224#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3227 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003230 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003231 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003233 mutex_lock(&codec->spdif_mutex);
3234 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003235 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003236 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
3239
Stephen Warren74b654c2011-06-01 11:14:18 -06003240static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3241 int dig1, int dig2)
3242{
3243 set_dig_out_convert(codec, nid, dig1, dig2);
3244 /* unmute amp switch (if any) */
3245 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3246 (dig1 & AC_DIG1_ENABLE))
3247 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3248 HDA_AMP_MUTE, 0);
3249}
3250
Takashi Iwai0ba21762007-04-16 11:29:14 +02003251static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3252 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003255 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003256 struct hda_spdif_out *spdif;
3257 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 unsigned short val;
3259 int change;
3260
Ingo Molnar62932df2006-01-16 16:34:20 +01003261 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003262 spdif = snd_array_elem(&codec->spdif_out, idx);
3263 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003264 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003266 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003267 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003268 spdif->ctls = val;
3269 if (change && nid != (u16)-1)
3270 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003271 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return change;
3273}
3274
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003275static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 {
3277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 .info = snd_hda_spdif_mask_info,
3281 .get = snd_hda_spdif_cmask_get,
3282 },
3283 {
3284 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003286 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 .info = snd_hda_spdif_mask_info,
3288 .get = snd_hda_spdif_pmask_get,
3289 },
3290 {
3291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003292 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 .info = snd_hda_spdif_mask_info,
3294 .get = snd_hda_spdif_default_get,
3295 .put = snd_hda_spdif_default_put,
3296 },
3297 {
3298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003299 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .info = snd_hda_spdif_out_switch_info,
3301 .get = snd_hda_spdif_out_switch_get,
3302 .put = snd_hda_spdif_out_switch_put,
3303 },
3304 { } /* end */
3305};
3306
3307/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003308 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003310 * @associated_nid: NID that new ctls associated with
3311 * @cvt_nid: converter NID
3312 * @type: HDA_PCM_TYPE_*
3313 * Creates controls related with the digital output.
3314 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *
3316 * Returns 0 if successful, or a negative error code.
3317 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003318int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3319 hda_nid_t associated_nid,
3320 hda_nid_t cvt_nid,
3321 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003324 struct snd_kcontrol *kctl;
3325 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003326 int idx = 0;
3327 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003328 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003329 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003331 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003332 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003333 idx = spdif_index;
3334 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003335 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003336 /* suppose a single SPDIF device */
3337 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3338 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3339 if (!kctl)
3340 break;
3341 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003342 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003343 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003344 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003345 if (!bus->primary_dig_out_type)
3346 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003347
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003348 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003349 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003350 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3351 return -EBUSY;
3352 }
Stephen Warren7c935972011-06-01 11:14:17 -06003353 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003354 if (!spdif)
3355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3357 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003358 if (!kctl)
3359 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003360 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003361 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003362 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003363 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return err;
3365 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003366 spdif->nid = cvt_nid;
3367 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003368 AC_VERB_GET_DIGI_CONVERT_1, 0);
3369 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return 0;
3371}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003372EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003374/* get the hda_spdif_out entry from the given NID
3375 * call within spdif_mutex lock
3376 */
Stephen Warren7c935972011-06-01 11:14:17 -06003377struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3378 hda_nid_t nid)
3379{
3380 int i;
3381 for (i = 0; i < codec->spdif_out.used; i++) {
3382 struct hda_spdif_out *spdif =
3383 snd_array_elem(&codec->spdif_out, i);
3384 if (spdif->nid == nid)
3385 return spdif;
3386 }
3387 return NULL;
3388}
3389EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3390
Stephen Warren74b654c2011-06-01 11:14:18 -06003391void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3392{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003393 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003394
3395 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003396 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003397 spdif->nid = (u16)-1;
3398 mutex_unlock(&codec->spdif_mutex);
3399}
3400EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3401
3402void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3403{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003404 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003405 unsigned short val;
3406
3407 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003408 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003409 if (spdif->nid != nid) {
3410 spdif->nid = nid;
3411 val = spdif->ctls;
3412 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3413 }
3414 mutex_unlock(&codec->spdif_mutex);
3415}
3416EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003419 * SPDIF sharing with analog output
3420 */
3421static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3423{
3424 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3425 ucontrol->value.integer.value[0] = mout->share_spdif;
3426 return 0;
3427}
3428
3429static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3431{
3432 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3433 mout->share_spdif = !!ucontrol->value.integer.value[0];
3434 return 0;
3435}
3436
3437static struct snd_kcontrol_new spdif_share_sw = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "IEC958 Default PCM Playback Switch",
3440 .info = snd_ctl_boolean_mono_info,
3441 .get = spdif_share_sw_get,
3442 .put = spdif_share_sw_put,
3443};
3444
Takashi Iwaid5191e52009-11-16 14:58:17 +01003445/**
3446 * snd_hda_create_spdif_share_sw - create Default PCM switch
3447 * @codec: the HDA codec
3448 * @mout: multi-out instance
3449 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003450int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3451 struct hda_multi_out *mout)
3452{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003453 struct snd_kcontrol *kctl;
3454
Takashi Iwai9a081602008-02-12 18:37:26 +01003455 if (!mout->dig_out_nid)
3456 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003457
3458 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3459 if (!kctl)
3460 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003461 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003462 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003464EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003465
3466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 * SPDIF input
3468 */
3469
3470#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3471
Takashi Iwai0ba21762007-04-16 11:29:14 +02003472static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3476
3477 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3478 return 0;
3479}
3480
Takashi Iwai0ba21762007-04-16 11:29:14 +02003481static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3482 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
3484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3485 hda_nid_t nid = kcontrol->private_value;
3486 unsigned int val = !!ucontrol->value.integer.value[0];
3487 int change;
3488
Ingo Molnar62932df2006-01-16 16:34:20 +01003489 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003491 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003493 snd_hda_codec_write_cache(codec, nid, 0,
3494 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003496 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return change;
3498}
3499
Takashi Iwai0ba21762007-04-16 11:29:14 +02003500static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3504 hda_nid_t nid = kcontrol->private_value;
3505 unsigned short val;
3506 unsigned int sbits;
3507
Andrew Paprocki3982d172007-12-19 12:13:44 +01003508 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 sbits = convert_to_spdif_status(val);
3510 ucontrol->value.iec958.status[0] = sbits;
3511 ucontrol->value.iec958.status[1] = sbits >> 8;
3512 ucontrol->value.iec958.status[2] = sbits >> 16;
3513 ucontrol->value.iec958.status[3] = sbits >> 24;
3514 return 0;
3515}
3516
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003517static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003520 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 .info = snd_hda_spdif_in_switch_info,
3522 .get = snd_hda_spdif_in_switch_get,
3523 .put = snd_hda_spdif_in_switch_put,
3524 },
3525 {
3526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003528 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 .info = snd_hda_spdif_mask_info,
3530 .get = snd_hda_spdif_in_status_get,
3531 },
3532 { } /* end */
3533};
3534
3535/**
3536 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3537 * @codec: the HDA codec
3538 * @nid: audio in widget NID
3539 *
3540 * Creates controls related with the SPDIF input.
3541 * Called from each patch supporting the SPDIF in.
3542 *
3543 * Returns 0 if successful, or a negative error code.
3544 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003545int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003548 struct snd_kcontrol *kctl;
3549 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003550 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Takashi Iwaidcda5802012-10-12 17:24:51 +02003552 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003553 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003554 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3555 return -EBUSY;
3556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3558 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003559 if (!kctl)
3560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003562 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return err;
3565 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003566 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003567 snd_hda_codec_read(codec, nid, 0,
3568 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003569 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003572EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003574/*
3575 * command cache
3576 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Takashi Iwaic370dd62012-12-13 18:30:04 +01003578/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003579#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3580#define get_cmd_cache_nid(key) ((key) & 0xff)
3581#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3582
3583/**
3584 * snd_hda_codec_write_cache - send a single command with caching
3585 * @codec: the HDA codec
3586 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003587 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003588 * @verb: the verb to send
3589 * @parm: the parameter for the verb
3590 *
3591 * Send a single command without waiting for response.
3592 *
3593 * Returns 0 if successful, or a negative error code.
3594 */
3595int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003596 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003597{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003598 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003599 struct hda_cache_head *c;
3600 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003601 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003602
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003603 cache_only = codec->cached_write;
3604 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003605 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003606 if (err < 0)
3607 return err;
3608 }
3609
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003610 /* parm may contain the verb stuff for get/set amp */
3611 verb = verb | (parm >> 8);
3612 parm &= 0xff;
3613 key = build_cmd_cache_key(nid, verb);
3614 mutex_lock(&codec->bus->cmd_mutex);
3615 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003616 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003617 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003618 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003619 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003620 mutex_unlock(&codec->bus->cmd_mutex);
3621 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003622}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003623EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003624
Takashi Iwaid5191e52009-11-16 14:58:17 +01003625/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003626 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3627 * @codec: the HDA codec
3628 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003629 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003630 * @verb: the verb to send
3631 * @parm: the parameter for the verb
3632 *
3633 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3634 * command if the parameter is already identical with the cached value.
3635 * If not, it sends the command and refreshes the cache.
3636 *
3637 * Returns 0 if successful, or a negative error code.
3638 */
3639int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003640 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003641{
3642 struct hda_cache_head *c;
3643 u32 key;
3644
3645 /* parm may contain the verb stuff for get/set amp */
3646 verb = verb | (parm >> 8);
3647 parm &= 0xff;
3648 key = build_cmd_cache_key(nid, verb);
3649 mutex_lock(&codec->bus->cmd_mutex);
3650 c = get_hash(&codec->cmd_cache, key);
3651 if (c && c->val == parm) {
3652 mutex_unlock(&codec->bus->cmd_mutex);
3653 return 0;
3654 }
3655 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003656 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003657}
3658EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3659
3660/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003661 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3662 * @codec: HD-audio codec
3663 *
3664 * Execute all verbs recorded in the command caches to resume.
3665 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003666void snd_hda_codec_resume_cache(struct hda_codec *codec)
3667{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003668 int i;
3669
Takashi Iwaic370dd62012-12-13 18:30:04 +01003670 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003671 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003672 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3673 struct hda_cache_head *buffer;
3674 u32 key;
3675
3676 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3677 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003678 if (!key)
3679 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003680 if (!buffer->dirty)
3681 continue;
3682 buffer->dirty = 0;
3683 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003684 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3685 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003686 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003687 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003688 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003689}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003690EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003691
3692/**
3693 * snd_hda_sequence_write_cache - sequence writes with caching
3694 * @codec: the HDA codec
3695 * @seq: VERB array to send
3696 *
3697 * Send the commands sequentially from the given array.
3698 * Thte commands are recorded on cache for power-save and resume.
3699 * The array must be terminated with NID=0.
3700 */
3701void snd_hda_sequence_write_cache(struct hda_codec *codec,
3702 const struct hda_verb *seq)
3703{
3704 for (; seq->nid; seq++)
3705 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3706 seq->param);
3707}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003708EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003709
Takashi Iwaidc870f32013-01-22 15:24:30 +01003710/**
3711 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3712 * @codec: HD-audio codec
3713 */
3714void snd_hda_codec_flush_cache(struct hda_codec *codec)
3715{
3716 snd_hda_codec_resume_amp(codec);
3717 snd_hda_codec_resume_cache(codec);
3718}
3719EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3720
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003721void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003722 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003723{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003726
Takashi Iwaicb53c622007-08-10 17:21:45 +02003727 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003728 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003729 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003730 if (!(wcaps & AC_WCAP_POWER))
3731 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003732 if (codec->power_filter) {
3733 state = codec->power_filter(codec, nid, power_state);
3734 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003736 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003738 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003739 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003740}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003741EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3742
3743/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003744 * supported power states check
3745 */
3746static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3747 unsigned int power_state)
3748{
3749 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3750
Mengdong Line037cb42012-08-10 14:11:58 +02003751 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003752 return false;
3753 if (sup & power_state)
3754 return true;
3755 else
3756 return false;
3757}
3758
3759/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003760 * wait until the state is reached, returns the current state
3761 */
3762static unsigned int hda_sync_power_state(struct hda_codec *codec,
3763 hda_nid_t fg,
3764 unsigned int power_state)
3765{
3766 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3767 unsigned int state, actual_state;
3768
3769 for (;;) {
3770 state = snd_hda_codec_read(codec, fg, 0,
3771 AC_VERB_GET_POWER_STATE, 0);
3772 if (state & AC_PWRST_ERROR)
3773 break;
3774 actual_state = (state >> 4) & 0x0f;
3775 if (actual_state == power_state)
3776 break;
3777 if (time_after_eq(jiffies, end_time))
3778 break;
3779 /* wait until the codec reachs to the target state */
3780 msleep(1);
3781 }
3782 return state;
3783}
3784
Takashi Iwai9419ab62013-01-24 17:23:35 +01003785/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003786unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3787 hda_nid_t nid,
3788 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003789{
3790 if (power_state == AC_PWRST_D3 &&
3791 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3792 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3793 int eapd = snd_hda_codec_read(codec, nid, 0,
3794 AC_VERB_GET_EAPD_BTLENABLE, 0);
3795 if (eapd & 0x02)
3796 return AC_PWRST_D0;
3797 }
3798 return power_state;
3799}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003800EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003801
Takashi Iwai432c6412012-08-28 09:59:20 -07003802/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003803 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003804 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003805static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003806 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003807{
Takashi Iwaid8193872012-08-31 07:54:38 -07003808 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003809 int count;
3810 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003811 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003812
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003813 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003814 if (power_state == AC_PWRST_D3) {
3815 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003816 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003817 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003818 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003819
3820 /* repeat power states setting at most 10 times*/
3821 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003822 if (codec->patch_ops.set_power_state)
3823 codec->patch_ops.set_power_state(codec, fg,
3824 power_state);
3825 else {
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003826 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003827 AC_VERB_SET_POWER_STATE,
3828 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003829 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003830 }
3831 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003832 if (!(state & AC_PWRST_ERROR))
3833 break;
3834 }
Mengdong Linb8dfc462012-08-23 17:32:30 +08003835
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003836 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003837}
Takashi Iwai54d17402005-11-21 16:33:22 +01003838
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003839/* sync power states of all widgets;
3840 * this is called at the end of codec parsing
3841 */
3842static void sync_power_up_states(struct hda_codec *codec)
3843{
3844 hda_nid_t nid = codec->start_nid;
3845 int i;
3846
Takashi Iwaiba615b82013-03-13 14:47:21 +01003847 /* don't care if no filter is used */
3848 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003849 return;
3850
3851 for (i = 0; i < codec->num_nodes; i++, nid++) {
3852 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003853 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003854 if (!(wcaps & AC_WCAP_POWER))
3855 continue;
3856 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3857 if (target == AC_PWRST_D0)
3858 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003859 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003860 snd_hda_codec_write(codec, nid, 0,
3861 AC_VERB_SET_POWER_STATE, target);
3862 }
3863}
3864
Takashi Iwai11aeff02008-07-30 15:01:46 +02003865#ifdef CONFIG_SND_HDA_HWDEP
3866/* execute additional init verbs */
3867static void hda_exec_init_verbs(struct hda_codec *codec)
3868{
3869 if (codec->init_verbs.list)
3870 snd_hda_sequence_write(codec, codec->init_verbs.list);
3871}
3872#else
3873static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3874#endif
3875
Takashi Iwai2a439522011-07-26 09:52:50 +02003876#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003877/*
3878 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003879 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003880 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003881static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003882{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003883 unsigned int state;
3884
Takashi Iwai989c3182012-11-19 14:14:58 +01003885 codec->in_pm = 1;
3886
Takashi Iwaicb53c622007-08-10 17:21:45 +02003887 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003888 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003889 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003890 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003891 /* Cancel delayed work if we aren't currently running from it. */
3892 if (!in_wq)
3893 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003894 spin_lock(&codec->power_lock);
3895 snd_hda_update_power_acct(codec);
3896 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003897 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003898 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003899 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003900 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003901 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003902 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003903}
3904
Takashi Iwaic370dd62012-12-13 18:30:04 +01003905/* mark all entries of cmd and amp caches dirty */
3906static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3907{
3908 int i;
3909 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3910 struct hda_cache_head *cmd;
3911 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3912 cmd->dirty = 1;
3913 }
3914 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3915 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003916 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003917 amp->head.dirty = 1;
3918 }
3919}
3920
Takashi Iwaicb53c622007-08-10 17:21:45 +02003921/*
3922 * kick up codec; used both from PM and power-save
3923 */
3924static void hda_call_codec_resume(struct hda_codec *codec)
3925{
Takashi Iwai989c3182012-11-19 14:14:58 +01003926 codec->in_pm = 1;
3927
Takashi Iwaic370dd62012-12-13 18:30:04 +01003928 hda_mark_cmd_cache_dirty(codec);
3929
Takashi Iwai7f308302012-05-08 16:52:23 +02003930 /* set as if powered on for avoiding re-entering the resume
3931 * in the resume / power-save sequence
3932 */
3933 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003934 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003935 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003936 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003937 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003938 if (codec->patch_ops.resume)
3939 codec->patch_ops.resume(codec);
3940 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003941 if (codec->patch_ops.init)
3942 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003943 snd_hda_codec_resume_amp(codec);
3944 snd_hda_codec_resume_cache(codec);
3945 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003946
3947 if (codec->jackpoll_interval)
3948 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003949 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003950 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003951
3952 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003953 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003954}
Takashi Iwai2a439522011-07-26 09:52:50 +02003955#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003956
Takashi Iwai54d17402005-11-21 16:33:22 +01003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958/**
3959 * snd_hda_build_controls - build mixer controls
3960 * @bus: the BUS
3961 *
3962 * Creates mixer controls for each codec included in the bus.
3963 *
3964 * Returns 0 if successful, otherwise a negative error code.
3965 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003966int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003968 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Takashi Iwai0ba21762007-04-16 11:29:14 +02003970 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003971 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003972 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003973 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003974 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003975 err = snd_hda_codec_reset(codec);
3976 if (err < 0) {
3977 printk(KERN_ERR
3978 "hda_codec: cannot revert codec\n");
3979 return err;
3980 }
3981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003983 return 0;
3984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003985EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003987/*
3988 * add standard channel maps if not specified
3989 */
3990static int add_std_chmaps(struct hda_codec *codec)
3991{
3992 int i, str, err;
3993
3994 for (i = 0; i < codec->num_pcms; i++) {
3995 for (str = 0; str < 2; str++) {
3996 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3997 struct hda_pcm_stream *hinfo =
3998 &codec->pcm_info[i].stream[str];
3999 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004000 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004001
4002 if (codec->pcm_info[i].own_chmap)
4003 continue;
4004 if (!pcm || !hinfo->substreams)
4005 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004006 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4007 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004008 hinfo->channels_max,
4009 0, &chmap);
4010 if (err < 0)
4011 return err;
4012 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4013 }
4014 }
4015 return 0;
4016}
4017
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004018/* default channel maps for 2.1 speakers;
4019 * since HD-audio supports only stereo, odd number channels are omitted
4020 */
4021const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4022 { .channels = 2,
4023 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4024 { .channels = 4,
4025 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4026 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4027 { }
4028};
4029EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4030
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004031int snd_hda_codec_build_controls(struct hda_codec *codec)
4032{
4033 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004034 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004035 /* continue to initialize... */
4036 if (codec->patch_ops.init)
4037 err = codec->patch_ops.init(codec);
4038 if (!err && codec->patch_ops.build_controls)
4039 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004040 if (err < 0)
4041 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004042
4043 /* we create chmaps here instead of build_pcms */
4044 err = add_std_chmaps(codec);
4045 if (err < 0)
4046 return err;
4047
David Henningsson26a6cb62012-10-09 15:04:21 +02004048 if (codec->jackpoll_interval)
4049 hda_jackpoll_work(&codec->jackpoll_work.work);
4050 else
4051 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004052 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return 0;
4054}
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/*
4057 * stream formats
4058 */
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004059struct hda_rate_tbl {
4060 unsigned int hz;
4061 unsigned int alsa_bits;
4062 unsigned int hda_fmt;
4063};
4064
Takashi Iwai92f10b32010-08-03 14:21:00 +02004065/* rate = base * mult / div */
4066#define HDA_RATE(base, mult, div) \
4067 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4068 (((div) - 1) << AC_FMT_DIV_SHIFT))
4069
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004070static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004072
4073 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004074 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4075 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4076 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4077 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4078 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4079 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4080 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4081 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4082 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4083 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4084 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004085#define AC_PAR_PCM_RATE_BITS 11
4086 /* up to bits 10, 384kHZ isn't supported properly */
4087
4088 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004089 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004090
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004091 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092};
4093
4094/**
4095 * snd_hda_calc_stream_format - calculate format bitset
4096 * @rate: the sample rate
4097 * @channels: the number of channels
4098 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4099 * @maxbps: the max. bps
4100 *
4101 * Calculate the format bitset from the given rate, channels and th PCM format.
4102 *
4103 * Return zero if invalid.
4104 */
4105unsigned int snd_hda_calc_stream_format(unsigned int rate,
4106 unsigned int channels,
4107 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004108 unsigned int maxbps,
4109 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 int i;
4112 unsigned int val = 0;
4113
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004114 for (i = 0; rate_bits[i].hz; i++)
4115 if (rate_bits[i].hz == rate) {
4116 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 break;
4118 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004119 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 snd_printdd("invalid rate %d\n", rate);
4121 return 0;
4122 }
4123
4124 if (channels == 0 || channels > 8) {
4125 snd_printdd("invalid channels %d\n", channels);
4126 return 0;
4127 }
4128 val |= channels - 1;
4129
4130 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004131 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004132 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004133 break;
4134 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004135 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 case 20:
4138 case 24:
4139 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004140 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004141 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004143 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004145 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 break;
4147 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004148 snd_printdd("invalid format width %d\n",
4149 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return 0;
4151 }
4152
Anssi Hannula32c168c2010-08-03 13:28:57 +03004153 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004154 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return val;
4157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004158EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004160static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4161 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004162{
4163 unsigned int val = 0;
4164 if (nid != codec->afg &&
4165 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4166 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4167 if (!val || val == -1)
4168 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4169 if (!val || val == -1)
4170 return 0;
4171 return val;
4172}
4173
4174static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4175{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004176 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004177 get_pcm_param);
4178}
4179
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004180static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4181 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004182{
4183 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4184 if (!streams || streams == -1)
4185 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4186 if (!streams || streams == -1)
4187 return 0;
4188 return streams;
4189}
4190
4191static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4192{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004193 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004194 get_stream_param);
4195}
4196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197/**
4198 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4199 * @codec: the HDA codec
4200 * @nid: NID to query
4201 * @ratesp: the pointer to store the detected rate bitflags
4202 * @formatsp: the pointer to store the detected formats
4203 * @bpsp: the pointer to store the detected format widths
4204 *
4205 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4206 * or @bsps argument is ignored.
4207 *
4208 * Returns 0 if successful, otherwise a negative error code.
4209 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004210int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4212{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004213 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004215 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004216 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
4218 if (ratesp) {
4219 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004220 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 if (val & (1 << i))
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004222 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004224 if (rates == 0) {
4225 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4226 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4227 nid, val,
4228 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4229 return -EIO;
4230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 *ratesp = rates;
4232 }
4233
4234 if (formatsp || bpsp) {
4235 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004236 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004238 streams = query_stream_param(codec, nid);
4239 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242 bps = 0;
4243 if (streams & AC_SUPFMT_PCM) {
4244 if (val & AC_SUPPCM_BITS_8) {
4245 formats |= SNDRV_PCM_FMTBIT_U8;
4246 bps = 8;
4247 }
4248 if (val & AC_SUPPCM_BITS_16) {
4249 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4250 bps = 16;
4251 }
4252 if (wcaps & AC_WCAP_DIGITAL) {
4253 if (val & AC_SUPPCM_BITS_32)
4254 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4255 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4256 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4257 if (val & AC_SUPPCM_BITS_24)
4258 bps = 24;
4259 else if (val & AC_SUPPCM_BITS_20)
4260 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004261 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4262 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4264 if (val & AC_SUPPCM_BITS_32)
4265 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 else if (val & AC_SUPPCM_BITS_24)
4267 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004268 else if (val & AC_SUPPCM_BITS_20)
4269 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 }
4271 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004272#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004273 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004275 if (!bps)
4276 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004277 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004278#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004279 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004280 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 /* temporary hack: we have still no proper support
4282 * for the direct AC3 stream...
4283 */
4284 formats |= SNDRV_PCM_FMTBIT_U8;
4285 bps = 8;
4286 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004287 if (formats == 0) {
4288 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4289 "(nid=0x%x, val=0x%x, ovrd=%i, "
4290 "streams=0x%x)\n",
4291 nid, val,
4292 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4293 streams);
4294 return -EIO;
4295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 if (formatsp)
4297 *formatsp = formats;
4298 if (bpsp)
4299 *bpsp = bps;
4300 }
4301
4302 return 0;
4303}
Stephen Warren384a48d2011-06-01 11:14:21 -06004304EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004307 * snd_hda_is_supported_format - Check the validity of the format
4308 * @codec: HD-audio codec
4309 * @nid: NID to check
4310 * @format: the HD-audio format value to check
4311 *
4312 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 *
4314 * Returns 1 if supported, 0 if not.
4315 */
4316int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4317 unsigned int format)
4318{
4319 int i;
4320 unsigned int val = 0, rate, stream;
4321
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004322 val = query_pcm_param(codec, nid);
4323 if (!val)
4324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004327 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004328 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (val & (1 << i))
4330 break;
4331 return 0;
4332 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004333 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return 0;
4335
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004336 stream = query_stream_param(codec, nid);
4337 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return 0;
4339
4340 if (stream & AC_SUPFMT_PCM) {
4341 switch (format & 0xf0) {
4342 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004343 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return 0;
4345 break;
4346 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004347 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return 0;
4349 break;
4350 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004351 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return 0;
4353 break;
4354 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004355 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return 0;
4357 break;
4358 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004359 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return 0;
4361 break;
4362 default:
4363 return 0;
4364 }
4365 } else {
4366 /* FIXME: check for float32 and AC3? */
4367 }
4368
4369 return 1;
4370}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004371EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373/*
4374 * PCM stuff
4375 */
4376static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4377 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004378 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
4380 return 0;
4381}
4382
4383static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4384 struct hda_codec *codec,
4385 unsigned int stream_tag,
4386 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004387 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
4389 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4390 return 0;
4391}
4392
4393static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4394 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004395 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Takashi Iwai888afa12008-03-18 09:57:50 +01004397 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return 0;
4399}
4400
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004401static int set_pcm_default_values(struct hda_codec *codec,
4402 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004404 int err;
4405
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 /* query support PCM information from the given NID */
4407 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004408 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004409 info->rates ? NULL : &info->rates,
4410 info->formats ? NULL : &info->formats,
4411 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004412 if (err < 0)
4413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415 if (info->ops.open == NULL)
4416 info->ops.open = hda_pcm_default_open_close;
4417 if (info->ops.close == NULL)
4418 info->ops.close = hda_pcm_default_open_close;
4419 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004420 if (snd_BUG_ON(!info->nid))
4421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 info->ops.prepare = hda_pcm_default_prepare;
4423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004425 if (snd_BUG_ON(!info->nid))
4426 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 info->ops.cleanup = hda_pcm_default_cleanup;
4428 }
4429 return 0;
4430}
4431
Takashi Iwaieb541332010-08-06 13:48:11 +02004432/*
4433 * codec prepare/cleanup entries
4434 */
4435int snd_hda_codec_prepare(struct hda_codec *codec,
4436 struct hda_pcm_stream *hinfo,
4437 unsigned int stream,
4438 unsigned int format,
4439 struct snd_pcm_substream *substream)
4440{
4441 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004442 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004443 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4444 if (ret >= 0)
4445 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004446 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004447 return ret;
4448}
4449EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4450
4451void snd_hda_codec_cleanup(struct hda_codec *codec,
4452 struct hda_pcm_stream *hinfo,
4453 struct snd_pcm_substream *substream)
4454{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004455 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004456 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004457 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004458}
4459EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4460
Takashi Iwaid5191e52009-11-16 14:58:17 +01004461/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004462const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4463 "Audio", "SPDIF", "HDMI", "Modem"
4464};
4465
Takashi Iwai176d5332008-07-30 15:01:44 +02004466/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004467 * get the empty PCM device number to assign
4468 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004469static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004470{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004471 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004472 /* assigned to static slots up to dev#10; if more needed, assign
4473 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4474 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004475 static int audio_idx[HDA_PCM_NTYPES][5] = {
4476 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4477 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004478 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004479 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004480 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004481 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004482
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004483 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004484 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4485 return -EINVAL;
4486 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004487
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004488 for (i = 0; audio_idx[type][i] >= 0; i++) {
4489#ifndef CONFIG_SND_DYNAMIC_MINORS
4490 if (audio_idx[type][i] >= 8)
4491 break;
4492#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004493 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4494 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004495 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004496
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004497#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004498 /* non-fixed slots starting from 10 */
4499 for (i = 10; i < 32; i++) {
4500 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4501 return i;
4502 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004503#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004504
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004505 snd_printk(KERN_WARNING "Too many %s devices\n",
4506 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004507#ifndef CONFIG_SND_DYNAMIC_MINORS
4508 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4509#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004510 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004511}
4512
4513/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004514 * attach a new PCM stream
4515 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004516static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004517{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004518 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004519 struct hda_pcm_stream *info;
4520 int stream, err;
4521
Takashi Iwaib91f0802008-11-04 08:43:08 +01004522 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004523 return -EINVAL;
4524 for (stream = 0; stream < 2; stream++) {
4525 info = &pcm->stream[stream];
4526 if (info->substreams) {
4527 err = set_pcm_default_values(codec, info);
4528 if (err < 0)
4529 return err;
4530 }
4531 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004532 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004533}
4534
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004535/* assign all PCMs of the given codec */
4536int snd_hda_codec_build_pcms(struct hda_codec *codec)
4537{
4538 unsigned int pcm;
4539 int err;
4540
4541 if (!codec->num_pcms) {
4542 if (!codec->patch_ops.build_pcms)
4543 return 0;
4544 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004545 if (err < 0) {
4546 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004547 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004548 err = snd_hda_codec_reset(codec);
4549 if (err < 0) {
4550 printk(KERN_ERR
4551 "hda_codec: cannot revert codec\n");
4552 return err;
4553 }
4554 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004555 }
4556 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4557 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4558 int dev;
4559
4560 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004561 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004562
4563 if (!cpcm->pcm) {
4564 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4565 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004566 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004567 cpcm->device = dev;
4568 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004569 if (err < 0) {
4570 printk(KERN_ERR "hda_codec: cannot attach "
4571 "PCM stream %d for codec #%d\n",
4572 dev, codec->addr);
4573 continue; /* no fatal error */
4574 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004575 }
4576 }
4577 return 0;
4578}
4579
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580/**
4581 * snd_hda_build_pcms - build PCM information
4582 * @bus: the BUS
4583 *
4584 * Create PCM information for each codec included in the bus.
4585 *
4586 * The build_pcms codec patch is requested to set up codec->num_pcms and
4587 * codec->pcm_info properly. The array is referred by the top-level driver
4588 * to create its PCM instances.
4589 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4590 * callback.
4591 *
4592 * At least, substreams, channels_min and channels_max must be filled for
4593 * each stream. substreams = 0 indicates that the stream doesn't exist.
4594 * When rates and/or formats are zero, the supported values are queried
4595 * from the given nid. The nid is used also by the default ops.prepare
4596 * and ops.cleanup callbacks.
4597 *
4598 * The driver needs to call ops.open in its open callback. Similarly,
4599 * ops.close is supposed to be called in the close callback.
4600 * ops.prepare should be called in the prepare or hw_params callback
4601 * with the proper parameters for set up.
4602 * ops.cleanup should be called in hw_free for clean up of streams.
4603 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004604 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004606int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004608 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Takashi Iwai0ba21762007-04-16 11:29:14 +02004610 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004611 int err = snd_hda_codec_build_pcms(codec);
4612 if (err < 0)
4613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615 return 0;
4616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004617EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619/**
4620 * snd_hda_check_board_config - compare the current codec with the config table
4621 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004622 * @num_configs: number of config enums
4623 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 * @tbl: configuration table, terminated by null entries
4625 *
4626 * Compares the modelname or PCI subsystem id of the current codec with the
4627 * given configuration table. If a matching entry is found, returns its
4628 * config value (supposed to be 0 or positive).
4629 *
4630 * If no entries are matching, the function returns a negative value.
4631 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004632int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004633 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004634 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004636 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004637 int i;
4638 for (i = 0; i < num_configs; i++) {
4639 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004640 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004641 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4642 "selected\n", models[i]);
4643 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645 }
4646 }
4647
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004648 if (!codec->bus->pci || !tbl)
4649 return -1;
4650
4651 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4652 if (!tbl)
4653 return -1;
4654 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004655#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004656 char tmp[10];
4657 const char *model = NULL;
4658 if (models)
4659 model = models[tbl->value];
4660 if (!model) {
4661 sprintf(tmp, "#%d", tbl->value);
4662 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004664 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4665 "for config %x:%x (%s)\n",
4666 model, tbl->subvendor, tbl->subdevice,
4667 (tbl->name ? tbl->name : "Unknown device"));
4668#endif
4669 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
4671 return -1;
4672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004673EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004676 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004677 subsystem ID with the
4678 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004679
4680 This is important for Gateway notebooks with SB450 HDA Audio
4681 where the vendor ID of the PCI device is:
4682 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4683 and the vendor/subvendor are found only at the codec.
4684
4685 * @codec: the HDA codec
4686 * @num_configs: number of config enums
4687 * @models: array of model name strings
4688 * @tbl: configuration table, terminated by null entries
4689 *
4690 * Compares the modelname or PCI subsystem id of the current codec with the
4691 * given configuration table. If a matching entry is found, returns its
4692 * config value (supposed to be 0 or positive).
4693 *
4694 * If no entries are matching, the function returns a negative value.
4695 */
4696int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004697 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004698 const struct snd_pci_quirk *tbl)
4699{
4700 const struct snd_pci_quirk *q;
4701
4702 /* Search for codec ID */
4703 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004704 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4705 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4706 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004707 break;
4708 }
4709
4710 if (!q->subvendor)
4711 return -1;
4712
4713 tbl = q;
4714
4715 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004716#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004717 char tmp[10];
4718 const char *model = NULL;
4719 if (models)
4720 model = models[tbl->value];
4721 if (!model) {
4722 sprintf(tmp, "#%d", tbl->value);
4723 model = tmp;
4724 }
4725 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4726 "for config %x:%x (%s)\n",
4727 model, tbl->subvendor, tbl->subdevice,
4728 (tbl->name ? tbl->name : "Unknown device"));
4729#endif
4730 return tbl->value;
4731 }
4732 return -1;
4733}
4734EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4735
4736/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 * snd_hda_add_new_ctls - create controls from the array
4738 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004739 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 *
4741 * This helper function creates and add new controls in the given array.
4742 * The array must be terminated with an empty entry as terminator.
4743 *
4744 * Returns 0 if successful, or a negative error code.
4745 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004746int snd_hda_add_new_ctls(struct hda_codec *codec,
4747 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004749 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004752 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004753 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004754 if (knew->iface == -1) /* skip this codec private value */
4755 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004756 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004757 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004758 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004759 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004760 if (addr > 0)
4761 kctl->id.device = addr;
4762 if (idx > 0)
4763 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004764 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004765 if (!err)
4766 break;
4767 /* try first with another device index corresponding to
4768 * the codec addr; if it still fails (or it's the
4769 * primary codec), then try another control index
4770 */
4771 if (!addr && codec->addr)
4772 addr = codec->addr;
4773 else if (!idx && !knew->index) {
4774 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004775 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004776 if (idx <= 0)
4777 return err;
4778 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004779 return err;
4780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782 return 0;
4783}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004784EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Takashi Iwai83012a72012-08-24 18:38:08 +02004786#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004787static void hda_power_work(struct work_struct *work)
4788{
4789 struct hda_codec *codec =
4790 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004791 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004792 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004794 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004795 if (codec->power_transition > 0) { /* during power-up sequence? */
4796 spin_unlock(&codec->power_lock);
4797 return;
4798 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004799 if (!codec->power_on || codec->power_count) {
4800 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004801 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004802 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004803 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004804 spin_unlock(&codec->power_lock);
4805
Dylan Reidd17344b2012-09-28 15:57:01 -07004806 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004807 codec->pm_down_notified = 0;
4808 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4809 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004810 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004811 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004812}
4813
4814static void hda_keep_power_on(struct hda_codec *codec)
4815{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004816 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004817 codec->power_count++;
4818 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004819 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004820 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004821}
4822
Takashi Iwaid5191e52009-11-16 14:58:17 +01004823/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004824void snd_hda_update_power_acct(struct hda_codec *codec)
4825{
4826 unsigned long delta = jiffies - codec->power_jiffies;
4827 if (codec->power_on)
4828 codec->power_on_acct += delta;
4829 else
4830 codec->power_off_acct += delta;
4831 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004832}
4833
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004834/* Transition to powered up, if wait_power_down then wait for a pending
4835 * transition to D3 to complete. A pending D3 transition is indicated
4836 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004837/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004838static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004839{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004840 struct hda_bus *bus = codec->bus;
4841
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004842 /* Return if power_on or transitioning to power_on, unless currently
4843 * powering down. */
4844 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004845 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004846 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004847 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004848
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004849 cancel_delayed_work_sync(&codec->power_work);
4850
4851 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004852 /* If the power down delayed work was cancelled above before starting,
4853 * then there is no need to go through power up here.
4854 */
4855 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004856 if (codec->power_transition < 0)
4857 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004858 return;
4859 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004860
Takashi Iwaid66fee52011-08-02 15:39:31 +02004861 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004862 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004863 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004864 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004865 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004866 spin_unlock(&codec->power_lock);
4867
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004868 if (codec->pm_down_notified) {
4869 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004870 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004871 }
4872
Takashi Iwaicb53c622007-08-10 17:21:45 +02004873 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004874
4875 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004876 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004877}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004878
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004879#define power_save(codec) \
4880 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004881
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004882/* Transition to powered down */
4883static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004884{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004885 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004886 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004887
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004888 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004889 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004890 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004891 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004892 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004893}
4894
4895/**
4896 * snd_hda_power_save - Power-up/down/sync the codec
4897 * @codec: HD-audio codec
4898 * @delta: the counter delta to change
4899 *
4900 * Change the power-up counter via @delta, and power up or down the hardware
4901 * appropriately. For the power-down, queue to the delayed action.
4902 * Passing zero to @delta means to synchronize the power state.
4903 */
4904void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4905{
4906 spin_lock(&codec->power_lock);
4907 codec->power_count += delta;
4908 trace_hda_power_count(codec);
4909 if (delta > 0)
4910 __snd_hda_power_up(codec, d3wait);
4911 else
4912 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004913 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004914}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004915EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004916
Takashi Iwaid5191e52009-11-16 14:58:17 +01004917/**
4918 * snd_hda_check_amp_list_power - Check the amp list and update the power
4919 * @codec: HD-audio codec
4920 * @check: the object containing an AMP list and the status
4921 * @nid: NID to check / update
4922 *
4923 * Check whether the given NID is in the amp list. If it's in the list,
4924 * check the current AMP status, and update the the power-status according
4925 * to the mute status.
4926 *
4927 * This function is supposed to be set or called from the check_power_status
4928 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004929 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004930int snd_hda_check_amp_list_power(struct hda_codec *codec,
4931 struct hda_loopback_check *check,
4932 hda_nid_t nid)
4933{
Takashi Iwai031024e2011-05-02 11:29:30 +02004934 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004935 int ch, v;
4936
4937 if (!check->amplist)
4938 return 0;
4939 for (p = check->amplist; p->nid; p++) {
4940 if (p->nid == nid)
4941 break;
4942 }
4943 if (!p->nid)
4944 return 0; /* nothing changed */
4945
4946 for (p = check->amplist; p->nid; p++) {
4947 for (ch = 0; ch < 2; ch++) {
4948 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4949 p->idx);
4950 if (!(v & HDA_AMP_MUTE) && v > 0) {
4951 if (!check->power_on) {
4952 check->power_on = 1;
4953 snd_hda_power_up(codec);
4954 }
4955 return 1;
4956 }
4957 }
4958 }
4959 if (check->power_on) {
4960 check->power_on = 0;
4961 snd_hda_power_down(codec);
4962 }
4963 return 0;
4964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004965EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004968/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004969 * Channel mode helper
4970 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004971
4972/**
4973 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004975int snd_hda_ch_mode_info(struct hda_codec *codec,
4976 struct snd_ctl_elem_info *uinfo,
4977 const struct hda_channel_mode *chmode,
4978 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004979{
4980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4981 uinfo->count = 1;
4982 uinfo->value.enumerated.items = num_chmodes;
4983 if (uinfo->value.enumerated.item >= num_chmodes)
4984 uinfo->value.enumerated.item = num_chmodes - 1;
4985 sprintf(uinfo->value.enumerated.name, "%dch",
4986 chmode[uinfo->value.enumerated.item].channels);
4987 return 0;
4988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004989EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004990
Takashi Iwaid5191e52009-11-16 14:58:17 +01004991/**
4992 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4993 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004994int snd_hda_ch_mode_get(struct hda_codec *codec,
4995 struct snd_ctl_elem_value *ucontrol,
4996 const struct hda_channel_mode *chmode,
4997 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004998 int max_channels)
4999{
5000 int i;
5001
5002 for (i = 0; i < num_chmodes; i++) {
5003 if (max_channels == chmode[i].channels) {
5004 ucontrol->value.enumerated.item[0] = i;
5005 break;
5006 }
5007 }
5008 return 0;
5009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005010EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005011
Takashi Iwaid5191e52009-11-16 14:58:17 +01005012/**
5013 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005015int snd_hda_ch_mode_put(struct hda_codec *codec,
5016 struct snd_ctl_elem_value *ucontrol,
5017 const struct hda_channel_mode *chmode,
5018 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005019 int *max_channelsp)
5020{
5021 unsigned int mode;
5022
5023 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005024 if (mode >= num_chmodes)
5025 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005026 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005027 return 0;
5028 /* change the current channel setting */
5029 *max_channelsp = chmode[mode].channels;
5030 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005031 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005032 return 1;
5033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005034EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036/*
5037 * input MUX helper
5038 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005039
5040/**
5041 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5042 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005043int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5044 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
5046 unsigned int index;
5047
5048 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5049 uinfo->count = 1;
5050 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005051 if (!imux->num_items)
5052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 index = uinfo->value.enumerated.item;
5054 if (index >= imux->num_items)
5055 index = imux->num_items - 1;
5056 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5057 return 0;
5058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005059EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Takashi Iwaid5191e52009-11-16 14:58:17 +01005061/**
5062 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5063 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005064int snd_hda_input_mux_put(struct hda_codec *codec,
5065 const struct hda_input_mux *imux,
5066 struct snd_ctl_elem_value *ucontrol,
5067 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 unsigned int *cur_val)
5069{
5070 unsigned int idx;
5071
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005072 if (!imux->num_items)
5073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 idx = ucontrol->value.enumerated.item[0];
5075 if (idx >= imux->num_items)
5076 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005077 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005079 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5080 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 *cur_val = idx;
5082 return 1;
5083}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005084EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
5086
5087/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005088 * process kcontrol info callback of a simple string enum array
5089 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5090 */
5091int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5092 struct snd_ctl_elem_info *uinfo,
5093 int num_items, const char * const *texts)
5094{
5095 static const char * const texts_default[] = {
5096 "Disabled", "Enabled"
5097 };
5098
5099 if (!texts || !num_items) {
5100 num_items = 2;
5101 texts = texts_default;
5102 }
5103
5104 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5105 uinfo->count = 1;
5106 uinfo->value.enumerated.items = num_items;
5107 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5108 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5109 strcpy(uinfo->value.enumerated.name,
5110 texts[uinfo->value.enumerated.item]);
5111 return 0;
5112}
5113EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5114
5115/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 * Multi-channel / digital-out PCM helper functions
5117 */
5118
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005119/* setup SPDIF output stream */
5120static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5121 unsigned int stream_tag, unsigned int format)
5122{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005123 struct hda_spdif_out *spdif;
5124 unsigned int curr_fmt;
5125 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005126
Laurence Darby3bef1c32012-11-03 17:00:06 +00005127 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5128 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5129 AC_VERB_GET_STREAM_FORMAT, 0);
5130 reset = codec->spdif_status_reset &&
5131 (spdif->ctls & AC_DIG1_ENABLE) &&
5132 curr_fmt != format;
5133
5134 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5135 updated */
5136 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005137 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005138 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005139 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005140 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005141 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005142 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005143 for (d = codec->slave_dig_outs; *d; d++)
5144 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5145 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005146 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005147 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005148 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005149 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005150 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005151}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005152
Takashi Iwai2f728532008-09-25 16:32:41 +02005153static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5154{
5155 snd_hda_codec_cleanup_stream(codec, nid);
5156 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005157 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005158 for (d = codec->slave_dig_outs; *d; d++)
5159 snd_hda_codec_cleanup_stream(codec, *d);
5160 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005161}
5162
Takashi Iwaid5191e52009-11-16 14:58:17 +01005163/**
5164 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5165 * @bus: HD-audio bus
5166 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005167void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5168{
5169 struct hda_codec *codec;
5170
5171 if (!bus)
5172 return;
5173 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005174 if (hda_codec_is_power_on(codec) &&
5175 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005176 codec->patch_ops.reboot_notify(codec);
5177 }
5178}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005179EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005180
Takashi Iwaid5191e52009-11-16 14:58:17 +01005181/**
5182 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005184int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5185 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186{
Ingo Molnar62932df2006-01-16 16:34:20 +01005187 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005188 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5189 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005190 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005192 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 return 0;
5194}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005195EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Takashi Iwaid5191e52009-11-16 14:58:17 +01005197/**
5198 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5199 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005200int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5201 struct hda_multi_out *mout,
5202 unsigned int stream_tag,
5203 unsigned int format,
5204 struct snd_pcm_substream *substream)
5205{
5206 mutex_lock(&codec->spdif_mutex);
5207 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5208 mutex_unlock(&codec->spdif_mutex);
5209 return 0;
5210}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005211EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005212
Takashi Iwaid5191e52009-11-16 14:58:17 +01005213/**
5214 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5215 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005216int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5217 struct hda_multi_out *mout)
5218{
5219 mutex_lock(&codec->spdif_mutex);
5220 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5221 mutex_unlock(&codec->spdif_mutex);
5222 return 0;
5223}
5224EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5225
Takashi Iwaid5191e52009-11-16 14:58:17 +01005226/**
5227 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005229int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5230 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
Ingo Molnar62932df2006-01-16 16:34:20 +01005232 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005234 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 return 0;
5236}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005237EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Takashi Iwaid5191e52009-11-16 14:58:17 +01005239/**
5240 * snd_hda_multi_out_analog_open - open analog outputs
5241 *
5242 * Open analog outputs and set up the hw-constraints.
5243 * If the digital outputs can be opened as slave, open the digital
5244 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005246int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5247 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005248 struct snd_pcm_substream *substream,
5249 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
Takashi Iwai9a081602008-02-12 18:37:26 +01005251 struct snd_pcm_runtime *runtime = substream->runtime;
5252 runtime->hw.channels_max = mout->max_channels;
5253 if (mout->dig_out_nid) {
5254 if (!mout->analog_rates) {
5255 mout->analog_rates = hinfo->rates;
5256 mout->analog_formats = hinfo->formats;
5257 mout->analog_maxbps = hinfo->maxbps;
5258 } else {
5259 runtime->hw.rates = mout->analog_rates;
5260 runtime->hw.formats = mout->analog_formats;
5261 hinfo->maxbps = mout->analog_maxbps;
5262 }
5263 if (!mout->spdif_rates) {
5264 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5265 &mout->spdif_rates,
5266 &mout->spdif_formats,
5267 &mout->spdif_maxbps);
5268 }
5269 mutex_lock(&codec->spdif_mutex);
5270 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005271 if ((runtime->hw.rates & mout->spdif_rates) &&
5272 (runtime->hw.formats & mout->spdif_formats)) {
5273 runtime->hw.rates &= mout->spdif_rates;
5274 runtime->hw.formats &= mout->spdif_formats;
5275 if (mout->spdif_maxbps < hinfo->maxbps)
5276 hinfo->maxbps = mout->spdif_maxbps;
5277 } else {
5278 mout->share_spdif = 0;
5279 /* FIXME: need notify? */
5280 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005281 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005282 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5285 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005287EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Takashi Iwaid5191e52009-11-16 14:58:17 +01005289/**
5290 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5291 *
5292 * Set up the i/o for analog out.
5293 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005295int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5296 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 unsigned int stream_tag,
5298 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005299 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Takashi Iwaidda14412011-05-02 11:29:30 +02005301 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005303 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 int i;
5305
Ingo Molnar62932df2006-01-16 16:34:20 +01005306 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005307 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005308 if (mout->dig_out_nid && mout->share_spdif &&
5309 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005311 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5312 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005313 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005315 setup_dig_out_stream(codec, mout->dig_out_nid,
5316 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 } else {
5318 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005319 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 }
5321 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005322 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
5324 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005325 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5326 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005327 if (!mout->no_share_stream &&
5328 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005330 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5331 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005332 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005333 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5334 if (!mout->no_share_stream && mout->hp_out_nid[i])
5335 snd_hda_codec_setup_stream(codec,
5336 mout->hp_out_nid[i],
5337 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005338 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005339 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005340 snd_hda_codec_setup_stream(codec,
5341 mout->extra_out_nid[i],
5342 stream_tag, 0, format);
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 /* surrounds */
5345 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005346 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005347 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5348 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005349 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005350 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5351 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
5353 return 0;
5354}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005355EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
Takashi Iwaid5191e52009-11-16 14:58:17 +01005357/**
5358 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005360int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5361 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Takashi Iwaidda14412011-05-02 11:29:30 +02005363 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 int i;
5365
5366 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005367 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005369 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005370 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5371 if (mout->hp_out_nid[i])
5372 snd_hda_codec_cleanup_stream(codec,
5373 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005374 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5375 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005376 snd_hda_codec_cleanup_stream(codec,
5377 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005378 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005380 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 mout->dig_out_used = 0;
5382 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005383 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 return 0;
5385}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005386EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
Takashi Iwai47408602012-04-20 13:06:53 +02005388/**
5389 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5390 *
5391 * Guess the suitable VREF pin bits to be set as the pin-control value.
5392 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5393 */
5394unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5395{
5396 unsigned int pincap;
5397 unsigned int oldval;
5398 oldval = snd_hda_codec_read(codec, pin, 0,
5399 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5400 pincap = snd_hda_query_pin_caps(codec, pin);
5401 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5402 /* Exception: if the default pin setup is vref50, we give it priority */
5403 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5404 return AC_PINCTL_VREF_80;
5405 else if (pincap & AC_PINCAP_VREF_50)
5406 return AC_PINCTL_VREF_50;
5407 else if (pincap & AC_PINCAP_VREF_100)
5408 return AC_PINCTL_VREF_100;
5409 else if (pincap & AC_PINCAP_VREF_GRD)
5410 return AC_PINCTL_VREF_GRD;
5411 return AC_PINCTL_VREF_HIZ;
5412}
5413EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5414
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005415/* correct the pin ctl value for matching with the pin cap */
5416unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5417 hda_nid_t pin, unsigned int val)
5418{
5419 static unsigned int cap_lists[][2] = {
5420 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5421 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5422 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5423 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5424 };
5425 unsigned int cap;
5426
5427 if (!val)
5428 return 0;
5429 cap = snd_hda_query_pin_caps(codec, pin);
5430 if (!cap)
5431 return val; /* don't know what to do... */
5432
5433 if (val & AC_PINCTL_OUT_EN) {
5434 if (!(cap & AC_PINCAP_OUT))
5435 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5436 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5437 val &= ~AC_PINCTL_HP_EN;
5438 }
5439
5440 if (val & AC_PINCTL_IN_EN) {
5441 if (!(cap & AC_PINCAP_IN))
5442 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5443 else {
5444 unsigned int vcap, vref;
5445 int i;
5446 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5447 vref = val & AC_PINCTL_VREFEN;
5448 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5449 if (vref == cap_lists[i][0] &&
5450 !(vcap & cap_lists[i][1])) {
5451 if (i == ARRAY_SIZE(cap_lists) - 1)
5452 vref = AC_PINCTL_VREF_HIZ;
5453 else
5454 vref = cap_lists[i + 1][0];
5455 }
5456 }
5457 val &= ~AC_PINCTL_VREFEN;
5458 val |= vref;
5459 }
5460 }
5461
5462 return val;
5463}
5464EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5465
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005466int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5467 unsigned int val, bool cached)
5468{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005469 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005470 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005471 if (cached)
5472 return snd_hda_codec_update_cache(codec, pin, 0,
5473 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5474 else
5475 return snd_hda_codec_write(codec, pin, 0,
5476 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5477}
5478EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5479
Takashi Iwai990061c2010-09-09 22:08:44 +02005480/**
5481 * snd_hda_add_imux_item - Add an item to input_mux
5482 *
5483 * When the same label is used already in the existing items, the number
5484 * suffix is appended to the label. This label index number is stored
5485 * to type_idx when non-NULL pointer is given.
5486 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005487int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5488 int index, int *type_idx)
5489{
5490 int i, label_idx = 0;
5491 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5492 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5493 return -EINVAL;
5494 }
5495 for (i = 0; i < imux->num_items; i++) {
5496 if (!strncmp(label, imux->items[i].label, strlen(label)))
5497 label_idx++;
5498 }
5499 if (type_idx)
5500 *type_idx = label_idx;
5501 if (label_idx > 0)
5502 snprintf(imux->items[imux->num_items].label,
5503 sizeof(imux->items[imux->num_items].label),
5504 "%s %d", label, label_idx);
5505 else
5506 strlcpy(imux->items[imux->num_items].label, label,
5507 sizeof(imux->items[imux->num_items].label));
5508 imux->items[imux->num_items].index = index;
5509 imux->num_items++;
5510 return 0;
5511}
5512EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005513
Takashi Iwai4a471b72005-12-07 13:56:29 +01005514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515#ifdef CONFIG_PM
5516/*
5517 * power management
5518 */
5519
5520/**
5521 * snd_hda_suspend - suspend the codecs
5522 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 *
5524 * Returns 0 if successful.
5525 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005526int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005528 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Takashi Iwai0ba21762007-04-16 11:29:14 +02005530 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005531 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005532 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005533 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
5535 return 0;
5536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005537EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539/**
5540 * snd_hda_resume - resume the codecs
5541 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 *
5543 * Returns 0 if successful.
5544 */
5545int snd_hda_resume(struct hda_bus *bus)
5546{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005547 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Takashi Iwai0ba21762007-04-16 11:29:14 +02005549 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005550 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return 0;
5553}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005554EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005555#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005556
5557/*
5558 * generic arrays
5559 */
5560
Takashi Iwaid5191e52009-11-16 14:58:17 +01005561/**
5562 * snd_array_new - get a new element from the given array
5563 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005564 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005565 * Get a new element from the given array. If it exceeds the
5566 * pre-allocated array size, re-allocate the array.
5567 *
5568 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005569 */
5570void *snd_array_new(struct snd_array *array)
5571{
Takashi Iwai12f17712012-10-10 08:59:14 +02005572 if (snd_BUG_ON(!array->elem_size))
5573 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005574 if (array->used >= array->alloced) {
5575 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005576 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005577 void *nlist;
5578 if (snd_BUG_ON(num >= 4096))
5579 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005580 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005581 if (!nlist)
5582 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005583 array->list = nlist;
5584 array->alloced = num;
5585 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005586 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005587}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005588EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005589
Takashi Iwaid5191e52009-11-16 14:58:17 +01005590/**
5591 * snd_array_free - free the given array elements
5592 * @array: the array object
5593 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005594void snd_array_free(struct snd_array *array)
5595{
5596 kfree(array->list);
5597 array->used = 0;
5598 array->alloced = 0;
5599 array->list = NULL;
5600}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005601EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005602
Takashi Iwaid5191e52009-11-16 14:58:17 +01005603/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005604 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5605 * @pcm: PCM caps bits
5606 * @buf: the string buffer to write
5607 * @buflen: the max buffer length
5608 *
5609 * used by hda_proc.c and hda_eld.c
5610 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005611void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5612{
5613 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5614 int i, j;
5615
5616 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5617 if (pcm & (AC_SUPPCM_BITS_8 << i))
5618 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5619
5620 buf[j] = '\0'; /* necessary when j == 0 */
5621}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005622EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005623
5624MODULE_DESCRIPTION("HDA codec core");
5625MODULE_LICENSE("GPL");