blob: 6deb8d1852cc5f3b0f340c30deb9e94a8b6814e7 [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);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001513#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001514 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001515 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001516#ifdef CONFIG_PM
1517 if (!codec->d3_stop_clk || !codec->epss)
1518 bus->power_keep_link_on = 1;
1519#endif
1520
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001521
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001522 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001523 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001524
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001525 snd_hda_codec_proc_new(codec);
1526
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001527 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001528
1529 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1530 codec->subsystem_id, codec->revision_id);
1531 snd_component_add(codec->bus->card, component);
1532
1533 if (codecp)
1534 *codecp = codec;
1535 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001536
1537 error:
1538 snd_hda_codec_free(codec);
1539 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001541EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001542
Mengdong Lina15d05d2013-02-08 17:09:31 -05001543int snd_hda_codec_update_widgets(struct hda_codec *codec)
1544{
1545 hda_nid_t fg;
1546 int err;
1547
1548 /* Assume the function group node does not change,
1549 * only the widget nodes may change.
1550 */
1551 kfree(codec->wcaps);
1552 fg = codec->afg ? codec->afg : codec->mfg;
1553 err = read_widget_caps(codec, fg);
1554 if (err < 0) {
1555 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1556 return err;
1557 }
1558
1559 snd_array_free(&codec->init_pins);
1560 err = read_pin_defaults(codec);
1561
1562 return err;
1563}
1564EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1565
1566
Takashi Iwaif0639272013-11-18 12:07:29 +01001567#ifdef CONFIG_SND_HDA_CODEC_HDMI
1568/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1569static bool is_likely_hdmi_codec(struct hda_codec *codec)
1570{
1571 hda_nid_t nid = codec->start_nid;
1572 int i;
1573
1574 for (i = 0; i < codec->num_nodes; i++, nid++) {
1575 unsigned int wcaps = get_wcaps(codec, nid);
1576 switch (get_wcaps_type(wcaps)) {
1577 case AC_WID_AUD_IN:
1578 return false; /* HDMI parser supports only HDMI out */
1579 case AC_WID_AUD_OUT:
1580 if (!(wcaps & AC_WCAP_DIGITAL))
1581 return false;
1582 break;
1583 }
1584 }
1585 return true;
1586}
1587#else
1588/* no HDMI codec parser support */
1589#define is_likely_hdmi_codec(codec) false
1590#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1591
Takashi Iwaid5191e52009-11-16 14:58:17 +01001592/**
1593 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1594 * @codec: the HDA codec
1595 *
1596 * Start parsing of the given codec tree and (re-)initialize the whole
1597 * patch instance.
1598 *
1599 * Returns 0 if successful or a negative error code.
1600 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001601int snd_hda_codec_configure(struct hda_codec *codec)
1602{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001603 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001604 int err;
1605
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001606 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001607 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001608 err = get_codec_name(codec);
1609 if (err < 0)
1610 return err;
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001613 if (!is_generic_config(codec) && codec->preset)
1614 patch = codec->preset->patch;
1615 if (!patch) {
1616 unload_parser(codec); /* to be sure */
Takashi Iwaif0639272013-11-18 12:07:29 +01001617 if (is_likely_hdmi_codec(codec))
1618 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001619#ifdef CONFIG_SND_HDA_GENERIC
1620 if (!patch)
1621 patch = load_parser(codec, snd_hda_parse_generic_codec);
1622#endif
1623 if (!patch) {
1624 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1625 return -ENODEV;
1626 }
Takashi Iwai82467612007-07-27 19:15:54 +02001627 }
1628
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001629 err = patch(codec);
1630 if (err < 0) {
1631 unload_parser(codec);
1632 return err;
1633 }
Takashi Iwai82467612007-07-27 19:15:54 +02001634
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001635 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001636 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001637 if (err < 0)
1638 return err;
1639 }
1640
Takashi Iwaif62faed2009-12-23 09:27:51 +01001641 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001642 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001643 snprintf(codec->bus->card->mixername,
1644 sizeof(codec->bus->card->mixername),
1645 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001648EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Takashi Iwaied360812012-08-08 17:12:52 +02001650/* update the stream-id if changed */
1651static void update_pcm_stream_id(struct hda_codec *codec,
1652 struct hda_cvt_setup *p, hda_nid_t nid,
1653 u32 stream_tag, int channel_id)
1654{
1655 unsigned int oldval, newval;
1656
1657 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1658 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1659 newval = (stream_tag << 4) | channel_id;
1660 if (oldval != newval)
1661 snd_hda_codec_write(codec, nid, 0,
1662 AC_VERB_SET_CHANNEL_STREAMID,
1663 newval);
1664 p->stream_tag = stream_tag;
1665 p->channel_id = channel_id;
1666 }
1667}
1668
1669/* update the format-id if changed */
1670static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1671 hda_nid_t nid, int format)
1672{
1673 unsigned int oldval;
1674
1675 if (p->format_id != format) {
1676 oldval = snd_hda_codec_read(codec, nid, 0,
1677 AC_VERB_GET_STREAM_FORMAT, 0);
1678 if (oldval != format) {
1679 msleep(1);
1680 snd_hda_codec_write(codec, nid, 0,
1681 AC_VERB_SET_STREAM_FORMAT,
1682 format);
1683 }
1684 p->format_id = format;
1685 }
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/**
1689 * snd_hda_codec_setup_stream - set up the codec for streaming
1690 * @codec: the CODEC to set up
1691 * @nid: the NID to set up
1692 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1693 * @channel_id: channel id to pass, zero based.
1694 * @format: stream format.
1695 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1697 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int channel_id, int format)
1699{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001700 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001701 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001702 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001703 int i;
1704
Takashi Iwai0ba21762007-04-16 11:29:14 +02001705 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001706 return;
1707
Takashi Iwai0ba21762007-04-16 11:29:14 +02001708 snd_printdd("hda_codec_setup_stream: "
1709 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001711 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001712 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001713 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001714
1715 if (codec->pcm_format_first)
1716 update_pcm_format(codec, p, nid, format);
1717 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1718 if (!codec->pcm_format_first)
1719 update_pcm_format(codec, p, nid, format);
1720
Takashi Iwaieb541332010-08-06 13:48:11 +02001721 p->active = 1;
1722 p->dirty = 0;
1723
1724 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001725 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001726 list_for_each_entry(c, &codec->bus->codec_list, list) {
1727 for (i = 0; i < c->cvt_setups.used; i++) {
1728 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001729 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001730 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001731 p->dirty = 1;
1732 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001735EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Takashi Iwaif0cea792010-08-13 11:56:53 +02001737static void really_cleanup_stream(struct hda_codec *codec,
1738 struct hda_cvt_setup *q);
1739
Takashi Iwaid5191e52009-11-16 14:58:17 +01001740/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001741 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001742 * @codec: the CODEC to clean up
1743 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001744 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001745 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001746void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1747 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001748{
Takashi Iwaieb541332010-08-06 13:48:11 +02001749 struct hda_cvt_setup *p;
1750
Takashi Iwai888afa12008-03-18 09:57:50 +01001751 if (!nid)
1752 return;
1753
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001754 if (codec->no_sticky_stream)
1755 do_now = 1;
1756
Takashi Iwai888afa12008-03-18 09:57:50 +01001757 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001758 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001759 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001760 /* here we just clear the active flag when do_now isn't set;
1761 * actual clean-ups will be done later in
1762 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1763 */
1764 if (do_now)
1765 really_cleanup_stream(codec, p);
1766 else
1767 p->active = 0;
1768 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001769}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001770EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001771
Takashi Iwaieb541332010-08-06 13:48:11 +02001772static void really_cleanup_stream(struct hda_codec *codec,
1773 struct hda_cvt_setup *q)
1774{
1775 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001776 if (q->stream_tag || q->channel_id)
1777 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1778 if (q->format_id)
1779 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1780);
Takashi Iwaieb541332010-08-06 13:48:11 +02001781 memset(q, 0, sizeof(*q));
1782 q->nid = nid;
1783}
1784
1785/* clean up the all conflicting obsolete streams */
1786static void purify_inactive_streams(struct hda_codec *codec)
1787{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001788 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001789 int i;
1790
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001791 list_for_each_entry(c, &codec->bus->codec_list, list) {
1792 for (i = 0; i < c->cvt_setups.used; i++) {
1793 struct hda_cvt_setup *p;
1794 p = snd_array_elem(&c->cvt_setups, i);
1795 if (p->dirty)
1796 really_cleanup_stream(c, p);
1797 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001798 }
1799}
1800
Takashi Iwai2a439522011-07-26 09:52:50 +02001801#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001802/* clean up all streams; called from suspend */
1803static void hda_cleanup_all_streams(struct hda_codec *codec)
1804{
1805 int i;
1806
1807 for (i = 0; i < codec->cvt_setups.used; i++) {
1808 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1809 if (p->stream_tag)
1810 really_cleanup_stream(codec, p);
1811 }
1812}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001813#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*
1816 * amp access functions
1817 */
1818
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001819/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001820#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001821#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001822#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1823#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001825#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001828static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001829 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Takashi Iwai01751f52007-08-10 16:59:39 +02001831 memset(cache, 0, sizeof(*cache));
1832 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001833 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001834}
1835
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001836static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001837{
Takashi Iwai603c4012008-07-30 15:01:44 +02001838 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001842static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Takashi Iwai01751f52007-08-10 16:59:39 +02001844 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1845 u16 cur = cache->hash[idx];
1846 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001849 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (info->key == key)
1851 return info;
1852 cur = info->next;
1853 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001854 return NULL;
1855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001857/* query the hash. allocate an entry if not found. */
1858static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1859 u32 key)
1860{
1861 struct hda_cache_head *info = get_hash(cache, key);
1862 if (!info) {
1863 u16 idx, cur;
1864 /* add a new hash entry */
1865 info = snd_array_new(&cache->buf);
1866 if (!info)
1867 return NULL;
1868 cur = snd_array_index(&cache->buf, info);
1869 info->key = key;
1870 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001871 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001872 idx = key % (u16)ARRAY_SIZE(cache->hash);
1873 info->next = cache->hash[idx];
1874 cache->hash[idx] = cur;
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return info;
1877}
1878
Takashi Iwai01751f52007-08-10 16:59:39 +02001879/* query and allocate an amp hash entry */
1880static inline struct hda_amp_info *
1881get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1882{
1883 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1884}
1885
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001886/* overwrite the value with the key in the caps hash */
1887static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1888{
1889 struct hda_amp_info *info;
1890
1891 mutex_lock(&codec->hash_mutex);
1892 info = get_alloc_amp_hash(codec, key);
1893 if (!info) {
1894 mutex_unlock(&codec->hash_mutex);
1895 return -EINVAL;
1896 }
1897 info->amp_caps = val;
1898 info->head.val |= INFO_AMP_CAPS;
1899 mutex_unlock(&codec->hash_mutex);
1900 return 0;
1901}
1902
1903/* query the value from the caps hash; if not found, fetch the current
1904 * value from the given function and store in the hash
1905 */
1906static unsigned int
1907query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1908 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1909{
1910 struct hda_amp_info *info;
1911 unsigned int val;
1912
1913 mutex_lock(&codec->hash_mutex);
1914 info = get_alloc_amp_hash(codec, key);
1915 if (!info) {
1916 mutex_unlock(&codec->hash_mutex);
1917 return 0;
1918 }
1919 if (!(info->head.val & INFO_AMP_CAPS)) {
1920 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1921 val = func(codec, nid, dir);
1922 write_caps_hash(codec, key, val);
1923 } else {
1924 val = info->amp_caps;
1925 mutex_unlock(&codec->hash_mutex);
1926 }
1927 return val;
1928}
1929
1930static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1931 int direction)
1932{
1933 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1934 nid = codec->afg;
1935 return snd_hda_param_read(codec, nid,
1936 direction == HDA_OUTPUT ?
1937 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1938}
1939
Takashi Iwaid5191e52009-11-16 14:58:17 +01001940/**
1941 * query_amp_caps - query AMP capabilities
1942 * @codec: the HD-auio codec
1943 * @nid: the NID to query
1944 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1945 *
1946 * Query AMP capabilities for the given widget and direction.
1947 * Returns the obtained capability bits.
1948 *
1949 * When cap bits have been already read, this doesn't read again but
1950 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001952u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001954 return query_caps_hash(codec, nid, direction,
1955 HDA_HASH_KEY(nid, direction, 0),
1956 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001958EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Takashi Iwaid5191e52009-11-16 14:58:17 +01001960/**
1961 * snd_hda_override_amp_caps - Override the AMP capabilities
1962 * @codec: the CODEC to clean up
1963 * @nid: the NID to clean up
1964 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1965 * @caps: the capability bits to set
1966 *
1967 * Override the cached AMP caps bits value by the given one.
1968 * This function is useful if the driver needs to adjust the AMP ranges,
1969 * e.g. limit to 0dB, etc.
1970 *
1971 * Returns zero if successful or a negative error code.
1972 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001973int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1974 unsigned int caps)
1975{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001976 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001978EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001979
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001980static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1981 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001982{
1983 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1984}
1985
Takashi Iwaid5191e52009-11-16 14:58:17 +01001986/**
1987 * snd_hda_query_pin_caps - Query PIN capabilities
1988 * @codec: the HD-auio codec
1989 * @nid: the NID to query
1990 *
1991 * Query PIN capabilities for the given widget.
1992 * Returns the obtained capability bits.
1993 *
1994 * When cap bits have been already read, this doesn't read again but
1995 * returns the cached value.
1996 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001997u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1998{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001999 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002000 read_pin_cap);
2001}
Takashi Iwai1327a322009-03-23 13:07:47 +01002002EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
2003
Wu Fengguang864f92b2009-11-18 12:38:02 +08002004/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002005 * snd_hda_override_pin_caps - Override the pin capabilities
2006 * @codec: the CODEC
2007 * @nid: the NID to override
2008 * @caps: the capability bits to set
2009 *
2010 * Override the cached PIN capabilitiy bits value by the given one.
2011 *
2012 * Returns zero if successful or a negative error code.
2013 */
2014int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2015 unsigned int caps)
2016{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002017 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002018}
2019EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2020
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002021/* read or sync the hash value with the current value;
2022 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002024static struct hda_amp_info *
2025update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002026 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002028 struct hda_amp_info *info;
2029 unsigned int parm, val = 0;
2030 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002032 retry:
2033 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2034 if (!info)
2035 return NULL;
2036 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2037 if (!val_read) {
2038 mutex_unlock(&codec->hash_mutex);
2039 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2040 parm |= direction == HDA_OUTPUT ?
2041 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2042 parm |= index;
2043 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002044 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002045 val &= 0xff;
2046 val_read = true;
2047 mutex_lock(&codec->hash_mutex);
2048 goto retry;
2049 }
2050 info->vol[ch] = val;
2051 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002052 } else if (init_only)
2053 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002054 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002058 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002060static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002061 hda_nid_t nid, int ch, int direction, int index,
2062 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 u32 parm;
2065
2066 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2067 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2068 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002069 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2070 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002071 ; /* set the zero value as a fake mute */
2072 else
2073 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2075}
2076
Takashi Iwaid5191e52009-11-16 14:58:17 +01002077/**
2078 * snd_hda_codec_amp_read - Read AMP value
2079 * @codec: HD-audio codec
2080 * @nid: NID to read the AMP value
2081 * @ch: channel (left=0 or right=1)
2082 * @direction: #HDA_INPUT or #HDA_OUTPUT
2083 * @index: the index value (only for input direction)
2084 *
2085 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 */
Takashi Iwai834be882006-03-01 14:16:17 +01002087int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2088 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002091 unsigned int val = 0;
2092
2093 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002094 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002095 if (info)
2096 val = info->vol[ch];
2097 mutex_unlock(&codec->hash_mutex);
2098 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002100EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Takashi Iwai280e57d2012-12-14 10:32:21 +01002102static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2103 int direction, int idx, int mask, int val,
2104 bool init_only)
2105{
2106 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002107 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002108 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002109
2110 if (snd_BUG_ON(mask & ~0xff))
2111 mask &= 0xff;
2112 val &= mask;
2113
2114 mutex_lock(&codec->hash_mutex);
2115 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2116 if (!info) {
2117 mutex_unlock(&codec->hash_mutex);
2118 return 0;
2119 }
2120 val |= info->vol[ch] & ~mask;
2121 if (info->vol[ch] == val) {
2122 mutex_unlock(&codec->hash_mutex);
2123 return 0;
2124 }
2125 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002126 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002127 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002128 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002129 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002130 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002131 return 1;
2132}
2133
Takashi Iwaid5191e52009-11-16 14:58:17 +01002134/**
2135 * snd_hda_codec_amp_update - update the AMP value
2136 * @codec: HD-audio codec
2137 * @nid: NID to read the AMP value
2138 * @ch: channel (left=0 or right=1)
2139 * @direction: #HDA_INPUT or #HDA_OUTPUT
2140 * @idx: the index value (only for input direction)
2141 * @mask: bit mask to set
2142 * @val: the bits value to set
2143 *
2144 * Update the AMP value with a bit mask.
2145 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002146 */
Takashi Iwai834be882006-03-01 14:16:17 +01002147int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2148 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002150 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002152EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154/**
2155 * snd_hda_codec_amp_stereo - update the AMP stereo values
2156 * @codec: HD-audio codec
2157 * @nid: NID to read the AMP value
2158 * @direction: #HDA_INPUT or #HDA_OUTPUT
2159 * @idx: the index value (only for input direction)
2160 * @mask: bit mask to set
2161 * @val: the bits value to set
2162 *
2163 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2164 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002165 */
2166int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2167 int direction, int idx, int mask, int val)
2168{
2169 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002170
2171 if (snd_BUG_ON(mask & ~0xff))
2172 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002173 for (ch = 0; ch < 2; ch++)
2174 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2175 idx, mask, val);
2176 return ret;
2177}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002178EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002179
Takashi Iwai280e57d2012-12-14 10:32:21 +01002180/* Works like snd_hda_codec_amp_update() but it writes the value only at
2181 * the first access. If the amp was already initialized / updated beforehand,
2182 * this does nothing.
2183 */
2184int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2185 int dir, int idx, int mask, int val)
2186{
2187 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2188}
2189EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2190
2191int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2192 int dir, int idx, int mask, int val)
2193{
2194 int ch, ret = 0;
2195
2196 if (snd_BUG_ON(mask & ~0xff))
2197 mask &= 0xff;
2198 for (ch = 0; ch < 2; ch++)
2199 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2200 idx, mask, val);
2201 return ret;
2202}
2203EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2204
Takashi Iwaid5191e52009-11-16 14:58:17 +01002205/**
2206 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2207 * @codec: HD-audio codec
2208 *
2209 * Resume the all amp commands from the cache.
2210 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002211void snd_hda_codec_resume_amp(struct hda_codec *codec)
2212{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002213 int i;
2214
Takashi Iwaic370dd62012-12-13 18:30:04 +01002215 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002216 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002217 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2218 struct hda_amp_info *buffer;
2219 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002220 hda_nid_t nid;
2221 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002222 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002223
2224 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002225 if (!buffer->head.dirty)
2226 continue;
2227 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002228 info = *buffer;
2229 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002230 if (!key)
2231 continue;
2232 nid = key & 0xff;
2233 idx = (key >> 16) & 0xff;
2234 dir = (key >> 24) & 0xff;
2235 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002236 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002237 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002238 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002239 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2240 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002241 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002242 }
2243 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002244 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002245}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002246EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002248static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2249 unsigned int ofs)
2250{
2251 u32 caps = query_amp_caps(codec, nid, dir);
2252 /* get num steps */
2253 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2254 if (ofs < caps)
2255 caps -= ofs;
2256 return caps;
2257}
2258
Takashi Iwaid5191e52009-11-16 14:58:17 +01002259/**
2260 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2261 *
2262 * The control element is supposed to have the private_value field
2263 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2264 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002265int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2269 u16 nid = get_amp_nid(kcontrol);
2270 u8 chs = get_amp_channels(kcontrol);
2271 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002272 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2275 uinfo->count = chs == 3 ? 2 : 1;
2276 uinfo->value.integer.min = 0;
2277 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2278 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002279 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002280 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2281 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return -EINVAL;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return 0;
2285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002286EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002288
2289static inline unsigned int
2290read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2291 int ch, int dir, int idx, unsigned int ofs)
2292{
2293 unsigned int val;
2294 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2295 val &= HDA_AMP_VOLMASK;
2296 if (val >= ofs)
2297 val -= ofs;
2298 else
2299 val = 0;
2300 return val;
2301}
2302
2303static inline int
2304update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2305 int ch, int dir, int idx, unsigned int ofs,
2306 unsigned int val)
2307{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002308 unsigned int maxval;
2309
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002310 if (val > 0)
2311 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002312 /* ofs = 0: raw max value */
2313 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002314 if (val > maxval)
2315 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002316 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2317 HDA_AMP_VOLMASK, val);
2318}
2319
Takashi Iwaid5191e52009-11-16 14:58:17 +01002320/**
2321 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2322 *
2323 * The control element is supposed to have the private_value field
2324 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2325 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002326int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2327 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2330 hda_nid_t nid = get_amp_nid(kcontrol);
2331 int chs = get_amp_channels(kcontrol);
2332 int dir = get_amp_direction(kcontrol);
2333 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002334 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 long *valp = ucontrol->value.integer.value;
2336
2337 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002338 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002340 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002343EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345/**
2346 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2347 *
2348 * The control element is supposed to have the private_value field
2349 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2350 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002351int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2355 hda_nid_t nid = get_amp_nid(kcontrol);
2356 int chs = get_amp_channels(kcontrol);
2357 int dir = get_amp_direction(kcontrol);
2358 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002359 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 long *valp = ucontrol->value.integer.value;
2361 int change = 0;
2362
Takashi Iwaicb53c622007-08-10 17:21:45 +02002363 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002364 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002365 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002366 valp++;
2367 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002368 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002369 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002370 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return change;
2372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002373EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Takashi Iwaid5191e52009-11-16 14:58:17 +01002375/**
2376 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2377 *
2378 * The control element is supposed to have the private_value field
2379 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2380 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002381int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2382 unsigned int size, unsigned int __user *_tlv)
2383{
2384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2385 hda_nid_t nid = get_amp_nid(kcontrol);
2386 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002387 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002388 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002389 u32 caps, val1, val2;
2390
2391 if (size < 4 * sizeof(unsigned int))
2392 return -ENOMEM;
2393 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2395 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002396 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002397 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002398 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002399 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002400 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002401 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2402 return -EFAULT;
2403 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2404 return -EFAULT;
2405 if (put_user(val1, _tlv + 2))
2406 return -EFAULT;
2407 if (put_user(val2, _tlv + 3))
2408 return -EFAULT;
2409 return 0;
2410}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002411EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002412
Takashi Iwaid5191e52009-11-16 14:58:17 +01002413/**
2414 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2415 * @codec: HD-audio codec
2416 * @nid: NID of a reference widget
2417 * @dir: #HDA_INPUT or #HDA_OUTPUT
2418 * @tlv: TLV data to be stored, at least 4 elements
2419 *
2420 * Set (static) TLV data for a virtual master volume using the AMP caps
2421 * obtained from the reference NID.
2422 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002423 */
2424void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2425 unsigned int *tlv)
2426{
2427 u32 caps;
2428 int nums, step;
2429
2430 caps = query_amp_caps(codec, nid, dir);
2431 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2432 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2433 step = (step + 1) * 25;
2434 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2435 tlv[1] = 2 * sizeof(unsigned int);
2436 tlv[2] = -nums * step;
2437 tlv[3] = step;
2438}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002439EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002440
2441/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002442static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002443find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002444{
2445 struct snd_ctl_elem_id id;
2446 memset(&id, 0, sizeof(id));
2447 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002448 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002449 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002450 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2451 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002452 strcpy(id.name, name);
2453 return snd_ctl_find_id(codec->bus->card, &id);
2454}
2455
Takashi Iwaid5191e52009-11-16 14:58:17 +01002456/**
2457 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2458 * @codec: HD-audio codec
2459 * @name: ctl id name string
2460 *
2461 * Get the control element with the given id string and IFACE_MIXER.
2462 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002463struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2464 const char *name)
2465{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002466 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002468EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002469
Takashi Iwaidcda5802012-10-12 17:24:51 +02002470static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002471 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002472{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002473 int i, idx;
2474 /* 16 ctlrs should be large enough */
2475 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2476 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002477 return idx;
2478 }
2479 return -EBUSY;
2480}
2481
Takashi Iwaid5191e52009-11-16 14:58:17 +01002482/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002483 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002484 * @codec: HD-audio codec
2485 * @nid: corresponding NID (optional)
2486 * @kctl: the control element to assign
2487 *
2488 * Add the given control element to an array inside the codec instance.
2489 * All control elements belonging to a codec are supposed to be added
2490 * by this function so that a proper clean-up works at the free or
2491 * reconfiguration time.
2492 *
2493 * If non-zero @nid is passed, the NID is assigned to the control element.
2494 * The assignment is shown in the codec proc file.
2495 *
2496 * snd_hda_ctl_add() checks the control subdev id field whether
2497 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002498 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2499 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002500 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002501int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2502 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002503{
2504 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002505 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002506 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002507
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002508 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002509 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002510 if (nid == 0)
2511 nid = get_amp_nid_(kctl->private_value);
2512 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002513 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2514 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002515 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002516 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002517 err = snd_ctl_add(codec->bus->card, kctl);
2518 if (err < 0)
2519 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002520 item = snd_array_new(&codec->mixers);
2521 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002522 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002523 item->kctl = kctl;
2524 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002525 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002526 return 0;
2527}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002528EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002529
Takashi Iwaid5191e52009-11-16 14:58:17 +01002530/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002531 * snd_hda_add_nid - Assign a NID to a control element
2532 * @codec: HD-audio codec
2533 * @nid: corresponding NID (optional)
2534 * @kctl: the control element to assign
2535 * @index: index to kctl
2536 *
2537 * Add the given control element to an array inside the codec instance.
2538 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2539 * NID:KCTL mapping - for example "Capture Source" selector.
2540 */
2541int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2542 unsigned int index, hda_nid_t nid)
2543{
2544 struct hda_nid_item *item;
2545
2546 if (nid > 0) {
2547 item = snd_array_new(&codec->nids);
2548 if (!item)
2549 return -ENOMEM;
2550 item->kctl = kctl;
2551 item->index = index;
2552 item->nid = nid;
2553 return 0;
2554 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002555 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2556 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002557 return -EINVAL;
2558}
2559EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2560
2561/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002562 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2563 * @codec: HD-audio codec
2564 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002565void snd_hda_ctls_clear(struct hda_codec *codec)
2566{
2567 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002568 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002569 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002570 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002571 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002572 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002573}
2574
Takashi Iwaia65d6292009-02-23 16:57:04 +01002575/* pseudo device locking
2576 * toggle card->shutdown to allow/disallow the device access (as a hack)
2577 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002578int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002579{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002580 struct snd_card *card = bus->card;
2581 struct hda_codec *codec;
2582
Takashi Iwaia65d6292009-02-23 16:57:04 +01002583 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002584 if (card->shutdown)
2585 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002586 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002587 if (!list_empty(&card->ctl_files))
2588 goto err_clear;
2589
2590 list_for_each_entry(codec, &bus->codec_list, list) {
2591 int pcm;
2592 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2593 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2594 if (!cpcm->pcm)
2595 continue;
2596 if (cpcm->pcm->streams[0].substream_opened ||
2597 cpcm->pcm->streams[1].substream_opened)
2598 goto err_clear;
2599 }
2600 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002601 spin_unlock(&card->files_lock);
2602 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002603
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002604 err_clear:
2605 card->shutdown = 0;
2606 err_unlock:
2607 spin_unlock(&card->files_lock);
2608 return -EINVAL;
2609}
2610EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2611
2612void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002613{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002614 struct snd_card *card = bus->card;
2615
2616 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002617 spin_lock(&card->files_lock);
2618 card->shutdown = 0;
2619 spin_unlock(&card->files_lock);
2620}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002621EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002622
Takashi Iwaid5191e52009-11-16 14:58:17 +01002623/**
2624 * snd_hda_codec_reset - Clear all objects assigned to the codec
2625 * @codec: HD-audio codec
2626 *
2627 * This frees the all PCM and control elements assigned to the codec, and
2628 * clears the caches and restores the pin default configurations.
2629 *
2630 * When a device is being used, it returns -EBSY. If successfully freed,
2631 * returns zero.
2632 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002633int snd_hda_codec_reset(struct hda_codec *codec)
2634{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002635 struct hda_bus *bus = codec->bus;
2636 struct snd_card *card = bus->card;
2637 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002638
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002639 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002640 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002641
2642 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002643 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002644#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002645 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002646 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002647#endif
2648 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002649 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002650 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002651 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002652 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002653 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002654 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002655 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002656 }
2657 if (codec->patch_ops.free)
2658 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002659 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002660 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002661 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002662 codec->spec = NULL;
2663 free_hda_cache(&codec->amp_cache);
2664 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002665 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2666 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002667 /* free only driver_pins so that init_pins + user_pins are restored */
2668 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002669 snd_array_free(&codec->cvt_setups);
2670 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002671 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002672 codec->num_pcms = 0;
2673 codec->pcm_info = NULL;
2674 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002675 codec->slave_dig_outs = NULL;
2676 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002677 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002678 module_put(codec->owner);
2679 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002680
2681 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002682 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002683 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002684}
2685
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002686typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2687
2688/* apply the function to all matching slave ctls in the mixer list */
2689static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002690 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002691{
2692 struct hda_nid_item *items;
2693 const char * const *s;
2694 int i, err;
2695
2696 items = codec->mixers.list;
2697 for (i = 0; i < codec->mixers.used; i++) {
2698 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002699 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002700 continue;
2701 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002702 char tmpname[sizeof(sctl->id.name)];
2703 const char *name = *s;
2704 if (suffix) {
2705 snprintf(tmpname, sizeof(tmpname), "%s %s",
2706 name, suffix);
2707 name = tmpname;
2708 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002709 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002710 err = func(data, sctl);
2711 if (err)
2712 return err;
2713 break;
2714 }
2715 }
2716 }
2717 return 0;
2718}
2719
2720static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2721{
2722 return 1;
2723}
2724
Takashi Iwai18478e82012-03-09 17:51:10 +01002725/* guess the value corresponding to 0dB */
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002726static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002727{
2728 int _tlv[4];
2729 const int *tlv = NULL;
2730 int val = -1;
2731
2732 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2733 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2734 mm_segment_t fs = get_fs();
2735 set_fs(get_ds());
2736 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2737 tlv = _tlv;
2738 set_fs(fs);
2739 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2740 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002741 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2742 int step = tlv[3];
2743 step &= ~TLV_DB_SCALE_MUTE;
2744 if (!step)
2745 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002746 if (*step_to_check && *step_to_check != step) {
2747 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2748 *step_to_check, step);
2749 return -1;
2750 }
2751 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002752 val = -tlv[2] / step;
2753 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002754 return val;
2755}
2756
2757/* call kctl->put with the given value(s) */
2758static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2759{
2760 struct snd_ctl_elem_value *ucontrol;
2761 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2762 if (!ucontrol)
2763 return -ENOMEM;
2764 ucontrol->value.integer.value[0] = val;
2765 ucontrol->value.integer.value[1] = val;
2766 kctl->put(kctl, ucontrol);
2767 kfree(ucontrol);
2768 return 0;
2769}
2770
2771/* initialize the slave volume with 0dB */
2772static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2773{
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002774 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002775 if (offset > 0)
2776 put_kctl_with_value(slave, offset);
2777 return 0;
2778}
2779
2780/* unmute the slave */
2781static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2782{
2783 return put_kctl_with_value(slave, 1);
2784}
2785
Takashi Iwaid5191e52009-11-16 14:58:17 +01002786/**
2787 * snd_hda_add_vmaster - create a virtual master control and add slaves
2788 * @codec: HD-audio codec
2789 * @name: vmaster control name
2790 * @tlv: TLV data (optional)
2791 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002792 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002793 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002794 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002795 *
2796 * Create a virtual master control with the given name. The TLV data
2797 * must be either NULL or a valid data.
2798 *
2799 * @slaves is a NULL-terminated array of strings, each of which is a
2800 * slave control name. All controls with these names are assigned to
2801 * the new virtual master control.
2802 *
2803 * This function returns zero if successful or a negative error code.
2804 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002805int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002806 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002807 const char *suffix, bool init_slave_vol,
2808 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002809{
2810 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002811 int err;
2812
Takashi Iwai29e58532012-03-12 12:25:03 +01002813 if (ctl_ret)
2814 *ctl_ret = NULL;
2815
Takashi Iwai9322ca52012-02-03 14:28:01 +01002816 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002817 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002818 snd_printdd("No slave found for %s\n", name);
2819 return 0;
2820 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002821 kctl = snd_ctl_make_virtual_master(name, tlv);
2822 if (!kctl)
2823 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002824 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002825 if (err < 0)
2826 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002827
Takashi Iwai9322ca52012-02-03 14:28:01 +01002828 err = map_slaves(codec, slaves, suffix,
2829 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002830 if (err < 0)
2831 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002832
2833 /* init with master mute & zero volume */
2834 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002835 if (init_slave_vol) {
2836 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002837 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002838 tlv ? init_slave_0dB : init_slave_unmute, &step);
2839 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002840
Takashi Iwai29e58532012-03-12 12:25:03 +01002841 if (ctl_ret)
2842 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002843 return 0;
2844}
Takashi Iwai18478e82012-03-09 17:51:10 +01002845EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002846
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002847/*
2848 * mute-LED control using vmaster
2849 */
2850static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_info *uinfo)
2852{
2853 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002854 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002855 };
2856 unsigned int index;
2857
2858 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2859 uinfo->count = 1;
2860 uinfo->value.enumerated.items = 3;
2861 index = uinfo->value.enumerated.item;
2862 if (index >= 3)
2863 index = 2;
2864 strcpy(uinfo->value.enumerated.name, texts[index]);
2865 return 0;
2866}
2867
2868static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2870{
2871 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2872 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2873 return 0;
2874}
2875
2876static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2878{
2879 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2880 unsigned int old_mode = hook->mute_mode;
2881
2882 hook->mute_mode = ucontrol->value.enumerated.item[0];
2883 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2884 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2885 if (old_mode == hook->mute_mode)
2886 return 0;
2887 snd_hda_sync_vmaster_hook(hook);
2888 return 1;
2889}
2890
2891static struct snd_kcontrol_new vmaster_mute_mode = {
2892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2893 .name = "Mute-LED Mode",
2894 .info = vmaster_mute_mode_info,
2895 .get = vmaster_mute_mode_get,
2896 .put = vmaster_mute_mode_put,
2897};
2898
2899/*
2900 * Add a mute-LED hook with the given vmaster switch kctl
2901 * "Mute-LED Mode" control is automatically created and associated with
2902 * the given hook.
2903 */
2904int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002905 struct hda_vmaster_mute_hook *hook,
2906 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002907{
2908 struct snd_kcontrol *kctl;
2909
2910 if (!hook->hook || !hook->sw_kctl)
2911 return 0;
2912 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2913 hook->codec = codec;
2914 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002915 if (!expose_enum_ctl)
2916 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002917 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2918 if (!kctl)
2919 return -ENOMEM;
2920 return snd_hda_ctl_add(codec, 0, kctl);
2921}
2922EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2923
2924/*
2925 * Call the hook with the current value for synchronization
2926 * Should be called in init callback
2927 */
2928void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2929{
2930 if (!hook->hook || !hook->codec)
2931 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002932 /* don't call vmaster hook in the destructor since it might have
2933 * been already destroyed
2934 */
2935 if (hook->codec->bus->shutdown)
2936 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002937 switch (hook->mute_mode) {
2938 case HDA_VMUTE_FOLLOW_MASTER:
2939 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2940 break;
2941 default:
2942 hook->hook(hook->codec, hook->mute_mode);
2943 break;
2944 }
2945}
2946EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2947
2948
Takashi Iwaid5191e52009-11-16 14:58:17 +01002949/**
2950 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2951 *
2952 * The control element is supposed to have the private_value field
2953 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2954 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002955int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 int chs = get_amp_channels(kcontrol);
2959
2960 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2961 uinfo->count = chs == 3 ? 2 : 1;
2962 uinfo->value.integer.min = 0;
2963 uinfo->value.integer.max = 1;
2964 return 0;
2965}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002966EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Takashi Iwaid5191e52009-11-16 14:58:17 +01002968/**
2969 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2970 *
2971 * The control element is supposed to have the private_value field
2972 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2973 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 hda_nid_t nid = get_amp_nid(kcontrol);
2979 int chs = get_amp_channels(kcontrol);
2980 int dir = get_amp_direction(kcontrol);
2981 int idx = get_amp_index(kcontrol);
2982 long *valp = ucontrol->value.integer.value;
2983
2984 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002986 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002989 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return 0;
2991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002992EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Takashi Iwaid5191e52009-11-16 14:58:17 +01002994/**
2995 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2996 *
2997 * The control element is supposed to have the private_value field
2998 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2999 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003000int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
3003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3004 hda_nid_t nid = get_amp_nid(kcontrol);
3005 int chs = get_amp_channels(kcontrol);
3006 int dir = get_amp_direction(kcontrol);
3007 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 long *valp = ucontrol->value.integer.value;
3009 int change = 0;
3010
Takashi Iwaicb53c622007-08-10 17:21:45 +02003011 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003012 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003013 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003014 HDA_AMP_MUTE,
3015 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003016 valp++;
3017 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003018 if (chs & 2)
3019 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003020 HDA_AMP_MUTE,
3021 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003022 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003023 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return change;
3025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003026EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028/*
Takashi Iwai985be542005-11-02 18:26:49 +01003029 * bound volume controls
3030 *
3031 * bind multiple volumes (# indices, from 0)
3032 */
3033
3034#define AMP_VAL_IDX_SHIFT 19
3035#define AMP_VAL_IDX_MASK (0x0f<<19)
3036
Takashi Iwaid5191e52009-11-16 14:58:17 +01003037/**
3038 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3039 *
3040 * The control element is supposed to have the private_value field
3041 * set up via HDA_BIND_MUTE*() macros.
3042 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003043int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3044 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003045{
3046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3047 unsigned long pval;
3048 int err;
3049
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003050 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003051 pval = kcontrol->private_value;
3052 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3053 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3054 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003055 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003056 return err;
3057}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003058EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003059
Takashi Iwaid5191e52009-11-16 14:58:17 +01003060/**
3061 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3062 *
3063 * The control element is supposed to have the private_value field
3064 * set up via HDA_BIND_MUTE*() macros.
3065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003068{
3069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3070 unsigned long pval;
3071 int i, indices, err = 0, change = 0;
3072
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003073 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003074 pval = kcontrol->private_value;
3075 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3076 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003077 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3078 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003079 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3080 if (err < 0)
3081 break;
3082 change |= err;
3083 }
3084 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003085 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003086 return err < 0 ? err : change;
3087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003088EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003089
Takashi Iwaid5191e52009-11-16 14:58:17 +01003090/**
3091 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3092 *
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003095 */
3096int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_info *uinfo)
3098{
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 struct hda_bind_ctls *c;
3101 int err;
3102
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003103 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003104 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003105 kcontrol->private_value = *c->values;
3106 err = c->ops->info(kcontrol, uinfo);
3107 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003108 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003109 return err;
3110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003111EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003112
Takashi Iwaid5191e52009-11-16 14:58:17 +01003113/**
3114 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3115 *
3116 * The control element is supposed to have the private_value field
3117 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3118 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003119int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
3122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3123 struct hda_bind_ctls *c;
3124 int err;
3125
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003126 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003127 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003128 kcontrol->private_value = *c->values;
3129 err = c->ops->get(kcontrol, ucontrol);
3130 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003131 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003132 return err;
3133}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003134EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003135
Takashi Iwaid5191e52009-11-16 14:58:17 +01003136/**
3137 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3138 *
3139 * The control element is supposed to have the private_value field
3140 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3141 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003142int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
3145 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3146 struct hda_bind_ctls *c;
3147 unsigned long *vals;
3148 int err = 0, change = 0;
3149
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003150 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003151 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003152 for (vals = c->values; *vals; vals++) {
3153 kcontrol->private_value = *vals;
3154 err = c->ops->put(kcontrol, ucontrol);
3155 if (err < 0)
3156 break;
3157 change |= err;
3158 }
3159 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003160 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003161 return err < 0 ? err : change;
3162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003163EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003164
Takashi Iwaid5191e52009-11-16 14:58:17 +01003165/**
3166 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3167 *
3168 * The control element is supposed to have the private_value field
3169 * set up via HDA_BIND_VOL() macro.
3170 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003171int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3172 unsigned int size, unsigned int __user *tlv)
3173{
3174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3175 struct hda_bind_ctls *c;
3176 int err;
3177
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003178 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003179 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003180 kcontrol->private_value = *c->values;
3181 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3182 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003183 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003184 return err;
3185}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003186EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003187
3188struct hda_ctl_ops snd_hda_bind_vol = {
3189 .info = snd_hda_mixer_amp_volume_info,
3190 .get = snd_hda_mixer_amp_volume_get,
3191 .put = snd_hda_mixer_amp_volume_put,
3192 .tlv = snd_hda_mixer_amp_tlv
3193};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003194EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003195
3196struct hda_ctl_ops snd_hda_bind_sw = {
3197 .info = snd_hda_mixer_amp_switch_info,
3198 .get = snd_hda_mixer_amp_switch_get,
3199 .put = snd_hda_mixer_amp_switch_put,
3200 .tlv = snd_hda_mixer_amp_tlv
3201};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003202EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003203
3204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 * SPDIF out controls
3206 */
3207
Takashi Iwai0ba21762007-04-16 11:29:14 +02003208static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3212 uinfo->count = 1;
3213 return 0;
3214}
3215
Takashi Iwai0ba21762007-04-16 11:29:14 +02003216static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3220 IEC958_AES0_NONAUDIO |
3221 IEC958_AES0_CON_EMPHASIS_5015 |
3222 IEC958_AES0_CON_NOT_COPYRIGHT;
3223 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3224 IEC958_AES1_CON_ORIGINAL;
3225 return 0;
3226}
3227
Takashi Iwai0ba21762007-04-16 11:29:14 +02003228static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3229 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3232 IEC958_AES0_NONAUDIO |
3233 IEC958_AES0_PRO_EMPHASIS_5015;
3234 return 0;
3235}
3236
Takashi Iwai0ba21762007-04-16 11:29:14 +02003237static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3238 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
3240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003241 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003242 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003244 mutex_lock(&codec->spdif_mutex);
3245 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003246 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3247 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3248 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3249 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003250 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 return 0;
3253}
3254
3255/* convert from SPDIF status bits to HDA SPDIF bits
3256 * bit 0 (DigEn) is always set zero (to be filled later)
3257 */
3258static unsigned short convert_from_spdif_status(unsigned int sbits)
3259{
3260 unsigned short val = 0;
3261
3262 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003263 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003265 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003267 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3268 IEC958_AES0_PRO_EMPHASIS_5015)
3269 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003271 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3272 IEC958_AES0_CON_EMPHASIS_5015)
3273 val |= AC_DIG1_EMPHASIS;
3274 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3275 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003277 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3279 }
3280 return val;
3281}
3282
3283/* convert to SPDIF status bits from HDA SPDIF bits
3284 */
3285static unsigned int convert_to_spdif_status(unsigned short val)
3286{
3287 unsigned int sbits = 0;
3288
Takashi Iwai0ba21762007-04-16 11:29:14 +02003289 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003291 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 sbits |= IEC958_AES0_PROFESSIONAL;
3293 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003294 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3296 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003297 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003299 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003301 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3303 sbits |= val & (0x7f << 8);
3304 }
3305 return sbits;
3306}
3307
Takashi Iwai2f728532008-09-25 16:32:41 +02003308/* set digital convert verbs both for the given NID and its slaves */
3309static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3310 int verb, int val)
3311{
Takashi Iwaidda14412011-05-02 11:29:30 +02003312 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003313
Takashi Iwai9e976972008-11-25 08:17:20 +01003314 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003315 d = codec->slave_dig_outs;
3316 if (!d)
3317 return;
3318 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003319 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003320}
3321
3322static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3323 int dig1, int dig2)
3324{
3325 if (dig1 != -1)
3326 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3327 if (dig2 != -1)
3328 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3329}
3330
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003335 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003336 struct hda_spdif_out *spdif;
3337 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 unsigned short val;
3339 int change;
3340
Ingo Molnar62932df2006-01-16 16:34:20 +01003341 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003342 spdif = snd_array_elem(&codec->spdif_out, idx);
3343 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003344 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3346 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3347 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003348 val = convert_from_spdif_status(spdif->status);
3349 val |= spdif->ctls & 1;
3350 change = spdif->ctls != val;
3351 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003352 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003353 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003354 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return change;
3356}
3357
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003358#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Takashi Iwai0ba21762007-04-16 11:29:14 +02003360static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3361 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003364 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003365 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003367 mutex_lock(&codec->spdif_mutex);
3368 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003369 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003370 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return 0;
3372}
3373
Stephen Warren74b654c2011-06-01 11:14:18 -06003374static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3375 int dig1, int dig2)
3376{
3377 set_dig_out_convert(codec, nid, dig1, dig2);
3378 /* unmute amp switch (if any) */
3379 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3380 (dig1 & AC_DIG1_ENABLE))
3381 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3382 HDA_AMP_MUTE, 0);
3383}
3384
Takashi Iwai0ba21762007-04-16 11:29:14 +02003385static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
3388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003389 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003390 struct hda_spdif_out *spdif;
3391 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 unsigned short val;
3393 int change;
3394
Ingo Molnar62932df2006-01-16 16:34:20 +01003395 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003396 spdif = snd_array_elem(&codec->spdif_out, idx);
3397 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003398 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003400 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003401 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003402 spdif->ctls = val;
3403 if (change && nid != (u16)-1)
3404 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003405 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return change;
3407}
3408
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003409static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 {
3411 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3412 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003413 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 .info = snd_hda_spdif_mask_info,
3415 .get = snd_hda_spdif_cmask_get,
3416 },
3417 {
3418 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3419 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003420 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 .info = snd_hda_spdif_mask_info,
3422 .get = snd_hda_spdif_pmask_get,
3423 },
3424 {
3425 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003426 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 .info = snd_hda_spdif_mask_info,
3428 .get = snd_hda_spdif_default_get,
3429 .put = snd_hda_spdif_default_put,
3430 },
3431 {
3432 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003433 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 .info = snd_hda_spdif_out_switch_info,
3435 .get = snd_hda_spdif_out_switch_get,
3436 .put = snd_hda_spdif_out_switch_put,
3437 },
3438 { } /* end */
3439};
3440
3441/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003442 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003444 * @associated_nid: NID that new ctls associated with
3445 * @cvt_nid: converter NID
3446 * @type: HDA_PCM_TYPE_*
3447 * Creates controls related with the digital output.
3448 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 *
3450 * Returns 0 if successful, or a negative error code.
3451 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003452int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3453 hda_nid_t associated_nid,
3454 hda_nid_t cvt_nid,
3455 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003458 struct snd_kcontrol *kctl;
3459 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003460 int idx = 0;
3461 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003462 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003463 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003465 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003466 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003467 idx = spdif_index;
3468 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003469 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003470 /* suppose a single SPDIF device */
3471 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3472 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3473 if (!kctl)
3474 break;
3475 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003476 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003477 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003478 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003479 if (!bus->primary_dig_out_type)
3480 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003481
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003482 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003483 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003484 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3485 return -EBUSY;
3486 }
Stephen Warren7c935972011-06-01 11:14:17 -06003487 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003488 if (!spdif)
3489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3491 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003492 if (!kctl)
3493 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003494 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003495 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003496 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003497 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return err;
3499 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003500 spdif->nid = cvt_nid;
3501 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003502 AC_VERB_GET_DIGI_CONVERT_1, 0);
3503 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return 0;
3505}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003506EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003508/* get the hda_spdif_out entry from the given NID
3509 * call within spdif_mutex lock
3510 */
Stephen Warren7c935972011-06-01 11:14:17 -06003511struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3512 hda_nid_t nid)
3513{
3514 int i;
3515 for (i = 0; i < codec->spdif_out.used; i++) {
3516 struct hda_spdif_out *spdif =
3517 snd_array_elem(&codec->spdif_out, i);
3518 if (spdif->nid == nid)
3519 return spdif;
3520 }
3521 return NULL;
3522}
3523EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3524
Stephen Warren74b654c2011-06-01 11:14:18 -06003525void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3526{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003527 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003528
3529 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003530 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003531 spdif->nid = (u16)-1;
3532 mutex_unlock(&codec->spdif_mutex);
3533}
3534EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3535
3536void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3537{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003538 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003539 unsigned short val;
3540
3541 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003542 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003543 if (spdif->nid != nid) {
3544 spdif->nid = nid;
3545 val = spdif->ctls;
3546 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3547 }
3548 mutex_unlock(&codec->spdif_mutex);
3549}
3550EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003553 * SPDIF sharing with analog output
3554 */
3555static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3556 struct snd_ctl_elem_value *ucontrol)
3557{
3558 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3559 ucontrol->value.integer.value[0] = mout->share_spdif;
3560 return 0;
3561}
3562
3563static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3564 struct snd_ctl_elem_value *ucontrol)
3565{
3566 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3567 mout->share_spdif = !!ucontrol->value.integer.value[0];
3568 return 0;
3569}
3570
3571static struct snd_kcontrol_new spdif_share_sw = {
3572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3573 .name = "IEC958 Default PCM Playback Switch",
3574 .info = snd_ctl_boolean_mono_info,
3575 .get = spdif_share_sw_get,
3576 .put = spdif_share_sw_put,
3577};
3578
Takashi Iwaid5191e52009-11-16 14:58:17 +01003579/**
3580 * snd_hda_create_spdif_share_sw - create Default PCM switch
3581 * @codec: the HDA codec
3582 * @mout: multi-out instance
3583 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003584int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3585 struct hda_multi_out *mout)
3586{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003587 struct snd_kcontrol *kctl;
3588
Takashi Iwai9a081602008-02-12 18:37:26 +01003589 if (!mout->dig_out_nid)
3590 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003591
3592 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3593 if (!kctl)
3594 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003595 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003596 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003598EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003599
3600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 * SPDIF input
3602 */
3603
3604#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3605
Takashi Iwai0ba21762007-04-16 11:29:14 +02003606static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3607 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3610
3611 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3612 return 0;
3613}
3614
Takashi Iwai0ba21762007-04-16 11:29:14 +02003615static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3616 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
3618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3619 hda_nid_t nid = kcontrol->private_value;
3620 unsigned int val = !!ucontrol->value.integer.value[0];
3621 int change;
3622
Ingo Molnar62932df2006-01-16 16:34:20 +01003623 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003625 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003627 snd_hda_codec_write_cache(codec, nid, 0,
3628 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003630 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 return change;
3632}
3633
Takashi Iwai0ba21762007-04-16 11:29:14 +02003634static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3635 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3638 hda_nid_t nid = kcontrol->private_value;
3639 unsigned short val;
3640 unsigned int sbits;
3641
Andrew Paprocki3982d172007-12-19 12:13:44 +01003642 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 sbits = convert_to_spdif_status(val);
3644 ucontrol->value.iec958.status[0] = sbits;
3645 ucontrol->value.iec958.status[1] = sbits >> 8;
3646 ucontrol->value.iec958.status[2] = sbits >> 16;
3647 ucontrol->value.iec958.status[3] = sbits >> 24;
3648 return 0;
3649}
3650
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003651static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 {
3653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003654 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 .info = snd_hda_spdif_in_switch_info,
3656 .get = snd_hda_spdif_in_switch_get,
3657 .put = snd_hda_spdif_in_switch_put,
3658 },
3659 {
3660 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003662 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 .info = snd_hda_spdif_mask_info,
3664 .get = snd_hda_spdif_in_status_get,
3665 },
3666 { } /* end */
3667};
3668
3669/**
3670 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3671 * @codec: the HDA codec
3672 * @nid: audio in widget NID
3673 *
3674 * Creates controls related with the SPDIF input.
3675 * Called from each patch supporting the SPDIF in.
3676 *
3677 * Returns 0 if successful, or a negative error code.
3678 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003679int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
3681 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003682 struct snd_kcontrol *kctl;
3683 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003684 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Takashi Iwaidcda5802012-10-12 17:24:51 +02003686 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003687 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003688 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3689 return -EBUSY;
3690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3692 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003693 if (!kctl)
3694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003696 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003697 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return err;
3699 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003700 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003701 snd_hda_codec_read(codec, nid, 0,
3702 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003703 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003706EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003708/*
3709 * command cache
3710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Takashi Iwaic370dd62012-12-13 18:30:04 +01003712/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003713#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3714#define get_cmd_cache_nid(key) ((key) & 0xff)
3715#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3716
3717/**
3718 * snd_hda_codec_write_cache - send a single command with caching
3719 * @codec: the HDA codec
3720 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003721 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003722 * @verb: the verb to send
3723 * @parm: the parameter for the verb
3724 *
3725 * Send a single command without waiting for response.
3726 *
3727 * Returns 0 if successful, or a negative error code.
3728 */
3729int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003730 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003731{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003732 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003733 struct hda_cache_head *c;
3734 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003735 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003736
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003737 cache_only = codec->cached_write;
3738 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003739 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003740 if (err < 0)
3741 return err;
3742 }
3743
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003744 /* parm may contain the verb stuff for get/set amp */
3745 verb = verb | (parm >> 8);
3746 parm &= 0xff;
3747 key = build_cmd_cache_key(nid, verb);
3748 mutex_lock(&codec->bus->cmd_mutex);
3749 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003750 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003751 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003752 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003753 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003754 mutex_unlock(&codec->bus->cmd_mutex);
3755 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003756}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003757EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003758
Takashi Iwaid5191e52009-11-16 14:58:17 +01003759/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003760 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3761 * @codec: the HDA codec
3762 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003763 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003764 * @verb: the verb to send
3765 * @parm: the parameter for the verb
3766 *
3767 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3768 * command if the parameter is already identical with the cached value.
3769 * If not, it sends the command and refreshes the cache.
3770 *
3771 * Returns 0 if successful, or a negative error code.
3772 */
3773int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003774 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003775{
3776 struct hda_cache_head *c;
3777 u32 key;
3778
3779 /* parm may contain the verb stuff for get/set amp */
3780 verb = verb | (parm >> 8);
3781 parm &= 0xff;
3782 key = build_cmd_cache_key(nid, verb);
3783 mutex_lock(&codec->bus->cmd_mutex);
3784 c = get_hash(&codec->cmd_cache, key);
3785 if (c && c->val == parm) {
3786 mutex_unlock(&codec->bus->cmd_mutex);
3787 return 0;
3788 }
3789 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003790 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003791}
3792EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3793
3794/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003795 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3796 * @codec: HD-audio codec
3797 *
3798 * Execute all verbs recorded in the command caches to resume.
3799 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003800void snd_hda_codec_resume_cache(struct hda_codec *codec)
3801{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003802 int i;
3803
Takashi Iwaic370dd62012-12-13 18:30:04 +01003804 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003805 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003806 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3807 struct hda_cache_head *buffer;
3808 u32 key;
3809
3810 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3811 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003812 if (!key)
3813 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003814 if (!buffer->dirty)
3815 continue;
3816 buffer->dirty = 0;
3817 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003818 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3819 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003820 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003821 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003822 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003824EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003825
3826/**
3827 * snd_hda_sequence_write_cache - sequence writes with caching
3828 * @codec: the HDA codec
3829 * @seq: VERB array to send
3830 *
3831 * Send the commands sequentially from the given array.
3832 * Thte commands are recorded on cache for power-save and resume.
3833 * The array must be terminated with NID=0.
3834 */
3835void snd_hda_sequence_write_cache(struct hda_codec *codec,
3836 const struct hda_verb *seq)
3837{
3838 for (; seq->nid; seq++)
3839 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3840 seq->param);
3841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003842EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003843
Takashi Iwaidc870f32013-01-22 15:24:30 +01003844/**
3845 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3846 * @codec: HD-audio codec
3847 */
3848void snd_hda_codec_flush_cache(struct hda_codec *codec)
3849{
3850 snd_hda_codec_resume_amp(codec);
3851 snd_hda_codec_resume_cache(codec);
3852}
3853EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3854
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003855void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003856 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003857{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003858 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003859 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003860
Takashi Iwaicb53c622007-08-10 17:21:45 +02003861 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003862 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003863 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003864 if (!(wcaps & AC_WCAP_POWER))
3865 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003866 if (codec->power_filter) {
3867 state = codec->power_filter(codec, nid, power_state);
3868 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003869 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003870 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003871 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003872 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003873 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003874}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003875EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3876
3877/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003878 * supported power states check
3879 */
3880static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3881 unsigned int power_state)
3882{
3883 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3884
Mengdong Line037cb42012-08-10 14:11:58 +02003885 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003886 return false;
3887 if (sup & power_state)
3888 return true;
3889 else
3890 return false;
3891}
3892
3893/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003894 * wait until the state is reached, returns the current state
3895 */
3896static unsigned int hda_sync_power_state(struct hda_codec *codec,
3897 hda_nid_t fg,
3898 unsigned int power_state)
3899{
3900 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3901 unsigned int state, actual_state;
3902
3903 for (;;) {
3904 state = snd_hda_codec_read(codec, fg, 0,
3905 AC_VERB_GET_POWER_STATE, 0);
3906 if (state & AC_PWRST_ERROR)
3907 break;
3908 actual_state = (state >> 4) & 0x0f;
3909 if (actual_state == power_state)
3910 break;
3911 if (time_after_eq(jiffies, end_time))
3912 break;
3913 /* wait until the codec reachs to the target state */
3914 msleep(1);
3915 }
3916 return state;
3917}
3918
Takashi Iwai9419ab62013-01-24 17:23:35 +01003919/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003920unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3921 hda_nid_t nid,
3922 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003923{
3924 if (power_state == AC_PWRST_D3 &&
3925 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3926 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3927 int eapd = snd_hda_codec_read(codec, nid, 0,
3928 AC_VERB_GET_EAPD_BTLENABLE, 0);
3929 if (eapd & 0x02)
3930 return AC_PWRST_D0;
3931 }
3932 return power_state;
3933}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003934EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003935
Takashi Iwai432c6412012-08-28 09:59:20 -07003936/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003937 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003938 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003939static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003940 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003941{
Takashi Iwaid8193872012-08-31 07:54:38 -07003942 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003943 int count;
3944 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003945 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003946
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003947 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003948 if (power_state == AC_PWRST_D3) {
3949 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003950 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003951 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003952 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003953
3954 /* repeat power states setting at most 10 times*/
3955 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003956 if (codec->patch_ops.set_power_state)
3957 codec->patch_ops.set_power_state(codec, fg,
3958 power_state);
3959 else {
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003960 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003961 AC_VERB_SET_POWER_STATE,
3962 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003963 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003964 }
3965 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003966 if (!(state & AC_PWRST_ERROR))
3967 break;
3968 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003969
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003970 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003971}
Takashi Iwai54d17402005-11-21 16:33:22 +01003972
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003973/* sync power states of all widgets;
3974 * this is called at the end of codec parsing
3975 */
3976static void sync_power_up_states(struct hda_codec *codec)
3977{
3978 hda_nid_t nid = codec->start_nid;
3979 int i;
3980
Takashi Iwaiba615b82013-03-13 14:47:21 +01003981 /* don't care if no filter is used */
3982 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003983 return;
3984
3985 for (i = 0; i < codec->num_nodes; i++, nid++) {
3986 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003987 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003988 if (!(wcaps & AC_WCAP_POWER))
3989 continue;
3990 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3991 if (target == AC_PWRST_D0)
3992 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003993 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003994 snd_hda_codec_write(codec, nid, 0,
3995 AC_VERB_SET_POWER_STATE, target);
3996 }
3997}
3998
Takashi Iwai11aeff02008-07-30 15:01:46 +02003999#ifdef CONFIG_SND_HDA_HWDEP
4000/* execute additional init verbs */
4001static void hda_exec_init_verbs(struct hda_codec *codec)
4002{
4003 if (codec->init_verbs.list)
4004 snd_hda_sequence_write(codec, codec->init_verbs.list);
4005}
4006#else
4007static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4008#endif
4009
Takashi Iwai2a439522011-07-26 09:52:50 +02004010#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004011/*
4012 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004013 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004014 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004015static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004016{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004017 unsigned int state;
4018
Takashi Iwai989c3182012-11-19 14:14:58 +01004019 codec->in_pm = 1;
4020
Takashi Iwaicb53c622007-08-10 17:21:45 +02004021 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004022 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004023 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004024 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004025 /* Cancel delayed work if we aren't currently running from it. */
4026 if (!in_wq)
4027 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004028 spin_lock(&codec->power_lock);
4029 snd_hda_update_power_acct(codec);
4030 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004031 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004032 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004033 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004034 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004035 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004036 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004037}
4038
Takashi Iwaic370dd62012-12-13 18:30:04 +01004039/* mark all entries of cmd and amp caches dirty */
4040static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4041{
4042 int i;
4043 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4044 struct hda_cache_head *cmd;
4045 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4046 cmd->dirty = 1;
4047 }
4048 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4049 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004050 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004051 amp->head.dirty = 1;
4052 }
4053}
4054
Takashi Iwaicb53c622007-08-10 17:21:45 +02004055/*
4056 * kick up codec; used both from PM and power-save
4057 */
4058static void hda_call_codec_resume(struct hda_codec *codec)
4059{
Takashi Iwai989c3182012-11-19 14:14:58 +01004060 codec->in_pm = 1;
4061
Takashi Iwaic370dd62012-12-13 18:30:04 +01004062 hda_mark_cmd_cache_dirty(codec);
4063
Takashi Iwai7f308302012-05-08 16:52:23 +02004064 /* set as if powered on for avoiding re-entering the resume
4065 * in the resume / power-save sequence
4066 */
4067 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004068 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004069 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004070 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004071 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004072 if (codec->patch_ops.resume)
4073 codec->patch_ops.resume(codec);
4074 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004075 if (codec->patch_ops.init)
4076 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004077 snd_hda_codec_resume_amp(codec);
4078 snd_hda_codec_resume_cache(codec);
4079 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004080
4081 if (codec->jackpoll_interval)
4082 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004083 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004084 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004085
4086 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004087 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004088}
Takashi Iwai2a439522011-07-26 09:52:50 +02004089#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004090
Takashi Iwai54d17402005-11-21 16:33:22 +01004091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092/**
4093 * snd_hda_build_controls - build mixer controls
4094 * @bus: the BUS
4095 *
4096 * Creates mixer controls for each codec included in the bus.
4097 *
4098 * Returns 0 if successful, otherwise a negative error code.
4099 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004100int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004102 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Takashi Iwai0ba21762007-04-16 11:29:14 +02004104 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004105 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004106 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004107 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004108 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004109 err = snd_hda_codec_reset(codec);
4110 if (err < 0) {
4111 printk(KERN_ERR
4112 "hda_codec: cannot revert codec\n");
4113 return err;
4114 }
4115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004117 return 0;
4118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004119EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004121/*
4122 * add standard channel maps if not specified
4123 */
4124static int add_std_chmaps(struct hda_codec *codec)
4125{
4126 int i, str, err;
4127
4128 for (i = 0; i < codec->num_pcms; i++) {
4129 for (str = 0; str < 2; str++) {
4130 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4131 struct hda_pcm_stream *hinfo =
4132 &codec->pcm_info[i].stream[str];
4133 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004134 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004135
4136 if (codec->pcm_info[i].own_chmap)
4137 continue;
4138 if (!pcm || !hinfo->substreams)
4139 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004140 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4141 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004142 hinfo->channels_max,
4143 0, &chmap);
4144 if (err < 0)
4145 return err;
4146 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4147 }
4148 }
4149 return 0;
4150}
4151
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004152/* default channel maps for 2.1 speakers;
4153 * since HD-audio supports only stereo, odd number channels are omitted
4154 */
4155const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4156 { .channels = 2,
4157 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4158 { .channels = 4,
4159 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4160 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4161 { }
4162};
4163EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4164
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004165int snd_hda_codec_build_controls(struct hda_codec *codec)
4166{
4167 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004168 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004169 /* continue to initialize... */
4170 if (codec->patch_ops.init)
4171 err = codec->patch_ops.init(codec);
4172 if (!err && codec->patch_ops.build_controls)
4173 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004174 if (err < 0)
4175 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004176
4177 /* we create chmaps here instead of build_pcms */
4178 err = add_std_chmaps(codec);
4179 if (err < 0)
4180 return err;
4181
David Henningsson26a6cb62012-10-09 15:04:21 +02004182 if (codec->jackpoll_interval)
4183 hda_jackpoll_work(&codec->jackpoll_work.work);
4184 else
4185 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004186 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 return 0;
4188}
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190/*
4191 * stream formats
4192 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004193struct hda_rate_tbl {
4194 unsigned int hz;
4195 unsigned int alsa_bits;
4196 unsigned int hda_fmt;
4197};
4198
Takashi Iwai92f10b32010-08-03 14:21:00 +02004199/* rate = base * mult / div */
4200#define HDA_RATE(base, mult, div) \
4201 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4202 (((div) - 1) << AC_FMT_DIV_SHIFT))
4203
Takashi Iwaibefdf312005-08-22 13:57:55 +02004204static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004206
4207 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004208 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4209 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4210 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4211 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4212 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4213 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4214 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4215 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4216 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4217 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4218 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004219#define AC_PAR_PCM_RATE_BITS 11
4220 /* up to bits 10, 384kHZ isn't supported properly */
4221
4222 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004223 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004224
Takashi Iwaibefdf312005-08-22 13:57:55 +02004225 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226};
4227
4228/**
4229 * snd_hda_calc_stream_format - calculate format bitset
4230 * @rate: the sample rate
4231 * @channels: the number of channels
4232 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4233 * @maxbps: the max. bps
4234 *
4235 * Calculate the format bitset from the given rate, channels and th PCM format.
4236 *
4237 * Return zero if invalid.
4238 */
4239unsigned int snd_hda_calc_stream_format(unsigned int rate,
4240 unsigned int channels,
4241 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004242 unsigned int maxbps,
4243 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 int i;
4246 unsigned int val = 0;
4247
Takashi Iwaibefdf312005-08-22 13:57:55 +02004248 for (i = 0; rate_bits[i].hz; i++)
4249 if (rate_bits[i].hz == rate) {
4250 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 break;
4252 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004253 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 snd_printdd("invalid rate %d\n", rate);
4255 return 0;
4256 }
4257
4258 if (channels == 0 || channels > 8) {
4259 snd_printdd("invalid channels %d\n", channels);
4260 return 0;
4261 }
4262 val |= channels - 1;
4263
4264 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004265 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004266 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004267 break;
4268 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004269 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 case 20:
4272 case 24:
4273 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004274 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004275 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004277 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004279 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 break;
4281 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004282 snd_printdd("invalid format width %d\n",
4283 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 return 0;
4285 }
4286
Anssi Hannula32c168c2010-08-03 13:28:57 +03004287 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004288 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return val;
4291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004292EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004294static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4295 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004296{
4297 unsigned int val = 0;
4298 if (nid != codec->afg &&
4299 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4300 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4301 if (!val || val == -1)
4302 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4303 if (!val || val == -1)
4304 return 0;
4305 return val;
4306}
4307
4308static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4309{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004310 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004311 get_pcm_param);
4312}
4313
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004314static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4315 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004316{
4317 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4318 if (!streams || streams == -1)
4319 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4320 if (!streams || streams == -1)
4321 return 0;
4322 return streams;
4323}
4324
4325static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4326{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004327 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004328 get_stream_param);
4329}
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331/**
4332 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4333 * @codec: the HDA codec
4334 * @nid: NID to query
4335 * @ratesp: the pointer to store the detected rate bitflags
4336 * @formatsp: the pointer to store the detected formats
4337 * @bpsp: the pointer to store the detected format widths
4338 *
4339 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4340 * or @bsps argument is ignored.
4341 *
4342 * Returns 0 if successful, otherwise a negative error code.
4343 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004344int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4346{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004347 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004349 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004350 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 if (ratesp) {
4353 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004354 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004356 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004358 if (rates == 0) {
4359 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4360 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4361 nid, val,
4362 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4363 return -EIO;
4364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 *ratesp = rates;
4366 }
4367
4368 if (formatsp || bpsp) {
4369 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004370 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004372 streams = query_stream_param(codec, nid);
4373 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376 bps = 0;
4377 if (streams & AC_SUPFMT_PCM) {
4378 if (val & AC_SUPPCM_BITS_8) {
4379 formats |= SNDRV_PCM_FMTBIT_U8;
4380 bps = 8;
4381 }
4382 if (val & AC_SUPPCM_BITS_16) {
4383 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4384 bps = 16;
4385 }
4386 if (wcaps & AC_WCAP_DIGITAL) {
4387 if (val & AC_SUPPCM_BITS_32)
4388 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4389 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4390 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4391 if (val & AC_SUPPCM_BITS_24)
4392 bps = 24;
4393 else if (val & AC_SUPPCM_BITS_20)
4394 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004395 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4396 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4398 if (val & AC_SUPPCM_BITS_32)
4399 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 else if (val & AC_SUPPCM_BITS_24)
4401 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004402 else if (val & AC_SUPPCM_BITS_20)
4403 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
4405 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004406#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004407 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004409 if (!bps)
4410 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004411 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004412#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004413 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004414 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 /* temporary hack: we have still no proper support
4416 * for the direct AC3 stream...
4417 */
4418 formats |= SNDRV_PCM_FMTBIT_U8;
4419 bps = 8;
4420 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004421 if (formats == 0) {
4422 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4423 "(nid=0x%x, val=0x%x, ovrd=%i, "
4424 "streams=0x%x)\n",
4425 nid, val,
4426 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4427 streams);
4428 return -EIO;
4429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (formatsp)
4431 *formatsp = formats;
4432 if (bpsp)
4433 *bpsp = bps;
4434 }
4435
4436 return 0;
4437}
Stephen Warren384a48d2011-06-01 11:14:21 -06004438EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004441 * snd_hda_is_supported_format - Check the validity of the format
4442 * @codec: HD-audio codec
4443 * @nid: NID to check
4444 * @format: the HD-audio format value to check
4445 *
4446 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 *
4448 * Returns 1 if supported, 0 if not.
4449 */
4450int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4451 unsigned int format)
4452{
4453 int i;
4454 unsigned int val = 0, rate, stream;
4455
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004456 val = query_pcm_param(codec, nid);
4457 if (!val)
4458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
4460 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004461 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004462 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (val & (1 << i))
4464 break;
4465 return 0;
4466 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004467 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return 0;
4469
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004470 stream = query_stream_param(codec, nid);
4471 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return 0;
4473
4474 if (stream & AC_SUPFMT_PCM) {
4475 switch (format & 0xf0) {
4476 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004477 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return 0;
4479 break;
4480 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004481 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return 0;
4483 break;
4484 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004485 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return 0;
4487 break;
4488 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004489 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return 0;
4491 break;
4492 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004493 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 return 0;
4495 break;
4496 default:
4497 return 0;
4498 }
4499 } else {
4500 /* FIXME: check for float32 and AC3? */
4501 }
4502
4503 return 1;
4504}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004505EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507/*
4508 * PCM stuff
4509 */
4510static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4511 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004512 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
4514 return 0;
4515}
4516
4517static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4518 struct hda_codec *codec,
4519 unsigned int stream_tag,
4520 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004521 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
4523 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4524 return 0;
4525}
4526
4527static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4528 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004529 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
Takashi Iwai888afa12008-03-18 09:57:50 +01004531 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return 0;
4533}
4534
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004535static int set_pcm_default_values(struct hda_codec *codec,
4536 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004538 int err;
4539
Takashi Iwai0ba21762007-04-16 11:29:14 +02004540 /* query support PCM information from the given NID */
4541 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004542 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004543 info->rates ? NULL : &info->rates,
4544 info->formats ? NULL : &info->formats,
4545 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004546 if (err < 0)
4547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 }
4549 if (info->ops.open == NULL)
4550 info->ops.open = hda_pcm_default_open_close;
4551 if (info->ops.close == NULL)
4552 info->ops.close = hda_pcm_default_open_close;
4553 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004554 if (snd_BUG_ON(!info->nid))
4555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 info->ops.prepare = hda_pcm_default_prepare;
4557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004559 if (snd_BUG_ON(!info->nid))
4560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 info->ops.cleanup = hda_pcm_default_cleanup;
4562 }
4563 return 0;
4564}
4565
Takashi Iwaieb541332010-08-06 13:48:11 +02004566/*
4567 * codec prepare/cleanup entries
4568 */
4569int snd_hda_codec_prepare(struct hda_codec *codec,
4570 struct hda_pcm_stream *hinfo,
4571 unsigned int stream,
4572 unsigned int format,
4573 struct snd_pcm_substream *substream)
4574{
4575 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004576 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004577 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4578 if (ret >= 0)
4579 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004580 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004581 return ret;
4582}
4583EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4584
4585void snd_hda_codec_cleanup(struct hda_codec *codec,
4586 struct hda_pcm_stream *hinfo,
4587 struct snd_pcm_substream *substream)
4588{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004589 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004590 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004591 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004592}
4593EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4594
Takashi Iwaid5191e52009-11-16 14:58:17 +01004595/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004596const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4597 "Audio", "SPDIF", "HDMI", "Modem"
4598};
4599
Takashi Iwai176d5332008-07-30 15:01:44 +02004600/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004601 * get the empty PCM device number to assign
4602 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004603static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004604{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004605 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004606 /* assigned to static slots up to dev#10; if more needed, assign
4607 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4608 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004609 static int audio_idx[HDA_PCM_NTYPES][5] = {
4610 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4611 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004612 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004613 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004614 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004615 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004616
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004617 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004618 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4619 return -EINVAL;
4620 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004621
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004622 for (i = 0; audio_idx[type][i] >= 0; i++) {
4623#ifndef CONFIG_SND_DYNAMIC_MINORS
4624 if (audio_idx[type][i] >= 8)
4625 break;
4626#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004627 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4628 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004629 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004630
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004631#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004632 /* non-fixed slots starting from 10 */
4633 for (i = 10; i < 32; i++) {
4634 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4635 return i;
4636 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004637#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004638
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004639 snd_printk(KERN_WARNING "Too many %s devices\n",
4640 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004641#ifndef CONFIG_SND_DYNAMIC_MINORS
4642 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4643#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004644 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004645}
4646
4647/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004648 * attach a new PCM stream
4649 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004650static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004651{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004652 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004653 struct hda_pcm_stream *info;
4654 int stream, err;
4655
Takashi Iwaib91f0802008-11-04 08:43:08 +01004656 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004657 return -EINVAL;
4658 for (stream = 0; stream < 2; stream++) {
4659 info = &pcm->stream[stream];
4660 if (info->substreams) {
4661 err = set_pcm_default_values(codec, info);
4662 if (err < 0)
4663 return err;
4664 }
4665 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004666 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004667}
4668
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004669/* assign all PCMs of the given codec */
4670int snd_hda_codec_build_pcms(struct hda_codec *codec)
4671{
4672 unsigned int pcm;
4673 int err;
4674
4675 if (!codec->num_pcms) {
4676 if (!codec->patch_ops.build_pcms)
4677 return 0;
4678 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004679 if (err < 0) {
4680 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004681 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004682 err = snd_hda_codec_reset(codec);
4683 if (err < 0) {
4684 printk(KERN_ERR
4685 "hda_codec: cannot revert codec\n");
4686 return err;
4687 }
4688 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004689 }
4690 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4691 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4692 int dev;
4693
4694 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004695 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004696
4697 if (!cpcm->pcm) {
4698 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4699 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004700 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004701 cpcm->device = dev;
4702 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004703 if (err < 0) {
4704 printk(KERN_ERR "hda_codec: cannot attach "
4705 "PCM stream %d for codec #%d\n",
4706 dev, codec->addr);
4707 continue; /* no fatal error */
4708 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004709 }
4710 }
4711 return 0;
4712}
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714/**
4715 * snd_hda_build_pcms - build PCM information
4716 * @bus: the BUS
4717 *
4718 * Create PCM information for each codec included in the bus.
4719 *
4720 * The build_pcms codec patch is requested to set up codec->num_pcms and
4721 * codec->pcm_info properly. The array is referred by the top-level driver
4722 * to create its PCM instances.
4723 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4724 * callback.
4725 *
4726 * At least, substreams, channels_min and channels_max must be filled for
4727 * each stream. substreams = 0 indicates that the stream doesn't exist.
4728 * When rates and/or formats are zero, the supported values are queried
4729 * from the given nid. The nid is used also by the default ops.prepare
4730 * and ops.cleanup callbacks.
4731 *
4732 * The driver needs to call ops.open in its open callback. Similarly,
4733 * ops.close is supposed to be called in the close callback.
4734 * ops.prepare should be called in the prepare or hw_params callback
4735 * with the proper parameters for set up.
4736 * ops.cleanup should be called in hw_free for clean up of streams.
4737 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004738 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004740int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004742 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Takashi Iwai0ba21762007-04-16 11:29:14 +02004744 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004745 int err = snd_hda_codec_build_pcms(codec);
4746 if (err < 0)
4747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749 return 0;
4750}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004751EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753/**
4754 * snd_hda_check_board_config - compare the current codec with the config table
4755 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004756 * @num_configs: number of config enums
4757 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 * @tbl: configuration table, terminated by null entries
4759 *
4760 * Compares the modelname or PCI subsystem id of the current codec with the
4761 * given configuration table. If a matching entry is found, returns its
4762 * config value (supposed to be 0 or positive).
4763 *
4764 * If no entries are matching, the function returns a negative value.
4765 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004766int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004767 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004768 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004770 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004771 int i;
4772 for (i = 0; i < num_configs; i++) {
4773 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004774 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004775 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4776 "selected\n", models[i]);
4777 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779 }
4780 }
4781
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004782 if (!codec->bus->pci || !tbl)
4783 return -1;
4784
4785 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4786 if (!tbl)
4787 return -1;
4788 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004789#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004790 char tmp[10];
4791 const char *model = NULL;
4792 if (models)
4793 model = models[tbl->value];
4794 if (!model) {
4795 sprintf(tmp, "#%d", tbl->value);
4796 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004798 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4799 "for config %x:%x (%s)\n",
4800 model, tbl->subvendor, tbl->subdevice,
4801 (tbl->name ? tbl->name : "Unknown device"));
4802#endif
4803 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805 return -1;
4806}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004807EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004810 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004811 subsystem ID with the
4812 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004813
4814 This is important for Gateway notebooks with SB450 HDA Audio
4815 where the vendor ID of the PCI device is:
4816 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4817 and the vendor/subvendor are found only at the codec.
4818
4819 * @codec: the HDA codec
4820 * @num_configs: number of config enums
4821 * @models: array of model name strings
4822 * @tbl: configuration table, terminated by null entries
4823 *
4824 * Compares the modelname or PCI subsystem id of the current codec with the
4825 * given configuration table. If a matching entry is found, returns its
4826 * config value (supposed to be 0 or positive).
4827 *
4828 * If no entries are matching, the function returns a negative value.
4829 */
4830int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004831 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004832 const struct snd_pci_quirk *tbl)
4833{
4834 const struct snd_pci_quirk *q;
4835
4836 /* Search for codec ID */
4837 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004838 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4839 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4840 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004841 break;
4842 }
4843
4844 if (!q->subvendor)
4845 return -1;
4846
4847 tbl = q;
4848
4849 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004850#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004851 char tmp[10];
4852 const char *model = NULL;
4853 if (models)
4854 model = models[tbl->value];
4855 if (!model) {
4856 sprintf(tmp, "#%d", tbl->value);
4857 model = tmp;
4858 }
4859 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4860 "for config %x:%x (%s)\n",
4861 model, tbl->subvendor, tbl->subdevice,
4862 (tbl->name ? tbl->name : "Unknown device"));
4863#endif
4864 return tbl->value;
4865 }
4866 return -1;
4867}
4868EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4869
4870/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 * snd_hda_add_new_ctls - create controls from the array
4872 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004873 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 *
4875 * This helper function creates and add new controls in the given array.
4876 * The array must be terminated with an empty entry as terminator.
4877 *
4878 * Returns 0 if successful, or a negative error code.
4879 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004880int snd_hda_add_new_ctls(struct hda_codec *codec,
4881 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004883 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004886 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004887 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004888 if (knew->iface == -1) /* skip this codec private value */
4889 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004890 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004891 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004892 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004893 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004894 if (addr > 0)
4895 kctl->id.device = addr;
4896 if (idx > 0)
4897 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004898 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004899 if (!err)
4900 break;
4901 /* try first with another device index corresponding to
4902 * the codec addr; if it still fails (or it's the
4903 * primary codec), then try another control index
4904 */
4905 if (!addr && codec->addr)
4906 addr = codec->addr;
4907 else if (!idx && !knew->index) {
4908 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004909 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004910 if (idx <= 0)
4911 return err;
4912 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004913 return err;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
4916 return 0;
4917}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004918EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Takashi Iwai83012a72012-08-24 18:38:08 +02004920#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004921static void hda_power_work(struct work_struct *work)
4922{
4923 struct hda_codec *codec =
4924 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004925 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004926 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004927
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004928 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004929 if (codec->power_transition > 0) { /* during power-up sequence? */
4930 spin_unlock(&codec->power_lock);
4931 return;
4932 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004933 if (!codec->power_on || codec->power_count) {
4934 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004935 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004936 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004937 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004938 spin_unlock(&codec->power_lock);
4939
Dylan Reidd17344b2012-09-28 15:57:01 -07004940 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004941 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4942 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004943}
4944
4945static void hda_keep_power_on(struct hda_codec *codec)
4946{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004947 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004948 codec->power_count++;
4949 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004950 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004951 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004952 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004953}
4954
Takashi Iwaid5191e52009-11-16 14:58:17 +01004955/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004956void snd_hda_update_power_acct(struct hda_codec *codec)
4957{
4958 unsigned long delta = jiffies - codec->power_jiffies;
4959 if (codec->power_on)
4960 codec->power_on_acct += delta;
4961 else
4962 codec->power_off_acct += delta;
4963 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004964}
4965
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004966/* Transition to powered up, if wait_power_down then wait for a pending
4967 * transition to D3 to complete. A pending D3 transition is indicated
4968 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004969/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004970static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004971{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004972 /* Return if power_on or transitioning to power_on, unless currently
4973 * powering down. */
4974 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004975 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004976 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004977 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004978
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004979 cancel_delayed_work_sync(&codec->power_work);
4980
4981 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004982 /* If the power down delayed work was cancelled above before starting,
4983 * then there is no need to go through power up here.
4984 */
4985 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004986 if (codec->power_transition < 0)
4987 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004988 return;
4989 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004990
Takashi Iwaid66fee52011-08-02 15:39:31 +02004991 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004992 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004993 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004994 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004995 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004996 spin_unlock(&codec->power_lock);
4997
Takashi Iwaicb53c622007-08-10 17:21:45 +02004998 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004999
5000 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005001 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005002}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005003
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005004#define power_save(codec) \
5005 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005006
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005007/* Transition to powered down */
5008static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005009{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005010 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005011 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005012
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005013 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005014 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005015 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005016 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005017 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005018}
5019
5020/**
5021 * snd_hda_power_save - Power-up/down/sync the codec
5022 * @codec: HD-audio codec
5023 * @delta: the counter delta to change
5024 *
5025 * Change the power-up counter via @delta, and power up or down the hardware
5026 * appropriately. For the power-down, queue to the delayed action.
5027 * Passing zero to @delta means to synchronize the power state.
5028 */
5029void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5030{
5031 spin_lock(&codec->power_lock);
5032 codec->power_count += delta;
5033 trace_hda_power_count(codec);
5034 if (delta > 0)
5035 __snd_hda_power_up(codec, d3wait);
5036 else
5037 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005038 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005039}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005040EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005041
Takashi Iwaid5191e52009-11-16 14:58:17 +01005042/**
5043 * snd_hda_check_amp_list_power - Check the amp list and update the power
5044 * @codec: HD-audio codec
5045 * @check: the object containing an AMP list and the status
5046 * @nid: NID to check / update
5047 *
5048 * Check whether the given NID is in the amp list. If it's in the list,
5049 * check the current AMP status, and update the the power-status according
5050 * to the mute status.
5051 *
5052 * This function is supposed to be set or called from the check_power_status
5053 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005054 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005055int snd_hda_check_amp_list_power(struct hda_codec *codec,
5056 struct hda_loopback_check *check,
5057 hda_nid_t nid)
5058{
Takashi Iwai031024e2011-05-02 11:29:30 +02005059 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005060 int ch, v;
5061
5062 if (!check->amplist)
5063 return 0;
5064 for (p = check->amplist; p->nid; p++) {
5065 if (p->nid == nid)
5066 break;
5067 }
5068 if (!p->nid)
5069 return 0; /* nothing changed */
5070
5071 for (p = check->amplist; p->nid; p++) {
5072 for (ch = 0; ch < 2; ch++) {
5073 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5074 p->idx);
5075 if (!(v & HDA_AMP_MUTE) && v > 0) {
5076 if (!check->power_on) {
5077 check->power_on = 1;
5078 snd_hda_power_up(codec);
5079 }
5080 return 1;
5081 }
5082 }
5083 }
5084 if (check->power_on) {
5085 check->power_on = 0;
5086 snd_hda_power_down(codec);
5087 }
5088 return 0;
5089}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005090EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005093/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005094 * Channel mode helper
5095 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005096
5097/**
5098 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5099 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005100int snd_hda_ch_mode_info(struct hda_codec *codec,
5101 struct snd_ctl_elem_info *uinfo,
5102 const struct hda_channel_mode *chmode,
5103 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005104{
5105 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5106 uinfo->count = 1;
5107 uinfo->value.enumerated.items = num_chmodes;
5108 if (uinfo->value.enumerated.item >= num_chmodes)
5109 uinfo->value.enumerated.item = num_chmodes - 1;
5110 sprintf(uinfo->value.enumerated.name, "%dch",
5111 chmode[uinfo->value.enumerated.item].channels);
5112 return 0;
5113}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005114EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005115
Takashi Iwaid5191e52009-11-16 14:58:17 +01005116/**
5117 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5118 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005119int snd_hda_ch_mode_get(struct hda_codec *codec,
5120 struct snd_ctl_elem_value *ucontrol,
5121 const struct hda_channel_mode *chmode,
5122 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005123 int max_channels)
5124{
5125 int i;
5126
5127 for (i = 0; i < num_chmodes; i++) {
5128 if (max_channels == chmode[i].channels) {
5129 ucontrol->value.enumerated.item[0] = i;
5130 break;
5131 }
5132 }
5133 return 0;
5134}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005135EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005136
Takashi Iwaid5191e52009-11-16 14:58:17 +01005137/**
5138 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5139 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005140int snd_hda_ch_mode_put(struct hda_codec *codec,
5141 struct snd_ctl_elem_value *ucontrol,
5142 const struct hda_channel_mode *chmode,
5143 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005144 int *max_channelsp)
5145{
5146 unsigned int mode;
5147
5148 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005149 if (mode >= num_chmodes)
5150 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005151 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005152 return 0;
5153 /* change the current channel setting */
5154 *max_channelsp = chmode[mode].channels;
5155 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005156 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005157 return 1;
5158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005159EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161/*
5162 * input MUX helper
5163 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164
5165/**
5166 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5167 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005168int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5169 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
5171 unsigned int index;
5172
5173 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5174 uinfo->count = 1;
5175 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005176 if (!imux->num_items)
5177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 index = uinfo->value.enumerated.item;
5179 if (index >= imux->num_items)
5180 index = imux->num_items - 1;
5181 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5182 return 0;
5183}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005184EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Takashi Iwaid5191e52009-11-16 14:58:17 +01005186/**
5187 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005189int snd_hda_input_mux_put(struct hda_codec *codec,
5190 const struct hda_input_mux *imux,
5191 struct snd_ctl_elem_value *ucontrol,
5192 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 unsigned int *cur_val)
5194{
5195 unsigned int idx;
5196
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005197 if (!imux->num_items)
5198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 idx = ucontrol->value.enumerated.item[0];
5200 if (idx >= imux->num_items)
5201 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005202 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005204 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5205 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 *cur_val = idx;
5207 return 1;
5208}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005209EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211
5212/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005213 * process kcontrol info callback of a simple string enum array
5214 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5215 */
5216int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5217 struct snd_ctl_elem_info *uinfo,
5218 int num_items, const char * const *texts)
5219{
5220 static const char * const texts_default[] = {
5221 "Disabled", "Enabled"
5222 };
5223
5224 if (!texts || !num_items) {
5225 num_items = 2;
5226 texts = texts_default;
5227 }
5228
5229 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5230 uinfo->count = 1;
5231 uinfo->value.enumerated.items = num_items;
5232 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5233 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5234 strcpy(uinfo->value.enumerated.name,
5235 texts[uinfo->value.enumerated.item]);
5236 return 0;
5237}
5238EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5239
5240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 * Multi-channel / digital-out PCM helper functions
5242 */
5243
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005244/* setup SPDIF output stream */
5245static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5246 unsigned int stream_tag, unsigned int format)
5247{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005248 struct hda_spdif_out *spdif;
5249 unsigned int curr_fmt;
5250 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005251
Laurence Darby3bef1c32012-11-03 17:00:06 +00005252 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5253 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5254 AC_VERB_GET_STREAM_FORMAT, 0);
5255 reset = codec->spdif_status_reset &&
5256 (spdif->ctls & AC_DIG1_ENABLE) &&
5257 curr_fmt != format;
5258
5259 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5260 updated */
5261 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005262 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005263 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005264 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005265 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005266 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005267 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005268 for (d = codec->slave_dig_outs; *d; d++)
5269 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5270 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005271 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005272 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005273 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005274 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005275 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005276}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005277
Takashi Iwai2f728532008-09-25 16:32:41 +02005278static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5279{
5280 snd_hda_codec_cleanup_stream(codec, nid);
5281 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005282 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005283 for (d = codec->slave_dig_outs; *d; d++)
5284 snd_hda_codec_cleanup_stream(codec, *d);
5285 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005286}
5287
Takashi Iwaid5191e52009-11-16 14:58:17 +01005288/**
5289 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5290 * @bus: HD-audio bus
5291 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005292void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5293{
5294 struct hda_codec *codec;
5295
5296 if (!bus)
5297 return;
5298 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005299 if (hda_codec_is_power_on(codec) &&
5300 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005301 codec->patch_ops.reboot_notify(codec);
5302 }
5303}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005304EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005305
Takashi Iwaid5191e52009-11-16 14:58:17 +01005306/**
5307 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005309int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5310 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311{
Ingo Molnar62932df2006-01-16 16:34:20 +01005312 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005313 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5314 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005315 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005317 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return 0;
5319}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005320EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Takashi Iwaid5191e52009-11-16 14:58:17 +01005322/**
5323 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5324 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005325int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5326 struct hda_multi_out *mout,
5327 unsigned int stream_tag,
5328 unsigned int format,
5329 struct snd_pcm_substream *substream)
5330{
5331 mutex_lock(&codec->spdif_mutex);
5332 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5333 mutex_unlock(&codec->spdif_mutex);
5334 return 0;
5335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005336EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005337
Takashi Iwaid5191e52009-11-16 14:58:17 +01005338/**
5339 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5340 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005341int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5342 struct hda_multi_out *mout)
5343{
5344 mutex_lock(&codec->spdif_mutex);
5345 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5346 mutex_unlock(&codec->spdif_mutex);
5347 return 0;
5348}
5349EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5350
Takashi Iwaid5191e52009-11-16 14:58:17 +01005351/**
5352 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005354int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5355 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Ingo Molnar62932df2006-01-16 16:34:20 +01005357 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005359 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return 0;
5361}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005362EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Takashi Iwaid5191e52009-11-16 14:58:17 +01005364/**
5365 * snd_hda_multi_out_analog_open - open analog outputs
5366 *
5367 * Open analog outputs and set up the hw-constraints.
5368 * If the digital outputs can be opened as slave, open the digital
5369 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005371int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5372 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005373 struct snd_pcm_substream *substream,
5374 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
Takashi Iwai9a081602008-02-12 18:37:26 +01005376 struct snd_pcm_runtime *runtime = substream->runtime;
5377 runtime->hw.channels_max = mout->max_channels;
5378 if (mout->dig_out_nid) {
5379 if (!mout->analog_rates) {
5380 mout->analog_rates = hinfo->rates;
5381 mout->analog_formats = hinfo->formats;
5382 mout->analog_maxbps = hinfo->maxbps;
5383 } else {
5384 runtime->hw.rates = mout->analog_rates;
5385 runtime->hw.formats = mout->analog_formats;
5386 hinfo->maxbps = mout->analog_maxbps;
5387 }
5388 if (!mout->spdif_rates) {
5389 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5390 &mout->spdif_rates,
5391 &mout->spdif_formats,
5392 &mout->spdif_maxbps);
5393 }
5394 mutex_lock(&codec->spdif_mutex);
5395 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005396 if ((runtime->hw.rates & mout->spdif_rates) &&
5397 (runtime->hw.formats & mout->spdif_formats)) {
5398 runtime->hw.rates &= mout->spdif_rates;
5399 runtime->hw.formats &= mout->spdif_formats;
5400 if (mout->spdif_maxbps < hinfo->maxbps)
5401 hinfo->maxbps = mout->spdif_maxbps;
5402 } else {
5403 mout->share_spdif = 0;
5404 /* FIXME: need notify? */
5405 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005406 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005407 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5410 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5411}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005412EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Takashi Iwaid5191e52009-11-16 14:58:17 +01005414/**
5415 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5416 *
5417 * Set up the i/o for analog out.
5418 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005420int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5421 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 unsigned int stream_tag,
5423 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005424 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
Takashi Iwaidda14412011-05-02 11:29:30 +02005426 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005428 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 int i;
5430
Ingo Molnar62932df2006-01-16 16:34:20 +01005431 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005432 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005433 if (mout->dig_out_nid && mout->share_spdif &&
5434 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005436 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5437 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005438 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005440 setup_dig_out_stream(codec, mout->dig_out_nid,
5441 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 } else {
5443 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005444 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005447 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005450 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5451 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005452 if (!mout->no_share_stream &&
5453 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005455 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5456 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005457 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005458 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5459 if (!mout->no_share_stream && mout->hp_out_nid[i])
5460 snd_hda_codec_setup_stream(codec,
5461 mout->hp_out_nid[i],
5462 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 /* surrounds */
5465 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005466 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005467 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5468 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005469 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005470 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5471 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005473
5474 /* extra surrounds */
5475 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5476 int ch = 0;
5477 if (!mout->extra_out_nid[i])
5478 break;
5479 if (chs >= (i + 1) * 2)
5480 ch = i * 2;
5481 else if (!mout->no_share_stream)
5482 break;
5483 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5484 stream_tag, ch, format);
5485 }
5486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 return 0;
5488}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005489EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Takashi Iwaid5191e52009-11-16 14:58:17 +01005491/**
5492 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005494int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5495 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
Takashi Iwaidda14412011-05-02 11:29:30 +02005497 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 int i;
5499
5500 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005501 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005503 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005504 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5505 if (mout->hp_out_nid[i])
5506 snd_hda_codec_cleanup_stream(codec,
5507 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005508 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5509 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005510 snd_hda_codec_cleanup_stream(codec,
5511 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005512 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005514 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 mout->dig_out_used = 0;
5516 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005517 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 return 0;
5519}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005520EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Takashi Iwai47408602012-04-20 13:06:53 +02005522/**
5523 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5524 *
5525 * Guess the suitable VREF pin bits to be set as the pin-control value.
5526 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5527 */
5528unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5529{
5530 unsigned int pincap;
5531 unsigned int oldval;
5532 oldval = snd_hda_codec_read(codec, pin, 0,
5533 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5534 pincap = snd_hda_query_pin_caps(codec, pin);
5535 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5536 /* Exception: if the default pin setup is vref50, we give it priority */
5537 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5538 return AC_PINCTL_VREF_80;
5539 else if (pincap & AC_PINCAP_VREF_50)
5540 return AC_PINCTL_VREF_50;
5541 else if (pincap & AC_PINCAP_VREF_100)
5542 return AC_PINCTL_VREF_100;
5543 else if (pincap & AC_PINCAP_VREF_GRD)
5544 return AC_PINCTL_VREF_GRD;
5545 return AC_PINCTL_VREF_HIZ;
5546}
5547EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5548
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005549/* correct the pin ctl value for matching with the pin cap */
5550unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5551 hda_nid_t pin, unsigned int val)
5552{
5553 static unsigned int cap_lists[][2] = {
5554 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5555 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5556 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5557 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5558 };
5559 unsigned int cap;
5560
5561 if (!val)
5562 return 0;
5563 cap = snd_hda_query_pin_caps(codec, pin);
5564 if (!cap)
5565 return val; /* don't know what to do... */
5566
5567 if (val & AC_PINCTL_OUT_EN) {
5568 if (!(cap & AC_PINCAP_OUT))
5569 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5570 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5571 val &= ~AC_PINCTL_HP_EN;
5572 }
5573
5574 if (val & AC_PINCTL_IN_EN) {
5575 if (!(cap & AC_PINCAP_IN))
5576 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5577 else {
5578 unsigned int vcap, vref;
5579 int i;
5580 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5581 vref = val & AC_PINCTL_VREFEN;
5582 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5583 if (vref == cap_lists[i][0] &&
5584 !(vcap & cap_lists[i][1])) {
5585 if (i == ARRAY_SIZE(cap_lists) - 1)
5586 vref = AC_PINCTL_VREF_HIZ;
5587 else
5588 vref = cap_lists[i + 1][0];
5589 }
5590 }
5591 val &= ~AC_PINCTL_VREFEN;
5592 val |= vref;
5593 }
5594 }
5595
5596 return val;
5597}
5598EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5599
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005600int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5601 unsigned int val, bool cached)
5602{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005603 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005604 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005605 if (cached)
5606 return snd_hda_codec_update_cache(codec, pin, 0,
5607 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5608 else
5609 return snd_hda_codec_write(codec, pin, 0,
5610 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5611}
5612EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5613
Takashi Iwai990061c2010-09-09 22:08:44 +02005614/**
5615 * snd_hda_add_imux_item - Add an item to input_mux
5616 *
5617 * When the same label is used already in the existing items, the number
5618 * suffix is appended to the label. This label index number is stored
5619 * to type_idx when non-NULL pointer is given.
5620 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005621int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5622 int index, int *type_idx)
5623{
5624 int i, label_idx = 0;
5625 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5626 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5627 return -EINVAL;
5628 }
5629 for (i = 0; i < imux->num_items; i++) {
5630 if (!strncmp(label, imux->items[i].label, strlen(label)))
5631 label_idx++;
5632 }
5633 if (type_idx)
5634 *type_idx = label_idx;
5635 if (label_idx > 0)
5636 snprintf(imux->items[imux->num_items].label,
5637 sizeof(imux->items[imux->num_items].label),
5638 "%s %d", label, label_idx);
5639 else
5640 strlcpy(imux->items[imux->num_items].label, label,
5641 sizeof(imux->items[imux->num_items].label));
5642 imux->items[imux->num_items].index = index;
5643 imux->num_items++;
5644 return 0;
5645}
5646EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005647
Takashi Iwai4a471b72005-12-07 13:56:29 +01005648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649#ifdef CONFIG_PM
5650/*
5651 * power management
5652 */
5653
5654/**
5655 * snd_hda_suspend - suspend the codecs
5656 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 *
5658 * Returns 0 if successful.
5659 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005660int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005662 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
Takashi Iwai0ba21762007-04-16 11:29:14 +02005664 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005665 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005666 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005667 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669 return 0;
5670}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005671EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673/**
5674 * snd_hda_resume - resume the codecs
5675 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 *
5677 * Returns 0 if successful.
5678 */
5679int snd_hda_resume(struct hda_bus *bus)
5680{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005681 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Takashi Iwai0ba21762007-04-16 11:29:14 +02005683 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005684 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 return 0;
5687}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005688EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005689#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005690
5691/*
5692 * generic arrays
5693 */
5694
Takashi Iwaid5191e52009-11-16 14:58:17 +01005695/**
5696 * snd_array_new - get a new element from the given array
5697 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005698 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005699 * Get a new element from the given array. If it exceeds the
5700 * pre-allocated array size, re-allocate the array.
5701 *
5702 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005703 */
5704void *snd_array_new(struct snd_array *array)
5705{
Takashi Iwai12f17712012-10-10 08:59:14 +02005706 if (snd_BUG_ON(!array->elem_size))
5707 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005708 if (array->used >= array->alloced) {
5709 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005710 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005711 void *nlist;
5712 if (snd_BUG_ON(num >= 4096))
5713 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005714 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005715 if (!nlist)
5716 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005717 array->list = nlist;
5718 array->alloced = num;
5719 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005720 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005721}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005722EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005723
Takashi Iwaid5191e52009-11-16 14:58:17 +01005724/**
5725 * snd_array_free - free the given array elements
5726 * @array: the array object
5727 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005728void snd_array_free(struct snd_array *array)
5729{
5730 kfree(array->list);
5731 array->used = 0;
5732 array->alloced = 0;
5733 array->list = NULL;
5734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005735EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005736
Takashi Iwaid5191e52009-11-16 14:58:17 +01005737/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005738 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5739 * @pcm: PCM caps bits
5740 * @buf: the string buffer to write
5741 * @buflen: the max buffer length
5742 *
5743 * used by hda_proc.c and hda_eld.c
5744 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005745void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5746{
5747 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5748 int i, j;
5749
5750 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5751 if (pcm & (AC_SUPPCM_BITS_8 << i))
5752 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5753
5754 buf[j] = '\0'; /* necessary when j == 0 */
5755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005756EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005757
5758MODULE_DESCRIPTION("HDA codec core");
5759MODULE_LICENSE("GPL");