blob: 8f7295bdccbd1ed5796f2047bbec3888c05f0c7d [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 Iwaia40e0a82013-11-20 12:41:20 +0100102
103static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700104{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100105 struct hda_bus *bus = codec->bus;
106
107 if ((power_up && codec->pm_up_notified) ||
108 (!power_up && !codec->pm_up_notified))
109 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700110 if (bus->ops.pm_notify)
111 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100112 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700113}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100114
Takashi Iwaicb53c622007-08-10 17:21:45 +0200115#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100116#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200117static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200118#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100119#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200120#endif
121
Takashi Iwaid5191e52009-11-16 14:58:17 +0100122/**
123 * snd_hda_get_jack_location - Give a location string of the jack
124 * @cfg: pin default config value
125 *
126 * Parse the pin default config value and returns the string of the
127 * jack location, e.g. "Rear", "Front", etc.
128 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400129const char *snd_hda_get_jack_location(u32 cfg)
130{
131 static char *bases[7] = {
132 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
133 };
134 static unsigned char specials_idx[] = {
135 0x07, 0x08,
136 0x17, 0x18, 0x19,
137 0x37, 0x38
138 };
139 static char *specials[] = {
140 "Rear Panel", "Drive Bar",
141 "Riser", "HDMI", "ATAPI",
142 "Mobile-In", "Mobile-Out"
143 };
144 int i;
145 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
146 if ((cfg & 0x0f) < 7)
147 return bases[cfg & 0x0f];
148 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
149 if (cfg == specials_idx[i])
150 return specials[i];
151 }
152 return "UNKNOWN";
153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100154EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155
Takashi Iwaid5191e52009-11-16 14:58:17 +0100156/**
157 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
158 * @cfg: pin default config value
159 *
160 * Parse the pin default config value and returns the string of the
161 * jack connectivity, i.e. external or internal connection.
162 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400163const char *snd_hda_get_jack_connectivity(u32 cfg)
164{
165 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
166
167 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
168}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100169EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170
Takashi Iwaid5191e52009-11-16 14:58:17 +0100171/**
172 * snd_hda_get_jack_type - Give a type string of the jack
173 * @cfg: pin default config value
174 *
175 * Parse the pin default config value and returns the string of the
176 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
177 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400178const char *snd_hda_get_jack_type(u32 cfg)
179{
180 static char *jack_types[16] = {
181 "Line Out", "Speaker", "HP Out", "CD",
182 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
183 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200184 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400185 };
186
187 return jack_types[(cfg & AC_DEFCFG_DEVICE)
188 >> AC_DEFCFG_DEVICE_SHIFT];
189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100190EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400191
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100192/*
193 * Compose a 32bit command word to be sent to the HD-audio controller
194 */
195static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200196make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100197 unsigned int verb, unsigned int parm)
198{
199 u32 val;
200
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200201 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200202 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200203 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
204 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800205 return ~0;
206 }
207
208 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100209 val |= (u32)nid << 20;
210 val |= verb << 8;
211 val |= parm;
212 return val;
213}
214
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215/*
216 * Send and receive a verb
217 */
218static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200219 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220{
221 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223
Wu Fengguang6430aee2009-07-17 16:49:19 +0800224 if (cmd == ~0)
225 return -1;
226
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200227 if (res)
228 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200229 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200230 snd_hda_power_up(codec);
231 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200232 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
233 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100234 for (;;) {
235 trace_hda_send_cmd(codec, cmd);
236 err = bus->ops.command(bus, cmd);
237 if (err != -EAGAIN)
238 break;
239 /* process pending verbs */
240 bus->ops.get_response(bus, codec->addr);
241 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200242 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800243 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200244 trace_hda_get_response(codec, *res);
245 }
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200246 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200247 mutex_unlock(&bus->cmd_mutex);
248 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 if (bus->response_reset) {
251 snd_printd("hda_codec: resetting BUS due to "
252 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200253 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200254 bus->ops.bus_reset(bus);
255 }
256 goto again;
257 }
258 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100259 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200260 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200261 return err;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/**
265 * snd_hda_codec_read - send a command and get the response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200268 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * @verb: the verb to send
270 * @parm: the parameter for the verb
271 *
272 * Send a single command and read the corresponding response.
273 *
274 * Returns the obtained response value, or -1 for an error.
275 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200276unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200277 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned int verb, unsigned int parm)
279{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200280 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200281 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200282 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700283 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return res;
285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100286EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
289 * snd_hda_codec_write - send a single command without waiting for response
290 * @codec: the HDA codec
291 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200292 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * @verb: the verb to send
294 * @parm: the parameter for the verb
295 *
296 * Send a single command without waiting for response.
297 *
298 * Returns 0 if successful, or a negative error code.
299 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200300int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
301 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200303 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100304 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200305 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200306 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100308EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310/**
311 * snd_hda_sequence_write - sequence writes
312 * @codec: the HDA codec
313 * @seq: VERB array to send
314 *
315 * Send the commands sequentially from the given array.
316 * The array must be terminated with NID=0.
317 */
318void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
319{
320 for (; seq->nid; seq++)
321 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
322}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100323EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/**
326 * snd_hda_get_sub_nodes - get the range of sub nodes
327 * @codec: the HDA codec
328 * @nid: NID to parse
329 * @start_id: the pointer to store the start NID
330 *
331 * Parse the NID and store the start NID of its sub-nodes.
332 * Returns the number of sub-nodes.
333 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200334int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
335 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 unsigned int parm;
338
339 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200340 if (parm == -1)
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *start_id = (parm >> 16) & 0x7fff;
343 return (int)(parm & 0x7fff);
344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100345EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100347/* connection list element */
348struct hda_conn_list {
349 struct list_head list;
350 int len;
351 hda_nid_t nid;
352 hda_nid_t conns[0];
353};
354
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100356static struct hda_conn_list *
357lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200358{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359 struct hda_conn_list *p;
360 list_for_each_entry(p, &codec->conn_list, list) {
361 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200362 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 }
364 return NULL;
365}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200366
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100367static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
368 const hda_nid_t *list)
369{
370 struct hda_conn_list *p;
371
372 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375 p->len = len;
376 p->nid = nid;
377 memcpy(p->conns, list, len * sizeof(hda_nid_t));
378 list_add(&p->list, &codec->conn_list);
379 return 0;
380}
381
382static void remove_conn_list(struct hda_codec *codec)
383{
384 while (!list_empty(&codec->conn_list)) {
385 struct hda_conn_list *p;
386 p = list_first_entry(&codec->conn_list, typeof(*p), list);
387 list_del(&p->list);
388 kfree(p);
389 }
390}
391
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200392/* read the connection and add to the cache */
393static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200394{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100395 hda_nid_t list[32];
396 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200397 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200398
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200399 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100400 if (len == -ENOSPC) {
401 len = snd_hda_get_num_raw_conns(codec, nid);
402 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
403 if (!result)
404 return -ENOMEM;
405 len = snd_hda_get_raw_connections(codec, nid, result, len);
406 }
407 if (len >= 0)
408 len = snd_hda_override_conn_list(codec, nid, len, result);
409 if (result != list)
410 kfree(result);
411 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200412}
Takashi Iwaidce20792011-06-24 14:10:28 +0200413
414/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100415 * snd_hda_get_conn_list - get connection list
416 * @codec: the HDA codec
417 * @nid: NID to parse
418 * @len: number of connection list entries
419 * @listp: the pointer to store NID list
420 *
421 * Parses the connection list of the given widget and stores the pointer
422 * to the list of NIDs.
423 *
424 * Returns the number of connections, or a negative error code.
425 *
426 * Note that the returned pointer isn't protected against the list
427 * modification. If snd_hda_override_conn_list() might be called
428 * concurrently, protect with a mutex appropriately.
429 */
430int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
431 const hda_nid_t **listp)
432{
433 bool added = false;
434
435 for (;;) {
436 int err;
437 const struct hda_conn_list *p;
438
439 /* if the connection-list is already cached, read it */
440 p = lookup_conn_list(codec, nid);
441 if (p) {
442 if (listp)
443 *listp = p->conns;
444 return p->len;
445 }
446 if (snd_BUG_ON(added))
447 return -EINVAL;
448
449 err = read_and_add_raw_conns(codec, nid);
450 if (err < 0)
451 return err;
452 added = true;
453 }
454}
455EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
456
457/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200458 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * @codec: the HDA codec
460 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200461 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * @max_conns: max. number of connections to store
463 *
464 * Parses the connection list of the given widget and stores the list
465 * of NIDs.
466 *
467 * Returns the number of connections, or a negative error code.
468 */
469int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200471{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 const hda_nid_t *list;
473 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 if (len > 0 && conn_list) {
476 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200477 snd_printk(KERN_ERR "hda_codec: "
478 "Too many connections %d for NID 0x%x\n",
479 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200480 return -EINVAL;
481 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100482 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200483 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200484
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100485 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200486}
487EXPORT_SYMBOL_HDA(snd_hda_get_connections);
488
Takashi Iwai4eea3092013-02-07 18:18:19 +0100489/* return CONNLIST_LEN parameter of the given widget */
490static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
491{
492 unsigned int wcaps = get_wcaps(codec, nid);
493 unsigned int parm;
494
495 if (!(wcaps & AC_WCAP_CONN_LIST) &&
496 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
497 return 0;
498
499 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
500 if (parm == -1)
501 parm = 0;
502 return parm;
503}
504
505int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
506{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100507 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100508}
509
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200510/**
511 * snd_hda_get_raw_connections - copy connection list without cache
512 * @codec: the HDA codec
513 * @nid: NID to parse
514 * @conn_list: connection list array
515 * @max_conns: max. number of connections to store
516 *
517 * Like snd_hda_get_connections(), copy the connection list but without
518 * checking through the connection-list cache.
519 * Currently called only from hda_proc.c, so not exported.
520 */
521int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
522 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100525 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100527 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100528 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Takashi Iwai4eea3092013-02-07 18:18:19 +0100530 parm = get_num_conns(codec, nid);
531 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200532 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (parm & AC_CLIST_LONG) {
535 /* long form */
536 shift = 16;
537 num_elems = 2;
538 } else {
539 /* short form */
540 shift = 8;
541 num_elems = 4;
542 }
543 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mask = (1 << (shift-1)) - 1;
545
Takashi Iwai0ba21762007-04-16 11:29:14 +0200546 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0; /* no connection */
548
549 if (conn_len == 1) {
550 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200551 parm = snd_hda_codec_read(codec, nid, 0,
552 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200553 if (parm == -1 && codec->bus->rirb_error)
554 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100555 if (conn_list)
556 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 1;
558 }
559
560 /* multi connection */
561 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100562 prev_nid = 0;
563 for (i = 0; i < conn_len; i++) {
564 int range_val;
565 hda_nid_t val, n;
566
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200567 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100568 parm = snd_hda_codec_read(codec, nid, 0,
569 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200570 if (parm == -1 && codec->bus->rirb_error)
571 return -EIO;
572 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200573 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100574 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100575 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4758fed2013-10-17 17:56:25 +0200576 snd_printdd("hda_codec: "
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200577 "invalid CONNECT_LIST verb %x[%i]:%x\n",
578 nid, i, parm);
579 return 0;
580 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100581 parm >>= shift;
582 if (range_val) {
583 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200584 if (!prev_nid || prev_nid >= val) {
585 snd_printk(KERN_WARNING "hda_codec: "
586 "invalid dep_range_val %x:%x\n",
587 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100591 if (conn_list) {
592 if (conns >= max_conns)
593 return -ENOSPC;
594 conn_list[conns] = n;
595 }
596 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 }
598 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100599 if (conn_list) {
600 if (conns >= max_conns)
601 return -ENOSPC;
602 conn_list[conns] = val;
603 }
604 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100606 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 return conns;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200612 * snd_hda_override_conn_list - add/modify the connection-list to cache
613 * @codec: the HDA codec
614 * @nid: NID to parse
615 * @len: number of connection list entries
616 * @list: the list of connection entries
617 *
618 * Add or modify the given connection-list to the cache. If the corresponding
619 * cache already exists, invalidate it and append a new one.
620 *
621 * Returns zero or a negative error code.
622 */
623int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
624 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100626 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200627
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100628 p = lookup_conn_list(codec, nid);
629 if (p) {
630 list_del(&p->list);
631 kfree(p);
632 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200633
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100634 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200636EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
637
638/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 * snd_hda_get_conn_index - get the connection index of the given NID
640 * @codec: the HDA codec
641 * @mux: NID containing the list
642 * @nid: NID to select
643 * @recursive: 1 when searching NID recursively, otherwise 0
644 *
645 * Parses the connection list of the widget @mux and checks whether the
646 * widget @nid is present. If it is, return the connection index.
647 * Otherwise it returns -1.
648 */
649int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
650 hda_nid_t nid, int recursive)
651{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100652 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200653 int i, nums;
654
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100655 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200656 for (i = 0; i < nums; i++)
657 if (conn[i] == nid)
658 return i;
659 if (!recursive)
660 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100661 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
663 return -1;
664 }
665 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200666 for (i = 0; i < nums; i++) {
667 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
668 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
669 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200670 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
671 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200672 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200673 return -1;
674}
675EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Mengdong Linf1aa0682013-08-26 21:35:21 -0400677
678/* return DEVLIST_LEN parameter of the given widget */
679static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
680{
681 unsigned int wcaps = get_wcaps(codec, nid);
682 unsigned int parm;
683
684 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
685 get_wcaps_type(wcaps) != AC_WID_PIN)
686 return 0;
687
688 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
689 if (parm == -1 && codec->bus->rirb_error)
690 parm = 0;
691 return parm & AC_DEV_LIST_LEN_MASK;
692}
693
694/**
695 * snd_hda_get_devices - copy device list without cache
696 * @codec: the HDA codec
697 * @nid: NID of the pin to parse
698 * @dev_list: device list array
699 * @max_devices: max. number of devices to store
700 *
701 * Copy the device list. This info is dynamic and so not cached.
702 * Currently called only from hda_proc.c, so not exported.
703 */
704int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
705 u8 *dev_list, int max_devices)
706{
707 unsigned int parm;
708 int i, dev_len, devices;
709
710 parm = get_num_devices(codec, nid);
711 if (!parm) /* not multi-stream capable */
712 return 0;
713
714 dev_len = parm + 1;
715 dev_len = dev_len < max_devices ? dev_len : max_devices;
716
717 devices = 0;
718 while (devices < dev_len) {
719 parm = snd_hda_codec_read(codec, nid, 0,
720 AC_VERB_GET_DEVICE_LIST, devices);
721 if (parm == -1 && codec->bus->rirb_error)
722 break;
723
724 for (i = 0; i < 8; i++) {
725 dev_list[devices] = (u8)parm;
726 parm >>= 4;
727 devices++;
728 if (devices >= dev_len)
729 break;
730 }
731 }
732 return devices;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/**
736 * snd_hda_queue_unsol_event - add an unsolicited event to queue
737 * @bus: the BUS
738 * @res: unsolicited event (lower 32bit of RIRB entry)
739 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
740 *
741 * Adds the given event to the queue. The events are processed in
742 * the workqueue asynchronously. Call this function in the interrupt
743 * hanlder when RIRB receives an unsolicited event.
744 *
745 * Returns 0 if successful, or a negative error code.
746 */
747int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
748{
749 struct hda_bus_unsolicited *unsol;
750 unsigned int wp;
751
Wang YanQing2195b062013-05-07 11:27:33 +0800752 if (!bus || !bus->workq)
753 return 0;
754
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200755 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 unsol = bus->unsol;
757 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759
760 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
761 unsol->wp = wp;
762
763 wp <<= 1;
764 unsol->queue[wp] = res;
765 unsol->queue[wp + 1] = res_ex;
766
Takashi Iwai6acaed32009-01-12 10:09:24 +0100767 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 return 0;
770}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100771EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800774 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
David Howellsc4028952006-11-22 14:57:56 +0000776static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
David Howellsc4028952006-11-22 14:57:56 +0000778 struct hda_bus_unsolicited *unsol =
779 container_of(work, struct hda_bus_unsolicited, work);
780 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct hda_codec *codec;
782 unsigned int rp, caddr, res;
783
784 while (unsol->rp != unsol->wp) {
785 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
786 unsol->rp = rp;
787 rp <<= 1;
788 res = unsol->queue[rp];
789 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200790 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 continue;
792 codec = bus->caddr_tbl[caddr & 0x0f];
793 if (codec && codec->patch_ops.unsol_event)
794 codec->patch_ops.unsol_event(codec, res);
795 }
796}
797
798/*
799 * initialize unsolicited queue
800 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200801static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct hda_bus_unsolicited *unsol;
804
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100805 if (bus->unsol) /* already initialized */
806 return 0;
807
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200808 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200809 if (!unsol) {
810 snd_printk(KERN_ERR "hda_codec: "
811 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -ENOMEM;
813 }
David Howellsc4028952006-11-22 14:57:56 +0000814 INIT_WORK(&unsol->work, process_unsol_events);
815 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 bus->unsol = unsol;
817 return 0;
818}
819
820/*
821 * destructor
822 */
823static void snd_hda_codec_free(struct hda_codec *codec);
824
825static int snd_hda_bus_free(struct hda_bus *bus)
826{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200827 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Takashi Iwai0ba21762007-04-16 11:29:14 +0200829 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100831 if (bus->workq)
832 flush_workqueue(bus->workq);
833 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 snd_hda_codec_free(codec);
837 }
838 if (bus->ops.private_free)
839 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100840 if (bus->workq)
841 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 kfree(bus);
843 return 0;
844}
845
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100846static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100849 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return snd_hda_bus_free(bus);
851}
852
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200853#ifdef CONFIG_SND_HDA_HWDEP
854static int snd_hda_bus_dev_register(struct snd_device *device)
855{
856 struct hda_bus *bus = device->device_data;
857 struct hda_codec *codec;
858 list_for_each_entry(codec, &bus->codec_list, list) {
859 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100860 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200861 }
862 return 0;
863}
864#else
865#define snd_hda_bus_dev_register NULL
866#endif
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
869 * snd_hda_bus_new - create a HDA bus
870 * @card: the card entry
871 * @temp: the template for hda_bus information
872 * @busp: the pointer to store the created bus instance
873 *
874 * Returns 0 if successful, or a negative error code.
875 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100876int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200877 const struct hda_bus_template *temp,
878 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct hda_bus *bus;
881 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100882 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200883 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 .dev_free = snd_hda_bus_dev_free,
885 };
886
Takashi Iwaida3cec32008-08-08 17:12:14 +0200887 if (snd_BUG_ON(!temp))
888 return -EINVAL;
889 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (busp)
893 *busp = NULL;
894
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200895 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (bus == NULL) {
897 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
898 return -ENOMEM;
899 }
900
901 bus->card = card;
902 bus->private_data = temp->private_data;
903 bus->pci = temp->pci;
904 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100905 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 bus->ops = temp->ops;
907
Ingo Molnar62932df2006-01-16 16:34:20 +0100908 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200909 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 INIT_LIST_HEAD(&bus->codec_list);
911
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100912 snprintf(bus->workq_name, sizeof(bus->workq_name),
913 "hd-audio%d", card->number);
914 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100915 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100916 snd_printk(KERN_ERR "cannot create workqueue %s\n",
917 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100918 kfree(bus);
919 return -ENOMEM;
920 }
921
Takashi Iwai0ba21762007-04-16 11:29:14 +0200922 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
923 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 snd_hda_bus_free(bus);
925 return err;
926 }
927 if (busp)
928 *busp = bus;
929 return 0;
930}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100931EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Takashi Iwai82467612007-07-27 19:15:54 +0200933#ifdef CONFIG_SND_HDA_GENERIC
934#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200935 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200936#else
937#define is_generic_config(codec) 0
938#endif
939
Takashi Iwai645f10c2008-11-28 15:07:37 +0100940#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100941#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
942#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100943#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100944#endif
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * find a matching codec preset
948 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200949static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200950find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100952 struct hda_codec_preset_list *tbl;
953 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200954 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100956 again:
957 mutex_lock(&preset_mutex);
958 list_for_each_entry(tbl, &hda_preset_tables, list) {
959 if (!try_module_get(tbl->owner)) {
960 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
961 continue;
962 }
963 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100965 if (preset->afg && preset->afg != codec->afg)
966 continue;
967 if (preset->mfg && preset->mfg != codec->mfg)
968 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200969 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200971 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200972 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100973 preset->rev == codec->revision_id)) {
974 mutex_unlock(&preset_mutex);
975 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100979 module_put(tbl->owner);
980 }
981 mutex_unlock(&preset_mutex);
982
983 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
984 char name[32];
985 if (!mod_requested)
986 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
987 codec->vendor_id);
988 else
989 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
990 (codec->vendor_id >> 16) & 0xffff);
991 request_module(name);
992 mod_requested++;
993 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 return NULL;
996}
997
998/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200999 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Takashi Iwaif44ac832008-07-30 15:01:45 +02001001static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 const struct hda_vendor_id *c;
1004 const char *vendor = NULL;
1005 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001006 char tmp[16];
1007
1008 if (codec->vendor_name)
1009 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 for (c = hda_vendor_ids; c->id; c++) {
1012 if (c->id == vendor_id) {
1013 vendor = c->name;
1014 break;
1015 }
1016 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001017 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sprintf(tmp, "Generic %04x", vendor_id);
1019 vendor = tmp;
1020 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001021 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1022 if (!codec->vendor_name)
1023 return -ENOMEM;
1024
1025 get_chip_name:
1026 if (codec->chip_name)
1027 return 0;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001030 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1031 else {
1032 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1033 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1034 }
1035 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001036 return -ENOMEM;
1037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001041 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001043static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001045 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 hda_nid_t nid;
1047
1048 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1049 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001050 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001051 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001052 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001053 case AC_GRP_AUDIO_FUNCTION:
1054 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001055 codec->afg_function_id = function_id & 0xff;
1056 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001057 break;
1058 case AC_GRP_MODEM_FUNCTION:
1059 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001060 codec->mfg_function_id = function_id & 0xff;
1061 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001062 break;
1063 default:
1064 break;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001070 * read widget caps for each widget and store in cache
1071 */
1072static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1073{
1074 int i;
1075 hda_nid_t nid;
1076
1077 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1078 &codec->start_nid);
1079 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001080 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001081 return -ENOMEM;
1082 nid = codec->start_nid;
1083 for (i = 0; i < codec->num_nodes; i++, nid++)
1084 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1085 AC_PAR_AUDIO_WIDGET_CAP);
1086 return 0;
1087}
1088
Takashi Iwai3be14142009-02-20 14:11:16 +01001089/* read all pin default configurations and save codec->init_pins */
1090static int read_pin_defaults(struct hda_codec *codec)
1091{
1092 int i;
1093 hda_nid_t nid = codec->start_nid;
1094
1095 for (i = 0; i < codec->num_nodes; i++, nid++) {
1096 struct hda_pincfg *pin;
1097 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001098 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001099 if (wid_type != AC_WID_PIN)
1100 continue;
1101 pin = snd_array_new(&codec->init_pins);
1102 if (!pin)
1103 return -ENOMEM;
1104 pin->nid = nid;
1105 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1106 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001107 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1108 AC_VERB_GET_PIN_WIDGET_CONTROL,
1109 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001110 }
1111 return 0;
1112}
1113
1114/* look up the given pin config list and return the item matching with NID */
1115static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1116 struct snd_array *array,
1117 hda_nid_t nid)
1118{
1119 int i;
1120 for (i = 0; i < array->used; i++) {
1121 struct hda_pincfg *pin = snd_array_elem(array, i);
1122 if (pin->nid == nid)
1123 return pin;
1124 }
1125 return NULL;
1126}
1127
Takashi Iwai3be14142009-02-20 14:11:16 +01001128/* set the current pin config value for the given NID.
1129 * the value is cached, and read via snd_hda_codec_get_pincfg()
1130 */
1131int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1132 hda_nid_t nid, unsigned int cfg)
1133{
1134 struct hda_pincfg *pin;
1135
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001136 /* the check below may be invalid when pins are added by a fixup
1137 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1138 * for now
1139 */
1140 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001141 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1142 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001143 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001144
Takashi Iwai3be14142009-02-20 14:11:16 +01001145 pin = look_up_pincfg(codec, list, nid);
1146 if (!pin) {
1147 pin = snd_array_new(list);
1148 if (!pin)
1149 return -ENOMEM;
1150 pin->nid = nid;
1151 }
1152 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001153 return 0;
1154}
1155
Takashi Iwaid5191e52009-11-16 14:58:17 +01001156/**
1157 * snd_hda_codec_set_pincfg - Override a pin default configuration
1158 * @codec: the HDA codec
1159 * @nid: NID to set the pin config
1160 * @cfg: the pin default config value
1161 *
1162 * Override a pin default configuration value in the cache.
1163 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1164 * priority than the real hardware value.
1165 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001166int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1167 hda_nid_t nid, unsigned int cfg)
1168{
Takashi Iwai346ff702009-02-23 09:42:57 +01001169 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001170}
1171EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1172
Takashi Iwaid5191e52009-11-16 14:58:17 +01001173/**
1174 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1175 * @codec: the HDA codec
1176 * @nid: NID to get the pin config
1177 *
1178 * Get the current pin config value of the given pin NID.
1179 * If the pincfg value is cached or overridden via sysfs or driver,
1180 * returns the cached value.
1181 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001182unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1183{
1184 struct hda_pincfg *pin;
1185
Takashi Iwai3be14142009-02-20 14:11:16 +01001186#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001187 {
1188 unsigned int cfg = 0;
1189 mutex_lock(&codec->user_mutex);
1190 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1191 if (pin)
1192 cfg = pin->cfg;
1193 mutex_unlock(&codec->user_mutex);
1194 if (cfg)
1195 return cfg;
1196 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001197#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001198 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1199 if (pin)
1200 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001201 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1202 if (pin)
1203 return pin->cfg;
1204 return 0;
1205}
1206EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1207
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001208/* remember the current pinctl target value */
1209int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1210 unsigned int val)
1211{
1212 struct hda_pincfg *pin;
1213
1214 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1215 if (!pin)
1216 return -EINVAL;
1217 pin->target = val;
1218 return 0;
1219}
1220EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1221
1222/* return the current pinctl target value */
1223int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1224{
1225 struct hda_pincfg *pin;
1226
1227 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1228 if (!pin)
1229 return 0;
1230 return pin->target;
1231}
1232EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1233
Takashi Iwai92ee6162009-12-27 11:18:59 +01001234/**
1235 * snd_hda_shutup_pins - Shut up all pins
1236 * @codec: the HDA codec
1237 *
1238 * Clear all pin controls to shup up before suspend for avoiding click noise.
1239 * The controls aren't cached so that they can be resumed properly.
1240 */
1241void snd_hda_shutup_pins(struct hda_codec *codec)
1242{
1243 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001244 /* don't shut up pins when unloading the driver; otherwise it breaks
1245 * the default pin setup at the next load of the driver
1246 */
1247 if (codec->bus->shutdown)
1248 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001249 for (i = 0; i < codec->init_pins.used; i++) {
1250 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1251 /* use read here for syncing after issuing each verb */
1252 snd_hda_codec_read(codec, pin->nid, 0,
1253 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1254 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001255 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001256}
1257EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1258
Takashi Iwai2a439522011-07-26 09:52:50 +02001259#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001260/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1261static void restore_shutup_pins(struct hda_codec *codec)
1262{
1263 int i;
1264 if (!codec->pins_shutup)
1265 return;
1266 if (codec->bus->shutdown)
1267 return;
1268 for (i = 0; i < codec->init_pins.used; i++) {
1269 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1270 snd_hda_codec_write(codec, pin->nid, 0,
1271 AC_VERB_SET_PIN_WIDGET_CONTROL,
1272 pin->ctrl);
1273 }
1274 codec->pins_shutup = 0;
1275}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001276#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001277
David Henningsson26a6cb62012-10-09 15:04:21 +02001278static void hda_jackpoll_work(struct work_struct *work)
1279{
1280 struct hda_codec *codec =
1281 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001282
1283 snd_hda_jack_set_dirty_all(codec);
1284 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001285
1286 if (!codec->jackpoll_interval)
1287 return;
1288
David Henningsson26a6cb62012-10-09 15:04:21 +02001289 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1290 codec->jackpoll_interval);
1291}
1292
Takashi Iwai01751f52007-08-10 16:59:39 +02001293static void init_hda_cache(struct hda_cache_rec *cache,
1294 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001295static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001296
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001297/* release all pincfg lists */
1298static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001299{
Takashi Iwai346ff702009-02-23 09:42:57 +01001300 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001301#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001302 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001303#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 snd_array_free(&codec->init_pins);
1305}
1306
Takashi Iwai54d17402005-11-21 16:33:22 +01001307/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001308 * audio-converter setup caches
1309 */
1310struct hda_cvt_setup {
1311 hda_nid_t nid;
1312 u8 stream_tag;
1313 u8 channel_id;
1314 u16 format_id;
1315 unsigned char active; /* cvt is currently used */
1316 unsigned char dirty; /* setups should be cleared */
1317};
1318
1319/* get or create a cache entry for the given audio converter NID */
1320static struct hda_cvt_setup *
1321get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1322{
1323 struct hda_cvt_setup *p;
1324 int i;
1325
1326 for (i = 0; i < codec->cvt_setups.used; i++) {
1327 p = snd_array_elem(&codec->cvt_setups, i);
1328 if (p->nid == nid)
1329 return p;
1330 }
1331 p = snd_array_new(&codec->cvt_setups);
1332 if (p)
1333 p->nid = nid;
1334 return p;
1335}
1336
1337/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001338 * Dynamic symbol binding for the codec parsers
1339 */
1340#ifdef MODULE
1341#define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym))
1342#define unload_parser_addr(addr) symbol_put_addr(addr)
1343#else
1344#define load_parser_sym(sym) (sym)
1345#define unload_parser_addr(addr) do {} while (0)
1346#endif
1347
1348#define load_parser(codec, sym) \
1349 ((codec)->parser = load_parser_sym(sym))
1350
1351static void unload_parser(struct hda_codec *codec)
1352{
1353 if (codec->parser) {
1354 unload_parser_addr(codec->parser);
1355 codec->parser = NULL;
1356 }
1357}
1358
1359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * codec destructor
1361 */
1362static void snd_hda_codec_free(struct hda_codec *codec)
1363{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001364 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001366 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001367 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001368 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001369#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001370 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001371 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001372#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001374 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001375 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001376 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001377 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001378 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 codec->bus->caddr_tbl[codec->addr] = NULL;
1380 if (codec->patch_ops.free)
1381 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001382 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001383 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001384 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001385 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001386 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001387 kfree(codec->vendor_name);
1388 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001389 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001390 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 kfree(codec);
1392}
1393
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001394static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1395 hda_nid_t fg, unsigned int power_state);
1396
Takashi Iwaid8193872012-08-31 07:54:38 -07001397static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001398 unsigned int power_state);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/**
1401 * snd_hda_codec_new - create a HDA codec
1402 * @bus: the bus to assign
1403 * @codec_addr: the codec address
1404 * @codecp: the pointer to store the generated codec
1405 *
1406 * Returns 0 if successful, or a negative error code.
1407 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001408int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001409 unsigned int codec_addr,
1410 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001413 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001414 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int err;
1416
Takashi Iwaida3cec32008-08-08 17:12:14 +02001417 if (snd_BUG_ON(!bus))
1418 return -EINVAL;
1419 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001423 snd_printk(KERN_ERR "hda_codec: "
1424 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -EBUSY;
1426 }
1427
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001428 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (codec == NULL) {
1430 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1431 return -ENOMEM;
1432 }
1433
1434 codec->bus = bus;
1435 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001436 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001437 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001438 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001439 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001440 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001441 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1442 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001443 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001444 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001445 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001446 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001447 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001448 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001449 INIT_LIST_HEAD(&codec->conn_list);
1450
David Henningsson26a6cb62012-10-09 15:04:21 +02001451 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Takashi Iwai83012a72012-08-24 18:38:08 +02001453#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001454 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001455 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1456 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1457 * the caller has to power down appropriatley after initialization
1458 * phase.
1459 */
1460 hda_keep_power_on(codec);
1461#endif
1462
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001463 if (codec->bus->modelname) {
1464 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1465 if (!codec->modelname) {
1466 snd_hda_codec_free(codec);
1467 return -ENODEV;
1468 }
1469 }
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 list_add_tail(&codec->list, &bus->codec_list);
1472 bus->caddr_tbl[codec_addr] = codec;
1473
Takashi Iwai0ba21762007-04-16 11:29:14 +02001474 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1475 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001476 if (codec->vendor_id == -1)
1477 /* read again, hopefully the access method was corrected
1478 * in the last read...
1479 */
1480 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1481 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001482 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1483 AC_PAR_SUBSYSTEM_ID);
1484 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1485 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001487 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001488 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001489 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001490 err = -ENODEV;
1491 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
Takashi Iwaid8193872012-08-31 07:54:38 -07001494 fg = codec->afg ? codec->afg : codec->mfg;
1495 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001496 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001497 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001498 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001499 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001500 err = read_pin_defaults(codec);
1501 if (err < 0)
1502 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001503
Takashi Iwai0ba21762007-04-16 11:29:14 +02001504 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001505 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001506 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001507 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001508 }
1509
Takashi Iwai83012a72012-08-24 18:38:08 +02001510#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001511 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001512 AC_PWRST_CLKSTOP);
1513 if (!codec->d3_stop_clk)
1514 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001515#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001516 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001517 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001518
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001519 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001520 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001521
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001522 snd_hda_codec_proc_new(codec);
1523
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001524 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001525
1526 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1527 codec->subsystem_id, codec->revision_id);
1528 snd_component_add(codec->bus->card, component);
1529
1530 if (codecp)
1531 *codecp = codec;
1532 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001533
1534 error:
1535 snd_hda_codec_free(codec);
1536 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001537}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001538EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001539
Mengdong Lina15d05d2013-02-08 17:09:31 -05001540int snd_hda_codec_update_widgets(struct hda_codec *codec)
1541{
1542 hda_nid_t fg;
1543 int err;
1544
1545 /* Assume the function group node does not change,
1546 * only the widget nodes may change.
1547 */
1548 kfree(codec->wcaps);
1549 fg = codec->afg ? codec->afg : codec->mfg;
1550 err = read_widget_caps(codec, fg);
1551 if (err < 0) {
1552 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1553 return err;
1554 }
1555
1556 snd_array_free(&codec->init_pins);
1557 err = read_pin_defaults(codec);
1558
1559 return err;
1560}
1561EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1562
1563
Takashi Iwaif0639272013-11-18 12:07:29 +01001564#ifdef CONFIG_SND_HDA_CODEC_HDMI
1565/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1566static bool is_likely_hdmi_codec(struct hda_codec *codec)
1567{
1568 hda_nid_t nid = codec->start_nid;
1569 int i;
1570
1571 for (i = 0; i < codec->num_nodes; i++, nid++) {
1572 unsigned int wcaps = get_wcaps(codec, nid);
1573 switch (get_wcaps_type(wcaps)) {
1574 case AC_WID_AUD_IN:
1575 return false; /* HDMI parser supports only HDMI out */
1576 case AC_WID_AUD_OUT:
1577 if (!(wcaps & AC_WCAP_DIGITAL))
1578 return false;
1579 break;
1580 }
1581 }
1582 return true;
1583}
1584#else
1585/* no HDMI codec parser support */
1586#define is_likely_hdmi_codec(codec) false
1587#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1588
Takashi Iwaid5191e52009-11-16 14:58:17 +01001589/**
1590 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1591 * @codec: the HDA codec
1592 *
1593 * Start parsing of the given codec tree and (re-)initialize the whole
1594 * patch instance.
1595 *
1596 * Returns 0 if successful or a negative error code.
1597 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001598int snd_hda_codec_configure(struct hda_codec *codec)
1599{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001600 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001601 int err;
1602
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001603 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001604 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001605 err = get_codec_name(codec);
1606 if (err < 0)
1607 return err;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001610 if (!is_generic_config(codec) && codec->preset)
1611 patch = codec->preset->patch;
1612 if (!patch) {
1613 unload_parser(codec); /* to be sure */
Takashi Iwaif0639272013-11-18 12:07:29 +01001614 if (is_likely_hdmi_codec(codec))
1615 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001616#ifdef CONFIG_SND_HDA_GENERIC
1617 if (!patch)
1618 patch = load_parser(codec, snd_hda_parse_generic_codec);
1619#endif
1620 if (!patch) {
1621 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1622 return -ENODEV;
1623 }
Takashi Iwai82467612007-07-27 19:15:54 +02001624 }
1625
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001626 err = patch(codec);
1627 if (err < 0) {
1628 unload_parser(codec);
1629 return err;
1630 }
Takashi Iwai82467612007-07-27 19:15:54 +02001631
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001632 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001633 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001634 if (err < 0)
1635 return err;
1636 }
1637
Takashi Iwaif62faed2009-12-23 09:27:51 +01001638 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001639 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001640 snprintf(codec->bus->card->mixername,
1641 sizeof(codec->bus->card->mixername),
1642 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001645EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Takashi Iwaied360812012-08-08 17:12:52 +02001647/* update the stream-id if changed */
1648static void update_pcm_stream_id(struct hda_codec *codec,
1649 struct hda_cvt_setup *p, hda_nid_t nid,
1650 u32 stream_tag, int channel_id)
1651{
1652 unsigned int oldval, newval;
1653
1654 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1655 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1656 newval = (stream_tag << 4) | channel_id;
1657 if (oldval != newval)
1658 snd_hda_codec_write(codec, nid, 0,
1659 AC_VERB_SET_CHANNEL_STREAMID,
1660 newval);
1661 p->stream_tag = stream_tag;
1662 p->channel_id = channel_id;
1663 }
1664}
1665
1666/* update the format-id if changed */
1667static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1668 hda_nid_t nid, int format)
1669{
1670 unsigned int oldval;
1671
1672 if (p->format_id != format) {
1673 oldval = snd_hda_codec_read(codec, nid, 0,
1674 AC_VERB_GET_STREAM_FORMAT, 0);
1675 if (oldval != format) {
1676 msleep(1);
1677 snd_hda_codec_write(codec, nid, 0,
1678 AC_VERB_SET_STREAM_FORMAT,
1679 format);
1680 }
1681 p->format_id = format;
1682 }
1683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/**
1686 * snd_hda_codec_setup_stream - set up the codec for streaming
1687 * @codec: the CODEC to set up
1688 * @nid: the NID to set up
1689 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1690 * @channel_id: channel id to pass, zero based.
1691 * @format: stream format.
1692 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001693void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1694 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int channel_id, int format)
1696{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001697 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001698 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001699 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001700 int i;
1701
Takashi Iwai0ba21762007-04-16 11:29:14 +02001702 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001703 return;
1704
Takashi Iwai0ba21762007-04-16 11:29:14 +02001705 snd_printdd("hda_codec_setup_stream: "
1706 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001708 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001709 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001710 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001711
1712 if (codec->pcm_format_first)
1713 update_pcm_format(codec, p, nid, format);
1714 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1715 if (!codec->pcm_format_first)
1716 update_pcm_format(codec, p, nid, format);
1717
Takashi Iwaieb541332010-08-06 13:48:11 +02001718 p->active = 1;
1719 p->dirty = 0;
1720
1721 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001722 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001723 list_for_each_entry(c, &codec->bus->codec_list, list) {
1724 for (i = 0; i < c->cvt_setups.used; i++) {
1725 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001726 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001727 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001728 p->dirty = 1;
1729 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001732EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Takashi Iwaif0cea792010-08-13 11:56:53 +02001734static void really_cleanup_stream(struct hda_codec *codec,
1735 struct hda_cvt_setup *q);
1736
Takashi Iwaid5191e52009-11-16 14:58:17 +01001737/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001738 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001739 * @codec: the CODEC to clean up
1740 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001741 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001742 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001743void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1744 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001745{
Takashi Iwaieb541332010-08-06 13:48:11 +02001746 struct hda_cvt_setup *p;
1747
Takashi Iwai888afa12008-03-18 09:57:50 +01001748 if (!nid)
1749 return;
1750
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001751 if (codec->no_sticky_stream)
1752 do_now = 1;
1753
Takashi Iwai888afa12008-03-18 09:57:50 +01001754 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001755 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001756 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001757 /* here we just clear the active flag when do_now isn't set;
1758 * actual clean-ups will be done later in
1759 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1760 */
1761 if (do_now)
1762 really_cleanup_stream(codec, p);
1763 else
1764 p->active = 0;
1765 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001766}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001767EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001768
Takashi Iwaieb541332010-08-06 13:48:11 +02001769static void really_cleanup_stream(struct hda_codec *codec,
1770 struct hda_cvt_setup *q)
1771{
1772 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001773 if (q->stream_tag || q->channel_id)
1774 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1775 if (q->format_id)
1776 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1777);
Takashi Iwaieb541332010-08-06 13:48:11 +02001778 memset(q, 0, sizeof(*q));
1779 q->nid = nid;
1780}
1781
1782/* clean up the all conflicting obsolete streams */
1783static void purify_inactive_streams(struct hda_codec *codec)
1784{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001785 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001786 int i;
1787
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001788 list_for_each_entry(c, &codec->bus->codec_list, list) {
1789 for (i = 0; i < c->cvt_setups.used; i++) {
1790 struct hda_cvt_setup *p;
1791 p = snd_array_elem(&c->cvt_setups, i);
1792 if (p->dirty)
1793 really_cleanup_stream(c, p);
1794 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001795 }
1796}
1797
Takashi Iwai2a439522011-07-26 09:52:50 +02001798#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001799/* clean up all streams; called from suspend */
1800static void hda_cleanup_all_streams(struct hda_codec *codec)
1801{
1802 int i;
1803
1804 for (i = 0; i < codec->cvt_setups.used; i++) {
1805 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1806 if (p->stream_tag)
1807 really_cleanup_stream(codec, p);
1808 }
1809}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001810#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
1813 * amp access functions
1814 */
1815
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001816/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001817#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001818#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001819#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1820#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001822#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001825static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001826 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwai01751f52007-08-10 16:59:39 +02001828 memset(cache, 0, sizeof(*cache));
1829 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001830 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001831}
1832
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001833static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001834{
Takashi Iwai603c4012008-07-30 15:01:44 +02001835 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001839static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Takashi Iwai01751f52007-08-10 16:59:39 +02001841 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1842 u16 cur = cache->hash[idx];
1843 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001846 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (info->key == key)
1848 return info;
1849 cur = info->next;
1850 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001851 return NULL;
1852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001854/* query the hash. allocate an entry if not found. */
1855static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1856 u32 key)
1857{
1858 struct hda_cache_head *info = get_hash(cache, key);
1859 if (!info) {
1860 u16 idx, cur;
1861 /* add a new hash entry */
1862 info = snd_array_new(&cache->buf);
1863 if (!info)
1864 return NULL;
1865 cur = snd_array_index(&cache->buf, info);
1866 info->key = key;
1867 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001868 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001869 idx = key % (u16)ARRAY_SIZE(cache->hash);
1870 info->next = cache->hash[idx];
1871 cache->hash[idx] = cur;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return info;
1874}
1875
Takashi Iwai01751f52007-08-10 16:59:39 +02001876/* query and allocate an amp hash entry */
1877static inline struct hda_amp_info *
1878get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1879{
1880 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1881}
1882
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001883/* overwrite the value with the key in the caps hash */
1884static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1885{
1886 struct hda_amp_info *info;
1887
1888 mutex_lock(&codec->hash_mutex);
1889 info = get_alloc_amp_hash(codec, key);
1890 if (!info) {
1891 mutex_unlock(&codec->hash_mutex);
1892 return -EINVAL;
1893 }
1894 info->amp_caps = val;
1895 info->head.val |= INFO_AMP_CAPS;
1896 mutex_unlock(&codec->hash_mutex);
1897 return 0;
1898}
1899
1900/* query the value from the caps hash; if not found, fetch the current
1901 * value from the given function and store in the hash
1902 */
1903static unsigned int
1904query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1905 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1906{
1907 struct hda_amp_info *info;
1908 unsigned int val;
1909
1910 mutex_lock(&codec->hash_mutex);
1911 info = get_alloc_amp_hash(codec, key);
1912 if (!info) {
1913 mutex_unlock(&codec->hash_mutex);
1914 return 0;
1915 }
1916 if (!(info->head.val & INFO_AMP_CAPS)) {
1917 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1918 val = func(codec, nid, dir);
1919 write_caps_hash(codec, key, val);
1920 } else {
1921 val = info->amp_caps;
1922 mutex_unlock(&codec->hash_mutex);
1923 }
1924 return val;
1925}
1926
1927static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1928 int direction)
1929{
1930 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1931 nid = codec->afg;
1932 return snd_hda_param_read(codec, nid,
1933 direction == HDA_OUTPUT ?
1934 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1935}
1936
Takashi Iwaid5191e52009-11-16 14:58:17 +01001937/**
1938 * query_amp_caps - query AMP capabilities
1939 * @codec: the HD-auio codec
1940 * @nid: the NID to query
1941 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1942 *
1943 * Query AMP capabilities for the given widget and direction.
1944 * Returns the obtained capability bits.
1945 *
1946 * When cap bits have been already read, this doesn't read again but
1947 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001949u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001951 return query_caps_hash(codec, nid, direction,
1952 HDA_HASH_KEY(nid, direction, 0),
1953 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001955EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Takashi Iwaid5191e52009-11-16 14:58:17 +01001957/**
1958 * snd_hda_override_amp_caps - Override the AMP capabilities
1959 * @codec: the CODEC to clean up
1960 * @nid: the NID to clean up
1961 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1962 * @caps: the capability bits to set
1963 *
1964 * Override the cached AMP caps bits value by the given one.
1965 * This function is useful if the driver needs to adjust the AMP ranges,
1966 * e.g. limit to 0dB, etc.
1967 *
1968 * Returns zero if successful or a negative error code.
1969 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001970int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1971 unsigned int caps)
1972{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001973 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001975EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001976
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001977static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1978 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001979{
1980 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1981}
1982
Takashi Iwaid5191e52009-11-16 14:58:17 +01001983/**
1984 * snd_hda_query_pin_caps - Query PIN capabilities
1985 * @codec: the HD-auio codec
1986 * @nid: the NID to query
1987 *
1988 * Query PIN capabilities for the given widget.
1989 * Returns the obtained capability bits.
1990 *
1991 * When cap bits have been already read, this doesn't read again but
1992 * returns the cached value.
1993 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001994u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1995{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001996 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001997 read_pin_cap);
1998}
Takashi Iwai1327a322009-03-23 13:07:47 +01001999EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
2000
Wu Fengguang864f92b2009-11-18 12:38:02 +08002001/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002002 * snd_hda_override_pin_caps - Override the pin capabilities
2003 * @codec: the CODEC
2004 * @nid: the NID to override
2005 * @caps: the capability bits to set
2006 *
2007 * Override the cached PIN capabilitiy bits value by the given one.
2008 *
2009 * Returns zero if successful or a negative error code.
2010 */
2011int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2012 unsigned int caps)
2013{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002014 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002015}
2016EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2017
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002018/* read or sync the hash value with the current value;
2019 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002021static struct hda_amp_info *
2022update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002023 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002025 struct hda_amp_info *info;
2026 unsigned int parm, val = 0;
2027 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002029 retry:
2030 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2031 if (!info)
2032 return NULL;
2033 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2034 if (!val_read) {
2035 mutex_unlock(&codec->hash_mutex);
2036 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2037 parm |= direction == HDA_OUTPUT ?
2038 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2039 parm |= index;
2040 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002041 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002042 val &= 0xff;
2043 val_read = true;
2044 mutex_lock(&codec->hash_mutex);
2045 goto retry;
2046 }
2047 info->vol[ch] = val;
2048 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002049 } else if (init_only)
2050 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002051 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002055 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002057static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002058 hda_nid_t nid, int ch, int direction, int index,
2059 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 u32 parm;
2062
2063 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2064 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2065 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002066 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2067 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002068 ; /* set the zero value as a fake mute */
2069 else
2070 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2072}
2073
Takashi Iwaid5191e52009-11-16 14:58:17 +01002074/**
2075 * snd_hda_codec_amp_read - Read AMP value
2076 * @codec: HD-audio codec
2077 * @nid: NID to read the AMP value
2078 * @ch: channel (left=0 or right=1)
2079 * @direction: #HDA_INPUT or #HDA_OUTPUT
2080 * @index: the index value (only for input direction)
2081 *
2082 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Takashi Iwai834be882006-03-01 14:16:17 +01002084int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2085 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002087 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088 unsigned int val = 0;
2089
2090 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002091 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002092 if (info)
2093 val = info->vol[ch];
2094 mutex_unlock(&codec->hash_mutex);
2095 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002097EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Takashi Iwai280e57d2012-12-14 10:32:21 +01002099static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2100 int direction, int idx, int mask, int val,
2101 bool init_only)
2102{
2103 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002104 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002105 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002106
2107 if (snd_BUG_ON(mask & ~0xff))
2108 mask &= 0xff;
2109 val &= mask;
2110
2111 mutex_lock(&codec->hash_mutex);
2112 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2113 if (!info) {
2114 mutex_unlock(&codec->hash_mutex);
2115 return 0;
2116 }
2117 val |= info->vol[ch] & ~mask;
2118 if (info->vol[ch] == val) {
2119 mutex_unlock(&codec->hash_mutex);
2120 return 0;
2121 }
2122 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002123 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002124 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002125 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002126 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002127 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002128 return 1;
2129}
2130
Takashi Iwaid5191e52009-11-16 14:58:17 +01002131/**
2132 * snd_hda_codec_amp_update - update the AMP value
2133 * @codec: HD-audio codec
2134 * @nid: NID to read the AMP value
2135 * @ch: channel (left=0 or right=1)
2136 * @direction: #HDA_INPUT or #HDA_OUTPUT
2137 * @idx: the index value (only for input direction)
2138 * @mask: bit mask to set
2139 * @val: the bits value to set
2140 *
2141 * Update the AMP value with a bit mask.
2142 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002143 */
Takashi Iwai834be882006-03-01 14:16:17 +01002144int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2145 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002147 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002149EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Takashi Iwaid5191e52009-11-16 14:58:17 +01002151/**
2152 * snd_hda_codec_amp_stereo - update the AMP stereo values
2153 * @codec: HD-audio codec
2154 * @nid: NID to read the AMP value
2155 * @direction: #HDA_INPUT or #HDA_OUTPUT
2156 * @idx: the index value (only for input direction)
2157 * @mask: bit mask to set
2158 * @val: the bits value to set
2159 *
2160 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2161 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002162 */
2163int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2164 int direction, int idx, int mask, int val)
2165{
2166 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002167
2168 if (snd_BUG_ON(mask & ~0xff))
2169 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002170 for (ch = 0; ch < 2; ch++)
2171 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2172 idx, mask, val);
2173 return ret;
2174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002175EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002176
Takashi Iwai280e57d2012-12-14 10:32:21 +01002177/* Works like snd_hda_codec_amp_update() but it writes the value only at
2178 * the first access. If the amp was already initialized / updated beforehand,
2179 * this does nothing.
2180 */
2181int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2182 int dir, int idx, int mask, int val)
2183{
2184 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2185}
2186EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2187
2188int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2189 int dir, int idx, int mask, int val)
2190{
2191 int ch, ret = 0;
2192
2193 if (snd_BUG_ON(mask & ~0xff))
2194 mask &= 0xff;
2195 for (ch = 0; ch < 2; ch++)
2196 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2197 idx, mask, val);
2198 return ret;
2199}
2200EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2201
Takashi Iwaid5191e52009-11-16 14:58:17 +01002202/**
2203 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2204 * @codec: HD-audio codec
2205 *
2206 * Resume the all amp commands from the cache.
2207 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002208void snd_hda_codec_resume_amp(struct hda_codec *codec)
2209{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002210 int i;
2211
Takashi Iwaic370dd62012-12-13 18:30:04 +01002212 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002213 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002214 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2215 struct hda_amp_info *buffer;
2216 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002217 hda_nid_t nid;
2218 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002219 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002220
2221 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002222 if (!buffer->head.dirty)
2223 continue;
2224 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002225 info = *buffer;
2226 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002227 if (!key)
2228 continue;
2229 nid = key & 0xff;
2230 idx = (key >> 16) & 0xff;
2231 dir = (key >> 24) & 0xff;
2232 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002233 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002234 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002235 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002236 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2237 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002238 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002239 }
2240 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002241 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002242}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002243EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002245static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2246 unsigned int ofs)
2247{
2248 u32 caps = query_amp_caps(codec, nid, dir);
2249 /* get num steps */
2250 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2251 if (ofs < caps)
2252 caps -= ofs;
2253 return caps;
2254}
2255
Takashi Iwaid5191e52009-11-16 14:58:17 +01002256/**
2257 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2258 *
2259 * The control element is supposed to have the private_value field
2260 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2261 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002262int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2266 u16 nid = get_amp_nid(kcontrol);
2267 u8 chs = get_amp_channels(kcontrol);
2268 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002269 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002271 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2272 uinfo->count = chs == 3 ? 2 : 1;
2273 uinfo->value.integer.min = 0;
2274 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2275 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002276 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002277 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2278 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return -EINVAL;
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return 0;
2282}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002283EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002285
2286static inline unsigned int
2287read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2288 int ch, int dir, int idx, unsigned int ofs)
2289{
2290 unsigned int val;
2291 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2292 val &= HDA_AMP_VOLMASK;
2293 if (val >= ofs)
2294 val -= ofs;
2295 else
2296 val = 0;
2297 return val;
2298}
2299
2300static inline int
2301update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2302 int ch, int dir, int idx, unsigned int ofs,
2303 unsigned int val)
2304{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002305 unsigned int maxval;
2306
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002307 if (val > 0)
2308 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002309 /* ofs = 0: raw max value */
2310 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002311 if (val > maxval)
2312 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002313 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2314 HDA_AMP_VOLMASK, val);
2315}
2316
Takashi Iwaid5191e52009-11-16 14:58:17 +01002317/**
2318 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2319 *
2320 * The control element is supposed to have the private_value field
2321 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2322 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002323int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2327 hda_nid_t nid = get_amp_nid(kcontrol);
2328 int chs = get_amp_channels(kcontrol);
2329 int dir = get_amp_direction(kcontrol);
2330 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002331 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 long *valp = ucontrol->value.integer.value;
2333
2334 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002335 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002337 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return 0;
2339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002340EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Takashi Iwaid5191e52009-11-16 14:58:17 +01002342/**
2343 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2344 *
2345 * The control element is supposed to have the private_value field
2346 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2347 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002348int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2352 hda_nid_t nid = get_amp_nid(kcontrol);
2353 int chs = get_amp_channels(kcontrol);
2354 int dir = get_amp_direction(kcontrol);
2355 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002356 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 long *valp = ucontrol->value.integer.value;
2358 int change = 0;
2359
Takashi Iwaicb53c622007-08-10 17:21:45 +02002360 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002361 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002362 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002363 valp++;
2364 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002365 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002366 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002367 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return change;
2369}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002370EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Takashi Iwaid5191e52009-11-16 14:58:17 +01002372/**
2373 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2374 *
2375 * The control element is supposed to have the private_value field
2376 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2377 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002378int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2379 unsigned int size, unsigned int __user *_tlv)
2380{
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 hda_nid_t nid = get_amp_nid(kcontrol);
2383 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002384 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002385 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002386 u32 caps, val1, val2;
2387
2388 if (size < 4 * sizeof(unsigned int))
2389 return -ENOMEM;
2390 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002391 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2392 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002393 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002394 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002395 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002396 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002397 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002398 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2399 return -EFAULT;
2400 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2401 return -EFAULT;
2402 if (put_user(val1, _tlv + 2))
2403 return -EFAULT;
2404 if (put_user(val2, _tlv + 3))
2405 return -EFAULT;
2406 return 0;
2407}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002408EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002409
Takashi Iwaid5191e52009-11-16 14:58:17 +01002410/**
2411 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2412 * @codec: HD-audio codec
2413 * @nid: NID of a reference widget
2414 * @dir: #HDA_INPUT or #HDA_OUTPUT
2415 * @tlv: TLV data to be stored, at least 4 elements
2416 *
2417 * Set (static) TLV data for a virtual master volume using the AMP caps
2418 * obtained from the reference NID.
2419 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002420 */
2421void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2422 unsigned int *tlv)
2423{
2424 u32 caps;
2425 int nums, step;
2426
2427 caps = query_amp_caps(codec, nid, dir);
2428 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2429 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2430 step = (step + 1) * 25;
2431 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2432 tlv[1] = 2 * sizeof(unsigned int);
2433 tlv[2] = -nums * step;
2434 tlv[3] = step;
2435}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002436EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002437
2438/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002439static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002440find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002441{
2442 struct snd_ctl_elem_id id;
2443 memset(&id, 0, sizeof(id));
2444 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002445 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002446 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002447 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2448 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002449 strcpy(id.name, name);
2450 return snd_ctl_find_id(codec->bus->card, &id);
2451}
2452
Takashi Iwaid5191e52009-11-16 14:58:17 +01002453/**
2454 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2455 * @codec: HD-audio codec
2456 * @name: ctl id name string
2457 *
2458 * Get the control element with the given id string and IFACE_MIXER.
2459 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002460struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2461 const char *name)
2462{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002463 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002464}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002465EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002466
Takashi Iwaidcda5802012-10-12 17:24:51 +02002467static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002468 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002469{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002470 int i, idx;
2471 /* 16 ctlrs should be large enough */
2472 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2473 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002474 return idx;
2475 }
2476 return -EBUSY;
2477}
2478
Takashi Iwaid5191e52009-11-16 14:58:17 +01002479/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002480 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002481 * @codec: HD-audio codec
2482 * @nid: corresponding NID (optional)
2483 * @kctl: the control element to assign
2484 *
2485 * Add the given control element to an array inside the codec instance.
2486 * All control elements belonging to a codec are supposed to be added
2487 * by this function so that a proper clean-up works at the free or
2488 * reconfiguration time.
2489 *
2490 * If non-zero @nid is passed, the NID is assigned to the control element.
2491 * The assignment is shown in the codec proc file.
2492 *
2493 * snd_hda_ctl_add() checks the control subdev id field whether
2494 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002495 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2496 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002497 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002498int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2499 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002500{
2501 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002502 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002503 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002504
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002505 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002506 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002507 if (nid == 0)
2508 nid = get_amp_nid_(kctl->private_value);
2509 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002510 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2511 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002512 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002513 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002514 err = snd_ctl_add(codec->bus->card, kctl);
2515 if (err < 0)
2516 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002517 item = snd_array_new(&codec->mixers);
2518 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002519 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002520 item->kctl = kctl;
2521 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002522 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002523 return 0;
2524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002525EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002526
Takashi Iwaid5191e52009-11-16 14:58:17 +01002527/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002528 * snd_hda_add_nid - Assign a NID to a control element
2529 * @codec: HD-audio codec
2530 * @nid: corresponding NID (optional)
2531 * @kctl: the control element to assign
2532 * @index: index to kctl
2533 *
2534 * Add the given control element to an array inside the codec instance.
2535 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2536 * NID:KCTL mapping - for example "Capture Source" selector.
2537 */
2538int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2539 unsigned int index, hda_nid_t nid)
2540{
2541 struct hda_nid_item *item;
2542
2543 if (nid > 0) {
2544 item = snd_array_new(&codec->nids);
2545 if (!item)
2546 return -ENOMEM;
2547 item->kctl = kctl;
2548 item->index = index;
2549 item->nid = nid;
2550 return 0;
2551 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002552 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2553 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002554 return -EINVAL;
2555}
2556EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2557
2558/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002559 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2560 * @codec: HD-audio codec
2561 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002562void snd_hda_ctls_clear(struct hda_codec *codec)
2563{
2564 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002565 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002566 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002567 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002568 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002569 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002570}
2571
Takashi Iwaia65d6292009-02-23 16:57:04 +01002572/* pseudo device locking
2573 * toggle card->shutdown to allow/disallow the device access (as a hack)
2574 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002575int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002576{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002577 struct snd_card *card = bus->card;
2578 struct hda_codec *codec;
2579
Takashi Iwaia65d6292009-02-23 16:57:04 +01002580 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002581 if (card->shutdown)
2582 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002583 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002584 if (!list_empty(&card->ctl_files))
2585 goto err_clear;
2586
2587 list_for_each_entry(codec, &bus->codec_list, list) {
2588 int pcm;
2589 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2590 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2591 if (!cpcm->pcm)
2592 continue;
2593 if (cpcm->pcm->streams[0].substream_opened ||
2594 cpcm->pcm->streams[1].substream_opened)
2595 goto err_clear;
2596 }
2597 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002598 spin_unlock(&card->files_lock);
2599 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002600
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002601 err_clear:
2602 card->shutdown = 0;
2603 err_unlock:
2604 spin_unlock(&card->files_lock);
2605 return -EINVAL;
2606}
2607EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2608
2609void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002610{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002611 struct snd_card *card = bus->card;
2612
2613 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002614 spin_lock(&card->files_lock);
2615 card->shutdown = 0;
2616 spin_unlock(&card->files_lock);
2617}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002618EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002619
Takashi Iwaid5191e52009-11-16 14:58:17 +01002620/**
2621 * snd_hda_codec_reset - Clear all objects assigned to the codec
2622 * @codec: HD-audio codec
2623 *
2624 * This frees the all PCM and control elements assigned to the codec, and
2625 * clears the caches and restores the pin default configurations.
2626 *
2627 * When a device is being used, it returns -EBSY. If successfully freed,
2628 * returns zero.
2629 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002630int snd_hda_codec_reset(struct hda_codec *codec)
2631{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002632 struct hda_bus *bus = codec->bus;
2633 struct snd_card *card = bus->card;
2634 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002635
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002636 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002637 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002638
2639 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002640 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002641#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002642 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002643 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002644#endif
2645 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002646 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002647 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002648 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002649 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002650 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002651 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002652 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002653 }
2654 if (codec->patch_ops.free)
2655 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002656 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002657 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002658 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002659 codec->spec = NULL;
2660 free_hda_cache(&codec->amp_cache);
2661 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002662 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2663 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002664 /* free only driver_pins so that init_pins + user_pins are restored */
2665 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002666 snd_array_free(&codec->cvt_setups);
2667 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002668 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002669 codec->num_pcms = 0;
2670 codec->pcm_info = NULL;
2671 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002672 codec->slave_dig_outs = NULL;
2673 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002674 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002675 module_put(codec->owner);
2676 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002677
2678 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002679 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002680 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002681}
2682
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002683typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2684
2685/* apply the function to all matching slave ctls in the mixer list */
2686static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002687 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002688{
2689 struct hda_nid_item *items;
2690 const char * const *s;
2691 int i, err;
2692
2693 items = codec->mixers.list;
2694 for (i = 0; i < codec->mixers.used; i++) {
2695 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002696 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002697 continue;
2698 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002699 char tmpname[sizeof(sctl->id.name)];
2700 const char *name = *s;
2701 if (suffix) {
2702 snprintf(tmpname, sizeof(tmpname), "%s %s",
2703 name, suffix);
2704 name = tmpname;
2705 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002706 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002707 err = func(data, sctl);
2708 if (err)
2709 return err;
2710 break;
2711 }
2712 }
2713 }
2714 return 0;
2715}
2716
2717static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2718{
2719 return 1;
2720}
2721
Takashi Iwai18478e82012-03-09 17:51:10 +01002722/* guess the value corresponding to 0dB */
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002723static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002724{
2725 int _tlv[4];
2726 const int *tlv = NULL;
2727 int val = -1;
2728
2729 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2730 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2731 mm_segment_t fs = get_fs();
2732 set_fs(get_ds());
2733 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2734 tlv = _tlv;
2735 set_fs(fs);
2736 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2737 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002738 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2739 int step = tlv[3];
2740 step &= ~TLV_DB_SCALE_MUTE;
2741 if (!step)
2742 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002743 if (*step_to_check && *step_to_check != step) {
2744 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2745 *step_to_check, step);
2746 return -1;
2747 }
2748 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002749 val = -tlv[2] / step;
2750 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002751 return val;
2752}
2753
2754/* call kctl->put with the given value(s) */
2755static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2756{
2757 struct snd_ctl_elem_value *ucontrol;
2758 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2759 if (!ucontrol)
2760 return -ENOMEM;
2761 ucontrol->value.integer.value[0] = val;
2762 ucontrol->value.integer.value[1] = val;
2763 kctl->put(kctl, ucontrol);
2764 kfree(ucontrol);
2765 return 0;
2766}
2767
2768/* initialize the slave volume with 0dB */
2769static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2770{
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002771 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002772 if (offset > 0)
2773 put_kctl_with_value(slave, offset);
2774 return 0;
2775}
2776
2777/* unmute the slave */
2778static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2779{
2780 return put_kctl_with_value(slave, 1);
2781}
2782
Takashi Iwaid5191e52009-11-16 14:58:17 +01002783/**
2784 * snd_hda_add_vmaster - create a virtual master control and add slaves
2785 * @codec: HD-audio codec
2786 * @name: vmaster control name
2787 * @tlv: TLV data (optional)
2788 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002789 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002790 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002791 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002792 *
2793 * Create a virtual master control with the given name. The TLV data
2794 * must be either NULL or a valid data.
2795 *
2796 * @slaves is a NULL-terminated array of strings, each of which is a
2797 * slave control name. All controls with these names are assigned to
2798 * the new virtual master control.
2799 *
2800 * This function returns zero if successful or a negative error code.
2801 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002802int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002803 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002804 const char *suffix, bool init_slave_vol,
2805 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002806{
2807 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002808 int err;
2809
Takashi Iwai29e58532012-03-12 12:25:03 +01002810 if (ctl_ret)
2811 *ctl_ret = NULL;
2812
Takashi Iwai9322ca52012-02-03 14:28:01 +01002813 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002814 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002815 snd_printdd("No slave found for %s\n", name);
2816 return 0;
2817 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002818 kctl = snd_ctl_make_virtual_master(name, tlv);
2819 if (!kctl)
2820 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002821 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002822 if (err < 0)
2823 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002824
Takashi Iwai9322ca52012-02-03 14:28:01 +01002825 err = map_slaves(codec, slaves, suffix,
2826 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002827 if (err < 0)
2828 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002829
2830 /* init with master mute & zero volume */
2831 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002832 if (init_slave_vol) {
2833 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002834 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002835 tlv ? init_slave_0dB : init_slave_unmute, &step);
2836 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002837
Takashi Iwai29e58532012-03-12 12:25:03 +01002838 if (ctl_ret)
2839 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002840 return 0;
2841}
Takashi Iwai18478e82012-03-09 17:51:10 +01002842EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002843
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002844/*
2845 * mute-LED control using vmaster
2846 */
2847static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_info *uinfo)
2849{
2850 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002851 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002852 };
2853 unsigned int index;
2854
2855 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2856 uinfo->count = 1;
2857 uinfo->value.enumerated.items = 3;
2858 index = uinfo->value.enumerated.item;
2859 if (index >= 3)
2860 index = 2;
2861 strcpy(uinfo->value.enumerated.name, texts[index]);
2862 return 0;
2863}
2864
2865static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_value *ucontrol)
2867{
2868 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2869 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2870 return 0;
2871}
2872
2873static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_value *ucontrol)
2875{
2876 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2877 unsigned int old_mode = hook->mute_mode;
2878
2879 hook->mute_mode = ucontrol->value.enumerated.item[0];
2880 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2881 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2882 if (old_mode == hook->mute_mode)
2883 return 0;
2884 snd_hda_sync_vmaster_hook(hook);
2885 return 1;
2886}
2887
2888static struct snd_kcontrol_new vmaster_mute_mode = {
2889 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2890 .name = "Mute-LED Mode",
2891 .info = vmaster_mute_mode_info,
2892 .get = vmaster_mute_mode_get,
2893 .put = vmaster_mute_mode_put,
2894};
2895
2896/*
2897 * Add a mute-LED hook with the given vmaster switch kctl
2898 * "Mute-LED Mode" control is automatically created and associated with
2899 * the given hook.
2900 */
2901int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002902 struct hda_vmaster_mute_hook *hook,
2903 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002904{
2905 struct snd_kcontrol *kctl;
2906
2907 if (!hook->hook || !hook->sw_kctl)
2908 return 0;
2909 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2910 hook->codec = codec;
2911 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002912 if (!expose_enum_ctl)
2913 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002914 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2915 if (!kctl)
2916 return -ENOMEM;
2917 return snd_hda_ctl_add(codec, 0, kctl);
2918}
2919EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2920
2921/*
2922 * Call the hook with the current value for synchronization
2923 * Should be called in init callback
2924 */
2925void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2926{
2927 if (!hook->hook || !hook->codec)
2928 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002929 /* don't call vmaster hook in the destructor since it might have
2930 * been already destroyed
2931 */
2932 if (hook->codec->bus->shutdown)
2933 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002934 switch (hook->mute_mode) {
2935 case HDA_VMUTE_FOLLOW_MASTER:
2936 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2937 break;
2938 default:
2939 hook->hook(hook->codec, hook->mute_mode);
2940 break;
2941 }
2942}
2943EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2944
2945
Takashi Iwaid5191e52009-11-16 14:58:17 +01002946/**
2947 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2948 *
2949 * The control element is supposed to have the private_value field
2950 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2951 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002952int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 int chs = get_amp_channels(kcontrol);
2956
2957 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2958 uinfo->count = chs == 3 ? 2 : 1;
2959 uinfo->value.integer.min = 0;
2960 uinfo->value.integer.max = 1;
2961 return 0;
2962}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002963EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Takashi Iwaid5191e52009-11-16 14:58:17 +01002965/**
2966 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2967 *
2968 * The control element is supposed to have the private_value field
2969 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2970 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2972 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2975 hda_nid_t nid = get_amp_nid(kcontrol);
2976 int chs = get_amp_channels(kcontrol);
2977 int dir = get_amp_direction(kcontrol);
2978 int idx = get_amp_index(kcontrol);
2979 long *valp = ucontrol->value.integer.value;
2980
2981 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002982 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002983 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002986 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return 0;
2988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002989EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Takashi Iwaid5191e52009-11-16 14:58:17 +01002991/**
2992 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2993 *
2994 * The control element is supposed to have the private_value field
2995 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2996 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002997int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3001 hda_nid_t nid = get_amp_nid(kcontrol);
3002 int chs = get_amp_channels(kcontrol);
3003 int dir = get_amp_direction(kcontrol);
3004 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 long *valp = ucontrol->value.integer.value;
3006 int change = 0;
3007
Takashi Iwaicb53c622007-08-10 17:21:45 +02003008 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003009 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003010 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003011 HDA_AMP_MUTE,
3012 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003013 valp++;
3014 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003015 if (chs & 2)
3016 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003017 HDA_AMP_MUTE,
3018 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003019 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003020 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return change;
3022}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003023EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025/*
Takashi Iwai985be542005-11-02 18:26:49 +01003026 * bound volume controls
3027 *
3028 * bind multiple volumes (# indices, from 0)
3029 */
3030
3031#define AMP_VAL_IDX_SHIFT 19
3032#define AMP_VAL_IDX_MASK (0x0f<<19)
3033
Takashi Iwaid5191e52009-11-16 14:58:17 +01003034/**
3035 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3036 *
3037 * The control element is supposed to have the private_value field
3038 * set up via HDA_BIND_MUTE*() macros.
3039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003040int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003042{
3043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3044 unsigned long pval;
3045 int err;
3046
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003047 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003048 pval = kcontrol->private_value;
3049 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3050 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3051 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003052 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003053 return err;
3054}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003055EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003056
Takashi Iwaid5191e52009-11-16 14:58:17 +01003057/**
3058 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3059 *
3060 * The control element is supposed to have the private_value field
3061 * set up via HDA_BIND_MUTE*() macros.
3062 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003063int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3064 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003065{
3066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3067 unsigned long pval;
3068 int i, indices, err = 0, change = 0;
3069
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003070 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003071 pval = kcontrol->private_value;
3072 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3073 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3075 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003076 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3077 if (err < 0)
3078 break;
3079 change |= err;
3080 }
3081 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003082 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003083 return err < 0 ? err : change;
3084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003085EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003086
Takashi Iwaid5191e52009-11-16 14:58:17 +01003087/**
3088 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3089 *
3090 * The control element is supposed to have the private_value field
3091 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003092 */
3093int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3094 struct snd_ctl_elem_info *uinfo)
3095{
3096 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3097 struct hda_bind_ctls *c;
3098 int err;
3099
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003100 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003101 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003102 kcontrol->private_value = *c->values;
3103 err = c->ops->info(kcontrol, uinfo);
3104 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003105 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003106 return err;
3107}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003108EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003109
Takashi Iwaid5191e52009-11-16 14:58:17 +01003110/**
3111 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3112 *
3113 * The control element is supposed to have the private_value field
3114 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3115 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003116int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol)
3118{
3119 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3120 struct hda_bind_ctls *c;
3121 int err;
3122
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003123 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003124 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003125 kcontrol->private_value = *c->values;
3126 err = c->ops->get(kcontrol, ucontrol);
3127 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003128 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003129 return err;
3130}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003131EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003132
Takashi Iwaid5191e52009-11-16 14:58:17 +01003133/**
3134 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3135 *
3136 * The control element is supposed to have the private_value field
3137 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3138 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003139int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3141{
3142 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3143 struct hda_bind_ctls *c;
3144 unsigned long *vals;
3145 int err = 0, change = 0;
3146
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003147 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003148 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003149 for (vals = c->values; *vals; vals++) {
3150 kcontrol->private_value = *vals;
3151 err = c->ops->put(kcontrol, ucontrol);
3152 if (err < 0)
3153 break;
3154 change |= err;
3155 }
3156 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003157 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003158 return err < 0 ? err : change;
3159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003160EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003161
Takashi Iwaid5191e52009-11-16 14:58:17 +01003162/**
3163 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3164 *
3165 * The control element is supposed to have the private_value field
3166 * set up via HDA_BIND_VOL() macro.
3167 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003168int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3169 unsigned int size, unsigned int __user *tlv)
3170{
3171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3172 struct hda_bind_ctls *c;
3173 int err;
3174
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003175 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003176 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003177 kcontrol->private_value = *c->values;
3178 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3179 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003180 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003181 return err;
3182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003183EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003184
3185struct hda_ctl_ops snd_hda_bind_vol = {
3186 .info = snd_hda_mixer_amp_volume_info,
3187 .get = snd_hda_mixer_amp_volume_get,
3188 .put = snd_hda_mixer_amp_volume_put,
3189 .tlv = snd_hda_mixer_amp_tlv
3190};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003191EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003192
3193struct hda_ctl_ops snd_hda_bind_sw = {
3194 .info = snd_hda_mixer_amp_switch_info,
3195 .get = snd_hda_mixer_amp_switch_get,
3196 .put = snd_hda_mixer_amp_switch_put,
3197 .tlv = snd_hda_mixer_amp_tlv
3198};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003199EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003200
3201/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 * SPDIF out controls
3203 */
3204
Takashi Iwai0ba21762007-04-16 11:29:14 +02003205static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3209 uinfo->count = 1;
3210 return 0;
3211}
3212
Takashi Iwai0ba21762007-04-16 11:29:14 +02003213static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3217 IEC958_AES0_NONAUDIO |
3218 IEC958_AES0_CON_EMPHASIS_5015 |
3219 IEC958_AES0_CON_NOT_COPYRIGHT;
3220 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3221 IEC958_AES1_CON_ORIGINAL;
3222 return 0;
3223}
3224
Takashi Iwai0ba21762007-04-16 11:29:14 +02003225static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3226 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3229 IEC958_AES0_NONAUDIO |
3230 IEC958_AES0_PRO_EMPHASIS_5015;
3231 return 0;
3232}
3233
Takashi Iwai0ba21762007-04-16 11:29:14 +02003234static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3235 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003238 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003239 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003241 mutex_lock(&codec->spdif_mutex);
3242 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003243 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3244 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3245 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3246 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003247 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 return 0;
3250}
3251
3252/* convert from SPDIF status bits to HDA SPDIF bits
3253 * bit 0 (DigEn) is always set zero (to be filled later)
3254 */
3255static unsigned short convert_from_spdif_status(unsigned int sbits)
3256{
3257 unsigned short val = 0;
3258
3259 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003260 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003262 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003264 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3265 IEC958_AES0_PRO_EMPHASIS_5015)
3266 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003268 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3269 IEC958_AES0_CON_EMPHASIS_5015)
3270 val |= AC_DIG1_EMPHASIS;
3271 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3272 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003274 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3276 }
3277 return val;
3278}
3279
3280/* convert to SPDIF status bits from HDA SPDIF bits
3281 */
3282static unsigned int convert_to_spdif_status(unsigned short val)
3283{
3284 unsigned int sbits = 0;
3285
Takashi Iwai0ba21762007-04-16 11:29:14 +02003286 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003288 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 sbits |= IEC958_AES0_PROFESSIONAL;
3290 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003291 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3293 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003294 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003296 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003298 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3300 sbits |= val & (0x7f << 8);
3301 }
3302 return sbits;
3303}
3304
Takashi Iwai2f728532008-09-25 16:32:41 +02003305/* set digital convert verbs both for the given NID and its slaves */
3306static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3307 int verb, int val)
3308{
Takashi Iwaidda14412011-05-02 11:29:30 +02003309 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003310
Takashi Iwai9e976972008-11-25 08:17:20 +01003311 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003312 d = codec->slave_dig_outs;
3313 if (!d)
3314 return;
3315 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003316 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003317}
3318
3319static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3320 int dig1, int dig2)
3321{
3322 if (dig1 != -1)
3323 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3324 if (dig2 != -1)
3325 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3326}
3327
Takashi Iwai0ba21762007-04-16 11:29:14 +02003328static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
3331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003332 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003333 struct hda_spdif_out *spdif;
3334 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 unsigned short val;
3336 int change;
3337
Ingo Molnar62932df2006-01-16 16:34:20 +01003338 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003339 spdif = snd_array_elem(&codec->spdif_out, idx);
3340 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003341 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3343 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3344 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003345 val = convert_from_spdif_status(spdif->status);
3346 val |= spdif->ctls & 1;
3347 change = spdif->ctls != val;
3348 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003349 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003350 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003351 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return change;
3353}
3354
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003355#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Takashi Iwai0ba21762007-04-16 11:29:14 +02003357static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3358 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003361 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003362 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003364 mutex_lock(&codec->spdif_mutex);
3365 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003366 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003367 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return 0;
3369}
3370
Stephen Warren74b654c2011-06-01 11:14:18 -06003371static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3372 int dig1, int dig2)
3373{
3374 set_dig_out_convert(codec, nid, dig1, dig2);
3375 /* unmute amp switch (if any) */
3376 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3377 (dig1 & AC_DIG1_ENABLE))
3378 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3379 HDA_AMP_MUTE, 0);
3380}
3381
Takashi Iwai0ba21762007-04-16 11:29:14 +02003382static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3383 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003386 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003387 struct hda_spdif_out *spdif;
3388 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 unsigned short val;
3390 int change;
3391
Ingo Molnar62932df2006-01-16 16:34:20 +01003392 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003393 spdif = snd_array_elem(&codec->spdif_out, idx);
3394 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003395 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003397 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003398 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003399 spdif->ctls = val;
3400 if (change && nid != (u16)-1)
3401 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003402 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return change;
3404}
3405
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003406static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 {
3408 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3409 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003410 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 .info = snd_hda_spdif_mask_info,
3412 .get = snd_hda_spdif_cmask_get,
3413 },
3414 {
3415 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3416 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003417 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 .info = snd_hda_spdif_mask_info,
3419 .get = snd_hda_spdif_pmask_get,
3420 },
3421 {
3422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003423 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 .info = snd_hda_spdif_mask_info,
3425 .get = snd_hda_spdif_default_get,
3426 .put = snd_hda_spdif_default_put,
3427 },
3428 {
3429 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003430 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 .info = snd_hda_spdif_out_switch_info,
3432 .get = snd_hda_spdif_out_switch_get,
3433 .put = snd_hda_spdif_out_switch_put,
3434 },
3435 { } /* end */
3436};
3437
3438/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003439 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003441 * @associated_nid: NID that new ctls associated with
3442 * @cvt_nid: converter NID
3443 * @type: HDA_PCM_TYPE_*
3444 * Creates controls related with the digital output.
3445 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 *
3447 * Returns 0 if successful, or a negative error code.
3448 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003449int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3450 hda_nid_t associated_nid,
3451 hda_nid_t cvt_nid,
3452 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
3454 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003455 struct snd_kcontrol *kctl;
3456 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003457 int idx = 0;
3458 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003459 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003460 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003462 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003463 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003464 idx = spdif_index;
3465 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003466 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003467 /* suppose a single SPDIF device */
3468 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3469 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3470 if (!kctl)
3471 break;
3472 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003473 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003474 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003475 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003476 if (!bus->primary_dig_out_type)
3477 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003478
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003479 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003480 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003481 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3482 return -EBUSY;
3483 }
Stephen Warren7c935972011-06-01 11:14:17 -06003484 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003485 if (!spdif)
3486 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3488 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003489 if (!kctl)
3490 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003491 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003492 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003493 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003494 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return err;
3496 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003497 spdif->nid = cvt_nid;
3498 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003499 AC_VERB_GET_DIGI_CONVERT_1, 0);
3500 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return 0;
3502}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003503EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003505/* get the hda_spdif_out entry from the given NID
3506 * call within spdif_mutex lock
3507 */
Stephen Warren7c935972011-06-01 11:14:17 -06003508struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3509 hda_nid_t nid)
3510{
3511 int i;
3512 for (i = 0; i < codec->spdif_out.used; i++) {
3513 struct hda_spdif_out *spdif =
3514 snd_array_elem(&codec->spdif_out, i);
3515 if (spdif->nid == nid)
3516 return spdif;
3517 }
3518 return NULL;
3519}
3520EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3521
Stephen Warren74b654c2011-06-01 11:14:18 -06003522void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3523{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003524 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003525
3526 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003527 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003528 spdif->nid = (u16)-1;
3529 mutex_unlock(&codec->spdif_mutex);
3530}
3531EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3532
3533void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3534{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003535 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003536 unsigned short val;
3537
3538 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003539 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003540 if (spdif->nid != nid) {
3541 spdif->nid = nid;
3542 val = spdif->ctls;
3543 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3544 }
3545 mutex_unlock(&codec->spdif_mutex);
3546}
3547EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003550 * SPDIF sharing with analog output
3551 */
3552static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3553 struct snd_ctl_elem_value *ucontrol)
3554{
3555 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3556 ucontrol->value.integer.value[0] = mout->share_spdif;
3557 return 0;
3558}
3559
3560static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3561 struct snd_ctl_elem_value *ucontrol)
3562{
3563 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3564 mout->share_spdif = !!ucontrol->value.integer.value[0];
3565 return 0;
3566}
3567
3568static struct snd_kcontrol_new spdif_share_sw = {
3569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3570 .name = "IEC958 Default PCM Playback Switch",
3571 .info = snd_ctl_boolean_mono_info,
3572 .get = spdif_share_sw_get,
3573 .put = spdif_share_sw_put,
3574};
3575
Takashi Iwaid5191e52009-11-16 14:58:17 +01003576/**
3577 * snd_hda_create_spdif_share_sw - create Default PCM switch
3578 * @codec: the HDA codec
3579 * @mout: multi-out instance
3580 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003581int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3582 struct hda_multi_out *mout)
3583{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003584 struct snd_kcontrol *kctl;
3585
Takashi Iwai9a081602008-02-12 18:37:26 +01003586 if (!mout->dig_out_nid)
3587 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003588
3589 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3590 if (!kctl)
3591 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003592 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003593 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003594}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003595EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003596
3597/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 * SPDIF input
3599 */
3600
3601#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3602
Takashi Iwai0ba21762007-04-16 11:29:14 +02003603static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3604 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3607
3608 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3609 return 0;
3610}
3611
Takashi Iwai0ba21762007-04-16 11:29:14 +02003612static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3613 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3616 hda_nid_t nid = kcontrol->private_value;
3617 unsigned int val = !!ucontrol->value.integer.value[0];
3618 int change;
3619
Ingo Molnar62932df2006-01-16 16:34:20 +01003620 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003622 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003624 snd_hda_codec_write_cache(codec, nid, 0,
3625 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003627 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return change;
3629}
3630
Takashi Iwai0ba21762007-04-16 11:29:14 +02003631static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3632 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3635 hda_nid_t nid = kcontrol->private_value;
3636 unsigned short val;
3637 unsigned int sbits;
3638
Andrew Paprocki3982d172007-12-19 12:13:44 +01003639 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 sbits = convert_to_spdif_status(val);
3641 ucontrol->value.iec958.status[0] = sbits;
3642 ucontrol->value.iec958.status[1] = sbits >> 8;
3643 ucontrol->value.iec958.status[2] = sbits >> 16;
3644 ucontrol->value.iec958.status[3] = sbits >> 24;
3645 return 0;
3646}
3647
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003648static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 {
3650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003651 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 .info = snd_hda_spdif_in_switch_info,
3653 .get = snd_hda_spdif_in_switch_get,
3654 .put = snd_hda_spdif_in_switch_put,
3655 },
3656 {
3657 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003659 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 .info = snd_hda_spdif_mask_info,
3661 .get = snd_hda_spdif_in_status_get,
3662 },
3663 { } /* end */
3664};
3665
3666/**
3667 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3668 * @codec: the HDA codec
3669 * @nid: audio in widget NID
3670 *
3671 * Creates controls related with the SPDIF input.
3672 * Called from each patch supporting the SPDIF in.
3673 *
3674 * Returns 0 if successful, or a negative error code.
3675 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003676int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003679 struct snd_kcontrol *kctl;
3680 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003681 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Takashi Iwaidcda5802012-10-12 17:24:51 +02003683 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003684 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003685 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3686 return -EBUSY;
3687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3689 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003690 if (!kctl)
3691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003693 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003694 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return err;
3696 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003697 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003698 snd_hda_codec_read(codec, nid, 0,
3699 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003700 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 return 0;
3702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003703EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003705/*
3706 * command cache
3707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Takashi Iwaic370dd62012-12-13 18:30:04 +01003709/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003710#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3711#define get_cmd_cache_nid(key) ((key) & 0xff)
3712#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3713
3714/**
3715 * snd_hda_codec_write_cache - send a single command with caching
3716 * @codec: the HDA codec
3717 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003718 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003719 * @verb: the verb to send
3720 * @parm: the parameter for the verb
3721 *
3722 * Send a single command without waiting for response.
3723 *
3724 * Returns 0 if successful, or a negative error code.
3725 */
3726int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003727 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003728{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003729 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003730 struct hda_cache_head *c;
3731 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003732 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003733
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003734 cache_only = codec->cached_write;
3735 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003736 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003737 if (err < 0)
3738 return err;
3739 }
3740
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003741 /* parm may contain the verb stuff for get/set amp */
3742 verb = verb | (parm >> 8);
3743 parm &= 0xff;
3744 key = build_cmd_cache_key(nid, verb);
3745 mutex_lock(&codec->bus->cmd_mutex);
3746 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003747 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003748 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003749 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003750 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003751 mutex_unlock(&codec->bus->cmd_mutex);
3752 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003753}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003754EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003755
Takashi Iwaid5191e52009-11-16 14:58:17 +01003756/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003757 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3758 * @codec: the HDA codec
3759 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003760 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003761 * @verb: the verb to send
3762 * @parm: the parameter for the verb
3763 *
3764 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3765 * command if the parameter is already identical with the cached value.
3766 * If not, it sends the command and refreshes the cache.
3767 *
3768 * Returns 0 if successful, or a negative error code.
3769 */
3770int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003771 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003772{
3773 struct hda_cache_head *c;
3774 u32 key;
3775
3776 /* parm may contain the verb stuff for get/set amp */
3777 verb = verb | (parm >> 8);
3778 parm &= 0xff;
3779 key = build_cmd_cache_key(nid, verb);
3780 mutex_lock(&codec->bus->cmd_mutex);
3781 c = get_hash(&codec->cmd_cache, key);
3782 if (c && c->val == parm) {
3783 mutex_unlock(&codec->bus->cmd_mutex);
3784 return 0;
3785 }
3786 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003787 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003788}
3789EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3790
3791/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003792 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3793 * @codec: HD-audio codec
3794 *
3795 * Execute all verbs recorded in the command caches to resume.
3796 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003797void snd_hda_codec_resume_cache(struct hda_codec *codec)
3798{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003799 int i;
3800
Takashi Iwaic370dd62012-12-13 18:30:04 +01003801 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003802 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003803 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3804 struct hda_cache_head *buffer;
3805 u32 key;
3806
3807 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3808 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003809 if (!key)
3810 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003811 if (!buffer->dirty)
3812 continue;
3813 buffer->dirty = 0;
3814 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003815 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3816 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003817 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003818 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003819 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003821EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003822
3823/**
3824 * snd_hda_sequence_write_cache - sequence writes with caching
3825 * @codec: the HDA codec
3826 * @seq: VERB array to send
3827 *
3828 * Send the commands sequentially from the given array.
3829 * Thte commands are recorded on cache for power-save and resume.
3830 * The array must be terminated with NID=0.
3831 */
3832void snd_hda_sequence_write_cache(struct hda_codec *codec,
3833 const struct hda_verb *seq)
3834{
3835 for (; seq->nid; seq++)
3836 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3837 seq->param);
3838}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003839EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003840
Takashi Iwaidc870f32013-01-22 15:24:30 +01003841/**
3842 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3843 * @codec: HD-audio codec
3844 */
3845void snd_hda_codec_flush_cache(struct hda_codec *codec)
3846{
3847 snd_hda_codec_resume_amp(codec);
3848 snd_hda_codec_resume_cache(codec);
3849}
3850EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3851
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003852void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003853 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003854{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003855 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003856 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003857
Takashi Iwaicb53c622007-08-10 17:21:45 +02003858 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003859 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003860 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003861 if (!(wcaps & AC_WCAP_POWER))
3862 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003863 if (codec->power_filter) {
3864 state = codec->power_filter(codec, nid, power_state);
3865 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003866 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003867 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003868 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003869 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003870 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003871}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003872EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3873
3874/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003875 * supported power states check
3876 */
3877static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3878 unsigned int power_state)
3879{
3880 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3881
Mengdong Line037cb42012-08-10 14:11:58 +02003882 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003883 return false;
3884 if (sup & power_state)
3885 return true;
3886 else
3887 return false;
3888}
3889
3890/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003891 * wait until the state is reached, returns the current state
3892 */
3893static unsigned int hda_sync_power_state(struct hda_codec *codec,
3894 hda_nid_t fg,
3895 unsigned int power_state)
3896{
3897 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3898 unsigned int state, actual_state;
3899
3900 for (;;) {
3901 state = snd_hda_codec_read(codec, fg, 0,
3902 AC_VERB_GET_POWER_STATE, 0);
3903 if (state & AC_PWRST_ERROR)
3904 break;
3905 actual_state = (state >> 4) & 0x0f;
3906 if (actual_state == power_state)
3907 break;
3908 if (time_after_eq(jiffies, end_time))
3909 break;
3910 /* wait until the codec reachs to the target state */
3911 msleep(1);
3912 }
3913 return state;
3914}
3915
Takashi Iwai9419ab62013-01-24 17:23:35 +01003916/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003917unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3918 hda_nid_t nid,
3919 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003920{
3921 if (power_state == AC_PWRST_D3 &&
3922 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3923 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3924 int eapd = snd_hda_codec_read(codec, nid, 0,
3925 AC_VERB_GET_EAPD_BTLENABLE, 0);
3926 if (eapd & 0x02)
3927 return AC_PWRST_D0;
3928 }
3929 return power_state;
3930}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003931EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003932
Takashi Iwai432c6412012-08-28 09:59:20 -07003933/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003934 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003935 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003936static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003937 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003938{
Takashi Iwaid8193872012-08-31 07:54:38 -07003939 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003940 int count;
3941 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003942 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003943
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003944 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003945 if (power_state == AC_PWRST_D3) {
3946 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003947 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003948 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003949 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003950
3951 /* repeat power states setting at most 10 times*/
3952 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003953 if (codec->patch_ops.set_power_state)
3954 codec->patch_ops.set_power_state(codec, fg,
3955 power_state);
3956 else {
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003957 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003958 AC_VERB_SET_POWER_STATE,
3959 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003960 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003961 }
3962 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003963 if (!(state & AC_PWRST_ERROR))
3964 break;
3965 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003966
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003967 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003968}
Takashi Iwai54d17402005-11-21 16:33:22 +01003969
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003970/* sync power states of all widgets;
3971 * this is called at the end of codec parsing
3972 */
3973static void sync_power_up_states(struct hda_codec *codec)
3974{
3975 hda_nid_t nid = codec->start_nid;
3976 int i;
3977
Takashi Iwaiba615b82013-03-13 14:47:21 +01003978 /* don't care if no filter is used */
3979 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003980 return;
3981
3982 for (i = 0; i < codec->num_nodes; i++, nid++) {
3983 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003984 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003985 if (!(wcaps & AC_WCAP_POWER))
3986 continue;
3987 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3988 if (target == AC_PWRST_D0)
3989 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003990 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003991 snd_hda_codec_write(codec, nid, 0,
3992 AC_VERB_SET_POWER_STATE, target);
3993 }
3994}
3995
Takashi Iwai11aeff02008-07-30 15:01:46 +02003996#ifdef CONFIG_SND_HDA_HWDEP
3997/* execute additional init verbs */
3998static void hda_exec_init_verbs(struct hda_codec *codec)
3999{
4000 if (codec->init_verbs.list)
4001 snd_hda_sequence_write(codec, codec->init_verbs.list);
4002}
4003#else
4004static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4005#endif
4006
Takashi Iwai2a439522011-07-26 09:52:50 +02004007#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004008/*
4009 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004010 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004011 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004012static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004013{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004014 unsigned int state;
4015
Takashi Iwai989c3182012-11-19 14:14:58 +01004016 codec->in_pm = 1;
4017
Takashi Iwaicb53c622007-08-10 17:21:45 +02004018 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004019 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004020 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004021 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004022 /* Cancel delayed work if we aren't currently running from it. */
4023 if (!in_wq)
4024 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004025 spin_lock(&codec->power_lock);
4026 snd_hda_update_power_acct(codec);
4027 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004028 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004029 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004030 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004031 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004032 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004033 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004034}
4035
Takashi Iwaic370dd62012-12-13 18:30:04 +01004036/* mark all entries of cmd and amp caches dirty */
4037static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4038{
4039 int i;
4040 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4041 struct hda_cache_head *cmd;
4042 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4043 cmd->dirty = 1;
4044 }
4045 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4046 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004047 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004048 amp->head.dirty = 1;
4049 }
4050}
4051
Takashi Iwaicb53c622007-08-10 17:21:45 +02004052/*
4053 * kick up codec; used both from PM and power-save
4054 */
4055static void hda_call_codec_resume(struct hda_codec *codec)
4056{
Takashi Iwai989c3182012-11-19 14:14:58 +01004057 codec->in_pm = 1;
4058
Takashi Iwaic370dd62012-12-13 18:30:04 +01004059 hda_mark_cmd_cache_dirty(codec);
4060
Takashi Iwai7f308302012-05-08 16:52:23 +02004061 /* set as if powered on for avoiding re-entering the resume
4062 * in the resume / power-save sequence
4063 */
4064 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004065 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004066 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004067 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004068 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004069 if (codec->patch_ops.resume)
4070 codec->patch_ops.resume(codec);
4071 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004072 if (codec->patch_ops.init)
4073 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004074 snd_hda_codec_resume_amp(codec);
4075 snd_hda_codec_resume_cache(codec);
4076 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004077
4078 if (codec->jackpoll_interval)
4079 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004080 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004081 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004082
4083 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004084 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004085}
Takashi Iwai2a439522011-07-26 09:52:50 +02004086#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087
Takashi Iwai54d17402005-11-21 16:33:22 +01004088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089/**
4090 * snd_hda_build_controls - build mixer controls
4091 * @bus: the BUS
4092 *
4093 * Creates mixer controls for each codec included in the bus.
4094 *
4095 * Returns 0 if successful, otherwise a negative error code.
4096 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004097int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004099 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Takashi Iwai0ba21762007-04-16 11:29:14 +02004101 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004102 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004103 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004104 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004105 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004106 err = snd_hda_codec_reset(codec);
4107 if (err < 0) {
4108 printk(KERN_ERR
4109 "hda_codec: cannot revert codec\n");
4110 return err;
4111 }
4112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004114 return 0;
4115}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004116EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004118/*
4119 * add standard channel maps if not specified
4120 */
4121static int add_std_chmaps(struct hda_codec *codec)
4122{
4123 int i, str, err;
4124
4125 for (i = 0; i < codec->num_pcms; i++) {
4126 for (str = 0; str < 2; str++) {
4127 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4128 struct hda_pcm_stream *hinfo =
4129 &codec->pcm_info[i].stream[str];
4130 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004131 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004132
4133 if (codec->pcm_info[i].own_chmap)
4134 continue;
4135 if (!pcm || !hinfo->substreams)
4136 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004137 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4138 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004139 hinfo->channels_max,
4140 0, &chmap);
4141 if (err < 0)
4142 return err;
4143 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4144 }
4145 }
4146 return 0;
4147}
4148
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004149/* default channel maps for 2.1 speakers;
4150 * since HD-audio supports only stereo, odd number channels are omitted
4151 */
4152const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4153 { .channels = 2,
4154 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4155 { .channels = 4,
4156 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4157 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4158 { }
4159};
4160EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4161
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004162int snd_hda_codec_build_controls(struct hda_codec *codec)
4163{
4164 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004165 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004166 /* continue to initialize... */
4167 if (codec->patch_ops.init)
4168 err = codec->patch_ops.init(codec);
4169 if (!err && codec->patch_ops.build_controls)
4170 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004171 if (err < 0)
4172 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004173
4174 /* we create chmaps here instead of build_pcms */
4175 err = add_std_chmaps(codec);
4176 if (err < 0)
4177 return err;
4178
David Henningsson26a6cb62012-10-09 15:04:21 +02004179 if (codec->jackpoll_interval)
4180 hda_jackpoll_work(&codec->jackpoll_work.work);
4181 else
4182 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004183 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return 0;
4185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/*
4188 * stream formats
4189 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004190struct hda_rate_tbl {
4191 unsigned int hz;
4192 unsigned int alsa_bits;
4193 unsigned int hda_fmt;
4194};
4195
Takashi Iwai92f10b32010-08-03 14:21:00 +02004196/* rate = base * mult / div */
4197#define HDA_RATE(base, mult, div) \
4198 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4199 (((div) - 1) << AC_FMT_DIV_SHIFT))
4200
Takashi Iwaibefdf312005-08-22 13:57:55 +02004201static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004203
4204 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004205 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4206 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4207 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4208 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4209 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4210 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4211 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4212 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4213 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4214 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4215 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004216#define AC_PAR_PCM_RATE_BITS 11
4217 /* up to bits 10, 384kHZ isn't supported properly */
4218
4219 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004220 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004221
Takashi Iwaibefdf312005-08-22 13:57:55 +02004222 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223};
4224
4225/**
4226 * snd_hda_calc_stream_format - calculate format bitset
4227 * @rate: the sample rate
4228 * @channels: the number of channels
4229 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4230 * @maxbps: the max. bps
4231 *
4232 * Calculate the format bitset from the given rate, channels and th PCM format.
4233 *
4234 * Return zero if invalid.
4235 */
4236unsigned int snd_hda_calc_stream_format(unsigned int rate,
4237 unsigned int channels,
4238 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004239 unsigned int maxbps,
4240 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
4242 int i;
4243 unsigned int val = 0;
4244
Takashi Iwaibefdf312005-08-22 13:57:55 +02004245 for (i = 0; rate_bits[i].hz; i++)
4246 if (rate_bits[i].hz == rate) {
4247 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 break;
4249 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004250 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 snd_printdd("invalid rate %d\n", rate);
4252 return 0;
4253 }
4254
4255 if (channels == 0 || channels > 8) {
4256 snd_printdd("invalid channels %d\n", channels);
4257 return 0;
4258 }
4259 val |= channels - 1;
4260
4261 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004262 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004263 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004264 break;
4265 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004266 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 case 20:
4269 case 24:
4270 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004271 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004272 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004274 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004276 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 break;
4278 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004279 snd_printdd("invalid format width %d\n",
4280 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return 0;
4282 }
4283
Anssi Hannula32c168c2010-08-03 13:28:57 +03004284 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004285 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return val;
4288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004289EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004291static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4292 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004293{
4294 unsigned int val = 0;
4295 if (nid != codec->afg &&
4296 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4297 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4298 if (!val || val == -1)
4299 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4300 if (!val || val == -1)
4301 return 0;
4302 return val;
4303}
4304
4305static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4306{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004307 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004308 get_pcm_param);
4309}
4310
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004311static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4312 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004313{
4314 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4315 if (!streams || streams == -1)
4316 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4317 if (!streams || streams == -1)
4318 return 0;
4319 return streams;
4320}
4321
4322static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4323{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004324 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004325 get_stream_param);
4326}
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328/**
4329 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4330 * @codec: the HDA codec
4331 * @nid: NID to query
4332 * @ratesp: the pointer to store the detected rate bitflags
4333 * @formatsp: the pointer to store the detected formats
4334 * @bpsp: the pointer to store the detected format widths
4335 *
4336 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4337 * or @bsps argument is ignored.
4338 *
4339 * Returns 0 if successful, otherwise a negative error code.
4340 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004341int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4343{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004344 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004346 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004347 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 if (ratesp) {
4350 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004351 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004353 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004355 if (rates == 0) {
4356 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4357 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4358 nid, val,
4359 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4360 return -EIO;
4361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 *ratesp = rates;
4363 }
4364
4365 if (formatsp || bpsp) {
4366 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004367 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004369 streams = query_stream_param(codec, nid);
4370 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 bps = 0;
4374 if (streams & AC_SUPFMT_PCM) {
4375 if (val & AC_SUPPCM_BITS_8) {
4376 formats |= SNDRV_PCM_FMTBIT_U8;
4377 bps = 8;
4378 }
4379 if (val & AC_SUPPCM_BITS_16) {
4380 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4381 bps = 16;
4382 }
4383 if (wcaps & AC_WCAP_DIGITAL) {
4384 if (val & AC_SUPPCM_BITS_32)
4385 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4386 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4387 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4388 if (val & AC_SUPPCM_BITS_24)
4389 bps = 24;
4390 else if (val & AC_SUPPCM_BITS_20)
4391 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004392 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4393 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4395 if (val & AC_SUPPCM_BITS_32)
4396 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 else if (val & AC_SUPPCM_BITS_24)
4398 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004399 else if (val & AC_SUPPCM_BITS_20)
4400 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004403#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004404 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004406 if (!bps)
4407 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004408 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004409#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004410 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004411 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 /* temporary hack: we have still no proper support
4413 * for the direct AC3 stream...
4414 */
4415 formats |= SNDRV_PCM_FMTBIT_U8;
4416 bps = 8;
4417 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004418 if (formats == 0) {
4419 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4420 "(nid=0x%x, val=0x%x, ovrd=%i, "
4421 "streams=0x%x)\n",
4422 nid, val,
4423 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4424 streams);
4425 return -EIO;
4426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (formatsp)
4428 *formatsp = formats;
4429 if (bpsp)
4430 *bpsp = bps;
4431 }
4432
4433 return 0;
4434}
Stephen Warren384a48d2011-06-01 11:14:21 -06004435EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004438 * snd_hda_is_supported_format - Check the validity of the format
4439 * @codec: HD-audio codec
4440 * @nid: NID to check
4441 * @format: the HD-audio format value to check
4442 *
4443 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 *
4445 * Returns 1 if supported, 0 if not.
4446 */
4447int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4448 unsigned int format)
4449{
4450 int i;
4451 unsigned int val = 0, rate, stream;
4452
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004453 val = query_pcm_param(codec, nid);
4454 if (!val)
4455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004458 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004459 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (val & (1 << i))
4461 break;
4462 return 0;
4463 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004464 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return 0;
4466
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004467 stream = query_stream_param(codec, nid);
4468 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return 0;
4470
4471 if (stream & AC_SUPFMT_PCM) {
4472 switch (format & 0xf0) {
4473 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004474 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return 0;
4476 break;
4477 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004478 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return 0;
4480 break;
4481 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004482 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return 0;
4484 break;
4485 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004486 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return 0;
4488 break;
4489 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004490 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return 0;
4492 break;
4493 default:
4494 return 0;
4495 }
4496 } else {
4497 /* FIXME: check for float32 and AC3? */
4498 }
4499
4500 return 1;
4501}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004502EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504/*
4505 * PCM stuff
4506 */
4507static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4508 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004509 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 return 0;
4512}
4513
4514static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4515 struct hda_codec *codec,
4516 unsigned int stream_tag,
4517 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004518 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4521 return 0;
4522}
4523
4524static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4525 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004526 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Takashi Iwai888afa12008-03-18 09:57:50 +01004528 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return 0;
4530}
4531
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004532static int set_pcm_default_values(struct hda_codec *codec,
4533 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004535 int err;
4536
Takashi Iwai0ba21762007-04-16 11:29:14 +02004537 /* query support PCM information from the given NID */
4538 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004539 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004540 info->rates ? NULL : &info->rates,
4541 info->formats ? NULL : &info->formats,
4542 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004543 if (err < 0)
4544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 }
4546 if (info->ops.open == NULL)
4547 info->ops.open = hda_pcm_default_open_close;
4548 if (info->ops.close == NULL)
4549 info->ops.close = hda_pcm_default_open_close;
4550 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004551 if (snd_BUG_ON(!info->nid))
4552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 info->ops.prepare = hda_pcm_default_prepare;
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004556 if (snd_BUG_ON(!info->nid))
4557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 info->ops.cleanup = hda_pcm_default_cleanup;
4559 }
4560 return 0;
4561}
4562
Takashi Iwaieb541332010-08-06 13:48:11 +02004563/*
4564 * codec prepare/cleanup entries
4565 */
4566int snd_hda_codec_prepare(struct hda_codec *codec,
4567 struct hda_pcm_stream *hinfo,
4568 unsigned int stream,
4569 unsigned int format,
4570 struct snd_pcm_substream *substream)
4571{
4572 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004573 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004574 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4575 if (ret >= 0)
4576 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004577 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004578 return ret;
4579}
4580EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4581
4582void snd_hda_codec_cleanup(struct hda_codec *codec,
4583 struct hda_pcm_stream *hinfo,
4584 struct snd_pcm_substream *substream)
4585{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004586 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004587 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004588 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004589}
4590EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4591
Takashi Iwaid5191e52009-11-16 14:58:17 +01004592/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004593const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4594 "Audio", "SPDIF", "HDMI", "Modem"
4595};
4596
Takashi Iwai176d5332008-07-30 15:01:44 +02004597/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004598 * get the empty PCM device number to assign
4599 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004600static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004601{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004602 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004603 /* assigned to static slots up to dev#10; if more needed, assign
4604 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4605 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004606 static int audio_idx[HDA_PCM_NTYPES][5] = {
4607 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4608 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004609 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004610 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004611 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004612 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004613
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004614 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004615 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4616 return -EINVAL;
4617 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004618
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004619 for (i = 0; audio_idx[type][i] >= 0; i++) {
4620#ifndef CONFIG_SND_DYNAMIC_MINORS
4621 if (audio_idx[type][i] >= 8)
4622 break;
4623#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004624 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4625 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004626 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004627
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004628#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004629 /* non-fixed slots starting from 10 */
4630 for (i = 10; i < 32; i++) {
4631 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4632 return i;
4633 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004634#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004635
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004636 snd_printk(KERN_WARNING "Too many %s devices\n",
4637 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004638#ifndef CONFIG_SND_DYNAMIC_MINORS
4639 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4640#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004641 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004642}
4643
4644/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004645 * attach a new PCM stream
4646 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004647static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004648{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004649 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004650 struct hda_pcm_stream *info;
4651 int stream, err;
4652
Takashi Iwaib91f0802008-11-04 08:43:08 +01004653 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004654 return -EINVAL;
4655 for (stream = 0; stream < 2; stream++) {
4656 info = &pcm->stream[stream];
4657 if (info->substreams) {
4658 err = set_pcm_default_values(codec, info);
4659 if (err < 0)
4660 return err;
4661 }
4662 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004663 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004664}
4665
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004666/* assign all PCMs of the given codec */
4667int snd_hda_codec_build_pcms(struct hda_codec *codec)
4668{
4669 unsigned int pcm;
4670 int err;
4671
4672 if (!codec->num_pcms) {
4673 if (!codec->patch_ops.build_pcms)
4674 return 0;
4675 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004676 if (err < 0) {
4677 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004678 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004679 err = snd_hda_codec_reset(codec);
4680 if (err < 0) {
4681 printk(KERN_ERR
4682 "hda_codec: cannot revert codec\n");
4683 return err;
4684 }
4685 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004686 }
4687 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4688 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4689 int dev;
4690
4691 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004692 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004693
4694 if (!cpcm->pcm) {
4695 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4696 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004697 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004698 cpcm->device = dev;
4699 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004700 if (err < 0) {
4701 printk(KERN_ERR "hda_codec: cannot attach "
4702 "PCM stream %d for codec #%d\n",
4703 dev, codec->addr);
4704 continue; /* no fatal error */
4705 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004706 }
4707 }
4708 return 0;
4709}
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711/**
4712 * snd_hda_build_pcms - build PCM information
4713 * @bus: the BUS
4714 *
4715 * Create PCM information for each codec included in the bus.
4716 *
4717 * The build_pcms codec patch is requested to set up codec->num_pcms and
4718 * codec->pcm_info properly. The array is referred by the top-level driver
4719 * to create its PCM instances.
4720 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4721 * callback.
4722 *
4723 * At least, substreams, channels_min and channels_max must be filled for
4724 * each stream. substreams = 0 indicates that the stream doesn't exist.
4725 * When rates and/or formats are zero, the supported values are queried
4726 * from the given nid. The nid is used also by the default ops.prepare
4727 * and ops.cleanup callbacks.
4728 *
4729 * The driver needs to call ops.open in its open callback. Similarly,
4730 * ops.close is supposed to be called in the close callback.
4731 * ops.prepare should be called in the prepare or hw_params callback
4732 * with the proper parameters for set up.
4733 * ops.cleanup should be called in hw_free for clean up of streams.
4734 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004735 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004737int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004739 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Takashi Iwai0ba21762007-04-16 11:29:14 +02004741 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004742 int err = snd_hda_codec_build_pcms(codec);
4743 if (err < 0)
4744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 }
4746 return 0;
4747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004748EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750/**
4751 * snd_hda_check_board_config - compare the current codec with the config table
4752 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004753 * @num_configs: number of config enums
4754 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 * @tbl: configuration table, terminated by null entries
4756 *
4757 * Compares the modelname or PCI subsystem id of the current codec with the
4758 * given configuration table. If a matching entry is found, returns its
4759 * config value (supposed to be 0 or positive).
4760 *
4761 * If no entries are matching, the function returns a negative value.
4762 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004763int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004764 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004765 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004767 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004768 int i;
4769 for (i = 0; i < num_configs; i++) {
4770 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004771 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004772 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4773 "selected\n", models[i]);
4774 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
4776 }
4777 }
4778
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004779 if (!codec->bus->pci || !tbl)
4780 return -1;
4781
4782 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4783 if (!tbl)
4784 return -1;
4785 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004786#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004787 char tmp[10];
4788 const char *model = NULL;
4789 if (models)
4790 model = models[tbl->value];
4791 if (!model) {
4792 sprintf(tmp, "#%d", tbl->value);
4793 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004795 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4796 "for config %x:%x (%s)\n",
4797 model, tbl->subvendor, tbl->subdevice,
4798 (tbl->name ? tbl->name : "Unknown device"));
4799#endif
4800 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 }
4802 return -1;
4803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004804EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004807 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004808 subsystem ID with the
4809 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004810
4811 This is important for Gateway notebooks with SB450 HDA Audio
4812 where the vendor ID of the PCI device is:
4813 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4814 and the vendor/subvendor are found only at the codec.
4815
4816 * @codec: the HDA codec
4817 * @num_configs: number of config enums
4818 * @models: array of model name strings
4819 * @tbl: configuration table, terminated by null entries
4820 *
4821 * Compares the modelname or PCI subsystem id of the current codec with the
4822 * given configuration table. If a matching entry is found, returns its
4823 * config value (supposed to be 0 or positive).
4824 *
4825 * If no entries are matching, the function returns a negative value.
4826 */
4827int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004828 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004829 const struct snd_pci_quirk *tbl)
4830{
4831 const struct snd_pci_quirk *q;
4832
4833 /* Search for codec ID */
4834 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004835 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4836 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4837 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004838 break;
4839 }
4840
4841 if (!q->subvendor)
4842 return -1;
4843
4844 tbl = q;
4845
4846 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004847#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004848 char tmp[10];
4849 const char *model = NULL;
4850 if (models)
4851 model = models[tbl->value];
4852 if (!model) {
4853 sprintf(tmp, "#%d", tbl->value);
4854 model = tmp;
4855 }
4856 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4857 "for config %x:%x (%s)\n",
4858 model, tbl->subvendor, tbl->subdevice,
4859 (tbl->name ? tbl->name : "Unknown device"));
4860#endif
4861 return tbl->value;
4862 }
4863 return -1;
4864}
4865EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4866
4867/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 * snd_hda_add_new_ctls - create controls from the array
4869 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004870 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 *
4872 * This helper function creates and add new controls in the given array.
4873 * The array must be terminated with an empty entry as terminator.
4874 *
4875 * Returns 0 if successful, or a negative error code.
4876 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004877int snd_hda_add_new_ctls(struct hda_codec *codec,
4878 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004880 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004883 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004884 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004885 if (knew->iface == -1) /* skip this codec private value */
4886 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004887 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004888 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004889 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004890 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004891 if (addr > 0)
4892 kctl->id.device = addr;
4893 if (idx > 0)
4894 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004895 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004896 if (!err)
4897 break;
4898 /* try first with another device index corresponding to
4899 * the codec addr; if it still fails (or it's the
4900 * primary codec), then try another control index
4901 */
4902 if (!addr && codec->addr)
4903 addr = codec->addr;
4904 else if (!idx && !knew->index) {
4905 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004906 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004907 if (idx <= 0)
4908 return err;
4909 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004910 return err;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913 return 0;
4914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004915EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Takashi Iwai83012a72012-08-24 18:38:08 +02004917#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004918static void hda_power_work(struct work_struct *work)
4919{
4920 struct hda_codec *codec =
4921 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004922 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004923 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004924
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004925 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004926 if (codec->power_transition > 0) { /* during power-up sequence? */
4927 spin_unlock(&codec->power_lock);
4928 return;
4929 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004930 if (!codec->power_on || codec->power_count) {
4931 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004932 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004933 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004934 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004935 spin_unlock(&codec->power_lock);
4936
Dylan Reidd17344b2012-09-28 15:57:01 -07004937 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004938 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4939 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004940}
4941
4942static void hda_keep_power_on(struct hda_codec *codec)
4943{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004944 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004945 codec->power_count++;
4946 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004947 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004948 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004949 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004950}
4951
Takashi Iwaid5191e52009-11-16 14:58:17 +01004952/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004953void snd_hda_update_power_acct(struct hda_codec *codec)
4954{
4955 unsigned long delta = jiffies - codec->power_jiffies;
4956 if (codec->power_on)
4957 codec->power_on_acct += delta;
4958 else
4959 codec->power_off_acct += delta;
4960 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004961}
4962
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004963/* Transition to powered up, if wait_power_down then wait for a pending
4964 * transition to D3 to complete. A pending D3 transition is indicated
4965 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004966/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004967static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004968{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004969 /* Return if power_on or transitioning to power_on, unless currently
4970 * powering down. */
4971 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004972 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004973 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004974 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004975
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004976 cancel_delayed_work_sync(&codec->power_work);
4977
4978 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004979 /* If the power down delayed work was cancelled above before starting,
4980 * then there is no need to go through power up here.
4981 */
4982 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004983 if (codec->power_transition < 0)
4984 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004985 return;
4986 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004987
Takashi Iwaid66fee52011-08-02 15:39:31 +02004988 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004989 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004990 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004991 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004992 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004993 spin_unlock(&codec->power_lock);
4994
Takashi Iwaicb53c622007-08-10 17:21:45 +02004995 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004996
4997 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004998 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004999}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005000
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005001#define power_save(codec) \
5002 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005003
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005004/* Transition to powered down */
5005static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005006{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005007 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005008 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005009
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005010 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005011 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005012 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005013 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005014 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005015}
5016
5017/**
5018 * snd_hda_power_save - Power-up/down/sync the codec
5019 * @codec: HD-audio codec
5020 * @delta: the counter delta to change
5021 *
5022 * Change the power-up counter via @delta, and power up or down the hardware
5023 * appropriately. For the power-down, queue to the delayed action.
5024 * Passing zero to @delta means to synchronize the power state.
5025 */
5026void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5027{
5028 spin_lock(&codec->power_lock);
5029 codec->power_count += delta;
5030 trace_hda_power_count(codec);
5031 if (delta > 0)
5032 __snd_hda_power_up(codec, d3wait);
5033 else
5034 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005035 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005036}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005037EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005038
Takashi Iwaid5191e52009-11-16 14:58:17 +01005039/**
5040 * snd_hda_check_amp_list_power - Check the amp list and update the power
5041 * @codec: HD-audio codec
5042 * @check: the object containing an AMP list and the status
5043 * @nid: NID to check / update
5044 *
5045 * Check whether the given NID is in the amp list. If it's in the list,
5046 * check the current AMP status, and update the the power-status according
5047 * to the mute status.
5048 *
5049 * This function is supposed to be set or called from the check_power_status
5050 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005051 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005052int snd_hda_check_amp_list_power(struct hda_codec *codec,
5053 struct hda_loopback_check *check,
5054 hda_nid_t nid)
5055{
Takashi Iwai031024e2011-05-02 11:29:30 +02005056 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005057 int ch, v;
5058
5059 if (!check->amplist)
5060 return 0;
5061 for (p = check->amplist; p->nid; p++) {
5062 if (p->nid == nid)
5063 break;
5064 }
5065 if (!p->nid)
5066 return 0; /* nothing changed */
5067
5068 for (p = check->amplist; p->nid; p++) {
5069 for (ch = 0; ch < 2; ch++) {
5070 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5071 p->idx);
5072 if (!(v & HDA_AMP_MUTE) && v > 0) {
5073 if (!check->power_on) {
5074 check->power_on = 1;
5075 snd_hda_power_up(codec);
5076 }
5077 return 1;
5078 }
5079 }
5080 }
5081 if (check->power_on) {
5082 check->power_on = 0;
5083 snd_hda_power_down(codec);
5084 }
5085 return 0;
5086}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005087EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005090/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005091 * Channel mode helper
5092 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005093
5094/**
5095 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5096 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005097int snd_hda_ch_mode_info(struct hda_codec *codec,
5098 struct snd_ctl_elem_info *uinfo,
5099 const struct hda_channel_mode *chmode,
5100 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005101{
5102 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5103 uinfo->count = 1;
5104 uinfo->value.enumerated.items = num_chmodes;
5105 if (uinfo->value.enumerated.item >= num_chmodes)
5106 uinfo->value.enumerated.item = num_chmodes - 1;
5107 sprintf(uinfo->value.enumerated.name, "%dch",
5108 chmode[uinfo->value.enumerated.item].channels);
5109 return 0;
5110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005111EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005112
Takashi Iwaid5191e52009-11-16 14:58:17 +01005113/**
5114 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5115 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005116int snd_hda_ch_mode_get(struct hda_codec *codec,
5117 struct snd_ctl_elem_value *ucontrol,
5118 const struct hda_channel_mode *chmode,
5119 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005120 int max_channels)
5121{
5122 int i;
5123
5124 for (i = 0; i < num_chmodes; i++) {
5125 if (max_channels == chmode[i].channels) {
5126 ucontrol->value.enumerated.item[0] = i;
5127 break;
5128 }
5129 }
5130 return 0;
5131}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005132EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005133
Takashi Iwaid5191e52009-11-16 14:58:17 +01005134/**
5135 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5136 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005137int snd_hda_ch_mode_put(struct hda_codec *codec,
5138 struct snd_ctl_elem_value *ucontrol,
5139 const struct hda_channel_mode *chmode,
5140 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005141 int *max_channelsp)
5142{
5143 unsigned int mode;
5144
5145 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005146 if (mode >= num_chmodes)
5147 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005148 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005149 return 0;
5150 /* change the current channel setting */
5151 *max_channelsp = chmode[mode].channels;
5152 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005153 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005154 return 1;
5155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005156EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158/*
5159 * input MUX helper
5160 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005161
5162/**
5163 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5164 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005165int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5166 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
5168 unsigned int index;
5169
5170 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5171 uinfo->count = 1;
5172 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005173 if (!imux->num_items)
5174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 index = uinfo->value.enumerated.item;
5176 if (index >= imux->num_items)
5177 index = imux->num_items - 1;
5178 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5179 return 0;
5180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005181EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Takashi Iwaid5191e52009-11-16 14:58:17 +01005183/**
5184 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5185 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005186int snd_hda_input_mux_put(struct hda_codec *codec,
5187 const struct hda_input_mux *imux,
5188 struct snd_ctl_elem_value *ucontrol,
5189 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 unsigned int *cur_val)
5191{
5192 unsigned int idx;
5193
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005194 if (!imux->num_items)
5195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 idx = ucontrol->value.enumerated.item[0];
5197 if (idx >= imux->num_items)
5198 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005199 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005201 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5202 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 *cur_val = idx;
5204 return 1;
5205}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005206EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208
5209/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005210 * process kcontrol info callback of a simple string enum array
5211 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5212 */
5213int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5214 struct snd_ctl_elem_info *uinfo,
5215 int num_items, const char * const *texts)
5216{
5217 static const char * const texts_default[] = {
5218 "Disabled", "Enabled"
5219 };
5220
5221 if (!texts || !num_items) {
5222 num_items = 2;
5223 texts = texts_default;
5224 }
5225
5226 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5227 uinfo->count = 1;
5228 uinfo->value.enumerated.items = num_items;
5229 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5230 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5231 strcpy(uinfo->value.enumerated.name,
5232 texts[uinfo->value.enumerated.item]);
5233 return 0;
5234}
5235EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5236
5237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 * Multi-channel / digital-out PCM helper functions
5239 */
5240
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005241/* setup SPDIF output stream */
5242static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5243 unsigned int stream_tag, unsigned int format)
5244{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005245 struct hda_spdif_out *spdif;
5246 unsigned int curr_fmt;
5247 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005248
Laurence Darby3bef1c32012-11-03 17:00:06 +00005249 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5250 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5251 AC_VERB_GET_STREAM_FORMAT, 0);
5252 reset = codec->spdif_status_reset &&
5253 (spdif->ctls & AC_DIG1_ENABLE) &&
5254 curr_fmt != format;
5255
5256 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5257 updated */
5258 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005259 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005260 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005261 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005262 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005263 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005264 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005265 for (d = codec->slave_dig_outs; *d; d++)
5266 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5267 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005268 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005269 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005270 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005271 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005272 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005273}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005274
Takashi Iwai2f728532008-09-25 16:32:41 +02005275static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5276{
5277 snd_hda_codec_cleanup_stream(codec, nid);
5278 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005279 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005280 for (d = codec->slave_dig_outs; *d; d++)
5281 snd_hda_codec_cleanup_stream(codec, *d);
5282 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005283}
5284
Takashi Iwaid5191e52009-11-16 14:58:17 +01005285/**
5286 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5287 * @bus: HD-audio bus
5288 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005289void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5290{
5291 struct hda_codec *codec;
5292
5293 if (!bus)
5294 return;
5295 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005296 if (hda_codec_is_power_on(codec) &&
5297 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005298 codec->patch_ops.reboot_notify(codec);
5299 }
5300}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005301EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005302
Takashi Iwaid5191e52009-11-16 14:58:17 +01005303/**
5304 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005306int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5307 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308{
Ingo Molnar62932df2006-01-16 16:34:20 +01005309 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005310 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5311 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005312 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005314 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 return 0;
5316}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005317EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
Takashi Iwaid5191e52009-11-16 14:58:17 +01005319/**
5320 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5321 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005322int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5323 struct hda_multi_out *mout,
5324 unsigned int stream_tag,
5325 unsigned int format,
5326 struct snd_pcm_substream *substream)
5327{
5328 mutex_lock(&codec->spdif_mutex);
5329 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5330 mutex_unlock(&codec->spdif_mutex);
5331 return 0;
5332}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005333EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005334
Takashi Iwaid5191e52009-11-16 14:58:17 +01005335/**
5336 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5337 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005338int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5339 struct hda_multi_out *mout)
5340{
5341 mutex_lock(&codec->spdif_mutex);
5342 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5343 mutex_unlock(&codec->spdif_mutex);
5344 return 0;
5345}
5346EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5347
Takashi Iwaid5191e52009-11-16 14:58:17 +01005348/**
5349 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005351int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5352 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Ingo Molnar62932df2006-01-16 16:34:20 +01005354 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005356 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return 0;
5358}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005359EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
Takashi Iwaid5191e52009-11-16 14:58:17 +01005361/**
5362 * snd_hda_multi_out_analog_open - open analog outputs
5363 *
5364 * Open analog outputs and set up the hw-constraints.
5365 * If the digital outputs can be opened as slave, open the digital
5366 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005368int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5369 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005370 struct snd_pcm_substream *substream,
5371 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
Takashi Iwai9a081602008-02-12 18:37:26 +01005373 struct snd_pcm_runtime *runtime = substream->runtime;
5374 runtime->hw.channels_max = mout->max_channels;
5375 if (mout->dig_out_nid) {
5376 if (!mout->analog_rates) {
5377 mout->analog_rates = hinfo->rates;
5378 mout->analog_formats = hinfo->formats;
5379 mout->analog_maxbps = hinfo->maxbps;
5380 } else {
5381 runtime->hw.rates = mout->analog_rates;
5382 runtime->hw.formats = mout->analog_formats;
5383 hinfo->maxbps = mout->analog_maxbps;
5384 }
5385 if (!mout->spdif_rates) {
5386 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5387 &mout->spdif_rates,
5388 &mout->spdif_formats,
5389 &mout->spdif_maxbps);
5390 }
5391 mutex_lock(&codec->spdif_mutex);
5392 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005393 if ((runtime->hw.rates & mout->spdif_rates) &&
5394 (runtime->hw.formats & mout->spdif_formats)) {
5395 runtime->hw.rates &= mout->spdif_rates;
5396 runtime->hw.formats &= mout->spdif_formats;
5397 if (mout->spdif_maxbps < hinfo->maxbps)
5398 hinfo->maxbps = mout->spdif_maxbps;
5399 } else {
5400 mout->share_spdif = 0;
5401 /* FIXME: need notify? */
5402 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005403 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005404 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5407 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5408}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005409EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Takashi Iwaid5191e52009-11-16 14:58:17 +01005411/**
5412 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5413 *
5414 * Set up the i/o for analog out.
5415 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005417int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5418 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 unsigned int stream_tag,
5420 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005421 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
Takashi Iwaidda14412011-05-02 11:29:30 +02005423 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005425 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 int i;
5427
Ingo Molnar62932df2006-01-16 16:34:20 +01005428 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005429 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005430 if (mout->dig_out_nid && mout->share_spdif &&
5431 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005433 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5434 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005435 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005437 setup_dig_out_stream(codec, mout->dig_out_nid,
5438 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 } else {
5440 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005441 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
5443 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005444 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005447 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5448 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005449 if (!mout->no_share_stream &&
5450 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005452 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5453 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005454 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005455 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5456 if (!mout->no_share_stream && mout->hp_out_nid[i])
5457 snd_hda_codec_setup_stream(codec,
5458 mout->hp_out_nid[i],
5459 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 /* surrounds */
5462 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005463 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005464 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5465 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005466 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005467 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5468 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005470
5471 /* extra surrounds */
5472 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5473 int ch = 0;
5474 if (!mout->extra_out_nid[i])
5475 break;
5476 if (chs >= (i + 1) * 2)
5477 ch = i * 2;
5478 else if (!mout->no_share_stream)
5479 break;
5480 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5481 stream_tag, ch, format);
5482 }
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 return 0;
5485}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005486EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Takashi Iwaid5191e52009-11-16 14:58:17 +01005488/**
5489 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005491int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5492 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Takashi Iwaidda14412011-05-02 11:29:30 +02005494 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 int i;
5496
5497 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005498 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005500 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005501 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5502 if (mout->hp_out_nid[i])
5503 snd_hda_codec_cleanup_stream(codec,
5504 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005505 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5506 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005507 snd_hda_codec_cleanup_stream(codec,
5508 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005509 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005511 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 mout->dig_out_used = 0;
5513 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005514 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 return 0;
5516}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005517EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Takashi Iwai47408602012-04-20 13:06:53 +02005519/**
5520 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5521 *
5522 * Guess the suitable VREF pin bits to be set as the pin-control value.
5523 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5524 */
5525unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5526{
5527 unsigned int pincap;
5528 unsigned int oldval;
5529 oldval = snd_hda_codec_read(codec, pin, 0,
5530 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5531 pincap = snd_hda_query_pin_caps(codec, pin);
5532 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5533 /* Exception: if the default pin setup is vref50, we give it priority */
5534 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5535 return AC_PINCTL_VREF_80;
5536 else if (pincap & AC_PINCAP_VREF_50)
5537 return AC_PINCTL_VREF_50;
5538 else if (pincap & AC_PINCAP_VREF_100)
5539 return AC_PINCTL_VREF_100;
5540 else if (pincap & AC_PINCAP_VREF_GRD)
5541 return AC_PINCTL_VREF_GRD;
5542 return AC_PINCTL_VREF_HIZ;
5543}
5544EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5545
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005546/* correct the pin ctl value for matching with the pin cap */
5547unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5548 hda_nid_t pin, unsigned int val)
5549{
5550 static unsigned int cap_lists[][2] = {
5551 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5552 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5553 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5554 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5555 };
5556 unsigned int cap;
5557
5558 if (!val)
5559 return 0;
5560 cap = snd_hda_query_pin_caps(codec, pin);
5561 if (!cap)
5562 return val; /* don't know what to do... */
5563
5564 if (val & AC_PINCTL_OUT_EN) {
5565 if (!(cap & AC_PINCAP_OUT))
5566 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5567 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5568 val &= ~AC_PINCTL_HP_EN;
5569 }
5570
5571 if (val & AC_PINCTL_IN_EN) {
5572 if (!(cap & AC_PINCAP_IN))
5573 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5574 else {
5575 unsigned int vcap, vref;
5576 int i;
5577 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5578 vref = val & AC_PINCTL_VREFEN;
5579 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5580 if (vref == cap_lists[i][0] &&
5581 !(vcap & cap_lists[i][1])) {
5582 if (i == ARRAY_SIZE(cap_lists) - 1)
5583 vref = AC_PINCTL_VREF_HIZ;
5584 else
5585 vref = cap_lists[i + 1][0];
5586 }
5587 }
5588 val &= ~AC_PINCTL_VREFEN;
5589 val |= vref;
5590 }
5591 }
5592
5593 return val;
5594}
5595EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5596
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005597int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5598 unsigned int val, bool cached)
5599{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005600 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005601 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005602 if (cached)
5603 return snd_hda_codec_update_cache(codec, pin, 0,
5604 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5605 else
5606 return snd_hda_codec_write(codec, pin, 0,
5607 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5608}
5609EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5610
Takashi Iwai990061c2010-09-09 22:08:44 +02005611/**
5612 * snd_hda_add_imux_item - Add an item to input_mux
5613 *
5614 * When the same label is used already in the existing items, the number
5615 * suffix is appended to the label. This label index number is stored
5616 * to type_idx when non-NULL pointer is given.
5617 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005618int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5619 int index, int *type_idx)
5620{
5621 int i, label_idx = 0;
5622 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5623 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5624 return -EINVAL;
5625 }
5626 for (i = 0; i < imux->num_items; i++) {
5627 if (!strncmp(label, imux->items[i].label, strlen(label)))
5628 label_idx++;
5629 }
5630 if (type_idx)
5631 *type_idx = label_idx;
5632 if (label_idx > 0)
5633 snprintf(imux->items[imux->num_items].label,
5634 sizeof(imux->items[imux->num_items].label),
5635 "%s %d", label, label_idx);
5636 else
5637 strlcpy(imux->items[imux->num_items].label, label,
5638 sizeof(imux->items[imux->num_items].label));
5639 imux->items[imux->num_items].index = index;
5640 imux->num_items++;
5641 return 0;
5642}
5643EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005644
Takashi Iwai4a471b72005-12-07 13:56:29 +01005645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646#ifdef CONFIG_PM
5647/*
5648 * power management
5649 */
5650
5651/**
5652 * snd_hda_suspend - suspend the codecs
5653 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 *
5655 * Returns 0 if successful.
5656 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005657int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005659 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Takashi Iwai0ba21762007-04-16 11:29:14 +02005661 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005662 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005663 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005664 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 }
5666 return 0;
5667}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005668EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
5670/**
5671 * snd_hda_resume - resume the codecs
5672 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 *
5674 * Returns 0 if successful.
5675 */
5676int snd_hda_resume(struct hda_bus *bus)
5677{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005678 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
Takashi Iwai0ba21762007-04-16 11:29:14 +02005680 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005681 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 return 0;
5684}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005685EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005686#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005687
5688/*
5689 * generic arrays
5690 */
5691
Takashi Iwaid5191e52009-11-16 14:58:17 +01005692/**
5693 * snd_array_new - get a new element from the given array
5694 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005695 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005696 * Get a new element from the given array. If it exceeds the
5697 * pre-allocated array size, re-allocate the array.
5698 *
5699 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005700 */
5701void *snd_array_new(struct snd_array *array)
5702{
Takashi Iwai12f17712012-10-10 08:59:14 +02005703 if (snd_BUG_ON(!array->elem_size))
5704 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005705 if (array->used >= array->alloced) {
5706 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005707 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005708 void *nlist;
5709 if (snd_BUG_ON(num >= 4096))
5710 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005711 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005712 if (!nlist)
5713 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005714 array->list = nlist;
5715 array->alloced = num;
5716 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005717 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005718}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005719EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005720
Takashi Iwaid5191e52009-11-16 14:58:17 +01005721/**
5722 * snd_array_free - free the given array elements
5723 * @array: the array object
5724 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005725void snd_array_free(struct snd_array *array)
5726{
5727 kfree(array->list);
5728 array->used = 0;
5729 array->alloced = 0;
5730 array->list = NULL;
5731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005732EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005733
Takashi Iwaid5191e52009-11-16 14:58:17 +01005734/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005735 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5736 * @pcm: PCM caps bits
5737 * @buf: the string buffer to write
5738 * @buflen: the max buffer length
5739 *
5740 * used by hda_proc.c and hda_eld.c
5741 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005742void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5743{
5744 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5745 int i, j;
5746
5747 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5748 if (pcm & (AC_SUPPCM_BITS_8 << i))
5749 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5750
5751 buf[j] = '\0'; /* necessary when j == 0 */
5752}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005753EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005754
5755MODULE_DESCRIPTION("HDA codec core");
5756MODULE_LICENSE("GPL");