blob: 503869aad7f933d36a91b95920f678240efe0c5a [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 Iwai3bcce5c2012-12-20 11:17:17 +0100224 for (;;) {
225 trace_hda_send_cmd(codec, cmd);
226 err = bus->ops.command(bus, cmd);
227 if (err != -EAGAIN)
228 break;
229 /* process pending verbs */
230 bus->ops.get_response(bus, codec->addr);
231 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200232 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800233 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200234 trace_hda_get_response(codec, *res);
235 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200236 mutex_unlock(&bus->cmd_mutex);
237 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100238 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200239 if (bus->response_reset) {
240 snd_printd("hda_codec: resetting BUS due to "
241 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200242 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200243 bus->ops.bus_reset(bus);
244 }
245 goto again;
246 }
247 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100248 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200249 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200250 return err;
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/**
254 * snd_hda_codec_read - send a command and get the response
255 * @codec: the HDA codec
256 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200257 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @verb: the verb to send
259 * @parm: the parameter for the verb
260 *
261 * Send a single command and read the corresponding response.
262 *
263 * Returns the obtained response value, or -1 for an error.
264 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200265unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200266 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned int verb, unsigned int parm)
268{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200269 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200271 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return res;
274}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100275EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/**
278 * snd_hda_codec_write - send a single command without waiting for response
279 * @codec: the HDA codec
280 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200281 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * @verb: the verb to send
283 * @parm: the parameter for the verb
284 *
285 * Send a single command without waiting for response.
286 *
287 * Returns 0 if successful, or a negative error code.
288 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200289int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
290 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200292 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100293 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200294 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100297EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/**
300 * snd_hda_sequence_write - sequence writes
301 * @codec: the HDA codec
302 * @seq: VERB array to send
303 *
304 * Send the commands sequentially from the given array.
305 * The array must be terminated with NID=0.
306 */
307void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
308{
309 for (; seq->nid; seq++)
310 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
311}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100312EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/**
315 * snd_hda_get_sub_nodes - get the range of sub nodes
316 * @codec: the HDA codec
317 * @nid: NID to parse
318 * @start_id: the pointer to store the start NID
319 *
320 * Parse the NID and store the start NID of its sub-nodes.
321 * Returns the number of sub-nodes.
322 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200323int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
324 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 unsigned int parm;
327
328 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200329 if (parm == -1)
330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *start_id = (parm >> 16) & 0x7fff;
332 return (int)(parm & 0x7fff);
333}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100334EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100336/* connection list element */
337struct hda_conn_list {
338 struct list_head list;
339 int len;
340 hda_nid_t nid;
341 hda_nid_t conns[0];
342};
343
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200344/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100345static struct hda_conn_list *
346lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200347{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348 struct hda_conn_list *p;
349 list_for_each_entry(p, &codec->conn_list, list) {
350 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200351 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 }
353 return NULL;
354}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100356static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
357 const hda_nid_t *list)
358{
359 struct hda_conn_list *p;
360
361 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
362 if (!p)
363 return -ENOMEM;
364 p->len = len;
365 p->nid = nid;
366 memcpy(p->conns, list, len * sizeof(hda_nid_t));
367 list_add(&p->list, &codec->conn_list);
368 return 0;
369}
370
371static void remove_conn_list(struct hda_codec *codec)
372{
373 while (!list_empty(&codec->conn_list)) {
374 struct hda_conn_list *p;
375 p = list_first_entry(&codec->conn_list, typeof(*p), list);
376 list_del(&p->list);
377 kfree(p);
378 }
379}
380
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200381/* read the connection and add to the cache */
382static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200383{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100384 hda_nid_t list[32];
385 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200388 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100389 if (len == -ENOSPC) {
390 len = snd_hda_get_num_raw_conns(codec, nid);
391 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
392 if (!result)
393 return -ENOMEM;
394 len = snd_hda_get_raw_connections(codec, nid, result, len);
395 }
396 if (len >= 0)
397 len = snd_hda_override_conn_list(codec, nid, len, result);
398 if (result != list)
399 kfree(result);
400 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200401}
Takashi Iwaidce20792011-06-24 14:10:28 +0200402
403/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100404 * snd_hda_get_conn_list - get connection list
405 * @codec: the HDA codec
406 * @nid: NID to parse
407 * @len: number of connection list entries
408 * @listp: the pointer to store NID list
409 *
410 * Parses the connection list of the given widget and stores the pointer
411 * to the list of NIDs.
412 *
413 * Returns the number of connections, or a negative error code.
414 *
415 * Note that the returned pointer isn't protected against the list
416 * modification. If snd_hda_override_conn_list() might be called
417 * concurrently, protect with a mutex appropriately.
418 */
419int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
420 const hda_nid_t **listp)
421{
422 bool added = false;
423
424 for (;;) {
425 int err;
426 const struct hda_conn_list *p;
427
428 /* if the connection-list is already cached, read it */
429 p = lookup_conn_list(codec, nid);
430 if (p) {
431 if (listp)
432 *listp = p->conns;
433 return p->len;
434 }
435 if (snd_BUG_ON(added))
436 return -EINVAL;
437
438 err = read_and_add_raw_conns(codec, nid);
439 if (err < 0)
440 return err;
441 added = true;
442 }
443}
444EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
445
446/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200447 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * @codec: the HDA codec
449 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200450 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @max_conns: max. number of connections to store
452 *
453 * Parses the connection list of the given widget and stores the list
454 * of NIDs.
455 *
456 * Returns the number of connections, or a negative error code.
457 */
458int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200459 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200460{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100461 const hda_nid_t *list;
462 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200463
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464 if (len > 0 && conn_list) {
465 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200466 snd_printk(KERN_ERR "hda_codec: "
467 "Too many connections %d for NID 0x%x\n",
468 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 return -EINVAL;
470 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100471 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200472 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200473
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100474 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200475}
476EXPORT_SYMBOL_HDA(snd_hda_get_connections);
477
Takashi Iwai4eea3092013-02-07 18:18:19 +0100478/* return CONNLIST_LEN parameter of the given widget */
479static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
480{
481 unsigned int wcaps = get_wcaps(codec, nid);
482 unsigned int parm;
483
484 if (!(wcaps & AC_WCAP_CONN_LIST) &&
485 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
486 return 0;
487
488 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
489 if (parm == -1)
490 parm = 0;
491 return parm;
492}
493
494int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
495{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100496 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100497}
498
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200499/**
500 * snd_hda_get_raw_connections - copy connection list without cache
501 * @codec: the HDA codec
502 * @nid: NID to parse
503 * @conn_list: connection list array
504 * @max_conns: max. number of connections to store
505 *
506 * Like snd_hda_get_connections(), copy the connection list but without
507 * checking through the connection-list cache.
508 * Currently called only from hda_proc.c, so not exported.
509 */
510int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
511 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100514 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100516 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100517 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Takashi Iwai4eea3092013-02-07 18:18:19 +0100519 parm = get_num_conns(codec, nid);
520 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200521 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (parm & AC_CLIST_LONG) {
524 /* long form */
525 shift = 16;
526 num_elems = 2;
527 } else {
528 /* short form */
529 shift = 8;
530 num_elems = 4;
531 }
532 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 mask = (1 << (shift-1)) - 1;
534
Takashi Iwai0ba21762007-04-16 11:29:14 +0200535 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0; /* no connection */
537
538 if (conn_len == 1) {
539 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200540 parm = snd_hda_codec_read(codec, nid, 0,
541 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200542 if (parm == -1 && codec->bus->rirb_error)
543 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100544 if (conn_list)
545 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 1;
547 }
548
549 /* multi connection */
550 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100551 prev_nid = 0;
552 for (i = 0; i < conn_len; i++) {
553 int range_val;
554 hda_nid_t val, n;
555
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200556 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100557 parm = snd_hda_codec_read(codec, nid, 0,
558 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (parm == -1 && codec->bus->rirb_error)
560 return -EIO;
561 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200562 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100563 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100564 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200565 snd_printk(KERN_WARNING "hda_codec: "
566 "invalid CONNECT_LIST verb %x[%i]:%x\n",
567 nid, i, parm);
568 return 0;
569 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100570 parm >>= shift;
571 if (range_val) {
572 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200573 if (!prev_nid || prev_nid >= val) {
574 snd_printk(KERN_WARNING "hda_codec: "
575 "invalid dep_range_val %x:%x\n",
576 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100579 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100580 if (conn_list) {
581 if (conns >= max_conns)
582 return -ENOSPC;
583 conn_list[conns] = n;
584 }
585 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100586 }
587 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100588 if (conn_list) {
589 if (conns >= max_conns)
590 return -ENOSPC;
591 conn_list[conns] = val;
592 }
593 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100595 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 return conns;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200601 * snd_hda_override_conn_list - add/modify the connection-list to cache
602 * @codec: the HDA codec
603 * @nid: NID to parse
604 * @len: number of connection list entries
605 * @list: the list of connection entries
606 *
607 * Add or modify the given connection-list to the cache. If the corresponding
608 * cache already exists, invalidate it and append a new one.
609 *
610 * Returns zero or a negative error code.
611 */
612int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
613 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100615 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200616
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100617 p = lookup_conn_list(codec, nid);
618 if (p) {
619 list_del(&p->list);
620 kfree(p);
621 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200622
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100623 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200625EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
626
627/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200628 * snd_hda_get_conn_index - get the connection index of the given NID
629 * @codec: the HDA codec
630 * @mux: NID containing the list
631 * @nid: NID to select
632 * @recursive: 1 when searching NID recursively, otherwise 0
633 *
634 * Parses the connection list of the widget @mux and checks whether the
635 * widget @nid is present. If it is, return the connection index.
636 * Otherwise it returns -1.
637 */
638int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
639 hda_nid_t nid, int recursive)
640{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200642 int i, nums;
643
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100644 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200645 for (i = 0; i < nums; i++)
646 if (conn[i] == nid)
647 return i;
648 if (!recursive)
649 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100650 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200651 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
652 return -1;
653 }
654 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200655 for (i = 0; i < nums; i++) {
656 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
657 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
658 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200659 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
660 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200661 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 return -1;
663}
664EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/**
667 * snd_hda_queue_unsol_event - add an unsolicited event to queue
668 * @bus: the BUS
669 * @res: unsolicited event (lower 32bit of RIRB entry)
670 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
671 *
672 * Adds the given event to the queue. The events are processed in
673 * the workqueue asynchronously. Call this function in the interrupt
674 * hanlder when RIRB receives an unsolicited event.
675 *
676 * Returns 0 if successful, or a negative error code.
677 */
678int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
679{
680 struct hda_bus_unsolicited *unsol;
681 unsigned int wp;
682
Wang YanQing2195b062013-05-07 11:27:33 +0800683 if (!bus || !bus->workq)
684 return 0;
685
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200686 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200687 unsol = bus->unsol;
688 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690
691 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
692 unsol->wp = wp;
693
694 wp <<= 1;
695 unsol->queue[wp] = res;
696 unsol->queue[wp + 1] = res_ex;
697
Takashi Iwai6acaed32009-01-12 10:09:24 +0100698 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 return 0;
701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100702EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800705 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
David Howellsc4028952006-11-22 14:57:56 +0000707static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
David Howellsc4028952006-11-22 14:57:56 +0000709 struct hda_bus_unsolicited *unsol =
710 container_of(work, struct hda_bus_unsolicited, work);
711 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct hda_codec *codec;
713 unsigned int rp, caddr, res;
714
715 while (unsol->rp != unsol->wp) {
716 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
717 unsol->rp = rp;
718 rp <<= 1;
719 res = unsol->queue[rp];
720 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200721 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 continue;
723 codec = bus->caddr_tbl[caddr & 0x0f];
724 if (codec && codec->patch_ops.unsol_event)
725 codec->patch_ops.unsol_event(codec, res);
726 }
727}
728
729/*
730 * initialize unsolicited queue
731 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200732static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct hda_bus_unsolicited *unsol;
735
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100736 if (bus->unsol) /* already initialized */
737 return 0;
738
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200739 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200740 if (!unsol) {
741 snd_printk(KERN_ERR "hda_codec: "
742 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ENOMEM;
744 }
David Howellsc4028952006-11-22 14:57:56 +0000745 INIT_WORK(&unsol->work, process_unsol_events);
746 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 bus->unsol = unsol;
748 return 0;
749}
750
751/*
752 * destructor
753 */
754static void snd_hda_codec_free(struct hda_codec *codec);
755
756static int snd_hda_bus_free(struct hda_bus *bus)
757{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100762 if (bus->workq)
763 flush_workqueue(bus->workq);
764 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200766 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 snd_hda_codec_free(codec);
768 }
769 if (bus->ops.private_free)
770 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100771 if (bus->workq)
772 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 kfree(bus);
774 return 0;
775}
776
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100777static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100780 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return snd_hda_bus_free(bus);
782}
783
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200784#ifdef CONFIG_SND_HDA_HWDEP
785static int snd_hda_bus_dev_register(struct snd_device *device)
786{
787 struct hda_bus *bus = device->device_data;
788 struct hda_codec *codec;
789 list_for_each_entry(codec, &bus->codec_list, list) {
790 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100791 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200792 }
793 return 0;
794}
795#else
796#define snd_hda_bus_dev_register NULL
797#endif
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/**
800 * snd_hda_bus_new - create a HDA bus
801 * @card: the card entry
802 * @temp: the template for hda_bus information
803 * @busp: the pointer to store the created bus instance
804 *
805 * Returns 0 if successful, or a negative error code.
806 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100807int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200808 const struct hda_bus_template *temp,
809 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct hda_bus *bus;
812 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100813 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200814 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .dev_free = snd_hda_bus_dev_free,
816 };
817
Takashi Iwaida3cec32008-08-08 17:12:14 +0200818 if (snd_BUG_ON(!temp))
819 return -EINVAL;
820 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (busp)
824 *busp = NULL;
825
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200826 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (bus == NULL) {
828 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
829 return -ENOMEM;
830 }
831
832 bus->card = card;
833 bus->private_data = temp->private_data;
834 bus->pci = temp->pci;
835 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100836 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 bus->ops = temp->ops;
838
Ingo Molnar62932df2006-01-16 16:34:20 +0100839 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200840 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 INIT_LIST_HEAD(&bus->codec_list);
842
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100843 snprintf(bus->workq_name, sizeof(bus->workq_name),
844 "hd-audio%d", card->number);
845 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100846 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100847 snd_printk(KERN_ERR "cannot create workqueue %s\n",
848 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100849 kfree(bus);
850 return -ENOMEM;
851 }
852
Takashi Iwai0ba21762007-04-16 11:29:14 +0200853 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
854 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 snd_hda_bus_free(bus);
856 return err;
857 }
858 if (busp)
859 *busp = bus;
860 return 0;
861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100862EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Takashi Iwai82467612007-07-27 19:15:54 +0200864#ifdef CONFIG_SND_HDA_GENERIC
865#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200866 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200867#else
868#define is_generic_config(codec) 0
869#endif
870
Takashi Iwai645f10c2008-11-28 15:07:37 +0100871#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100872#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
873#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100874#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100875#endif
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * find a matching codec preset
879 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200880static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200881find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100883 struct hda_codec_preset_list *tbl;
884 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200885 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Takashi Iwai82467612007-07-27 19:15:54 +0200887 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100888 return NULL; /* use the generic parser */
889
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100890 again:
891 mutex_lock(&preset_mutex);
892 list_for_each_entry(tbl, &hda_preset_tables, list) {
893 if (!try_module_get(tbl->owner)) {
894 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
895 continue;
896 }
897 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100899 if (preset->afg && preset->afg != codec->afg)
900 continue;
901 if (preset->mfg && preset->mfg != codec->mfg)
902 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200903 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200905 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100907 preset->rev == codec->revision_id)) {
908 mutex_unlock(&preset_mutex);
909 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100913 module_put(tbl->owner);
914 }
915 mutex_unlock(&preset_mutex);
916
917 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
918 char name[32];
919 if (!mod_requested)
920 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
921 codec->vendor_id);
922 else
923 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
924 (codec->vendor_id >> 16) & 0xffff);
925 request_module(name);
926 mod_requested++;
927 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 return NULL;
930}
931
932/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200933 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200935static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 const struct hda_vendor_id *c;
938 const char *vendor = NULL;
939 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200940 char tmp[16];
941
942 if (codec->vendor_name)
943 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 for (c = hda_vendor_ids; c->id; c++) {
946 if (c->id == vendor_id) {
947 vendor = c->name;
948 break;
949 }
950 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200951 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 sprintf(tmp, "Generic %04x", vendor_id);
953 vendor = tmp;
954 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200955 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
956 if (!codec->vendor_name)
957 return -ENOMEM;
958
959 get_chip_name:
960 if (codec->chip_name)
961 return 0;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200964 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
965 else {
966 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
967 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
968 }
969 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200970 return -ENOMEM;
971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200975 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100977static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200979 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 hda_nid_t nid;
981
982 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
983 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200984 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200985 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200986 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200987 case AC_GRP_AUDIO_FUNCTION:
988 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200989 codec->afg_function_id = function_id & 0xff;
990 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200991 break;
992 case AC_GRP_MODEM_FUNCTION:
993 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200994 codec->mfg_function_id = function_id & 0xff;
995 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200996 break;
997 default:
998 break;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001004 * read widget caps for each widget and store in cache
1005 */
1006static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1007{
1008 int i;
1009 hda_nid_t nid;
1010
1011 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1012 &codec->start_nid);
1013 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001014 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001015 return -ENOMEM;
1016 nid = codec->start_nid;
1017 for (i = 0; i < codec->num_nodes; i++, nid++)
1018 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1019 AC_PAR_AUDIO_WIDGET_CAP);
1020 return 0;
1021}
1022
Takashi Iwai3be14142009-02-20 14:11:16 +01001023/* read all pin default configurations and save codec->init_pins */
1024static int read_pin_defaults(struct hda_codec *codec)
1025{
1026 int i;
1027 hda_nid_t nid = codec->start_nid;
1028
1029 for (i = 0; i < codec->num_nodes; i++, nid++) {
1030 struct hda_pincfg *pin;
1031 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001032 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033 if (wid_type != AC_WID_PIN)
1034 continue;
1035 pin = snd_array_new(&codec->init_pins);
1036 if (!pin)
1037 return -ENOMEM;
1038 pin->nid = nid;
1039 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1040 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001041 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1042 AC_VERB_GET_PIN_WIDGET_CONTROL,
1043 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001044 }
1045 return 0;
1046}
1047
1048/* look up the given pin config list and return the item matching with NID */
1049static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1050 struct snd_array *array,
1051 hda_nid_t nid)
1052{
1053 int i;
1054 for (i = 0; i < array->used; i++) {
1055 struct hda_pincfg *pin = snd_array_elem(array, i);
1056 if (pin->nid == nid)
1057 return pin;
1058 }
1059 return NULL;
1060}
1061
Takashi Iwai3be14142009-02-20 14:11:16 +01001062/* set the current pin config value for the given NID.
1063 * the value is cached, and read via snd_hda_codec_get_pincfg()
1064 */
1065int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1066 hda_nid_t nid, unsigned int cfg)
1067{
1068 struct hda_pincfg *pin;
1069
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001070 /* the check below may be invalid when pins are added by a fixup
1071 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1072 * for now
1073 */
1074 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001075 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1076 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001077 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001078
Takashi Iwai3be14142009-02-20 14:11:16 +01001079 pin = look_up_pincfg(codec, list, nid);
1080 if (!pin) {
1081 pin = snd_array_new(list);
1082 if (!pin)
1083 return -ENOMEM;
1084 pin->nid = nid;
1085 }
1086 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001087 return 0;
1088}
1089
Takashi Iwaid5191e52009-11-16 14:58:17 +01001090/**
1091 * snd_hda_codec_set_pincfg - Override a pin default configuration
1092 * @codec: the HDA codec
1093 * @nid: NID to set the pin config
1094 * @cfg: the pin default config value
1095 *
1096 * Override a pin default configuration value in the cache.
1097 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1098 * priority than the real hardware value.
1099 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001100int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1101 hda_nid_t nid, unsigned int cfg)
1102{
Takashi Iwai346ff702009-02-23 09:42:57 +01001103 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001104}
1105EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1106
Takashi Iwaid5191e52009-11-16 14:58:17 +01001107/**
1108 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1109 * @codec: the HDA codec
1110 * @nid: NID to get the pin config
1111 *
1112 * Get the current pin config value of the given pin NID.
1113 * If the pincfg value is cached or overridden via sysfs or driver,
1114 * returns the cached value.
1115 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001116unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1117{
1118 struct hda_pincfg *pin;
1119
Takashi Iwai3be14142009-02-20 14:11:16 +01001120#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001121 {
1122 unsigned int cfg = 0;
1123 mutex_lock(&codec->user_mutex);
1124 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1125 if (pin)
1126 cfg = pin->cfg;
1127 mutex_unlock(&codec->user_mutex);
1128 if (cfg)
1129 return cfg;
1130 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001131#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001132 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1133 if (pin)
1134 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001135 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1136 if (pin)
1137 return pin->cfg;
1138 return 0;
1139}
1140EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1141
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001142/* remember the current pinctl target value */
1143int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1144 unsigned int val)
1145{
1146 struct hda_pincfg *pin;
1147
1148 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1149 if (!pin)
1150 return -EINVAL;
1151 pin->target = val;
1152 return 0;
1153}
1154EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1155
1156/* return the current pinctl target value */
1157int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1158{
1159 struct hda_pincfg *pin;
1160
1161 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1162 if (!pin)
1163 return 0;
1164 return pin->target;
1165}
1166EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1167
Takashi Iwai92ee6162009-12-27 11:18:59 +01001168/**
1169 * snd_hda_shutup_pins - Shut up all pins
1170 * @codec: the HDA codec
1171 *
1172 * Clear all pin controls to shup up before suspend for avoiding click noise.
1173 * The controls aren't cached so that they can be resumed properly.
1174 */
1175void snd_hda_shutup_pins(struct hda_codec *codec)
1176{
1177 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001178 /* don't shut up pins when unloading the driver; otherwise it breaks
1179 * the default pin setup at the next load of the driver
1180 */
1181 if (codec->bus->shutdown)
1182 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001183 for (i = 0; i < codec->init_pins.used; i++) {
1184 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1185 /* use read here for syncing after issuing each verb */
1186 snd_hda_codec_read(codec, pin->nid, 0,
1187 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1188 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001189 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001190}
1191EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1192
Takashi Iwai2a439522011-07-26 09:52:50 +02001193#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001194/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1195static void restore_shutup_pins(struct hda_codec *codec)
1196{
1197 int i;
1198 if (!codec->pins_shutup)
1199 return;
1200 if (codec->bus->shutdown)
1201 return;
1202 for (i = 0; i < codec->init_pins.used; i++) {
1203 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1204 snd_hda_codec_write(codec, pin->nid, 0,
1205 AC_VERB_SET_PIN_WIDGET_CONTROL,
1206 pin->ctrl);
1207 }
1208 codec->pins_shutup = 0;
1209}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001210#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001211
David Henningsson26a6cb62012-10-09 15:04:21 +02001212static void hda_jackpoll_work(struct work_struct *work)
1213{
1214 struct hda_codec *codec =
1215 container_of(work, struct hda_codec, jackpoll_work.work);
1216 if (!codec->jackpoll_interval)
1217 return;
1218
1219 snd_hda_jack_set_dirty_all(codec);
1220 snd_hda_jack_poll_all(codec);
1221 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1222 codec->jackpoll_interval);
1223}
1224
Takashi Iwai01751f52007-08-10 16:59:39 +02001225static void init_hda_cache(struct hda_cache_rec *cache,
1226 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001227static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001228
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001229/* release all pincfg lists */
1230static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001231{
Takashi Iwai346ff702009-02-23 09:42:57 +01001232 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001233#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001234 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001235#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001236 snd_array_free(&codec->init_pins);
1237}
1238
Takashi Iwai54d17402005-11-21 16:33:22 +01001239/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001240 * audio-converter setup caches
1241 */
1242struct hda_cvt_setup {
1243 hda_nid_t nid;
1244 u8 stream_tag;
1245 u8 channel_id;
1246 u16 format_id;
1247 unsigned char active; /* cvt is currently used */
1248 unsigned char dirty; /* setups should be cleared */
1249};
1250
1251/* get or create a cache entry for the given audio converter NID */
1252static struct hda_cvt_setup *
1253get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1254{
1255 struct hda_cvt_setup *p;
1256 int i;
1257
1258 for (i = 0; i < codec->cvt_setups.used; i++) {
1259 p = snd_array_elem(&codec->cvt_setups, i);
1260 if (p->nid == nid)
1261 return p;
1262 }
1263 p = snd_array_new(&codec->cvt_setups);
1264 if (p)
1265 p->nid = nid;
1266 return p;
1267}
1268
1269/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * codec destructor
1271 */
1272static void snd_hda_codec_free(struct hda_codec *codec)
1273{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001274 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001276 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001277 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001278 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001279#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001280 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001281 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001284 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001285 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001286 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001287 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001288 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 codec->bus->caddr_tbl[codec->addr] = NULL;
1290 if (codec->patch_ops.free)
1291 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001292#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001293 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001294 hda_call_pm_notify(codec->bus, false);
1295#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001296 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001297 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001298 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001299 kfree(codec->vendor_name);
1300 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001301 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001302 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 kfree(codec);
1304}
1305
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001306static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1307 hda_nid_t fg, unsigned int power_state);
1308
Takashi Iwaid8193872012-08-31 07:54:38 -07001309static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001310 unsigned int power_state);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/**
1313 * snd_hda_codec_new - create a HDA codec
1314 * @bus: the bus to assign
1315 * @codec_addr: the codec address
1316 * @codecp: the pointer to store the generated codec
1317 *
1318 * Returns 0 if successful, or a negative error code.
1319 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001320int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001321 unsigned int codec_addr,
1322 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001325 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001326 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int err;
1328
Takashi Iwaida3cec32008-08-08 17:12:14 +02001329 if (snd_BUG_ON(!bus))
1330 return -EINVAL;
1331 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1332 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001335 snd_printk(KERN_ERR "hda_codec: "
1336 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EBUSY;
1338 }
1339
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001340 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (codec == NULL) {
1342 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1343 return -ENOMEM;
1344 }
1345
1346 codec->bus = bus;
1347 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001348 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001349 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001350 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001351 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001352 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001353 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1354 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001355 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001356 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001357 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001358 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001359 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001360 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001361 INIT_LIST_HEAD(&codec->conn_list);
1362
David Henningsson26a6cb62012-10-09 15:04:21 +02001363 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Takashi Iwai83012a72012-08-24 18:38:08 +02001365#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001366 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001367 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1368 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1369 * the caller has to power down appropriatley after initialization
1370 * phase.
1371 */
1372 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001373 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001374#endif
1375
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001376 if (codec->bus->modelname) {
1377 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1378 if (!codec->modelname) {
1379 snd_hda_codec_free(codec);
1380 return -ENODEV;
1381 }
1382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 list_add_tail(&codec->list, &bus->codec_list);
1385 bus->caddr_tbl[codec_addr] = codec;
1386
Takashi Iwai0ba21762007-04-16 11:29:14 +02001387 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1388 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001389 if (codec->vendor_id == -1)
1390 /* read again, hopefully the access method was corrected
1391 * in the last read...
1392 */
1393 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1394 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1396 AC_PAR_SUBSYSTEM_ID);
1397 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1398 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001400 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001402 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001403 err = -ENODEV;
1404 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Takashi Iwaid8193872012-08-31 07:54:38 -07001407 fg = codec->afg ? codec->afg : codec->mfg;
1408 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001409 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001410 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001411 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001412 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001413 err = read_pin_defaults(codec);
1414 if (err < 0)
1415 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001416
Takashi Iwai0ba21762007-04-16 11:29:14 +02001417 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001419 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001420 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001421 }
1422
Takashi Iwai83012a72012-08-24 18:38:08 +02001423#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001424 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001425 AC_PWRST_CLKSTOP);
1426 if (!codec->d3_stop_clk)
1427 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001428#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001429 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001430 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001431
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001432 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001433 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001434
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001435 snd_hda_codec_proc_new(codec);
1436
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001437 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001438
1439 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1440 codec->subsystem_id, codec->revision_id);
1441 snd_component_add(codec->bus->card, component);
1442
1443 if (codecp)
1444 *codecp = codec;
1445 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001446
1447 error:
1448 snd_hda_codec_free(codec);
1449 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001451EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001452
Mengdong Lina15d05d2013-02-08 17:09:31 -05001453int snd_hda_codec_update_widgets(struct hda_codec *codec)
1454{
1455 hda_nid_t fg;
1456 int err;
1457
1458 /* Assume the function group node does not change,
1459 * only the widget nodes may change.
1460 */
1461 kfree(codec->wcaps);
1462 fg = codec->afg ? codec->afg : codec->mfg;
1463 err = read_widget_caps(codec, fg);
1464 if (err < 0) {
1465 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1466 return err;
1467 }
1468
1469 snd_array_free(&codec->init_pins);
1470 err = read_pin_defaults(codec);
1471
1472 return err;
1473}
1474EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1475
1476
Takashi Iwaid5191e52009-11-16 14:58:17 +01001477/**
1478 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1479 * @codec: the HDA codec
1480 *
1481 * Start parsing of the given codec tree and (re-)initialize the whole
1482 * patch instance.
1483 *
1484 * Returns 0 if successful or a negative error code.
1485 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001486int snd_hda_codec_configure(struct hda_codec *codec)
1487{
1488 int err;
1489
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001490 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001491 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001492 err = get_codec_name(codec);
1493 if (err < 0)
1494 return err;
1495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Takashi Iwai82467612007-07-27 19:15:54 +02001497 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001499 goto patched;
1500 }
Takashi Iwai82467612007-07-27 19:15:54 +02001501 if (codec->preset && codec->preset->patch) {
1502 err = codec->preset->patch(codec);
1503 goto patched;
1504 }
1505
1506 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001507 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001508 if (err < 0)
1509 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001510
1511 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001512 if (!err && codec->patch_ops.unsol_event)
1513 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001514 /* audio codec should override the mixer name */
1515 if (!err && (codec->afg || !*codec->bus->card->mixername))
1516 snprintf(codec->bus->card->mixername,
1517 sizeof(codec->bus->card->mixername),
1518 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001521EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Takashi Iwaied360812012-08-08 17:12:52 +02001523/* update the stream-id if changed */
1524static void update_pcm_stream_id(struct hda_codec *codec,
1525 struct hda_cvt_setup *p, hda_nid_t nid,
1526 u32 stream_tag, int channel_id)
1527{
1528 unsigned int oldval, newval;
1529
1530 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1531 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1532 newval = (stream_tag << 4) | channel_id;
1533 if (oldval != newval)
1534 snd_hda_codec_write(codec, nid, 0,
1535 AC_VERB_SET_CHANNEL_STREAMID,
1536 newval);
1537 p->stream_tag = stream_tag;
1538 p->channel_id = channel_id;
1539 }
1540}
1541
1542/* update the format-id if changed */
1543static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1544 hda_nid_t nid, int format)
1545{
1546 unsigned int oldval;
1547
1548 if (p->format_id != format) {
1549 oldval = snd_hda_codec_read(codec, nid, 0,
1550 AC_VERB_GET_STREAM_FORMAT, 0);
1551 if (oldval != format) {
1552 msleep(1);
1553 snd_hda_codec_write(codec, nid, 0,
1554 AC_VERB_SET_STREAM_FORMAT,
1555 format);
1556 }
1557 p->format_id = format;
1558 }
1559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561/**
1562 * snd_hda_codec_setup_stream - set up the codec for streaming
1563 * @codec: the CODEC to set up
1564 * @nid: the NID to set up
1565 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1566 * @channel_id: channel id to pass, zero based.
1567 * @format: stream format.
1568 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1570 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int channel_id, int format)
1572{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001573 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001574 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001575 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001576 int i;
1577
Takashi Iwai0ba21762007-04-16 11:29:14 +02001578 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001579 return;
1580
Takashi Iwai0ba21762007-04-16 11:29:14 +02001581 snd_printdd("hda_codec_setup_stream: "
1582 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001584 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001585 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001586 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001587
1588 if (codec->pcm_format_first)
1589 update_pcm_format(codec, p, nid, format);
1590 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1591 if (!codec->pcm_format_first)
1592 update_pcm_format(codec, p, nid, format);
1593
Takashi Iwaieb541332010-08-06 13:48:11 +02001594 p->active = 1;
1595 p->dirty = 0;
1596
1597 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001598 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001599 list_for_each_entry(c, &codec->bus->codec_list, list) {
1600 for (i = 0; i < c->cvt_setups.used; i++) {
1601 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001602 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001603 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001604 p->dirty = 1;
1605 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001608EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwaif0cea792010-08-13 11:56:53 +02001610static void really_cleanup_stream(struct hda_codec *codec,
1611 struct hda_cvt_setup *q);
1612
Takashi Iwaid5191e52009-11-16 14:58:17 +01001613/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001614 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001615 * @codec: the CODEC to clean up
1616 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001617 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001618 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001619void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1620 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001621{
Takashi Iwaieb541332010-08-06 13:48:11 +02001622 struct hda_cvt_setup *p;
1623
Takashi Iwai888afa12008-03-18 09:57:50 +01001624 if (!nid)
1625 return;
1626
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001627 if (codec->no_sticky_stream)
1628 do_now = 1;
1629
Takashi Iwai888afa12008-03-18 09:57:50 +01001630 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001631 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001632 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001633 /* here we just clear the active flag when do_now isn't set;
1634 * actual clean-ups will be done later in
1635 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1636 */
1637 if (do_now)
1638 really_cleanup_stream(codec, p);
1639 else
1640 p->active = 0;
1641 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001642}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001643EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001644
Takashi Iwaieb541332010-08-06 13:48:11 +02001645static void really_cleanup_stream(struct hda_codec *codec,
1646 struct hda_cvt_setup *q)
1647{
1648 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001649 if (q->stream_tag || q->channel_id)
1650 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1651 if (q->format_id)
1652 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1653);
Takashi Iwaieb541332010-08-06 13:48:11 +02001654 memset(q, 0, sizeof(*q));
1655 q->nid = nid;
1656}
1657
1658/* clean up the all conflicting obsolete streams */
1659static void purify_inactive_streams(struct hda_codec *codec)
1660{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001661 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001662 int i;
1663
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001664 list_for_each_entry(c, &codec->bus->codec_list, list) {
1665 for (i = 0; i < c->cvt_setups.used; i++) {
1666 struct hda_cvt_setup *p;
1667 p = snd_array_elem(&c->cvt_setups, i);
1668 if (p->dirty)
1669 really_cleanup_stream(c, p);
1670 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001671 }
1672}
1673
Takashi Iwai2a439522011-07-26 09:52:50 +02001674#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001675/* clean up all streams; called from suspend */
1676static void hda_cleanup_all_streams(struct hda_codec *codec)
1677{
1678 int i;
1679
1680 for (i = 0; i < codec->cvt_setups.used; i++) {
1681 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1682 if (p->stream_tag)
1683 really_cleanup_stream(codec, p);
1684 }
1685}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001686#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/*
1689 * amp access functions
1690 */
1691
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001692/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001693#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001694#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001695#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1696#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001698#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001701static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001702 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai01751f52007-08-10 16:59:39 +02001704 memset(cache, 0, sizeof(*cache));
1705 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001706 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001707}
1708
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001709static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001710{
Takashi Iwai603c4012008-07-30 15:01:44 +02001711 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001715static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Takashi Iwai01751f52007-08-10 16:59:39 +02001717 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1718 u16 cur = cache->hash[idx];
1719 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001722 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (info->key == key)
1724 return info;
1725 cur = info->next;
1726 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001727 return NULL;
1728}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001730/* query the hash. allocate an entry if not found. */
1731static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1732 u32 key)
1733{
1734 struct hda_cache_head *info = get_hash(cache, key);
1735 if (!info) {
1736 u16 idx, cur;
1737 /* add a new hash entry */
1738 info = snd_array_new(&cache->buf);
1739 if (!info)
1740 return NULL;
1741 cur = snd_array_index(&cache->buf, info);
1742 info->key = key;
1743 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001744 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001745 idx = key % (u16)ARRAY_SIZE(cache->hash);
1746 info->next = cache->hash[idx];
1747 cache->hash[idx] = cur;
1748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return info;
1750}
1751
Takashi Iwai01751f52007-08-10 16:59:39 +02001752/* query and allocate an amp hash entry */
1753static inline struct hda_amp_info *
1754get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1755{
1756 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1757}
1758
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001759/* overwrite the value with the key in the caps hash */
1760static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1761{
1762 struct hda_amp_info *info;
1763
1764 mutex_lock(&codec->hash_mutex);
1765 info = get_alloc_amp_hash(codec, key);
1766 if (!info) {
1767 mutex_unlock(&codec->hash_mutex);
1768 return -EINVAL;
1769 }
1770 info->amp_caps = val;
1771 info->head.val |= INFO_AMP_CAPS;
1772 mutex_unlock(&codec->hash_mutex);
1773 return 0;
1774}
1775
1776/* query the value from the caps hash; if not found, fetch the current
1777 * value from the given function and store in the hash
1778 */
1779static unsigned int
1780query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1781 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1782{
1783 struct hda_amp_info *info;
1784 unsigned int val;
1785
1786 mutex_lock(&codec->hash_mutex);
1787 info = get_alloc_amp_hash(codec, key);
1788 if (!info) {
1789 mutex_unlock(&codec->hash_mutex);
1790 return 0;
1791 }
1792 if (!(info->head.val & INFO_AMP_CAPS)) {
1793 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1794 val = func(codec, nid, dir);
1795 write_caps_hash(codec, key, val);
1796 } else {
1797 val = info->amp_caps;
1798 mutex_unlock(&codec->hash_mutex);
1799 }
1800 return val;
1801}
1802
1803static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1804 int direction)
1805{
1806 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1807 nid = codec->afg;
1808 return snd_hda_param_read(codec, nid,
1809 direction == HDA_OUTPUT ?
1810 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1811}
1812
Takashi Iwaid5191e52009-11-16 14:58:17 +01001813/**
1814 * query_amp_caps - query AMP capabilities
1815 * @codec: the HD-auio codec
1816 * @nid: the NID to query
1817 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1818 *
1819 * Query AMP capabilities for the given widget and direction.
1820 * Returns the obtained capability bits.
1821 *
1822 * When cap bits have been already read, this doesn't read again but
1823 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001825u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001827 return query_caps_hash(codec, nid, direction,
1828 HDA_HASH_KEY(nid, direction, 0),
1829 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001831EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Takashi Iwaid5191e52009-11-16 14:58:17 +01001833/**
1834 * snd_hda_override_amp_caps - Override the AMP capabilities
1835 * @codec: the CODEC to clean up
1836 * @nid: the NID to clean up
1837 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1838 * @caps: the capability bits to set
1839 *
1840 * Override the cached AMP caps bits value by the given one.
1841 * This function is useful if the driver needs to adjust the AMP ranges,
1842 * e.g. limit to 0dB, etc.
1843 *
1844 * Returns zero if successful or a negative error code.
1845 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001846int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1847 unsigned int caps)
1848{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001849 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001850}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001851EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001852
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001853static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1854 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001855{
1856 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1857}
1858
Takashi Iwaid5191e52009-11-16 14:58:17 +01001859/**
1860 * snd_hda_query_pin_caps - Query PIN capabilities
1861 * @codec: the HD-auio codec
1862 * @nid: the NID to query
1863 *
1864 * Query PIN capabilities for the given widget.
1865 * Returns the obtained capability bits.
1866 *
1867 * When cap bits have been already read, this doesn't read again but
1868 * returns the cached value.
1869 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001870u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1871{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001872 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001873 read_pin_cap);
1874}
Takashi Iwai1327a322009-03-23 13:07:47 +01001875EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1876
Wu Fengguang864f92b2009-11-18 12:38:02 +08001877/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001878 * snd_hda_override_pin_caps - Override the pin capabilities
1879 * @codec: the CODEC
1880 * @nid: the NID to override
1881 * @caps: the capability bits to set
1882 *
1883 * Override the cached PIN capabilitiy bits value by the given one.
1884 *
1885 * Returns zero if successful or a negative error code.
1886 */
1887int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1888 unsigned int caps)
1889{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001890 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001891}
1892EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1893
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001894/* read or sync the hash value with the current value;
1895 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001897static struct hda_amp_info *
1898update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001899 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001901 struct hda_amp_info *info;
1902 unsigned int parm, val = 0;
1903 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001905 retry:
1906 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1907 if (!info)
1908 return NULL;
1909 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1910 if (!val_read) {
1911 mutex_unlock(&codec->hash_mutex);
1912 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1913 parm |= direction == HDA_OUTPUT ?
1914 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1915 parm |= index;
1916 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001917 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001918 val &= 0xff;
1919 val_read = true;
1920 mutex_lock(&codec->hash_mutex);
1921 goto retry;
1922 }
1923 info->vol[ch] = val;
1924 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001925 } else if (init_only)
1926 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001927 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001931 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001933static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001934 hda_nid_t nid, int ch, int direction, int index,
1935 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 u32 parm;
1938
1939 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1940 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1941 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001942 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1943 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001944 ; /* set the zero value as a fake mute */
1945 else
1946 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1948}
1949
Takashi Iwaid5191e52009-11-16 14:58:17 +01001950/**
1951 * snd_hda_codec_amp_read - Read AMP value
1952 * @codec: HD-audio codec
1953 * @nid: NID to read the AMP value
1954 * @ch: channel (left=0 or right=1)
1955 * @direction: #HDA_INPUT or #HDA_OUTPUT
1956 * @index: the index value (only for input direction)
1957 *
1958 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Takashi Iwai834be882006-03-01 14:16:17 +01001960int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1961 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001963 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001964 unsigned int val = 0;
1965
1966 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001967 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001968 if (info)
1969 val = info->vol[ch];
1970 mutex_unlock(&codec->hash_mutex);
1971 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001973EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Takashi Iwai280e57d2012-12-14 10:32:21 +01001975static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1976 int direction, int idx, int mask, int val,
1977 bool init_only)
1978{
1979 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001980 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001981 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001982
1983 if (snd_BUG_ON(mask & ~0xff))
1984 mask &= 0xff;
1985 val &= mask;
1986
1987 mutex_lock(&codec->hash_mutex);
1988 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1989 if (!info) {
1990 mutex_unlock(&codec->hash_mutex);
1991 return 0;
1992 }
1993 val |= info->vol[ch] & ~mask;
1994 if (info->vol[ch] == val) {
1995 mutex_unlock(&codec->hash_mutex);
1996 return 0;
1997 }
1998 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001999 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002000 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002001 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002002 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002003 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002004 return 1;
2005}
2006
Takashi Iwaid5191e52009-11-16 14:58:17 +01002007/**
2008 * snd_hda_codec_amp_update - update the AMP value
2009 * @codec: HD-audio codec
2010 * @nid: NID to read the AMP value
2011 * @ch: channel (left=0 or right=1)
2012 * @direction: #HDA_INPUT or #HDA_OUTPUT
2013 * @idx: the index value (only for input direction)
2014 * @mask: bit mask to set
2015 * @val: the bits value to set
2016 *
2017 * Update the AMP value with a bit mask.
2018 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002019 */
Takashi Iwai834be882006-03-01 14:16:17 +01002020int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2021 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002023 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002025EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Takashi Iwaid5191e52009-11-16 14:58:17 +01002027/**
2028 * snd_hda_codec_amp_stereo - update the AMP stereo values
2029 * @codec: HD-audio codec
2030 * @nid: NID to read the AMP value
2031 * @direction: #HDA_INPUT or #HDA_OUTPUT
2032 * @idx: the index value (only for input direction)
2033 * @mask: bit mask to set
2034 * @val: the bits value to set
2035 *
2036 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2037 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002038 */
2039int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2040 int direction, int idx, int mask, int val)
2041{
2042 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002043
2044 if (snd_BUG_ON(mask & ~0xff))
2045 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002046 for (ch = 0; ch < 2; ch++)
2047 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2048 idx, mask, val);
2049 return ret;
2050}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002051EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002052
Takashi Iwai280e57d2012-12-14 10:32:21 +01002053/* Works like snd_hda_codec_amp_update() but it writes the value only at
2054 * the first access. If the amp was already initialized / updated beforehand,
2055 * this does nothing.
2056 */
2057int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2058 int dir, int idx, int mask, int val)
2059{
2060 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2061}
2062EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2063
2064int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2065 int dir, int idx, int mask, int val)
2066{
2067 int ch, ret = 0;
2068
2069 if (snd_BUG_ON(mask & ~0xff))
2070 mask &= 0xff;
2071 for (ch = 0; ch < 2; ch++)
2072 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2073 idx, mask, val);
2074 return ret;
2075}
2076EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2077
Takashi Iwaid5191e52009-11-16 14:58:17 +01002078/**
2079 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2080 * @codec: HD-audio codec
2081 *
2082 * Resume the all amp commands from the cache.
2083 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002084void snd_hda_codec_resume_amp(struct hda_codec *codec)
2085{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002086 int i;
2087
Takashi Iwaic370dd62012-12-13 18:30:04 +01002088 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002089 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002090 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2091 struct hda_amp_info *buffer;
2092 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002093 hda_nid_t nid;
2094 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002095 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002096
2097 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002098 if (!buffer->head.dirty)
2099 continue;
2100 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002101 info = *buffer;
2102 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002103 if (!key)
2104 continue;
2105 nid = key & 0xff;
2106 idx = (key >> 16) & 0xff;
2107 dir = (key >> 24) & 0xff;
2108 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002109 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002110 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002111 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002112 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2113 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002114 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002115 }
2116 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002117 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002119EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002121static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2122 unsigned int ofs)
2123{
2124 u32 caps = query_amp_caps(codec, nid, dir);
2125 /* get num steps */
2126 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2127 if (ofs < caps)
2128 caps -= ofs;
2129 return caps;
2130}
2131
Takashi Iwaid5191e52009-11-16 14:58:17 +01002132/**
2133 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2134 *
2135 * The control element is supposed to have the private_value field
2136 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2137 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002138int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2142 u16 nid = get_amp_nid(kcontrol);
2143 u8 chs = get_amp_channels(kcontrol);
2144 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002145 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002147 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2148 uinfo->count = chs == 3 ? 2 : 1;
2149 uinfo->value.integer.min = 0;
2150 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2151 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002152 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002153 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2154 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return -EINVAL;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002159EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002161
2162static inline unsigned int
2163read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2164 int ch, int dir, int idx, unsigned int ofs)
2165{
2166 unsigned int val;
2167 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2168 val &= HDA_AMP_VOLMASK;
2169 if (val >= ofs)
2170 val -= ofs;
2171 else
2172 val = 0;
2173 return val;
2174}
2175
2176static inline int
2177update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2178 int ch, int dir, int idx, unsigned int ofs,
2179 unsigned int val)
2180{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002181 unsigned int maxval;
2182
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002183 if (val > 0)
2184 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002185 /* ofs = 0: raw max value */
2186 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002187 if (val > maxval)
2188 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002189 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2190 HDA_AMP_VOLMASK, val);
2191}
2192
Takashi Iwaid5191e52009-11-16 14:58:17 +01002193/**
2194 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2195 *
2196 * The control element is supposed to have the private_value field
2197 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2198 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002199int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 hda_nid_t nid = get_amp_nid(kcontrol);
2204 int chs = get_amp_channels(kcontrol);
2205 int dir = get_amp_direction(kcontrol);
2206 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002207 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 long *valp = ucontrol->value.integer.value;
2209
2210 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002211 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002213 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return 0;
2215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002216EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
2219 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2220 *
2221 * The control element is supposed to have the private_value field
2222 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2223 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002224int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2228 hda_nid_t nid = get_amp_nid(kcontrol);
2229 int chs = get_amp_channels(kcontrol);
2230 int dir = get_amp_direction(kcontrol);
2231 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002232 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 long *valp = ucontrol->value.integer.value;
2234 int change = 0;
2235
Takashi Iwaicb53c622007-08-10 17:21:45 +02002236 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002237 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002238 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002239 valp++;
2240 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002241 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002242 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002243 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return change;
2245}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002246EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Takashi Iwaid5191e52009-11-16 14:58:17 +01002248/**
2249 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2250 *
2251 * The control element is supposed to have the private_value field
2252 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2253 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002254int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2255 unsigned int size, unsigned int __user *_tlv)
2256{
2257 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2258 hda_nid_t nid = get_amp_nid(kcontrol);
2259 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002260 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002261 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002262 u32 caps, val1, val2;
2263
2264 if (size < 4 * sizeof(unsigned int))
2265 return -ENOMEM;
2266 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002267 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2268 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002269 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002270 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002271 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002272 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002273 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002274 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2275 return -EFAULT;
2276 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2277 return -EFAULT;
2278 if (put_user(val1, _tlv + 2))
2279 return -EFAULT;
2280 if (put_user(val2, _tlv + 3))
2281 return -EFAULT;
2282 return 0;
2283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002284EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002285
Takashi Iwaid5191e52009-11-16 14:58:17 +01002286/**
2287 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2288 * @codec: HD-audio codec
2289 * @nid: NID of a reference widget
2290 * @dir: #HDA_INPUT or #HDA_OUTPUT
2291 * @tlv: TLV data to be stored, at least 4 elements
2292 *
2293 * Set (static) TLV data for a virtual master volume using the AMP caps
2294 * obtained from the reference NID.
2295 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002296 */
2297void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2298 unsigned int *tlv)
2299{
2300 u32 caps;
2301 int nums, step;
2302
2303 caps = query_amp_caps(codec, nid, dir);
2304 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2305 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2306 step = (step + 1) * 25;
2307 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2308 tlv[1] = 2 * sizeof(unsigned int);
2309 tlv[2] = -nums * step;
2310 tlv[3] = step;
2311}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002312EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002313
2314/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002315static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002316find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002317{
2318 struct snd_ctl_elem_id id;
2319 memset(&id, 0, sizeof(id));
2320 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002321 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002322 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002323 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2324 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002325 strcpy(id.name, name);
2326 return snd_ctl_find_id(codec->bus->card, &id);
2327}
2328
Takashi Iwaid5191e52009-11-16 14:58:17 +01002329/**
2330 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2331 * @codec: HD-audio codec
2332 * @name: ctl id name string
2333 *
2334 * Get the control element with the given id string and IFACE_MIXER.
2335 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002336struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2337 const char *name)
2338{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002339 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002341EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002342
Takashi Iwaidcda5802012-10-12 17:24:51 +02002343static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002344 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002345{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002346 int i, idx;
2347 /* 16 ctlrs should be large enough */
2348 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2349 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002350 return idx;
2351 }
2352 return -EBUSY;
2353}
2354
Takashi Iwaid5191e52009-11-16 14:58:17 +01002355/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002356 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002357 * @codec: HD-audio codec
2358 * @nid: corresponding NID (optional)
2359 * @kctl: the control element to assign
2360 *
2361 * Add the given control element to an array inside the codec instance.
2362 * All control elements belonging to a codec are supposed to be added
2363 * by this function so that a proper clean-up works at the free or
2364 * reconfiguration time.
2365 *
2366 * If non-zero @nid is passed, the NID is assigned to the control element.
2367 * The assignment is shown in the codec proc file.
2368 *
2369 * snd_hda_ctl_add() checks the control subdev id field whether
2370 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002371 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2372 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002373 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002374int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2375 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002376{
2377 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002378 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002379 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002380
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002381 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002382 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002383 if (nid == 0)
2384 nid = get_amp_nid_(kctl->private_value);
2385 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002386 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2387 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002388 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002389 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002390 err = snd_ctl_add(codec->bus->card, kctl);
2391 if (err < 0)
2392 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002393 item = snd_array_new(&codec->mixers);
2394 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002395 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002396 item->kctl = kctl;
2397 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002398 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002399 return 0;
2400}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002401EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002402
Takashi Iwaid5191e52009-11-16 14:58:17 +01002403/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002404 * snd_hda_add_nid - Assign a NID to a control element
2405 * @codec: HD-audio codec
2406 * @nid: corresponding NID (optional)
2407 * @kctl: the control element to assign
2408 * @index: index to kctl
2409 *
2410 * Add the given control element to an array inside the codec instance.
2411 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2412 * NID:KCTL mapping - for example "Capture Source" selector.
2413 */
2414int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2415 unsigned int index, hda_nid_t nid)
2416{
2417 struct hda_nid_item *item;
2418
2419 if (nid > 0) {
2420 item = snd_array_new(&codec->nids);
2421 if (!item)
2422 return -ENOMEM;
2423 item->kctl = kctl;
2424 item->index = index;
2425 item->nid = nid;
2426 return 0;
2427 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002428 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2429 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002430 return -EINVAL;
2431}
2432EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2433
2434/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002435 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2436 * @codec: HD-audio codec
2437 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002438void snd_hda_ctls_clear(struct hda_codec *codec)
2439{
2440 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002441 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002442 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002443 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002444 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002445 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002446}
2447
Takashi Iwaia65d6292009-02-23 16:57:04 +01002448/* pseudo device locking
2449 * toggle card->shutdown to allow/disallow the device access (as a hack)
2450 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002451int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002452{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002453 struct snd_card *card = bus->card;
2454 struct hda_codec *codec;
2455
Takashi Iwaia65d6292009-02-23 16:57:04 +01002456 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002457 if (card->shutdown)
2458 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002459 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002460 if (!list_empty(&card->ctl_files))
2461 goto err_clear;
2462
2463 list_for_each_entry(codec, &bus->codec_list, list) {
2464 int pcm;
2465 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2466 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2467 if (!cpcm->pcm)
2468 continue;
2469 if (cpcm->pcm->streams[0].substream_opened ||
2470 cpcm->pcm->streams[1].substream_opened)
2471 goto err_clear;
2472 }
2473 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002474 spin_unlock(&card->files_lock);
2475 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002476
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002477 err_clear:
2478 card->shutdown = 0;
2479 err_unlock:
2480 spin_unlock(&card->files_lock);
2481 return -EINVAL;
2482}
2483EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2484
2485void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002486{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002487 struct snd_card *card = bus->card;
2488
2489 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002490 spin_lock(&card->files_lock);
2491 card->shutdown = 0;
2492 spin_unlock(&card->files_lock);
2493}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002494EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002495
Takashi Iwaid5191e52009-11-16 14:58:17 +01002496/**
2497 * snd_hda_codec_reset - Clear all objects assigned to the codec
2498 * @codec: HD-audio codec
2499 *
2500 * This frees the all PCM and control elements assigned to the codec, and
2501 * clears the caches and restores the pin default configurations.
2502 *
2503 * When a device is being used, it returns -EBSY. If successfully freed,
2504 * returns zero.
2505 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002506int snd_hda_codec_reset(struct hda_codec *codec)
2507{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002508 struct hda_bus *bus = codec->bus;
2509 struct snd_card *card = bus->card;
2510 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002511
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002512 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002513 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002514
2515 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002516 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002517#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002518 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002519 codec->power_on = 0;
2520 codec->power_transition = 0;
2521 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002522 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002523#endif
2524 snd_hda_ctls_clear(codec);
2525 /* relase PCMs */
2526 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002527 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002528 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002529 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002530 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002531 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002532 }
2533 if (codec->patch_ops.free)
2534 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002535 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002536 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002537 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002538 codec->spec = NULL;
2539 free_hda_cache(&codec->amp_cache);
2540 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002541 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2542 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002543 /* free only driver_pins so that init_pins + user_pins are restored */
2544 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002545 snd_array_free(&codec->cvt_setups);
2546 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002547 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002548 codec->num_pcms = 0;
2549 codec->pcm_info = NULL;
2550 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002551 codec->slave_dig_outs = NULL;
2552 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002553 module_put(codec->owner);
2554 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002555
2556 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002557 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002558 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002559}
2560
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002561typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2562
2563/* apply the function to all matching slave ctls in the mixer list */
2564static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002565 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002566{
2567 struct hda_nid_item *items;
2568 const char * const *s;
2569 int i, err;
2570
2571 items = codec->mixers.list;
2572 for (i = 0; i < codec->mixers.used; i++) {
2573 struct snd_kcontrol *sctl = items[i].kctl;
2574 if (!sctl || !sctl->id.name ||
2575 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2576 continue;
2577 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002578 char tmpname[sizeof(sctl->id.name)];
2579 const char *name = *s;
2580 if (suffix) {
2581 snprintf(tmpname, sizeof(tmpname), "%s %s",
2582 name, suffix);
2583 name = tmpname;
2584 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002585 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002586 err = func(data, sctl);
2587 if (err)
2588 return err;
2589 break;
2590 }
2591 }
2592 }
2593 return 0;
2594}
2595
2596static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2597{
2598 return 1;
2599}
2600
Takashi Iwai18478e82012-03-09 17:51:10 +01002601/* guess the value corresponding to 0dB */
2602static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2603{
2604 int _tlv[4];
2605 const int *tlv = NULL;
2606 int val = -1;
2607
2608 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2609 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2610 mm_segment_t fs = get_fs();
2611 set_fs(get_ds());
2612 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2613 tlv = _tlv;
2614 set_fs(fs);
2615 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2616 tlv = kctl->tlv.p;
2617 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2618 val = -tlv[2] / tlv[3];
2619 return val;
2620}
2621
2622/* call kctl->put with the given value(s) */
2623static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2624{
2625 struct snd_ctl_elem_value *ucontrol;
2626 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2627 if (!ucontrol)
2628 return -ENOMEM;
2629 ucontrol->value.integer.value[0] = val;
2630 ucontrol->value.integer.value[1] = val;
2631 kctl->put(kctl, ucontrol);
2632 kfree(ucontrol);
2633 return 0;
2634}
2635
2636/* initialize the slave volume with 0dB */
2637static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2638{
2639 int offset = get_kctl_0dB_offset(slave);
2640 if (offset > 0)
2641 put_kctl_with_value(slave, offset);
2642 return 0;
2643}
2644
2645/* unmute the slave */
2646static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2647{
2648 return put_kctl_with_value(slave, 1);
2649}
2650
Takashi Iwaid5191e52009-11-16 14:58:17 +01002651/**
2652 * snd_hda_add_vmaster - create a virtual master control and add slaves
2653 * @codec: HD-audio codec
2654 * @name: vmaster control name
2655 * @tlv: TLV data (optional)
2656 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002657 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002658 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002659 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002660 *
2661 * Create a virtual master control with the given name. The TLV data
2662 * must be either NULL or a valid data.
2663 *
2664 * @slaves is a NULL-terminated array of strings, each of which is a
2665 * slave control name. All controls with these names are assigned to
2666 * the new virtual master control.
2667 *
2668 * This function returns zero if successful or a negative error code.
2669 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002670int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002671 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002672 const char *suffix, bool init_slave_vol,
2673 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002674{
2675 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002676 int err;
2677
Takashi Iwai29e58532012-03-12 12:25:03 +01002678 if (ctl_ret)
2679 *ctl_ret = NULL;
2680
Takashi Iwai9322ca52012-02-03 14:28:01 +01002681 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002682 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002683 snd_printdd("No slave found for %s\n", name);
2684 return 0;
2685 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002686 kctl = snd_ctl_make_virtual_master(name, tlv);
2687 if (!kctl)
2688 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002689 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002690 if (err < 0)
2691 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002692
Takashi Iwai9322ca52012-02-03 14:28:01 +01002693 err = map_slaves(codec, slaves, suffix,
2694 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002695 if (err < 0)
2696 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002697
2698 /* init with master mute & zero volume */
2699 put_kctl_with_value(kctl, 0);
2700 if (init_slave_vol)
2701 map_slaves(codec, slaves, suffix,
2702 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2703
Takashi Iwai29e58532012-03-12 12:25:03 +01002704 if (ctl_ret)
2705 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002706 return 0;
2707}
Takashi Iwai18478e82012-03-09 17:51:10 +01002708EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002709
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002710/*
2711 * mute-LED control using vmaster
2712 */
2713static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_info *uinfo)
2715{
2716 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002717 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002718 };
2719 unsigned int index;
2720
2721 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2722 uinfo->count = 1;
2723 uinfo->value.enumerated.items = 3;
2724 index = uinfo->value.enumerated.item;
2725 if (index >= 3)
2726 index = 2;
2727 strcpy(uinfo->value.enumerated.name, texts[index]);
2728 return 0;
2729}
2730
2731static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2733{
2734 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2735 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2736 return 0;
2737}
2738
2739static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2741{
2742 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2743 unsigned int old_mode = hook->mute_mode;
2744
2745 hook->mute_mode = ucontrol->value.enumerated.item[0];
2746 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2747 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2748 if (old_mode == hook->mute_mode)
2749 return 0;
2750 snd_hda_sync_vmaster_hook(hook);
2751 return 1;
2752}
2753
2754static struct snd_kcontrol_new vmaster_mute_mode = {
2755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2756 .name = "Mute-LED Mode",
2757 .info = vmaster_mute_mode_info,
2758 .get = vmaster_mute_mode_get,
2759 .put = vmaster_mute_mode_put,
2760};
2761
2762/*
2763 * Add a mute-LED hook with the given vmaster switch kctl
2764 * "Mute-LED Mode" control is automatically created and associated with
2765 * the given hook.
2766 */
2767int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002768 struct hda_vmaster_mute_hook *hook,
2769 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002770{
2771 struct snd_kcontrol *kctl;
2772
2773 if (!hook->hook || !hook->sw_kctl)
2774 return 0;
2775 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2776 hook->codec = codec;
2777 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002778 if (!expose_enum_ctl)
2779 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002780 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2781 if (!kctl)
2782 return -ENOMEM;
2783 return snd_hda_ctl_add(codec, 0, kctl);
2784}
2785EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2786
2787/*
2788 * Call the hook with the current value for synchronization
2789 * Should be called in init callback
2790 */
2791void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2792{
2793 if (!hook->hook || !hook->codec)
2794 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002795 /* don't call vmaster hook in the destructor since it might have
2796 * been already destroyed
2797 */
2798 if (hook->codec->bus->shutdown)
2799 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002800 switch (hook->mute_mode) {
2801 case HDA_VMUTE_FOLLOW_MASTER:
2802 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2803 break;
2804 default:
2805 hook->hook(hook->codec, hook->mute_mode);
2806 break;
2807 }
2808}
2809EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2810
2811
Takashi Iwaid5191e52009-11-16 14:58:17 +01002812/**
2813 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2814 *
2815 * The control element is supposed to have the private_value field
2816 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2817 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002818int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 int chs = get_amp_channels(kcontrol);
2822
2823 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2824 uinfo->count = chs == 3 ? 2 : 1;
2825 uinfo->value.integer.min = 0;
2826 uinfo->value.integer.max = 1;
2827 return 0;
2828}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002829EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Takashi Iwaid5191e52009-11-16 14:58:17 +01002831/**
2832 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2833 *
2834 * The control element is supposed to have the private_value field
2835 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2836 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002837int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2841 hda_nid_t nid = get_amp_nid(kcontrol);
2842 int chs = get_amp_channels(kcontrol);
2843 int dir = get_amp_direction(kcontrol);
2844 int idx = get_amp_index(kcontrol);
2845 long *valp = ucontrol->value.integer.value;
2846
2847 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002848 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002849 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002852 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
2854}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002855EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Takashi Iwaid5191e52009-11-16 14:58:17 +01002857/**
2858 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2859 *
2860 * The control element is supposed to have the private_value field
2861 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2862 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002863int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2864 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2867 hda_nid_t nid = get_amp_nid(kcontrol);
2868 int chs = get_amp_channels(kcontrol);
2869 int dir = get_amp_direction(kcontrol);
2870 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 long *valp = ucontrol->value.integer.value;
2872 int change = 0;
2873
Takashi Iwaicb53c622007-08-10 17:21:45 +02002874 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002875 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002876 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002877 HDA_AMP_MUTE,
2878 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002879 valp++;
2880 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002881 if (chs & 2)
2882 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002883 HDA_AMP_MUTE,
2884 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002885 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002886 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return change;
2888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002889EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891/*
Takashi Iwai985be542005-11-02 18:26:49 +01002892 * bound volume controls
2893 *
2894 * bind multiple volumes (# indices, from 0)
2895 */
2896
2897#define AMP_VAL_IDX_SHIFT 19
2898#define AMP_VAL_IDX_MASK (0x0f<<19)
2899
Takashi Iwaid5191e52009-11-16 14:58:17 +01002900/**
2901 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2902 *
2903 * The control element is supposed to have the private_value field
2904 * set up via HDA_BIND_MUTE*() macros.
2905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002906int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002908{
2909 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2910 unsigned long pval;
2911 int err;
2912
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002913 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002914 pval = kcontrol->private_value;
2915 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2916 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2917 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002918 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002919 return err;
2920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002921EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002922
Takashi Iwaid5191e52009-11-16 14:58:17 +01002923/**
2924 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2925 *
2926 * The control element is supposed to have the private_value field
2927 * set up via HDA_BIND_MUTE*() macros.
2928 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002929int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002931{
2932 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2933 unsigned long pval;
2934 int i, indices, err = 0, change = 0;
2935
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002936 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002937 pval = kcontrol->private_value;
2938 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2939 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002940 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2941 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002942 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2943 if (err < 0)
2944 break;
2945 change |= err;
2946 }
2947 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002948 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002949 return err < 0 ? err : change;
2950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002951EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002952
Takashi Iwaid5191e52009-11-16 14:58:17 +01002953/**
2954 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2955 *
2956 * The control element is supposed to have the private_value field
2957 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002958 */
2959int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_info *uinfo)
2961{
2962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2963 struct hda_bind_ctls *c;
2964 int err;
2965
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002966 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002967 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002968 kcontrol->private_value = *c->values;
2969 err = c->ops->info(kcontrol, uinfo);
2970 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002971 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002972 return err;
2973}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002974EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002975
Takashi Iwaid5191e52009-11-16 14:58:17 +01002976/**
2977 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2978 *
2979 * The control element is supposed to have the private_value field
2980 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2981 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002982int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2984{
2985 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2986 struct hda_bind_ctls *c;
2987 int err;
2988
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002989 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002990 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002991 kcontrol->private_value = *c->values;
2992 err = c->ops->get(kcontrol, ucontrol);
2993 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002994 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002995 return err;
2996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002997EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002998
Takashi Iwaid5191e52009-11-16 14:58:17 +01002999/**
3000 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3001 *
3002 * The control element is supposed to have the private_value field
3003 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3004 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003005int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
3007{
3008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3009 struct hda_bind_ctls *c;
3010 unsigned long *vals;
3011 int err = 0, change = 0;
3012
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003013 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003014 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003015 for (vals = c->values; *vals; vals++) {
3016 kcontrol->private_value = *vals;
3017 err = c->ops->put(kcontrol, ucontrol);
3018 if (err < 0)
3019 break;
3020 change |= err;
3021 }
3022 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003023 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003024 return err < 0 ? err : change;
3025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003026EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003027
Takashi Iwaid5191e52009-11-16 14:58:17 +01003028/**
3029 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3030 *
3031 * The control element is supposed to have the private_value field
3032 * set up via HDA_BIND_VOL() macro.
3033 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003034int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3035 unsigned int size, unsigned int __user *tlv)
3036{
3037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3038 struct hda_bind_ctls *c;
3039 int err;
3040
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003041 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003042 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003043 kcontrol->private_value = *c->values;
3044 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3045 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003046 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003047 return err;
3048}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003049EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003050
3051struct hda_ctl_ops snd_hda_bind_vol = {
3052 .info = snd_hda_mixer_amp_volume_info,
3053 .get = snd_hda_mixer_amp_volume_get,
3054 .put = snd_hda_mixer_amp_volume_put,
3055 .tlv = snd_hda_mixer_amp_tlv
3056};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003057EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003058
3059struct hda_ctl_ops snd_hda_bind_sw = {
3060 .info = snd_hda_mixer_amp_switch_info,
3061 .get = snd_hda_mixer_amp_switch_get,
3062 .put = snd_hda_mixer_amp_switch_put,
3063 .tlv = snd_hda_mixer_amp_tlv
3064};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003065EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003066
3067/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 * SPDIF out controls
3069 */
3070
Takashi Iwai0ba21762007-04-16 11:29:14 +02003071static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3075 uinfo->count = 1;
3076 return 0;
3077}
3078
Takashi Iwai0ba21762007-04-16 11:29:14 +02003079static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3083 IEC958_AES0_NONAUDIO |
3084 IEC958_AES0_CON_EMPHASIS_5015 |
3085 IEC958_AES0_CON_NOT_COPYRIGHT;
3086 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3087 IEC958_AES1_CON_ORIGINAL;
3088 return 0;
3089}
3090
Takashi Iwai0ba21762007-04-16 11:29:14 +02003091static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
3094 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3095 IEC958_AES0_NONAUDIO |
3096 IEC958_AES0_PRO_EMPHASIS_5015;
3097 return 0;
3098}
3099
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3101 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003104 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003105 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003107 mutex_lock(&codec->spdif_mutex);
3108 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003109 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3110 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3111 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3112 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003113 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 return 0;
3116}
3117
3118/* convert from SPDIF status bits to HDA SPDIF bits
3119 * bit 0 (DigEn) is always set zero (to be filled later)
3120 */
3121static unsigned short convert_from_spdif_status(unsigned int sbits)
3122{
3123 unsigned short val = 0;
3124
3125 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003126 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3131 IEC958_AES0_PRO_EMPHASIS_5015)
3132 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003134 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3135 IEC958_AES0_CON_EMPHASIS_5015)
3136 val |= AC_DIG1_EMPHASIS;
3137 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3138 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003140 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3142 }
3143 return val;
3144}
3145
3146/* convert to SPDIF status bits from HDA SPDIF bits
3147 */
3148static unsigned int convert_to_spdif_status(unsigned short val)
3149{
3150 unsigned int sbits = 0;
3151
Takashi Iwai0ba21762007-04-16 11:29:14 +02003152 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003154 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 sbits |= IEC958_AES0_PROFESSIONAL;
3156 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003157 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3159 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003160 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003162 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003164 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3166 sbits |= val & (0x7f << 8);
3167 }
3168 return sbits;
3169}
3170
Takashi Iwai2f728532008-09-25 16:32:41 +02003171/* set digital convert verbs both for the given NID and its slaves */
3172static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3173 int verb, int val)
3174{
Takashi Iwaidda14412011-05-02 11:29:30 +02003175 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003176
Takashi Iwai9e976972008-11-25 08:17:20 +01003177 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003178 d = codec->slave_dig_outs;
3179 if (!d)
3180 return;
3181 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003182 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003183}
3184
3185static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3186 int dig1, int dig2)
3187{
3188 if (dig1 != -1)
3189 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3190 if (dig2 != -1)
3191 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3192}
3193
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003198 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003199 struct hda_spdif_out *spdif;
3200 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 unsigned short val;
3202 int change;
3203
Ingo Molnar62932df2006-01-16 16:34:20 +01003204 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003205 spdif = snd_array_elem(&codec->spdif_out, idx);
3206 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003207 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3209 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3210 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003211 val = convert_from_spdif_status(spdif->status);
3212 val |= spdif->ctls & 1;
3213 change = spdif->ctls != val;
3214 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003215 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003216 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003217 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return change;
3219}
3220
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003221#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Takashi Iwai0ba21762007-04-16 11:29:14 +02003223static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003227 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003228 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003230 mutex_lock(&codec->spdif_mutex);
3231 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003232 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003233 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 0;
3235}
3236
Stephen Warren74b654c2011-06-01 11:14:18 -06003237static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3238 int dig1, int dig2)
3239{
3240 set_dig_out_convert(codec, nid, dig1, dig2);
3241 /* unmute amp switch (if any) */
3242 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3243 (dig1 & AC_DIG1_ENABLE))
3244 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3245 HDA_AMP_MUTE, 0);
3246}
3247
Takashi Iwai0ba21762007-04-16 11:29:14 +02003248static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3249 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003252 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003253 struct hda_spdif_out *spdif;
3254 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 unsigned short val;
3256 int change;
3257
Ingo Molnar62932df2006-01-16 16:34:20 +01003258 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003259 spdif = snd_array_elem(&codec->spdif_out, idx);
3260 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003261 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003263 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003264 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003265 spdif->ctls = val;
3266 if (change && nid != (u16)-1)
3267 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003268 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return change;
3270}
3271
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003272static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 {
3274 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3275 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003276 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 .info = snd_hda_spdif_mask_info,
3278 .get = snd_hda_spdif_cmask_get,
3279 },
3280 {
3281 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003283 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 .info = snd_hda_spdif_mask_info,
3285 .get = snd_hda_spdif_pmask_get,
3286 },
3287 {
3288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003289 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 .info = snd_hda_spdif_mask_info,
3291 .get = snd_hda_spdif_default_get,
3292 .put = snd_hda_spdif_default_put,
3293 },
3294 {
3295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003296 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 .info = snd_hda_spdif_out_switch_info,
3298 .get = snd_hda_spdif_out_switch_get,
3299 .put = snd_hda_spdif_out_switch_put,
3300 },
3301 { } /* end */
3302};
3303
3304/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003305 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003307 * @associated_nid: NID that new ctls associated with
3308 * @cvt_nid: converter NID
3309 * @type: HDA_PCM_TYPE_*
3310 * Creates controls related with the digital output.
3311 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 *
3313 * Returns 0 if successful, or a negative error code.
3314 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003315int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3316 hda_nid_t associated_nid,
3317 hda_nid_t cvt_nid,
3318 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003321 struct snd_kcontrol *kctl;
3322 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003323 int idx = 0;
3324 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003325 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003326 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003328 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003329 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003330 idx = spdif_index;
3331 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003332 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003333 /* suppose a single SPDIF device */
3334 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3335 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3336 if (!kctl)
3337 break;
3338 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003339 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003340 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003341 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003342 if (!bus->primary_dig_out_type)
3343 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003344
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003345 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003346 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003347 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3348 return -EBUSY;
3349 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003350 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003351 if (!spdif)
3352 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3354 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003355 if (!kctl)
3356 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003357 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003358 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003359 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003360 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return err;
3362 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003363 spdif->nid = cvt_nid;
3364 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003365 AC_VERB_GET_DIGI_CONVERT_1, 0);
3366 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return 0;
3368}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003369EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003371/* get the hda_spdif_out entry from the given NID
3372 * call within spdif_mutex lock
3373 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003374struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3375 hda_nid_t nid)
3376{
3377 int i;
3378 for (i = 0; i < codec->spdif_out.used; i++) {
3379 struct hda_spdif_out *spdif =
3380 snd_array_elem(&codec->spdif_out, i);
3381 if (spdif->nid == nid)
3382 return spdif;
3383 }
3384 return NULL;
3385}
3386EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3387
Stephen Warren74b654c2011-06-01 11:14:18 -06003388void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3389{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003390 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003391
3392 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003393 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003394 spdif->nid = (u16)-1;
3395 mutex_unlock(&codec->spdif_mutex);
3396}
3397EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3398
3399void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3400{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003401 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003402 unsigned short val;
3403
3404 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003405 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003406 if (spdif->nid != nid) {
3407 spdif->nid = nid;
3408 val = spdif->ctls;
3409 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3410 }
3411 mutex_unlock(&codec->spdif_mutex);
3412}
3413EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003416 * SPDIF sharing with analog output
3417 */
3418static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3420{
3421 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3422 ucontrol->value.integer.value[0] = mout->share_spdif;
3423 return 0;
3424}
3425
3426static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3428{
3429 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3430 mout->share_spdif = !!ucontrol->value.integer.value[0];
3431 return 0;
3432}
3433
3434static struct snd_kcontrol_new spdif_share_sw = {
3435 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3436 .name = "IEC958 Default PCM Playback Switch",
3437 .info = snd_ctl_boolean_mono_info,
3438 .get = spdif_share_sw_get,
3439 .put = spdif_share_sw_put,
3440};
3441
Takashi Iwaid5191e52009-11-16 14:58:17 +01003442/**
3443 * snd_hda_create_spdif_share_sw - create Default PCM switch
3444 * @codec: the HDA codec
3445 * @mout: multi-out instance
3446 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003447int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3448 struct hda_multi_out *mout)
3449{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003450 struct snd_kcontrol *kctl;
3451
Takashi Iwai9a081602008-02-12 18:37:26 +01003452 if (!mout->dig_out_nid)
3453 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003454
3455 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3456 if (!kctl)
3457 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003458 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003459 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003461EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003462
3463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * SPDIF input
3465 */
3466
3467#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3468
Takashi Iwai0ba21762007-04-16 11:29:14 +02003469static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3470 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3473
3474 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3475 return 0;
3476}
3477
Takashi Iwai0ba21762007-04-16 11:29:14 +02003478static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3479 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
3481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3482 hda_nid_t nid = kcontrol->private_value;
3483 unsigned int val = !!ucontrol->value.integer.value[0];
3484 int change;
3485
Ingo Molnar62932df2006-01-16 16:34:20 +01003486 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003488 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003490 snd_hda_codec_write_cache(codec, nid, 0,
3491 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003493 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return change;
3495}
3496
Takashi Iwai0ba21762007-04-16 11:29:14 +02003497static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3498 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
3500 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3501 hda_nid_t nid = kcontrol->private_value;
3502 unsigned short val;
3503 unsigned int sbits;
3504
Andrew Paprocki3982d172007-12-19 12:13:44 +01003505 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 sbits = convert_to_spdif_status(val);
3507 ucontrol->value.iec958.status[0] = sbits;
3508 ucontrol->value.iec958.status[1] = sbits >> 8;
3509 ucontrol->value.iec958.status[2] = sbits >> 16;
3510 ucontrol->value.iec958.status[3] = sbits >> 24;
3511 return 0;
3512}
3513
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003514static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 {
3516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003517 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 .info = snd_hda_spdif_in_switch_info,
3519 .get = snd_hda_spdif_in_switch_get,
3520 .put = snd_hda_spdif_in_switch_put,
3521 },
3522 {
3523 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3524 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003525 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 .info = snd_hda_spdif_mask_info,
3527 .get = snd_hda_spdif_in_status_get,
3528 },
3529 { } /* end */
3530};
3531
3532/**
3533 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3534 * @codec: the HDA codec
3535 * @nid: audio in widget NID
3536 *
3537 * Creates controls related with the SPDIF input.
3538 * Called from each patch supporting the SPDIF in.
3539 *
3540 * Returns 0 if successful, or a negative error code.
3541 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003542int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
3544 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003545 struct snd_kcontrol *kctl;
3546 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003547 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Takashi Iwaidcda5802012-10-12 17:24:51 +02003549 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003550 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003551 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3552 return -EBUSY;
3553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3555 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003556 if (!kctl)
3557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003559 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003560 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return err;
3562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003564 snd_hda_codec_read(codec, nid, 0,
3565 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003566 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return 0;
3568}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003569EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003571/*
3572 * command cache
3573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Takashi Iwaic370dd62012-12-13 18:30:04 +01003575/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003576#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3577#define get_cmd_cache_nid(key) ((key) & 0xff)
3578#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3579
3580/**
3581 * snd_hda_codec_write_cache - send a single command with caching
3582 * @codec: the HDA codec
3583 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003584 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003585 * @verb: the verb to send
3586 * @parm: the parameter for the verb
3587 *
3588 * Send a single command without waiting for response.
3589 *
3590 * Returns 0 if successful, or a negative error code.
3591 */
3592int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003593 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003594{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003595 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003596 struct hda_cache_head *c;
3597 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003598 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003599
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003600 cache_only = codec->cached_write;
3601 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003602 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 if (err < 0)
3604 return err;
3605 }
3606
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003607 /* parm may contain the verb stuff for get/set amp */
3608 verb = verb | (parm >> 8);
3609 parm &= 0xff;
3610 key = build_cmd_cache_key(nid, verb);
3611 mutex_lock(&codec->bus->cmd_mutex);
3612 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003613 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003614 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003615 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003616 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003617 mutex_unlock(&codec->bus->cmd_mutex);
3618 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003619}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003620EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003621
Takashi Iwaid5191e52009-11-16 14:58:17 +01003622/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003623 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3624 * @codec: the HDA codec
3625 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003626 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003627 * @verb: the verb to send
3628 * @parm: the parameter for the verb
3629 *
3630 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3631 * command if the parameter is already identical with the cached value.
3632 * If not, it sends the command and refreshes the cache.
3633 *
3634 * Returns 0 if successful, or a negative error code.
3635 */
3636int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003637 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003638{
3639 struct hda_cache_head *c;
3640 u32 key;
3641
3642 /* parm may contain the verb stuff for get/set amp */
3643 verb = verb | (parm >> 8);
3644 parm &= 0xff;
3645 key = build_cmd_cache_key(nid, verb);
3646 mutex_lock(&codec->bus->cmd_mutex);
3647 c = get_hash(&codec->cmd_cache, key);
3648 if (c && c->val == parm) {
3649 mutex_unlock(&codec->bus->cmd_mutex);
3650 return 0;
3651 }
3652 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003653 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003654}
3655EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3656
3657/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003658 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3659 * @codec: HD-audio codec
3660 *
3661 * Execute all verbs recorded in the command caches to resume.
3662 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003663void snd_hda_codec_resume_cache(struct hda_codec *codec)
3664{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003665 int i;
3666
Takashi Iwaic370dd62012-12-13 18:30:04 +01003667 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003668 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003669 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3670 struct hda_cache_head *buffer;
3671 u32 key;
3672
3673 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3674 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003675 if (!key)
3676 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003677 if (!buffer->dirty)
3678 continue;
3679 buffer->dirty = 0;
3680 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003681 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3682 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003683 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003684 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003685 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003686}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003687EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003688
3689/**
3690 * snd_hda_sequence_write_cache - sequence writes with caching
3691 * @codec: the HDA codec
3692 * @seq: VERB array to send
3693 *
3694 * Send the commands sequentially from the given array.
3695 * Thte commands are recorded on cache for power-save and resume.
3696 * The array must be terminated with NID=0.
3697 */
3698void snd_hda_sequence_write_cache(struct hda_codec *codec,
3699 const struct hda_verb *seq)
3700{
3701 for (; seq->nid; seq++)
3702 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3703 seq->param);
3704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003705EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003706
Takashi Iwaidc870f32013-01-22 15:24:30 +01003707/**
3708 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3709 * @codec: HD-audio codec
3710 */
3711void snd_hda_codec_flush_cache(struct hda_codec *codec)
3712{
3713 snd_hda_codec_resume_amp(codec);
3714 snd_hda_codec_resume_cache(codec);
3715}
3716EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3717
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003718void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003719 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003720{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003721 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003722 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003723
Takashi Iwaicb53c622007-08-10 17:21:45 +02003724 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003725 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003726 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003727 if (!(wcaps & AC_WCAP_POWER))
3728 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003729 if (codec->power_filter) {
3730 state = codec->power_filter(codec, nid, power_state);
3731 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003732 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003733 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003734 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003735 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003736 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003737}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003738EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3739
3740/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003741 * supported power states check
3742 */
3743static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3744 unsigned int power_state)
3745{
3746 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3747
Mengdong Line037cb42012-08-10 14:11:58 +02003748 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003749 return false;
3750 if (sup & power_state)
3751 return true;
3752 else
3753 return false;
3754}
3755
3756/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003757 * wait until the state is reached, returns the current state
3758 */
3759static unsigned int hda_sync_power_state(struct hda_codec *codec,
3760 hda_nid_t fg,
3761 unsigned int power_state)
3762{
3763 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3764 unsigned int state, actual_state;
3765
3766 for (;;) {
3767 state = snd_hda_codec_read(codec, fg, 0,
3768 AC_VERB_GET_POWER_STATE, 0);
3769 if (state & AC_PWRST_ERROR)
3770 break;
3771 actual_state = (state >> 4) & 0x0f;
3772 if (actual_state == power_state)
3773 break;
3774 if (time_after_eq(jiffies, end_time))
3775 break;
3776 /* wait until the codec reachs to the target state */
3777 msleep(1);
3778 }
3779 return state;
3780}
3781
Takashi Iwai9419ab62013-01-24 17:23:35 +01003782/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003783unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3784 hda_nid_t nid,
3785 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003786{
3787 if (power_state == AC_PWRST_D3 &&
3788 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3789 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3790 int eapd = snd_hda_codec_read(codec, nid, 0,
3791 AC_VERB_GET_EAPD_BTLENABLE, 0);
3792 if (eapd & 0x02)
3793 return AC_PWRST_D0;
3794 }
3795 return power_state;
3796}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003797EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003798
Takashi Iwai432c6412012-08-28 09:59:20 -07003799/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003800 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003801 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003802static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003803 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003804{
Takashi Iwaid8193872012-08-31 07:54:38 -07003805 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003806 int count;
3807 unsigned int state;
3808
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003809 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003810 if (power_state == AC_PWRST_D3) {
3811 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003812 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003813 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003814
3815 /* repeat power states setting at most 10 times*/
3816 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003817 if (codec->patch_ops.set_power_state)
3818 codec->patch_ops.set_power_state(codec, fg,
3819 power_state);
3820 else {
3821 snd_hda_codec_read(codec, fg, 0,
3822 AC_VERB_SET_POWER_STATE,
3823 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003824 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003825 }
3826 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003827 if (!(state & AC_PWRST_ERROR))
3828 break;
3829 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003830
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003831 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003832}
Takashi Iwai54d17402005-11-21 16:33:22 +01003833
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003834/* sync power states of all widgets;
3835 * this is called at the end of codec parsing
3836 */
3837static void sync_power_up_states(struct hda_codec *codec)
3838{
3839 hda_nid_t nid = codec->start_nid;
3840 int i;
3841
Takashi Iwaiba615b82013-03-13 14:47:21 +01003842 /* don't care if no filter is used */
3843 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003844 return;
3845
3846 for (i = 0; i < codec->num_nodes; i++, nid++) {
3847 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003848 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003849 if (!(wcaps & AC_WCAP_POWER))
3850 continue;
3851 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3852 if (target == AC_PWRST_D0)
3853 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003854 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003855 snd_hda_codec_write(codec, nid, 0,
3856 AC_VERB_SET_POWER_STATE, target);
3857 }
3858}
3859
Takashi Iwai11aeff02008-07-30 15:01:46 +02003860#ifdef CONFIG_SND_HDA_HWDEP
3861/* execute additional init verbs */
3862static void hda_exec_init_verbs(struct hda_codec *codec)
3863{
3864 if (codec->init_verbs.list)
3865 snd_hda_sequence_write(codec, codec->init_verbs.list);
3866}
3867#else
3868static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3869#endif
3870
Takashi Iwai2a439522011-07-26 09:52:50 +02003871#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003872/*
3873 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003874 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003875 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003876static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003877{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003878 unsigned int state;
3879
Takashi Iwai989c3182012-11-19 14:14:58 +01003880 codec->in_pm = 1;
3881
Takashi Iwaicb53c622007-08-10 17:21:45 +02003882 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003883 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003884 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003885 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003886 /* Cancel delayed work if we aren't currently running from it. */
3887 if (!in_wq)
3888 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003889 spin_lock(&codec->power_lock);
3890 snd_hda_update_power_acct(codec);
3891 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003892 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003893 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003894 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003895 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003896 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003897 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003898}
3899
Takashi Iwaic370dd62012-12-13 18:30:04 +01003900/* mark all entries of cmd and amp caches dirty */
3901static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3902{
3903 int i;
3904 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3905 struct hda_cache_head *cmd;
3906 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3907 cmd->dirty = 1;
3908 }
3909 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3910 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003911 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003912 amp->head.dirty = 1;
3913 }
3914}
3915
Takashi Iwaicb53c622007-08-10 17:21:45 +02003916/*
3917 * kick up codec; used both from PM and power-save
3918 */
3919static void hda_call_codec_resume(struct hda_codec *codec)
3920{
Takashi Iwai989c3182012-11-19 14:14:58 +01003921 codec->in_pm = 1;
3922
Takashi Iwaic370dd62012-12-13 18:30:04 +01003923 hda_mark_cmd_cache_dirty(codec);
3924
Takashi Iwai7f308302012-05-08 16:52:23 +02003925 /* set as if powered on for avoiding re-entering the resume
3926 * in the resume / power-save sequence
3927 */
3928 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003929 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003930 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003931 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003932 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003933 if (codec->patch_ops.resume)
3934 codec->patch_ops.resume(codec);
3935 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003936 if (codec->patch_ops.init)
3937 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003938 snd_hda_codec_resume_amp(codec);
3939 snd_hda_codec_resume_cache(codec);
3940 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003941
3942 if (codec->jackpoll_interval)
3943 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003944 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003945 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003946
3947 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003948 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003949}
Takashi Iwai2a439522011-07-26 09:52:50 +02003950#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003951
Takashi Iwai54d17402005-11-21 16:33:22 +01003952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953/**
3954 * snd_hda_build_controls - build mixer controls
3955 * @bus: the BUS
3956 *
3957 * Creates mixer controls for each codec included in the bus.
3958 *
3959 * Returns 0 if successful, otherwise a negative error code.
3960 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003961int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003963 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
Takashi Iwai0ba21762007-04-16 11:29:14 +02003965 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003966 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003967 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003968 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003969 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003970 err = snd_hda_codec_reset(codec);
3971 if (err < 0) {
3972 printk(KERN_ERR
3973 "hda_codec: cannot revert codec\n");
3974 return err;
3975 }
3976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003978 return 0;
3979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003980EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003982/*
3983 * add standard channel maps if not specified
3984 */
3985static int add_std_chmaps(struct hda_codec *codec)
3986{
3987 int i, str, err;
3988
3989 for (i = 0; i < codec->num_pcms; i++) {
3990 for (str = 0; str < 2; str++) {
3991 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3992 struct hda_pcm_stream *hinfo =
3993 &codec->pcm_info[i].stream[str];
3994 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003995 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003996
3997 if (codec->pcm_info[i].own_chmap)
3998 continue;
3999 if (!pcm || !hinfo->substreams)
4000 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004001 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4002 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004003 hinfo->channels_max,
4004 0, &chmap);
4005 if (err < 0)
4006 return err;
4007 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4008 }
4009 }
4010 return 0;
4011}
4012
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004013/* default channel maps for 2.1 speakers;
4014 * since HD-audio supports only stereo, odd number channels are omitted
4015 */
4016const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4017 { .channels = 2,
4018 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4019 { .channels = 4,
4020 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4021 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4022 { }
4023};
4024EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4025
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004026int snd_hda_codec_build_controls(struct hda_codec *codec)
4027{
4028 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004029 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004030 /* continue to initialize... */
4031 if (codec->patch_ops.init)
4032 err = codec->patch_ops.init(codec);
4033 if (!err && codec->patch_ops.build_controls)
4034 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004035 if (err < 0)
4036 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004037
4038 /* we create chmaps here instead of build_pcms */
4039 err = add_std_chmaps(codec);
4040 if (err < 0)
4041 return err;
4042
David Henningsson26a6cb62012-10-09 15:04:21 +02004043 if (codec->jackpoll_interval)
4044 hda_jackpoll_work(&codec->jackpoll_work.work);
4045 else
4046 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004047 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return 0;
4049}
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051/*
4052 * stream formats
4053 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004054struct hda_rate_tbl {
4055 unsigned int hz;
4056 unsigned int alsa_bits;
4057 unsigned int hda_fmt;
4058};
4059
Takashi Iwai92f10b32010-08-03 14:21:00 +02004060/* rate = base * mult / div */
4061#define HDA_RATE(base, mult, div) \
4062 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4063 (((div) - 1) << AC_FMT_DIV_SHIFT))
4064
Takashi Iwaibefdf312005-08-22 13:57:55 +02004065static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004067
4068 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004069 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4070 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4071 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4072 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4073 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4074 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4075 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4076 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4077 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4078 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4079 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004080#define AC_PAR_PCM_RATE_BITS 11
4081 /* up to bits 10, 384kHZ isn't supported properly */
4082
4083 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004084 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004085
Takashi Iwaibefdf312005-08-22 13:57:55 +02004086 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087};
4088
4089/**
4090 * snd_hda_calc_stream_format - calculate format bitset
4091 * @rate: the sample rate
4092 * @channels: the number of channels
4093 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4094 * @maxbps: the max. bps
4095 *
4096 * Calculate the format bitset from the given rate, channels and th PCM format.
4097 *
4098 * Return zero if invalid.
4099 */
4100unsigned int snd_hda_calc_stream_format(unsigned int rate,
4101 unsigned int channels,
4102 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004103 unsigned int maxbps,
4104 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 int i;
4107 unsigned int val = 0;
4108
Takashi Iwaibefdf312005-08-22 13:57:55 +02004109 for (i = 0; rate_bits[i].hz; i++)
4110 if (rate_bits[i].hz == rate) {
4111 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 break;
4113 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004114 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 snd_printdd("invalid rate %d\n", rate);
4116 return 0;
4117 }
4118
4119 if (channels == 0 || channels > 8) {
4120 snd_printdd("invalid channels %d\n", channels);
4121 return 0;
4122 }
4123 val |= channels - 1;
4124
4125 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004126 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004127 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004128 break;
4129 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004130 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 case 20:
4133 case 24:
4134 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004135 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004136 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004138 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004140 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 break;
4142 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004143 snd_printdd("invalid format width %d\n",
4144 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return 0;
4146 }
4147
Anssi Hannula32c168c2010-08-03 13:28:57 +03004148 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004149 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 return val;
4152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004153EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004155static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4156 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004157{
4158 unsigned int val = 0;
4159 if (nid != codec->afg &&
4160 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4161 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4162 if (!val || val == -1)
4163 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4164 if (!val || val == -1)
4165 return 0;
4166 return val;
4167}
4168
4169static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4170{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004171 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004172 get_pcm_param);
4173}
4174
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004175static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4176 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004177{
4178 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4179 if (!streams || streams == -1)
4180 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4181 if (!streams || streams == -1)
4182 return 0;
4183 return streams;
4184}
4185
4186static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4187{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004188 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004189 get_stream_param);
4190}
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192/**
4193 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4194 * @codec: the HDA codec
4195 * @nid: NID to query
4196 * @ratesp: the pointer to store the detected rate bitflags
4197 * @formatsp: the pointer to store the detected formats
4198 * @bpsp: the pointer to store the detected format widths
4199 *
4200 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4201 * or @bsps argument is ignored.
4202 *
4203 * Returns 0 if successful, otherwise a negative error code.
4204 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004205int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4207{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004208 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004210 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004211 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 if (ratesp) {
4214 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004215 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004217 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004219 if (rates == 0) {
4220 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4221 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4222 nid, val,
4223 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4224 return -EIO;
4225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 *ratesp = rates;
4227 }
4228
4229 if (formatsp || bpsp) {
4230 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004231 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004233 streams = query_stream_param(codec, nid);
4234 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 bps = 0;
4238 if (streams & AC_SUPFMT_PCM) {
4239 if (val & AC_SUPPCM_BITS_8) {
4240 formats |= SNDRV_PCM_FMTBIT_U8;
4241 bps = 8;
4242 }
4243 if (val & AC_SUPPCM_BITS_16) {
4244 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4245 bps = 16;
4246 }
4247 if (wcaps & AC_WCAP_DIGITAL) {
4248 if (val & AC_SUPPCM_BITS_32)
4249 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4250 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4251 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4252 if (val & AC_SUPPCM_BITS_24)
4253 bps = 24;
4254 else if (val & AC_SUPPCM_BITS_20)
4255 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004256 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4257 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4259 if (val & AC_SUPPCM_BITS_32)
4260 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 else if (val & AC_SUPPCM_BITS_24)
4262 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004263 else if (val & AC_SUPPCM_BITS_20)
4264 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004267#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004268 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004270 if (!bps)
4271 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004272 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004273#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004274 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004275 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 /* temporary hack: we have still no proper support
4277 * for the direct AC3 stream...
4278 */
4279 formats |= SNDRV_PCM_FMTBIT_U8;
4280 bps = 8;
4281 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004282 if (formats == 0) {
4283 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4284 "(nid=0x%x, val=0x%x, ovrd=%i, "
4285 "streams=0x%x)\n",
4286 nid, val,
4287 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4288 streams);
4289 return -EIO;
4290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (formatsp)
4292 *formatsp = formats;
4293 if (bpsp)
4294 *bpsp = bps;
4295 }
4296
4297 return 0;
4298}
Stephen Warren384a48d2011-06-01 11:14:21 -06004299EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004302 * snd_hda_is_supported_format - Check the validity of the format
4303 * @codec: HD-audio codec
4304 * @nid: NID to check
4305 * @format: the HD-audio format value to check
4306 *
4307 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 *
4309 * Returns 1 if supported, 0 if not.
4310 */
4311int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4312 unsigned int format)
4313{
4314 int i;
4315 unsigned int val = 0, rate, stream;
4316
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004317 val = query_pcm_param(codec, nid);
4318 if (!val)
4319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004322 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004323 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (val & (1 << i))
4325 break;
4326 return 0;
4327 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004328 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return 0;
4330
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004331 stream = query_stream_param(codec, nid);
4332 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return 0;
4334
4335 if (stream & AC_SUPFMT_PCM) {
4336 switch (format & 0xf0) {
4337 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004338 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return 0;
4340 break;
4341 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004342 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return 0;
4344 break;
4345 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004346 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348 break;
4349 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004350 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return 0;
4352 break;
4353 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004354 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return 0;
4356 break;
4357 default:
4358 return 0;
4359 }
4360 } else {
4361 /* FIXME: check for float32 and AC3? */
4362 }
4363
4364 return 1;
4365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004366EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368/*
4369 * PCM stuff
4370 */
4371static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4372 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004373 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
4375 return 0;
4376}
4377
4378static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4379 struct hda_codec *codec,
4380 unsigned int stream_tag,
4381 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004382 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
4384 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4385 return 0;
4386}
4387
4388static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4389 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004390 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
Takashi Iwai888afa12008-03-18 09:57:50 +01004392 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394}
4395
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004396static int set_pcm_default_values(struct hda_codec *codec,
4397 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004399 int err;
4400
Takashi Iwai0ba21762007-04-16 11:29:14 +02004401 /* query support PCM information from the given NID */
4402 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004403 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004404 info->rates ? NULL : &info->rates,
4405 info->formats ? NULL : &info->formats,
4406 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004407 if (err < 0)
4408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410 if (info->ops.open == NULL)
4411 info->ops.open = hda_pcm_default_open_close;
4412 if (info->ops.close == NULL)
4413 info->ops.close = hda_pcm_default_open_close;
4414 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004415 if (snd_BUG_ON(!info->nid))
4416 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 info->ops.prepare = hda_pcm_default_prepare;
4418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 if (info->ops.cleanup == 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.cleanup = hda_pcm_default_cleanup;
4423 }
4424 return 0;
4425}
4426
Takashi Iwaieb541332010-08-06 13:48:11 +02004427/*
4428 * codec prepare/cleanup entries
4429 */
4430int snd_hda_codec_prepare(struct hda_codec *codec,
4431 struct hda_pcm_stream *hinfo,
4432 unsigned int stream,
4433 unsigned int format,
4434 struct snd_pcm_substream *substream)
4435{
4436 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004437 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004438 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4439 if (ret >= 0)
4440 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004441 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004442 return ret;
4443}
4444EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4445
4446void snd_hda_codec_cleanup(struct hda_codec *codec,
4447 struct hda_pcm_stream *hinfo,
4448 struct snd_pcm_substream *substream)
4449{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004450 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004451 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004452 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004453}
4454EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4455
Takashi Iwaid5191e52009-11-16 14:58:17 +01004456/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004457const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4458 "Audio", "SPDIF", "HDMI", "Modem"
4459};
4460
Takashi Iwai176d5332008-07-30 15:01:44 +02004461/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004462 * get the empty PCM device number to assign
4463 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004464static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004465{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004466 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004467 /* assigned to static slots up to dev#10; if more needed, assign
4468 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4469 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004470 static int audio_idx[HDA_PCM_NTYPES][5] = {
4471 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4472 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004473 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004474 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004475 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004476 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004477
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004478 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004479 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4480 return -EINVAL;
4481 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004482
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004483 for (i = 0; audio_idx[type][i] >= 0; i++) {
4484#ifndef CONFIG_SND_DYNAMIC_MINORS
4485 if (audio_idx[type][i] >= 8)
4486 break;
4487#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004488 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4489 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004490 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004491
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004492#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004493 /* non-fixed slots starting from 10 */
4494 for (i = 10; i < 32; i++) {
4495 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4496 return i;
4497 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004498#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004499
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004500 snd_printk(KERN_WARNING "Too many %s devices\n",
4501 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004502#ifndef CONFIG_SND_DYNAMIC_MINORS
4503 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4504#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004505 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004506}
4507
4508/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004509 * attach a new PCM stream
4510 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004511static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004512{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004513 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004514 struct hda_pcm_stream *info;
4515 int stream, err;
4516
Takashi Iwaib91f0802008-11-04 08:43:08 +01004517 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004518 return -EINVAL;
4519 for (stream = 0; stream < 2; stream++) {
4520 info = &pcm->stream[stream];
4521 if (info->substreams) {
4522 err = set_pcm_default_values(codec, info);
4523 if (err < 0)
4524 return err;
4525 }
4526 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004527 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004528}
4529
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004530/* assign all PCMs of the given codec */
4531int snd_hda_codec_build_pcms(struct hda_codec *codec)
4532{
4533 unsigned int pcm;
4534 int err;
4535
4536 if (!codec->num_pcms) {
4537 if (!codec->patch_ops.build_pcms)
4538 return 0;
4539 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004540 if (err < 0) {
4541 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004542 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004543 err = snd_hda_codec_reset(codec);
4544 if (err < 0) {
4545 printk(KERN_ERR
4546 "hda_codec: cannot revert codec\n");
4547 return err;
4548 }
4549 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004550 }
4551 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4552 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4553 int dev;
4554
4555 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004556 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004557
4558 if (!cpcm->pcm) {
4559 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4560 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004561 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004562 cpcm->device = dev;
4563 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004564 if (err < 0) {
4565 printk(KERN_ERR "hda_codec: cannot attach "
4566 "PCM stream %d for codec #%d\n",
4567 dev, codec->addr);
4568 continue; /* no fatal error */
4569 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004570 }
4571 }
4572 return 0;
4573}
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575/**
4576 * snd_hda_build_pcms - build PCM information
4577 * @bus: the BUS
4578 *
4579 * Create PCM information for each codec included in the bus.
4580 *
4581 * The build_pcms codec patch is requested to set up codec->num_pcms and
4582 * codec->pcm_info properly. The array is referred by the top-level driver
4583 * to create its PCM instances.
4584 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4585 * callback.
4586 *
4587 * At least, substreams, channels_min and channels_max must be filled for
4588 * each stream. substreams = 0 indicates that the stream doesn't exist.
4589 * When rates and/or formats are zero, the supported values are queried
4590 * from the given nid. The nid is used also by the default ops.prepare
4591 * and ops.cleanup callbacks.
4592 *
4593 * The driver needs to call ops.open in its open callback. Similarly,
4594 * ops.close is supposed to be called in the close callback.
4595 * ops.prepare should be called in the prepare or hw_params callback
4596 * with the proper parameters for set up.
4597 * ops.cleanup should be called in hw_free for clean up of streams.
4598 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004599 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004601int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004603 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Takashi Iwai0ba21762007-04-16 11:29:14 +02004605 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004606 int err = snd_hda_codec_build_pcms(codec);
4607 if (err < 0)
4608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610 return 0;
4611}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004612EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614/**
4615 * snd_hda_check_board_config - compare the current codec with the config table
4616 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004617 * @num_configs: number of config enums
4618 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 * @tbl: configuration table, terminated by null entries
4620 *
4621 * Compares the modelname or PCI subsystem id of the current codec with the
4622 * given configuration table. If a matching entry is found, returns its
4623 * config value (supposed to be 0 or positive).
4624 *
4625 * If no entries are matching, the function returns a negative value.
4626 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004627int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004628 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004629 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004631 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004632 int i;
4633 for (i = 0; i < num_configs; i++) {
4634 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004635 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004636 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4637 "selected\n", models[i]);
4638 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
4640 }
4641 }
4642
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004643 if (!codec->bus->pci || !tbl)
4644 return -1;
4645
4646 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4647 if (!tbl)
4648 return -1;
4649 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004650#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004651 char tmp[10];
4652 const char *model = NULL;
4653 if (models)
4654 model = models[tbl->value];
4655 if (!model) {
4656 sprintf(tmp, "#%d", tbl->value);
4657 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004659 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4660 "for config %x:%x (%s)\n",
4661 model, tbl->subvendor, tbl->subdevice,
4662 (tbl->name ? tbl->name : "Unknown device"));
4663#endif
4664 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666 return -1;
4667}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004668EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
4670/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004671 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004672 subsystem ID with the
4673 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004674
4675 This is important for Gateway notebooks with SB450 HDA Audio
4676 where the vendor ID of the PCI device is:
4677 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4678 and the vendor/subvendor are found only at the codec.
4679
4680 * @codec: the HDA codec
4681 * @num_configs: number of config enums
4682 * @models: array of model name strings
4683 * @tbl: configuration table, terminated by null entries
4684 *
4685 * Compares the modelname or PCI subsystem id of the current codec with the
4686 * given configuration table. If a matching entry is found, returns its
4687 * config value (supposed to be 0 or positive).
4688 *
4689 * If no entries are matching, the function returns a negative value.
4690 */
4691int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004692 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004693 const struct snd_pci_quirk *tbl)
4694{
4695 const struct snd_pci_quirk *q;
4696
4697 /* Search for codec ID */
4698 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004699 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4700 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4701 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004702 break;
4703 }
4704
4705 if (!q->subvendor)
4706 return -1;
4707
4708 tbl = q;
4709
4710 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004711#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004712 char tmp[10];
4713 const char *model = NULL;
4714 if (models)
4715 model = models[tbl->value];
4716 if (!model) {
4717 sprintf(tmp, "#%d", tbl->value);
4718 model = tmp;
4719 }
4720 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4721 "for config %x:%x (%s)\n",
4722 model, tbl->subvendor, tbl->subdevice,
4723 (tbl->name ? tbl->name : "Unknown device"));
4724#endif
4725 return tbl->value;
4726 }
4727 return -1;
4728}
4729EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4730
4731/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 * snd_hda_add_new_ctls - create controls from the array
4733 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004734 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 *
4736 * This helper function creates and add new controls in the given array.
4737 * The array must be terminated with an empty entry as terminator.
4738 *
4739 * Returns 0 if successful, or a negative error code.
4740 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004741int snd_hda_add_new_ctls(struct hda_codec *codec,
4742 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004744 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004747 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004748 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004749 if (knew->iface == -1) /* skip this codec private value */
4750 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004751 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004752 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004753 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004754 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004755 if (addr > 0)
4756 kctl->id.device = addr;
4757 if (idx > 0)
4758 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004759 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004760 if (!err)
4761 break;
4762 /* try first with another device index corresponding to
4763 * the codec addr; if it still fails (or it's the
4764 * primary codec), then try another control index
4765 */
4766 if (!addr && codec->addr)
4767 addr = codec->addr;
4768 else if (!idx && !knew->index) {
4769 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004770 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004771 if (idx <= 0)
4772 return err;
4773 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004774 return err;
4775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 }
4777 return 0;
4778}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004779EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Takashi Iwai83012a72012-08-24 18:38:08 +02004781#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004782static void hda_power_work(struct work_struct *work)
4783{
4784 struct hda_codec *codec =
4785 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004786 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004787 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004788
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004789 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004790 if (codec->power_transition > 0) { /* during power-up sequence? */
4791 spin_unlock(&codec->power_lock);
4792 return;
4793 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004794 if (!codec->power_on || codec->power_count) {
4795 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004796 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004797 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004798 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004799 spin_unlock(&codec->power_lock);
4800
Dylan Reidd17344b2012-09-28 15:57:01 -07004801 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004802 codec->pm_down_notified = 0;
4803 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4804 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004805 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004806 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004807}
4808
4809static void hda_keep_power_on(struct hda_codec *codec)
4810{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004811 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004812 codec->power_count++;
4813 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004814 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004815 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004816}
4817
Takashi Iwaid5191e52009-11-16 14:58:17 +01004818/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004819void snd_hda_update_power_acct(struct hda_codec *codec)
4820{
4821 unsigned long delta = jiffies - codec->power_jiffies;
4822 if (codec->power_on)
4823 codec->power_on_acct += delta;
4824 else
4825 codec->power_off_acct += delta;
4826 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004827}
4828
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004829/* Transition to powered up, if wait_power_down then wait for a pending
4830 * transition to D3 to complete. A pending D3 transition is indicated
4831 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004832/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004833static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004834{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004835 struct hda_bus *bus = codec->bus;
4836
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004837 /* Return if power_on or transitioning to power_on, unless currently
4838 * powering down. */
4839 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004840 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004841 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004842 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004843
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004844 cancel_delayed_work_sync(&codec->power_work);
4845
4846 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004847 /* If the power down delayed work was cancelled above before starting,
4848 * then there is no need to go through power up here.
4849 */
4850 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004851 if (codec->power_transition < 0)
4852 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004853 return;
4854 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004855
Takashi Iwaid66fee52011-08-02 15:39:31 +02004856 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004857 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004858 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004859 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004860 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004861 spin_unlock(&codec->power_lock);
4862
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004863 if (codec->pm_down_notified) {
4864 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004865 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004866 }
4867
Takashi Iwaicb53c622007-08-10 17:21:45 +02004868 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004869
4870 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004871 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004872}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004873
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004874#define power_save(codec) \
4875 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004876
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004877/* Transition to powered down */
4878static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004879{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004880 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004881 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004882
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004883 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004884 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004885 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004886 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004887 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004888}
4889
4890/**
4891 * snd_hda_power_save - Power-up/down/sync the codec
4892 * @codec: HD-audio codec
4893 * @delta: the counter delta to change
4894 *
4895 * Change the power-up counter via @delta, and power up or down the hardware
4896 * appropriately. For the power-down, queue to the delayed action.
4897 * Passing zero to @delta means to synchronize the power state.
4898 */
4899void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4900{
4901 spin_lock(&codec->power_lock);
4902 codec->power_count += delta;
4903 trace_hda_power_count(codec);
4904 if (delta > 0)
4905 __snd_hda_power_up(codec, d3wait);
4906 else
4907 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004908 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004909}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004910EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004911
Takashi Iwaid5191e52009-11-16 14:58:17 +01004912/**
4913 * snd_hda_check_amp_list_power - Check the amp list and update the power
4914 * @codec: HD-audio codec
4915 * @check: the object containing an AMP list and the status
4916 * @nid: NID to check / update
4917 *
4918 * Check whether the given NID is in the amp list. If it's in the list,
4919 * check the current AMP status, and update the the power-status according
4920 * to the mute status.
4921 *
4922 * This function is supposed to be set or called from the check_power_status
4923 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004924 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004925int snd_hda_check_amp_list_power(struct hda_codec *codec,
4926 struct hda_loopback_check *check,
4927 hda_nid_t nid)
4928{
Takashi Iwai031024e2011-05-02 11:29:30 +02004929 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004930 int ch, v;
4931
4932 if (!check->amplist)
4933 return 0;
4934 for (p = check->amplist; p->nid; p++) {
4935 if (p->nid == nid)
4936 break;
4937 }
4938 if (!p->nid)
4939 return 0; /* nothing changed */
4940
4941 for (p = check->amplist; p->nid; p++) {
4942 for (ch = 0; ch < 2; ch++) {
4943 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4944 p->idx);
4945 if (!(v & HDA_AMP_MUTE) && v > 0) {
4946 if (!check->power_on) {
4947 check->power_on = 1;
4948 snd_hda_power_up(codec);
4949 }
4950 return 1;
4951 }
4952 }
4953 }
4954 if (check->power_on) {
4955 check->power_on = 0;
4956 snd_hda_power_down(codec);
4957 }
4958 return 0;
4959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004960EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004963/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004964 * Channel mode helper
4965 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004966
4967/**
4968 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4969 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004970int snd_hda_ch_mode_info(struct hda_codec *codec,
4971 struct snd_ctl_elem_info *uinfo,
4972 const struct hda_channel_mode *chmode,
4973 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004974{
4975 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4976 uinfo->count = 1;
4977 uinfo->value.enumerated.items = num_chmodes;
4978 if (uinfo->value.enumerated.item >= num_chmodes)
4979 uinfo->value.enumerated.item = num_chmodes - 1;
4980 sprintf(uinfo->value.enumerated.name, "%dch",
4981 chmode[uinfo->value.enumerated.item].channels);
4982 return 0;
4983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004984EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004985
Takashi Iwaid5191e52009-11-16 14:58:17 +01004986/**
4987 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4988 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004989int snd_hda_ch_mode_get(struct hda_codec *codec,
4990 struct snd_ctl_elem_value *ucontrol,
4991 const struct hda_channel_mode *chmode,
4992 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004993 int max_channels)
4994{
4995 int i;
4996
4997 for (i = 0; i < num_chmodes; i++) {
4998 if (max_channels == chmode[i].channels) {
4999 ucontrol->value.enumerated.item[0] = i;
5000 break;
5001 }
5002 }
5003 return 0;
5004}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005005EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005006
Takashi Iwaid5191e52009-11-16 14:58:17 +01005007/**
5008 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5009 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005010int snd_hda_ch_mode_put(struct hda_codec *codec,
5011 struct snd_ctl_elem_value *ucontrol,
5012 const struct hda_channel_mode *chmode,
5013 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005014 int *max_channelsp)
5015{
5016 unsigned int mode;
5017
5018 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005019 if (mode >= num_chmodes)
5020 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005021 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005022 return 0;
5023 /* change the current channel setting */
5024 *max_channelsp = chmode[mode].channels;
5025 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005026 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005027 return 1;
5028}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005029EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031/*
5032 * input MUX helper
5033 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005034
5035/**
5036 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5037 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005038int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5039 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
5041 unsigned int index;
5042
5043 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5044 uinfo->count = 1;
5045 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005046 if (!imux->num_items)
5047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 index = uinfo->value.enumerated.item;
5049 if (index >= imux->num_items)
5050 index = imux->num_items - 1;
5051 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5052 return 0;
5053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005054EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Takashi Iwaid5191e52009-11-16 14:58:17 +01005056/**
5057 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5058 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005059int snd_hda_input_mux_put(struct hda_codec *codec,
5060 const struct hda_input_mux *imux,
5061 struct snd_ctl_elem_value *ucontrol,
5062 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 unsigned int *cur_val)
5064{
5065 unsigned int idx;
5066
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005067 if (!imux->num_items)
5068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 idx = ucontrol->value.enumerated.item[0];
5070 if (idx >= imux->num_items)
5071 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005072 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005074 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5075 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 *cur_val = idx;
5077 return 1;
5078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005079EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081
5082/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005083 * process kcontrol info callback of a simple string enum array
5084 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5085 */
5086int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5087 struct snd_ctl_elem_info *uinfo,
5088 int num_items, const char * const *texts)
5089{
5090 static const char * const texts_default[] = {
5091 "Disabled", "Enabled"
5092 };
5093
5094 if (!texts || !num_items) {
5095 num_items = 2;
5096 texts = texts_default;
5097 }
5098
5099 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5100 uinfo->count = 1;
5101 uinfo->value.enumerated.items = num_items;
5102 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5103 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5104 strcpy(uinfo->value.enumerated.name,
5105 texts[uinfo->value.enumerated.item]);
5106 return 0;
5107}
5108EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5109
5110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 * Multi-channel / digital-out PCM helper functions
5112 */
5113
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005114/* setup SPDIF output stream */
5115static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5116 unsigned int stream_tag, unsigned int format)
5117{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005118 struct hda_spdif_out *spdif;
5119 unsigned int curr_fmt;
5120 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005121
Laurence Darby3bef1c32012-11-03 17:00:06 +00005122 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5123 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5124 AC_VERB_GET_STREAM_FORMAT, 0);
5125 reset = codec->spdif_status_reset &&
5126 (spdif->ctls & AC_DIG1_ENABLE) &&
5127 curr_fmt != format;
5128
5129 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5130 updated */
5131 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005132 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005133 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005134 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005135 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005136 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005137 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005138 for (d = codec->slave_dig_outs; *d; d++)
5139 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5140 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005141 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005142 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005143 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005144 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005145 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005146}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005147
Takashi Iwai2f728532008-09-25 16:32:41 +02005148static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5149{
5150 snd_hda_codec_cleanup_stream(codec, nid);
5151 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005152 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005153 for (d = codec->slave_dig_outs; *d; d++)
5154 snd_hda_codec_cleanup_stream(codec, *d);
5155 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005156}
5157
Takashi Iwaid5191e52009-11-16 14:58:17 +01005158/**
5159 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5160 * @bus: HD-audio bus
5161 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005162void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5163{
5164 struct hda_codec *codec;
5165
5166 if (!bus)
5167 return;
5168 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005169 if (hda_codec_is_power_on(codec) &&
5170 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005171 codec->patch_ops.reboot_notify(codec);
5172 }
5173}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005174EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005175
Takashi Iwaid5191e52009-11-16 14:58:17 +01005176/**
5177 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005179int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5180 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181{
Ingo Molnar62932df2006-01-16 16:34:20 +01005182 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005183 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5184 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005185 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005187 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 return 0;
5189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005190EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Takashi Iwaid5191e52009-11-16 14:58:17 +01005192/**
5193 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5194 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005195int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5196 struct hda_multi_out *mout,
5197 unsigned int stream_tag,
5198 unsigned int format,
5199 struct snd_pcm_substream *substream)
5200{
5201 mutex_lock(&codec->spdif_mutex);
5202 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5203 mutex_unlock(&codec->spdif_mutex);
5204 return 0;
5205}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005206EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005207
Takashi Iwaid5191e52009-11-16 14:58:17 +01005208/**
5209 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5210 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005211int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5212 struct hda_multi_out *mout)
5213{
5214 mutex_lock(&codec->spdif_mutex);
5215 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5216 mutex_unlock(&codec->spdif_mutex);
5217 return 0;
5218}
5219EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5220
Takashi Iwaid5191e52009-11-16 14:58:17 +01005221/**
5222 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005224int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5225 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226{
Ingo Molnar62932df2006-01-16 16:34:20 +01005227 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005229 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return 0;
5231}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005232EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Takashi Iwaid5191e52009-11-16 14:58:17 +01005234/**
5235 * snd_hda_multi_out_analog_open - open analog outputs
5236 *
5237 * Open analog outputs and set up the hw-constraints.
5238 * If the digital outputs can be opened as slave, open the digital
5239 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005241int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5242 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005243 struct snd_pcm_substream *substream,
5244 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
Takashi Iwai9a081602008-02-12 18:37:26 +01005246 struct snd_pcm_runtime *runtime = substream->runtime;
5247 runtime->hw.channels_max = mout->max_channels;
5248 if (mout->dig_out_nid) {
5249 if (!mout->analog_rates) {
5250 mout->analog_rates = hinfo->rates;
5251 mout->analog_formats = hinfo->formats;
5252 mout->analog_maxbps = hinfo->maxbps;
5253 } else {
5254 runtime->hw.rates = mout->analog_rates;
5255 runtime->hw.formats = mout->analog_formats;
5256 hinfo->maxbps = mout->analog_maxbps;
5257 }
5258 if (!mout->spdif_rates) {
5259 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5260 &mout->spdif_rates,
5261 &mout->spdif_formats,
5262 &mout->spdif_maxbps);
5263 }
5264 mutex_lock(&codec->spdif_mutex);
5265 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005266 if ((runtime->hw.rates & mout->spdif_rates) &&
5267 (runtime->hw.formats & mout->spdif_formats)) {
5268 runtime->hw.rates &= mout->spdif_rates;
5269 runtime->hw.formats &= mout->spdif_formats;
5270 if (mout->spdif_maxbps < hinfo->maxbps)
5271 hinfo->maxbps = mout->spdif_maxbps;
5272 } else {
5273 mout->share_spdif = 0;
5274 /* FIXME: need notify? */
5275 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005276 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005277 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5280 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5281}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005282EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Takashi Iwaid5191e52009-11-16 14:58:17 +01005284/**
5285 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5286 *
5287 * Set up the i/o for analog out.
5288 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005290int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5291 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 unsigned int stream_tag,
5293 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005294 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
Takashi Iwaidda14412011-05-02 11:29:30 +02005296 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005298 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 int i;
5300
Ingo Molnar62932df2006-01-16 16:34:20 +01005301 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005302 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005303 if (mout->dig_out_nid && mout->share_spdif &&
5304 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005306 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5307 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005308 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005310 setup_dig_out_stream(codec, mout->dig_out_nid,
5311 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 } else {
5313 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005314 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
5316 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005317 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
5319 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005320 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5321 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005322 if (!mout->no_share_stream &&
5323 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005325 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5326 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005327 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005328 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5329 if (!mout->no_share_stream && mout->hp_out_nid[i])
5330 snd_hda_codec_setup_stream(codec,
5331 mout->hp_out_nid[i],
5332 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005333 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005334 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005335 snd_hda_codec_setup_stream(codec,
5336 mout->extra_out_nid[i],
5337 stream_tag, 0, format);
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 /* surrounds */
5340 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005341 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005342 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5343 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005344 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005345 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5346 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 }
5348 return 0;
5349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005350EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Takashi Iwaid5191e52009-11-16 14:58:17 +01005352/**
5353 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005355int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5356 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
Takashi Iwaidda14412011-05-02 11:29:30 +02005358 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 int i;
5360
5361 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005362 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005364 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005365 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5366 if (mout->hp_out_nid[i])
5367 snd_hda_codec_cleanup_stream(codec,
5368 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005369 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5370 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005371 snd_hda_codec_cleanup_stream(codec,
5372 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005373 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005375 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 mout->dig_out_used = 0;
5377 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005378 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return 0;
5380}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005381EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Takashi Iwai47408602012-04-20 13:06:53 +02005383/**
5384 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5385 *
5386 * Guess the suitable VREF pin bits to be set as the pin-control value.
5387 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5388 */
5389unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5390{
5391 unsigned int pincap;
5392 unsigned int oldval;
5393 oldval = snd_hda_codec_read(codec, pin, 0,
5394 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5395 pincap = snd_hda_query_pin_caps(codec, pin);
5396 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5397 /* Exception: if the default pin setup is vref50, we give it priority */
5398 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5399 return AC_PINCTL_VREF_80;
5400 else if (pincap & AC_PINCAP_VREF_50)
5401 return AC_PINCTL_VREF_50;
5402 else if (pincap & AC_PINCAP_VREF_100)
5403 return AC_PINCTL_VREF_100;
5404 else if (pincap & AC_PINCAP_VREF_GRD)
5405 return AC_PINCTL_VREF_GRD;
5406 return AC_PINCTL_VREF_HIZ;
5407}
5408EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5409
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005410/* correct the pin ctl value for matching with the pin cap */
5411unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5412 hda_nid_t pin, unsigned int val)
5413{
5414 static unsigned int cap_lists[][2] = {
5415 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5416 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5417 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5418 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5419 };
5420 unsigned int cap;
5421
5422 if (!val)
5423 return 0;
5424 cap = snd_hda_query_pin_caps(codec, pin);
5425 if (!cap)
5426 return val; /* don't know what to do... */
5427
5428 if (val & AC_PINCTL_OUT_EN) {
5429 if (!(cap & AC_PINCAP_OUT))
5430 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5431 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5432 val &= ~AC_PINCTL_HP_EN;
5433 }
5434
5435 if (val & AC_PINCTL_IN_EN) {
5436 if (!(cap & AC_PINCAP_IN))
5437 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5438 else {
5439 unsigned int vcap, vref;
5440 int i;
5441 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5442 vref = val & AC_PINCTL_VREFEN;
5443 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5444 if (vref == cap_lists[i][0] &&
5445 !(vcap & cap_lists[i][1])) {
5446 if (i == ARRAY_SIZE(cap_lists) - 1)
5447 vref = AC_PINCTL_VREF_HIZ;
5448 else
5449 vref = cap_lists[i + 1][0];
5450 }
5451 }
5452 val &= ~AC_PINCTL_VREFEN;
5453 val |= vref;
5454 }
5455 }
5456
5457 return val;
5458}
5459EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5460
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005461int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5462 unsigned int val, bool cached)
5463{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005464 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005465 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005466 if (cached)
5467 return snd_hda_codec_update_cache(codec, pin, 0,
5468 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5469 else
5470 return snd_hda_codec_write(codec, pin, 0,
5471 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5472}
5473EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5474
Takashi Iwai990061c2010-09-09 22:08:44 +02005475/**
5476 * snd_hda_add_imux_item - Add an item to input_mux
5477 *
5478 * When the same label is used already in the existing items, the number
5479 * suffix is appended to the label. This label index number is stored
5480 * to type_idx when non-NULL pointer is given.
5481 */
Takashi Iwai10a20af72010-09-09 16:28:02 +02005482int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5483 int index, int *type_idx)
5484{
5485 int i, label_idx = 0;
5486 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5487 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5488 return -EINVAL;
5489 }
5490 for (i = 0; i < imux->num_items; i++) {
5491 if (!strncmp(label, imux->items[i].label, strlen(label)))
5492 label_idx++;
5493 }
5494 if (type_idx)
5495 *type_idx = label_idx;
5496 if (label_idx > 0)
5497 snprintf(imux->items[imux->num_items].label,
5498 sizeof(imux->items[imux->num_items].label),
5499 "%s %d", label, label_idx);
5500 else
5501 strlcpy(imux->items[imux->num_items].label, label,
5502 sizeof(imux->items[imux->num_items].label));
5503 imux->items[imux->num_items].index = index;
5504 imux->num_items++;
5505 return 0;
5506}
5507EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005508
Takashi Iwai4a471b72005-12-07 13:56:29 +01005509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510#ifdef CONFIG_PM
5511/*
5512 * power management
5513 */
5514
5515/**
5516 * snd_hda_suspend - suspend the codecs
5517 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 *
5519 * Returns 0 if successful.
5520 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005521int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005523 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Takashi Iwai0ba21762007-04-16 11:29:14 +02005525 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005526 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005527 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005528 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
5530 return 0;
5531}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005532EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
5534/**
5535 * snd_hda_resume - resume the codecs
5536 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 *
5538 * Returns 0 if successful.
5539 */
5540int snd_hda_resume(struct hda_bus *bus)
5541{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005542 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Takashi Iwai0ba21762007-04-16 11:29:14 +02005544 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005545 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 return 0;
5548}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005549EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005550#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005551
5552/*
5553 * generic arrays
5554 */
5555
Takashi Iwaid5191e52009-11-16 14:58:17 +01005556/**
5557 * snd_array_new - get a new element from the given array
5558 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005559 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005560 * Get a new element from the given array. If it exceeds the
5561 * pre-allocated array size, re-allocate the array.
5562 *
5563 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005564 */
5565void *snd_array_new(struct snd_array *array)
5566{
Takashi Iwai12f17712012-10-10 08:59:14 +02005567 if (snd_BUG_ON(!array->elem_size))
5568 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005569 if (array->used >= array->alloced) {
5570 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005571 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005572 void *nlist;
5573 if (snd_BUG_ON(num >= 4096))
5574 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005575 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005576 if (!nlist)
5577 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005578 array->list = nlist;
5579 array->alloced = num;
5580 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005581 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005582}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005583EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005584
Takashi Iwaid5191e52009-11-16 14:58:17 +01005585/**
5586 * snd_array_free - free the given array elements
5587 * @array: the array object
5588 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005589void snd_array_free(struct snd_array *array)
5590{
5591 kfree(array->list);
5592 array->used = 0;
5593 array->alloced = 0;
5594 array->list = NULL;
5595}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005596EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005597
Takashi Iwaid5191e52009-11-16 14:58:17 +01005598/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005599 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5600 * @pcm: PCM caps bits
5601 * @buf: the string buffer to write
5602 * @buflen: the max buffer length
5603 *
5604 * used by hda_proc.c and hda_eld.c
5605 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005606void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5607{
5608 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5609 int i, j;
5610
5611 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5612 if (pcm & (AC_SUPPCM_BITS_8 << i))
5613 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5614
5615 buf[j] = '\0'; /* necessary when j == 0 */
5616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005617EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005618
5619MODULE_DESCRIPTION("HDA codec core");
5620MODULE_LICENSE("GPL");