blob: 472e5d04701e9aec70f7f8b33d05fb12c5c3d3a0 [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)
Mengdong Lin12edb892013-11-26 23:32:23 -050099static void hda_suspend_work(struct work_struct *work);
100static void hda_resume_work(struct work_struct *work);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101static void hda_power_work(struct work_struct *work);
102static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200103#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100104
105static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700106{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100107 struct hda_bus *bus = codec->bus;
108
109 if ((power_up && codec->pm_up_notified) ||
110 (!power_up && !codec->pm_up_notified))
111 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700112 if (bus->ops.pm_notify)
113 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100114 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700115}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100116
Takashi Iwaicb53c622007-08-10 17:21:45 +0200117#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100118#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200119static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200120#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100121#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200122#endif
123
Takashi Iwaid5191e52009-11-16 14:58:17 +0100124/**
125 * snd_hda_get_jack_location - Give a location string of the jack
126 * @cfg: pin default config value
127 *
128 * Parse the pin default config value and returns the string of the
129 * jack location, e.g. "Rear", "Front", etc.
130 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131const char *snd_hda_get_jack_location(u32 cfg)
132{
133 static char *bases[7] = {
134 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
135 };
136 static unsigned char specials_idx[] = {
137 0x07, 0x08,
138 0x17, 0x18, 0x19,
139 0x37, 0x38
140 };
141 static char *specials[] = {
142 "Rear Panel", "Drive Bar",
143 "Riser", "HDMI", "ATAPI",
144 "Mobile-In", "Mobile-Out"
145 };
146 int i;
147 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
148 if ((cfg & 0x0f) < 7)
149 return bases[cfg & 0x0f];
150 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
151 if (cfg == specials_idx[i])
152 return specials[i];
153 }
154 return "UNKNOWN";
155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100156EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400157
Takashi Iwaid5191e52009-11-16 14:58:17 +0100158/**
159 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
160 * @cfg: pin default config value
161 *
162 * Parse the pin default config value and returns the string of the
163 * jack connectivity, i.e. external or internal connection.
164 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400165const char *snd_hda_get_jack_connectivity(u32 cfg)
166{
167 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
168
169 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100171EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172
Takashi Iwaid5191e52009-11-16 14:58:17 +0100173/**
174 * snd_hda_get_jack_type - Give a type string of the jack
175 * @cfg: pin default config value
176 *
177 * Parse the pin default config value and returns the string of the
178 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
179 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400180const char *snd_hda_get_jack_type(u32 cfg)
181{
182 static char *jack_types[16] = {
183 "Line Out", "Speaker", "HP Out", "CD",
184 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
185 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200186 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400187 };
188
189 return jack_types[(cfg & AC_DEFCFG_DEVICE)
190 >> AC_DEFCFG_DEVICE_SHIFT];
191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100192EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400193
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100194/*
195 * Compose a 32bit command word to be sent to the HD-audio controller
196 */
197static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200198make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100199 unsigned int verb, unsigned int parm)
200{
201 u32 val;
202
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200203 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200204 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200205 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
206 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800207 return ~0;
208 }
209
210 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100211 val |= (u32)nid << 20;
212 val |= verb << 8;
213 val |= parm;
214 return val;
215}
216
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200217/*
218 * Send and receive a verb
219 */
220static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200221 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200222{
223 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200224 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225
Wu Fengguang6430aee2009-07-17 16:49:19 +0800226 if (cmd == ~0)
227 return -1;
228
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229 if (res)
230 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200232 snd_hda_power_up(codec);
233 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200234 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
235 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100236 for (;;) {
237 trace_hda_send_cmd(codec, cmd);
238 err = bus->ops.command(bus, cmd);
239 if (err != -EAGAIN)
240 break;
241 /* process pending verbs */
242 bus->ops.get_response(bus, codec->addr);
243 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200244 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800245 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200246 trace_hda_get_response(codec, *res);
247 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200248 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200249 mutex_unlock(&bus->cmd_mutex);
250 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100251 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200252 if (bus->response_reset) {
253 snd_printd("hda_codec: resetting BUS due to "
254 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200255 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200256 bus->ops.bus_reset(bus);
257 }
258 goto again;
259 }
260 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100261 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200262 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200263 return err;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/**
267 * snd_hda_codec_read - send a command and get the response
268 * @codec: the HDA codec
269 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200270 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * @verb: the verb to send
272 * @parm: the parameter for the verb
273 *
274 * Send a single command and read the corresponding response.
275 *
276 * Returns the obtained response value, or -1 for an error.
277 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200278unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200279 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int verb, unsigned int parm)
281{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200282 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200283 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200284 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700285 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return res;
287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100288EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290/**
291 * snd_hda_codec_write - send a single command without waiting for response
292 * @codec: the HDA codec
293 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200294 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * @verb: the verb to send
296 * @parm: the parameter for the verb
297 *
298 * Send a single command without waiting for response.
299 *
300 * Returns 0 if successful, or a negative error code.
301 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200302int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
303 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200305 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100306 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200307 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200308 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100310EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/**
313 * snd_hda_sequence_write - sequence writes
314 * @codec: the HDA codec
315 * @seq: VERB array to send
316 *
317 * Send the commands sequentially from the given array.
318 * The array must be terminated with NID=0.
319 */
320void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
321{
322 for (; seq->nid; seq++)
323 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
324}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100325EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327/**
328 * snd_hda_get_sub_nodes - get the range of sub nodes
329 * @codec: the HDA codec
330 * @nid: NID to parse
331 * @start_id: the pointer to store the start NID
332 *
333 * Parse the NID and store the start NID of its sub-nodes.
334 * Returns the number of sub-nodes.
335 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200336int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
337 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 unsigned int parm;
340
341 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200342 if (parm == -1)
343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *start_id = (parm >> 16) & 0x7fff;
345 return (int)(parm & 0x7fff);
346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100347EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349/* connection list element */
350struct hda_conn_list {
351 struct list_head list;
352 int len;
353 hda_nid_t nid;
354 hda_nid_t conns[0];
355};
356
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200357/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358static struct hda_conn_list *
359lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200360{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100361 struct hda_conn_list *p;
362 list_for_each_entry(p, &codec->conn_list, list) {
363 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200364 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 }
366 return NULL;
367}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200368
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100369static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
370 const hda_nid_t *list)
371{
372 struct hda_conn_list *p;
373
374 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
375 if (!p)
376 return -ENOMEM;
377 p->len = len;
378 p->nid = nid;
379 memcpy(p->conns, list, len * sizeof(hda_nid_t));
380 list_add(&p->list, &codec->conn_list);
381 return 0;
382}
383
384static void remove_conn_list(struct hda_codec *codec)
385{
386 while (!list_empty(&codec->conn_list)) {
387 struct hda_conn_list *p;
388 p = list_first_entry(&codec->conn_list, typeof(*p), list);
389 list_del(&p->list);
390 kfree(p);
391 }
392}
393
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394/* read the connection and add to the cache */
395static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200396{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100397 hda_nid_t list[32];
398 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200399 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200400
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200401 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100402 if (len == -ENOSPC) {
403 len = snd_hda_get_num_raw_conns(codec, nid);
404 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
405 if (!result)
406 return -ENOMEM;
407 len = snd_hda_get_raw_connections(codec, nid, result, len);
408 }
409 if (len >= 0)
410 len = snd_hda_override_conn_list(codec, nid, len, result);
411 if (result != list)
412 kfree(result);
413 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200414}
Takashi Iwaidce20792011-06-24 14:10:28 +0200415
416/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100417 * snd_hda_get_conn_list - get connection list
418 * @codec: the HDA codec
419 * @nid: NID to parse
420 * @len: number of connection list entries
421 * @listp: the pointer to store NID list
422 *
423 * Parses the connection list of the given widget and stores the pointer
424 * to the list of NIDs.
425 *
426 * Returns the number of connections, or a negative error code.
427 *
428 * Note that the returned pointer isn't protected against the list
429 * modification. If snd_hda_override_conn_list() might be called
430 * concurrently, protect with a mutex appropriately.
431 */
432int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
433 const hda_nid_t **listp)
434{
435 bool added = false;
436
437 for (;;) {
438 int err;
439 const struct hda_conn_list *p;
440
441 /* if the connection-list is already cached, read it */
442 p = lookup_conn_list(codec, nid);
443 if (p) {
444 if (listp)
445 *listp = p->conns;
446 return p->len;
447 }
448 if (snd_BUG_ON(added))
449 return -EINVAL;
450
451 err = read_and_add_raw_conns(codec, nid);
452 if (err < 0)
453 return err;
454 added = true;
455 }
456}
457EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
458
459/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200460 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * @codec: the HDA codec
462 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200463 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * @max_conns: max. number of connections to store
465 *
466 * Parses the connection list of the given widget and stores the list
467 * of NIDs.
468 *
469 * Returns the number of connections, or a negative error code.
470 */
471int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200472 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200473{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100474 const hda_nid_t *list;
475 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100477 if (len > 0 && conn_list) {
478 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200479 snd_printk(KERN_ERR "hda_codec: "
480 "Too many connections %d for NID 0x%x\n",
481 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200482 return -EINVAL;
483 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100484 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200485 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200486
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100487 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200488}
489EXPORT_SYMBOL_HDA(snd_hda_get_connections);
490
Takashi Iwai4eea3092013-02-07 18:18:19 +0100491/* return CONNLIST_LEN parameter of the given widget */
492static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
493{
494 unsigned int wcaps = get_wcaps(codec, nid);
495 unsigned int parm;
496
497 if (!(wcaps & AC_WCAP_CONN_LIST) &&
498 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
499 return 0;
500
501 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
502 if (parm == -1)
503 parm = 0;
504 return parm;
505}
506
507int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
508{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100509 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100510}
511
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200512/**
513 * snd_hda_get_raw_connections - copy connection list without cache
514 * @codec: the HDA codec
515 * @nid: NID to parse
516 * @conn_list: connection list array
517 * @max_conns: max. number of connections to store
518 *
519 * Like snd_hda_get_connections(), copy the connection list but without
520 * checking through the connection-list cache.
521 * Currently called only from hda_proc.c, so not exported.
522 */
523int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
524 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100527 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100529 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100530 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Takashi Iwai4eea3092013-02-07 18:18:19 +0100532 parm = get_num_conns(codec, nid);
533 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200534 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (parm & AC_CLIST_LONG) {
537 /* long form */
538 shift = 16;
539 num_elems = 2;
540 } else {
541 /* short form */
542 shift = 8;
543 num_elems = 4;
544 }
545 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 mask = (1 << (shift-1)) - 1;
547
Takashi Iwai0ba21762007-04-16 11:29:14 +0200548 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0; /* no connection */
550
551 if (conn_len == 1) {
552 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200553 parm = snd_hda_codec_read(codec, nid, 0,
554 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200555 if (parm == -1 && codec->bus->rirb_error)
556 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100557 if (conn_list)
558 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
560 }
561
562 /* multi connection */
563 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100564 prev_nid = 0;
565 for (i = 0; i < conn_len; i++) {
566 int range_val;
567 hda_nid_t val, n;
568
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200569 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100570 parm = snd_hda_codec_read(codec, nid, 0,
571 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200572 if (parm == -1 && codec->bus->rirb_error)
573 return -EIO;
574 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200575 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100576 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100577 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4758fed2013-10-17 17:56:25 +0200578 snd_printdd("hda_codec: "
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200579 "invalid CONNECT_LIST verb %x[%i]:%x\n",
580 nid, i, parm);
581 return 0;
582 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100583 parm >>= shift;
584 if (range_val) {
585 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200586 if (!prev_nid || prev_nid >= val) {
587 snd_printk(KERN_WARNING "hda_codec: "
588 "invalid dep_range_val %x:%x\n",
589 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100592 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100593 if (conn_list) {
594 if (conns >= max_conns)
595 return -ENOSPC;
596 conn_list[conns] = n;
597 }
598 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100599 }
600 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100601 if (conn_list) {
602 if (conns >= max_conns)
603 return -ENOSPC;
604 conn_list[conns] = val;
605 }
606 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100608 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 return conns;
611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200614 * snd_hda_override_conn_list - add/modify the connection-list to cache
615 * @codec: the HDA codec
616 * @nid: NID to parse
617 * @len: number of connection list entries
618 * @list: the list of connection entries
619 *
620 * Add or modify the given connection-list to the cache. If the corresponding
621 * cache already exists, invalidate it and append a new one.
622 *
623 * Returns zero or a negative error code.
624 */
625int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
626 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100628 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200629
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100630 p = lookup_conn_list(codec, nid);
631 if (p) {
632 list_del(&p->list);
633 kfree(p);
634 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200635
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100636 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200638EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
639
640/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200641 * snd_hda_get_conn_index - get the connection index of the given NID
642 * @codec: the HDA codec
643 * @mux: NID containing the list
644 * @nid: NID to select
645 * @recursive: 1 when searching NID recursively, otherwise 0
646 *
647 * Parses the connection list of the widget @mux and checks whether the
648 * widget @nid is present. If it is, return the connection index.
649 * Otherwise it returns -1.
650 */
651int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
652 hda_nid_t nid, int recursive)
653{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100654 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200655 int i, nums;
656
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100657 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200658 for (i = 0; i < nums; i++)
659 if (conn[i] == nid)
660 return i;
661 if (!recursive)
662 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100663 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200664 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
665 return -1;
666 }
667 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200668 for (i = 0; i < nums; i++) {
669 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
670 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
671 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200672 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
673 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200674 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200675 return -1;
676}
677EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Mengdong Linf1aa0682013-08-26 21:35:21 -0400679
680/* return DEVLIST_LEN parameter of the given widget */
681static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
682{
683 unsigned int wcaps = get_wcaps(codec, nid);
684 unsigned int parm;
685
686 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
687 get_wcaps_type(wcaps) != AC_WID_PIN)
688 return 0;
689
690 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
691 if (parm == -1 && codec->bus->rirb_error)
692 parm = 0;
693 return parm & AC_DEV_LIST_LEN_MASK;
694}
695
696/**
697 * snd_hda_get_devices - copy device list without cache
698 * @codec: the HDA codec
699 * @nid: NID of the pin to parse
700 * @dev_list: device list array
701 * @max_devices: max. number of devices to store
702 *
703 * Copy the device list. This info is dynamic and so not cached.
704 * Currently called only from hda_proc.c, so not exported.
705 */
706int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
707 u8 *dev_list, int max_devices)
708{
709 unsigned int parm;
710 int i, dev_len, devices;
711
712 parm = get_num_devices(codec, nid);
713 if (!parm) /* not multi-stream capable */
714 return 0;
715
716 dev_len = parm + 1;
717 dev_len = dev_len < max_devices ? dev_len : max_devices;
718
719 devices = 0;
720 while (devices < dev_len) {
721 parm = snd_hda_codec_read(codec, nid, 0,
722 AC_VERB_GET_DEVICE_LIST, devices);
723 if (parm == -1 && codec->bus->rirb_error)
724 break;
725
726 for (i = 0; i < 8; i++) {
727 dev_list[devices] = (u8)parm;
728 parm >>= 4;
729 devices++;
730 if (devices >= dev_len)
731 break;
732 }
733 }
734 return devices;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/**
738 * snd_hda_queue_unsol_event - add an unsolicited event to queue
739 * @bus: the BUS
740 * @res: unsolicited event (lower 32bit of RIRB entry)
741 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
742 *
743 * Adds the given event to the queue. The events are processed in
744 * the workqueue asynchronously. Call this function in the interrupt
745 * hanlder when RIRB receives an unsolicited event.
746 *
747 * Returns 0 if successful, or a negative error code.
748 */
749int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
750{
751 struct hda_bus_unsolicited *unsol;
752 unsigned int wp;
753
Wang YanQing2195b062013-05-07 11:27:33 +0800754 if (!bus || !bus->workq)
755 return 0;
756
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200757 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 unsol = bus->unsol;
759 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 0;
761
762 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
763 unsol->wp = wp;
764
765 wp <<= 1;
766 unsol->queue[wp] = res;
767 unsol->queue[wp + 1] = res_ex;
768
Takashi Iwai6acaed32009-01-12 10:09:24 +0100769 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 return 0;
772}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100773EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800776 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
David Howellsc4028952006-11-22 14:57:56 +0000778static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
David Howellsc4028952006-11-22 14:57:56 +0000780 struct hda_bus_unsolicited *unsol =
781 container_of(work, struct hda_bus_unsolicited, work);
782 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct hda_codec *codec;
784 unsigned int rp, caddr, res;
785
786 while (unsol->rp != unsol->wp) {
787 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
788 unsol->rp = rp;
789 rp <<= 1;
790 res = unsol->queue[rp];
791 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200792 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 continue;
794 codec = bus->caddr_tbl[caddr & 0x0f];
795 if (codec && codec->patch_ops.unsol_event)
796 codec->patch_ops.unsol_event(codec, res);
797 }
798}
799
800/*
801 * initialize unsolicited queue
802 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200803static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct hda_bus_unsolicited *unsol;
806
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100807 if (bus->unsol) /* already initialized */
808 return 0;
809
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200810 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200811 if (!unsol) {
812 snd_printk(KERN_ERR "hda_codec: "
813 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -ENOMEM;
815 }
David Howellsc4028952006-11-22 14:57:56 +0000816 INIT_WORK(&unsol->work, process_unsol_events);
817 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 bus->unsol = unsol;
819 return 0;
820}
821
822/*
823 * destructor
824 */
825static void snd_hda_codec_free(struct hda_codec *codec);
826
827static int snd_hda_bus_free(struct hda_bus *bus)
828{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200829 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Takashi Iwai0ba21762007-04-16 11:29:14 +0200831 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100833 if (bus->workq)
834 flush_workqueue(bus->workq);
835 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 snd_hda_codec_free(codec);
839 }
840 if (bus->ops.private_free)
841 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100842 if (bus->workq)
843 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500844
845#ifdef CONFIG_PM
846 if (bus->pm_wq)
847 destroy_workqueue(bus->pm_wq);
848#endif
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 kfree(bus);
851 return 0;
852}
853
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100854static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100857 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return snd_hda_bus_free(bus);
859}
860
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200861#ifdef CONFIG_SND_HDA_HWDEP
862static int snd_hda_bus_dev_register(struct snd_device *device)
863{
864 struct hda_bus *bus = device->device_data;
865 struct hda_codec *codec;
866 list_for_each_entry(codec, &bus->codec_list, list) {
867 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100868 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200869 }
870 return 0;
871}
872#else
873#define snd_hda_bus_dev_register NULL
874#endif
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/**
877 * snd_hda_bus_new - create a HDA bus
878 * @card: the card entry
879 * @temp: the template for hda_bus information
880 * @busp: the pointer to store the created bus instance
881 *
882 * Returns 0 if successful, or a negative error code.
883 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100884int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200885 const struct hda_bus_template *temp,
886 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct hda_bus *bus;
889 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100890 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200891 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .dev_free = snd_hda_bus_dev_free,
893 };
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500894#ifdef CONFIG_PM
895 char wqname[16];
896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Takashi Iwaida3cec32008-08-08 17:12:14 +0200898 if (snd_BUG_ON(!temp))
899 return -EINVAL;
900 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
901 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (busp)
904 *busp = NULL;
905
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200906 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (bus == NULL) {
908 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
909 return -ENOMEM;
910 }
911
912 bus->card = card;
913 bus->private_data = temp->private_data;
914 bus->pci = temp->pci;
915 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100916 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 bus->ops = temp->ops;
918
Ingo Molnar62932df2006-01-16 16:34:20 +0100919 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200920 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 INIT_LIST_HEAD(&bus->codec_list);
922
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100923 snprintf(bus->workq_name, sizeof(bus->workq_name),
924 "hd-audio%d", card->number);
925 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100926 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100927 snd_printk(KERN_ERR "cannot create workqueue %s\n",
928 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100929 kfree(bus);
930 return -ENOMEM;
931 }
932
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500933#ifdef CONFIG_PM
934 sprintf(wqname, "hda-pm-wq-%d", card->number);
935 bus->pm_wq = create_workqueue(wqname);
936 if (!bus->pm_wq) {
937 snd_printk(KERN_ERR "cannot create PM workqueue\n");
938 snd_hda_bus_free(bus);
939 return -ENOMEM;
940 }
941#endif
942
Takashi Iwai0ba21762007-04-16 11:29:14 +0200943 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
944 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 snd_hda_bus_free(bus);
946 return err;
947 }
948 if (busp)
949 *busp = bus;
950 return 0;
951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100952EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Takashi Iwai82467612007-07-27 19:15:54 +0200954#ifdef CONFIG_SND_HDA_GENERIC
955#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200956 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200957#else
958#define is_generic_config(codec) 0
959#endif
960
Takashi Iwai645f10c2008-11-28 15:07:37 +0100961#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100962#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
963#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100964#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100965#endif
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
968 * find a matching codec preset
969 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200970static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200971find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100973 struct hda_codec_preset_list *tbl;
974 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200975 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100977 again:
978 mutex_lock(&preset_mutex);
979 list_for_each_entry(tbl, &hda_preset_tables, list) {
980 if (!try_module_get(tbl->owner)) {
981 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
982 continue;
983 }
984 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100986 if (preset->afg && preset->afg != codec->afg)
987 continue;
988 if (preset->mfg && preset->mfg != codec->mfg)
989 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200990 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200992 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200993 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100994 preset->rev == codec->revision_id)) {
995 mutex_unlock(&preset_mutex);
996 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001000 module_put(tbl->owner);
1001 }
1002 mutex_unlock(&preset_mutex);
1003
1004 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
1005 char name[32];
1006 if (!mod_requested)
1007 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
1008 codec->vendor_id);
1009 else
1010 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
1011 (codec->vendor_id >> 16) & 0xffff);
1012 request_module(name);
1013 mod_requested++;
1014 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 return NULL;
1017}
1018
1019/*
Takashi Iwaif44ac832008-07-30 15:01:45 +02001020 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Takashi Iwaif44ac832008-07-30 15:01:45 +02001022static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 const struct hda_vendor_id *c;
1025 const char *vendor = NULL;
1026 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001027 char tmp[16];
1028
1029 if (codec->vendor_name)
1030 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 for (c = hda_vendor_ids; c->id; c++) {
1033 if (c->id == vendor_id) {
1034 vendor = c->name;
1035 break;
1036 }
1037 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001038 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 sprintf(tmp, "Generic %04x", vendor_id);
1040 vendor = tmp;
1041 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001042 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1043 if (!codec->vendor_name)
1044 return -ENOMEM;
1045
1046 get_chip_name:
1047 if (codec->chip_name)
1048 return 0;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001051 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1052 else {
1053 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1054 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1055 }
1056 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001057 return -ENOMEM;
1058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001062 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001064static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001066 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 hda_nid_t nid;
1068
1069 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1070 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001071 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001072 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001073 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001074 case AC_GRP_AUDIO_FUNCTION:
1075 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001076 codec->afg_function_id = function_id & 0xff;
1077 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001078 break;
1079 case AC_GRP_MODEM_FUNCTION:
1080 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001081 codec->mfg_function_id = function_id & 0xff;
1082 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001083 break;
1084 default:
1085 break;
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001091 * read widget caps for each widget and store in cache
1092 */
1093static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1094{
1095 int i;
1096 hda_nid_t nid;
1097
1098 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1099 &codec->start_nid);
1100 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001101 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001102 return -ENOMEM;
1103 nid = codec->start_nid;
1104 for (i = 0; i < codec->num_nodes; i++, nid++)
1105 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1106 AC_PAR_AUDIO_WIDGET_CAP);
1107 return 0;
1108}
1109
Takashi Iwai3be14142009-02-20 14:11:16 +01001110/* read all pin default configurations and save codec->init_pins */
1111static int read_pin_defaults(struct hda_codec *codec)
1112{
1113 int i;
1114 hda_nid_t nid = codec->start_nid;
1115
1116 for (i = 0; i < codec->num_nodes; i++, nid++) {
1117 struct hda_pincfg *pin;
1118 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001119 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001120 if (wid_type != AC_WID_PIN)
1121 continue;
1122 pin = snd_array_new(&codec->init_pins);
1123 if (!pin)
1124 return -ENOMEM;
1125 pin->nid = nid;
1126 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1127 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001128 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1129 AC_VERB_GET_PIN_WIDGET_CONTROL,
1130 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001131 }
1132 return 0;
1133}
1134
1135/* look up the given pin config list and return the item matching with NID */
1136static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1137 struct snd_array *array,
1138 hda_nid_t nid)
1139{
1140 int i;
1141 for (i = 0; i < array->used; i++) {
1142 struct hda_pincfg *pin = snd_array_elem(array, i);
1143 if (pin->nid == nid)
1144 return pin;
1145 }
1146 return NULL;
1147}
1148
Takashi Iwai3be14142009-02-20 14:11:16 +01001149/* set the current pin config value for the given NID.
1150 * the value is cached, and read via snd_hda_codec_get_pincfg()
1151 */
1152int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1153 hda_nid_t nid, unsigned int cfg)
1154{
1155 struct hda_pincfg *pin;
1156
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001157 /* the check below may be invalid when pins are added by a fixup
1158 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1159 * for now
1160 */
1161 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001162 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1163 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001164 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001165
Takashi Iwai3be14142009-02-20 14:11:16 +01001166 pin = look_up_pincfg(codec, list, nid);
1167 if (!pin) {
1168 pin = snd_array_new(list);
1169 if (!pin)
1170 return -ENOMEM;
1171 pin->nid = nid;
1172 }
1173 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001174 return 0;
1175}
1176
Takashi Iwaid5191e52009-11-16 14:58:17 +01001177/**
1178 * snd_hda_codec_set_pincfg - Override a pin default configuration
1179 * @codec: the HDA codec
1180 * @nid: NID to set the pin config
1181 * @cfg: the pin default config value
1182 *
1183 * Override a pin default configuration value in the cache.
1184 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1185 * priority than the real hardware value.
1186 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001187int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1188 hda_nid_t nid, unsigned int cfg)
1189{
Takashi Iwai346ff702009-02-23 09:42:57 +01001190 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001191}
1192EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1193
Takashi Iwaid5191e52009-11-16 14:58:17 +01001194/**
1195 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1196 * @codec: the HDA codec
1197 * @nid: NID to get the pin config
1198 *
1199 * Get the current pin config value of the given pin NID.
1200 * If the pincfg value is cached or overridden via sysfs or driver,
1201 * returns the cached value.
1202 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001203unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1204{
1205 struct hda_pincfg *pin;
1206
Takashi Iwai3be14142009-02-20 14:11:16 +01001207#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001208 {
1209 unsigned int cfg = 0;
1210 mutex_lock(&codec->user_mutex);
1211 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1212 if (pin)
1213 cfg = pin->cfg;
1214 mutex_unlock(&codec->user_mutex);
1215 if (cfg)
1216 return cfg;
1217 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001218#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001219 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1220 if (pin)
1221 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001222 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1223 if (pin)
1224 return pin->cfg;
1225 return 0;
1226}
1227EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1228
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001229/* remember the current pinctl target value */
1230int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1231 unsigned int val)
1232{
1233 struct hda_pincfg *pin;
1234
1235 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1236 if (!pin)
1237 return -EINVAL;
1238 pin->target = val;
1239 return 0;
1240}
1241EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1242
1243/* return the current pinctl target value */
1244int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1245{
1246 struct hda_pincfg *pin;
1247
1248 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1249 if (!pin)
1250 return 0;
1251 return pin->target;
1252}
1253EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1254
Takashi Iwai92ee6162009-12-27 11:18:59 +01001255/**
1256 * snd_hda_shutup_pins - Shut up all pins
1257 * @codec: the HDA codec
1258 *
1259 * Clear all pin controls to shup up before suspend for avoiding click noise.
1260 * The controls aren't cached so that they can be resumed properly.
1261 */
1262void snd_hda_shutup_pins(struct hda_codec *codec)
1263{
1264 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001265 /* don't shut up pins when unloading the driver; otherwise it breaks
1266 * the default pin setup at the next load of the driver
1267 */
1268 if (codec->bus->shutdown)
1269 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001270 for (i = 0; i < codec->init_pins.used; i++) {
1271 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1272 /* use read here for syncing after issuing each verb */
1273 snd_hda_codec_read(codec, pin->nid, 0,
1274 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1275 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001276 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001277}
1278EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1279
Takashi Iwai2a439522011-07-26 09:52:50 +02001280#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001281/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1282static void restore_shutup_pins(struct hda_codec *codec)
1283{
1284 int i;
1285 if (!codec->pins_shutup)
1286 return;
1287 if (codec->bus->shutdown)
1288 return;
1289 for (i = 0; i < codec->init_pins.used; i++) {
1290 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1291 snd_hda_codec_write(codec, pin->nid, 0,
1292 AC_VERB_SET_PIN_WIDGET_CONTROL,
1293 pin->ctrl);
1294 }
1295 codec->pins_shutup = 0;
1296}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001297#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001298
David Henningsson26a6cb62012-10-09 15:04:21 +02001299static void hda_jackpoll_work(struct work_struct *work)
1300{
1301 struct hda_codec *codec =
1302 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001303
1304 snd_hda_jack_set_dirty_all(codec);
1305 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001306
1307 if (!codec->jackpoll_interval)
1308 return;
1309
David Henningsson26a6cb62012-10-09 15:04:21 +02001310 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1311 codec->jackpoll_interval);
1312}
1313
Takashi Iwai01751f52007-08-10 16:59:39 +02001314static void init_hda_cache(struct hda_cache_rec *cache,
1315 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001316static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001317
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001318/* release all pincfg lists */
1319static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001320{
Takashi Iwai346ff702009-02-23 09:42:57 +01001321 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001322#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001323 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001324#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001325 snd_array_free(&codec->init_pins);
1326}
1327
Takashi Iwai54d17402005-11-21 16:33:22 +01001328/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001329 * audio-converter setup caches
1330 */
1331struct hda_cvt_setup {
1332 hda_nid_t nid;
1333 u8 stream_tag;
1334 u8 channel_id;
1335 u16 format_id;
1336 unsigned char active; /* cvt is currently used */
1337 unsigned char dirty; /* setups should be cleared */
1338};
1339
1340/* get or create a cache entry for the given audio converter NID */
1341static struct hda_cvt_setup *
1342get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1343{
1344 struct hda_cvt_setup *p;
1345 int i;
1346
1347 for (i = 0; i < codec->cvt_setups.used; i++) {
1348 p = snd_array_elem(&codec->cvt_setups, i);
1349 if (p->nid == nid)
1350 return p;
1351 }
1352 p = snd_array_new(&codec->cvt_setups);
1353 if (p)
1354 p->nid = nid;
1355 return p;
1356}
1357
1358/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001359 * Dynamic symbol binding for the codec parsers
1360 */
1361#ifdef MODULE
1362#define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym))
1363#define unload_parser_addr(addr) symbol_put_addr(addr)
1364#else
1365#define load_parser_sym(sym) (sym)
1366#define unload_parser_addr(addr) do {} while (0)
1367#endif
1368
1369#define load_parser(codec, sym) \
1370 ((codec)->parser = load_parser_sym(sym))
1371
1372static void unload_parser(struct hda_codec *codec)
1373{
1374 if (codec->parser) {
1375 unload_parser_addr(codec->parser);
1376 codec->parser = NULL;
1377 }
1378}
1379
1380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * codec destructor
1382 */
1383static void snd_hda_codec_free(struct hda_codec *codec)
1384{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001387 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001388 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001389 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001390#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001391 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001392 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001395 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001396 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001397 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001398 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001399 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 codec->bus->caddr_tbl[codec->addr] = NULL;
1401 if (codec->patch_ops.free)
1402 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001403 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001404 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001405 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001406 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001407 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001408 kfree(codec->vendor_name);
1409 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001410 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001411 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001412 codec->bus->num_codecs--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 kfree(codec);
1414}
1415
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001416static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1417 hda_nid_t fg, unsigned int power_state);
1418
Takashi Iwaid8193872012-08-31 07:54:38 -07001419static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001420 unsigned int power_state);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/**
1423 * snd_hda_codec_new - create a HDA codec
1424 * @bus: the bus to assign
1425 * @codec_addr: the codec address
1426 * @codecp: the pointer to store the generated codec
1427 *
1428 * Returns 0 if successful, or a negative error code.
1429 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001430int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001431 unsigned int codec_addr,
1432 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001435 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001436 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int err;
1438
Takashi Iwaida3cec32008-08-08 17:12:14 +02001439 if (snd_BUG_ON(!bus))
1440 return -EINVAL;
1441 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001445 snd_printk(KERN_ERR "hda_codec: "
1446 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EBUSY;
1448 }
1449
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001450 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (codec == NULL) {
1452 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1453 return -ENOMEM;
1454 }
1455
1456 codec->bus = bus;
1457 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001458 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001459 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001460 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001461 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001462 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001463 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1464 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001465 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001466 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001467 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001468 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001469 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001470 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001471 INIT_LIST_HEAD(&codec->conn_list);
1472
David Henningsson26a6cb62012-10-09 15:04:21 +02001473 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Takashi Iwai83012a72012-08-24 18:38:08 +02001475#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001476 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001477 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
Mengdong Lin12edb892013-11-26 23:32:23 -05001478 INIT_WORK(&codec->suspend_work, hda_suspend_work);
1479 INIT_WORK(&codec->resume_work, hda_resume_work);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001480 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1481 * the caller has to power down appropriatley after initialization
1482 * phase.
1483 */
1484 hda_keep_power_on(codec);
1485#endif
1486
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001487 if (codec->bus->modelname) {
1488 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1489 if (!codec->modelname) {
1490 snd_hda_codec_free(codec);
1491 return -ENODEV;
1492 }
1493 }
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001496 bus->num_codecs++;
1497#ifdef CONFIG_PM
1498 workqueue_set_max_active(bus->pm_wq, bus->num_codecs);
1499#endif
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 bus->caddr_tbl[codec_addr] = codec;
1502
Takashi Iwai0ba21762007-04-16 11:29:14 +02001503 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1504 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001505 if (codec->vendor_id == -1)
1506 /* read again, hopefully the access method was corrected
1507 * in the last read...
1508 */
1509 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1510 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001511 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1512 AC_PAR_SUBSYSTEM_ID);
1513 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001516 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001517 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001518 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001519 err = -ENODEV;
1520 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Takashi Iwaid8193872012-08-31 07:54:38 -07001523 fg = codec->afg ? codec->afg : codec->mfg;
1524 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001525 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001526 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001527 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001528 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001529 err = read_pin_defaults(codec);
1530 if (err < 0)
1531 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001532
Takashi Iwai0ba21762007-04-16 11:29:14 +02001533 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001534 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001535 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001537 }
1538
Takashi Iwai83012a72012-08-24 18:38:08 +02001539#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001540 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001541 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001542#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001543 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001544 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001545#ifdef CONFIG_PM
1546 if (!codec->d3_stop_clk || !codec->epss)
1547 bus->power_keep_link_on = 1;
1548#endif
1549
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001550
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001551 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001552 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001553
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001554 snd_hda_codec_proc_new(codec);
1555
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001556 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001557
1558 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1559 codec->subsystem_id, codec->revision_id);
1560 snd_component_add(codec->bus->card, component);
1561
1562 if (codecp)
1563 *codecp = codec;
1564 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001565
1566 error:
1567 snd_hda_codec_free(codec);
1568 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001569}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001570EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001571
Mengdong Lina15d05d2013-02-08 17:09:31 -05001572int snd_hda_codec_update_widgets(struct hda_codec *codec)
1573{
1574 hda_nid_t fg;
1575 int err;
1576
1577 /* Assume the function group node does not change,
1578 * only the widget nodes may change.
1579 */
1580 kfree(codec->wcaps);
1581 fg = codec->afg ? codec->afg : codec->mfg;
1582 err = read_widget_caps(codec, fg);
1583 if (err < 0) {
1584 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1585 return err;
1586 }
1587
1588 snd_array_free(&codec->init_pins);
1589 err = read_pin_defaults(codec);
1590
1591 return err;
1592}
1593EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1594
1595
Takashi Iwaif0639272013-11-18 12:07:29 +01001596#ifdef CONFIG_SND_HDA_CODEC_HDMI
1597/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1598static bool is_likely_hdmi_codec(struct hda_codec *codec)
1599{
1600 hda_nid_t nid = codec->start_nid;
1601 int i;
1602
1603 for (i = 0; i < codec->num_nodes; i++, nid++) {
1604 unsigned int wcaps = get_wcaps(codec, nid);
1605 switch (get_wcaps_type(wcaps)) {
1606 case AC_WID_AUD_IN:
1607 return false; /* HDMI parser supports only HDMI out */
1608 case AC_WID_AUD_OUT:
1609 if (!(wcaps & AC_WCAP_DIGITAL))
1610 return false;
1611 break;
1612 }
1613 }
1614 return true;
1615}
1616#else
1617/* no HDMI codec parser support */
1618#define is_likely_hdmi_codec(codec) false
1619#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1620
Takashi Iwaid5191e52009-11-16 14:58:17 +01001621/**
1622 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1623 * @codec: the HDA codec
1624 *
1625 * Start parsing of the given codec tree and (re-)initialize the whole
1626 * patch instance.
1627 *
1628 * Returns 0 if successful or a negative error code.
1629 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001630int snd_hda_codec_configure(struct hda_codec *codec)
1631{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001632 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001633 int err;
1634
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001635 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001636 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001637 err = get_codec_name(codec);
1638 if (err < 0)
1639 return err;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001642 if (!is_generic_config(codec) && codec->preset)
1643 patch = codec->preset->patch;
1644 if (!patch) {
1645 unload_parser(codec); /* to be sure */
Takashi Iwaif0639272013-11-18 12:07:29 +01001646 if (is_likely_hdmi_codec(codec))
1647 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001648#ifdef CONFIG_SND_HDA_GENERIC
1649 if (!patch)
1650 patch = load_parser(codec, snd_hda_parse_generic_codec);
1651#endif
1652 if (!patch) {
1653 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1654 return -ENODEV;
1655 }
Takashi Iwai82467612007-07-27 19:15:54 +02001656 }
1657
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001658 err = patch(codec);
1659 if (err < 0) {
1660 unload_parser(codec);
1661 return err;
1662 }
Takashi Iwai82467612007-07-27 19:15:54 +02001663
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001664 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001665 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001666 if (err < 0)
1667 return err;
1668 }
1669
Takashi Iwaif62faed2009-12-23 09:27:51 +01001670 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001671 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001672 snprintf(codec->bus->card->mixername,
1673 sizeof(codec->bus->card->mixername),
1674 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001677EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Takashi Iwaied360812012-08-08 17:12:52 +02001679/* update the stream-id if changed */
1680static void update_pcm_stream_id(struct hda_codec *codec,
1681 struct hda_cvt_setup *p, hda_nid_t nid,
1682 u32 stream_tag, int channel_id)
1683{
1684 unsigned int oldval, newval;
1685
1686 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1687 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1688 newval = (stream_tag << 4) | channel_id;
1689 if (oldval != newval)
1690 snd_hda_codec_write(codec, nid, 0,
1691 AC_VERB_SET_CHANNEL_STREAMID,
1692 newval);
1693 p->stream_tag = stream_tag;
1694 p->channel_id = channel_id;
1695 }
1696}
1697
1698/* update the format-id if changed */
1699static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1700 hda_nid_t nid, int format)
1701{
1702 unsigned int oldval;
1703
1704 if (p->format_id != format) {
1705 oldval = snd_hda_codec_read(codec, nid, 0,
1706 AC_VERB_GET_STREAM_FORMAT, 0);
1707 if (oldval != format) {
1708 msleep(1);
1709 snd_hda_codec_write(codec, nid, 0,
1710 AC_VERB_SET_STREAM_FORMAT,
1711 format);
1712 }
1713 p->format_id = format;
1714 }
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/**
1718 * snd_hda_codec_setup_stream - set up the codec for streaming
1719 * @codec: the CODEC to set up
1720 * @nid: the NID to set up
1721 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1722 * @channel_id: channel id to pass, zero based.
1723 * @format: stream format.
1724 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001725void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1726 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int channel_id, int format)
1728{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001729 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001730 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001731 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001732 int i;
1733
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001735 return;
1736
Takashi Iwai0ba21762007-04-16 11:29:14 +02001737 snd_printdd("hda_codec_setup_stream: "
1738 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001740 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001741 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001742 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001743
1744 if (codec->pcm_format_first)
1745 update_pcm_format(codec, p, nid, format);
1746 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1747 if (!codec->pcm_format_first)
1748 update_pcm_format(codec, p, nid, format);
1749
Takashi Iwaieb541332010-08-06 13:48:11 +02001750 p->active = 1;
1751 p->dirty = 0;
1752
1753 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001754 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001755 list_for_each_entry(c, &codec->bus->codec_list, list) {
1756 for (i = 0; i < c->cvt_setups.used; i++) {
1757 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001758 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001759 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001760 p->dirty = 1;
1761 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001764EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Takashi Iwaif0cea792010-08-13 11:56:53 +02001766static void really_cleanup_stream(struct hda_codec *codec,
1767 struct hda_cvt_setup *q);
1768
Takashi Iwaid5191e52009-11-16 14:58:17 +01001769/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001770 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001771 * @codec: the CODEC to clean up
1772 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001773 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001774 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001775void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1776 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001777{
Takashi Iwaieb541332010-08-06 13:48:11 +02001778 struct hda_cvt_setup *p;
1779
Takashi Iwai888afa12008-03-18 09:57:50 +01001780 if (!nid)
1781 return;
1782
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001783 if (codec->no_sticky_stream)
1784 do_now = 1;
1785
Takashi Iwai888afa12008-03-18 09:57:50 +01001786 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001787 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001788 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001789 /* here we just clear the active flag when do_now isn't set;
1790 * actual clean-ups will be done later in
1791 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1792 */
1793 if (do_now)
1794 really_cleanup_stream(codec, p);
1795 else
1796 p->active = 0;
1797 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001798}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001799EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001800
Takashi Iwaieb541332010-08-06 13:48:11 +02001801static void really_cleanup_stream(struct hda_codec *codec,
1802 struct hda_cvt_setup *q)
1803{
1804 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001805 if (q->stream_tag || q->channel_id)
1806 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1807 if (q->format_id)
1808 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1809);
Takashi Iwaieb541332010-08-06 13:48:11 +02001810 memset(q, 0, sizeof(*q));
1811 q->nid = nid;
1812}
1813
1814/* clean up the all conflicting obsolete streams */
1815static void purify_inactive_streams(struct hda_codec *codec)
1816{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001817 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001818 int i;
1819
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001820 list_for_each_entry(c, &codec->bus->codec_list, list) {
1821 for (i = 0; i < c->cvt_setups.used; i++) {
1822 struct hda_cvt_setup *p;
1823 p = snd_array_elem(&c->cvt_setups, i);
1824 if (p->dirty)
1825 really_cleanup_stream(c, p);
1826 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001827 }
1828}
1829
Takashi Iwai2a439522011-07-26 09:52:50 +02001830#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001831/* clean up all streams; called from suspend */
1832static void hda_cleanup_all_streams(struct hda_codec *codec)
1833{
1834 int i;
1835
1836 for (i = 0; i < codec->cvt_setups.used; i++) {
1837 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1838 if (p->stream_tag)
1839 really_cleanup_stream(codec, p);
1840 }
1841}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001842#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/*
1845 * amp access functions
1846 */
1847
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001848/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001849#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001850#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001851#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1852#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001854#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001857static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001858 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Takashi Iwai01751f52007-08-10 16:59:39 +02001860 memset(cache, 0, sizeof(*cache));
1861 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001862 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001863}
1864
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001865static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001866{
Takashi Iwai603c4012008-07-30 15:01:44 +02001867 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001871static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai01751f52007-08-10 16:59:39 +02001873 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1874 u16 cur = cache->hash[idx];
1875 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001878 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (info->key == key)
1880 return info;
1881 cur = info->next;
1882 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001883 return NULL;
1884}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001886/* query the hash. allocate an entry if not found. */
1887static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1888 u32 key)
1889{
1890 struct hda_cache_head *info = get_hash(cache, key);
1891 if (!info) {
1892 u16 idx, cur;
1893 /* add a new hash entry */
1894 info = snd_array_new(&cache->buf);
1895 if (!info)
1896 return NULL;
1897 cur = snd_array_index(&cache->buf, info);
1898 info->key = key;
1899 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001900 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001901 idx = key % (u16)ARRAY_SIZE(cache->hash);
1902 info->next = cache->hash[idx];
1903 cache->hash[idx] = cur;
1904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return info;
1906}
1907
Takashi Iwai01751f52007-08-10 16:59:39 +02001908/* query and allocate an amp hash entry */
1909static inline struct hda_amp_info *
1910get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1911{
1912 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1913}
1914
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001915/* overwrite the value with the key in the caps hash */
1916static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1917{
1918 struct hda_amp_info *info;
1919
1920 mutex_lock(&codec->hash_mutex);
1921 info = get_alloc_amp_hash(codec, key);
1922 if (!info) {
1923 mutex_unlock(&codec->hash_mutex);
1924 return -EINVAL;
1925 }
1926 info->amp_caps = val;
1927 info->head.val |= INFO_AMP_CAPS;
1928 mutex_unlock(&codec->hash_mutex);
1929 return 0;
1930}
1931
1932/* query the value from the caps hash; if not found, fetch the current
1933 * value from the given function and store in the hash
1934 */
1935static unsigned int
1936query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1937 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1938{
1939 struct hda_amp_info *info;
1940 unsigned int val;
1941
1942 mutex_lock(&codec->hash_mutex);
1943 info = get_alloc_amp_hash(codec, key);
1944 if (!info) {
1945 mutex_unlock(&codec->hash_mutex);
1946 return 0;
1947 }
1948 if (!(info->head.val & INFO_AMP_CAPS)) {
1949 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1950 val = func(codec, nid, dir);
1951 write_caps_hash(codec, key, val);
1952 } else {
1953 val = info->amp_caps;
1954 mutex_unlock(&codec->hash_mutex);
1955 }
1956 return val;
1957}
1958
1959static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1960 int direction)
1961{
1962 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1963 nid = codec->afg;
1964 return snd_hda_param_read(codec, nid,
1965 direction == HDA_OUTPUT ?
1966 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1967}
1968
Takashi Iwaid5191e52009-11-16 14:58:17 +01001969/**
1970 * query_amp_caps - query AMP capabilities
1971 * @codec: the HD-auio codec
1972 * @nid: the NID to query
1973 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1974 *
1975 * Query AMP capabilities for the given widget and direction.
1976 * Returns the obtained capability bits.
1977 *
1978 * When cap bits have been already read, this doesn't read again but
1979 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001981u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001983 return query_caps_hash(codec, nid, direction,
1984 HDA_HASH_KEY(nid, direction, 0),
1985 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001987EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Takashi Iwaid5191e52009-11-16 14:58:17 +01001989/**
1990 * snd_hda_override_amp_caps - Override the AMP capabilities
1991 * @codec: the CODEC to clean up
1992 * @nid: the NID to clean up
1993 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1994 * @caps: the capability bits to set
1995 *
1996 * Override the cached AMP caps bits value by the given one.
1997 * This function is useful if the driver needs to adjust the AMP ranges,
1998 * e.g. limit to 0dB, etc.
1999 *
2000 * Returns zero if successful or a negative error code.
2001 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002002int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2003 unsigned int caps)
2004{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002005 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002006}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002007EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002008
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002009static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2010 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002011{
2012 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2013}
2014
Takashi Iwaid5191e52009-11-16 14:58:17 +01002015/**
2016 * snd_hda_query_pin_caps - Query PIN capabilities
2017 * @codec: the HD-auio codec
2018 * @nid: the NID to query
2019 *
2020 * Query PIN capabilities for the given widget.
2021 * Returns the obtained capability bits.
2022 *
2023 * When cap bits have been already read, this doesn't read again but
2024 * returns the cached value.
2025 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002026u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2027{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002028 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002029 read_pin_cap);
2030}
Takashi Iwai1327a322009-03-23 13:07:47 +01002031EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
2032
Wu Fengguang864f92b2009-11-18 12:38:02 +08002033/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002034 * snd_hda_override_pin_caps - Override the pin capabilities
2035 * @codec: the CODEC
2036 * @nid: the NID to override
2037 * @caps: the capability bits to set
2038 *
2039 * Override the cached PIN capabilitiy bits value by the given one.
2040 *
2041 * Returns zero if successful or a negative error code.
2042 */
2043int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2044 unsigned int caps)
2045{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002046 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002047}
2048EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2049
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002050/* read or sync the hash value with the current value;
2051 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002053static struct hda_amp_info *
2054update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002055 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002057 struct hda_amp_info *info;
2058 unsigned int parm, val = 0;
2059 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002061 retry:
2062 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2063 if (!info)
2064 return NULL;
2065 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2066 if (!val_read) {
2067 mutex_unlock(&codec->hash_mutex);
2068 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2069 parm |= direction == HDA_OUTPUT ?
2070 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2071 parm |= index;
2072 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002073 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002074 val &= 0xff;
2075 val_read = true;
2076 mutex_lock(&codec->hash_mutex);
2077 goto retry;
2078 }
2079 info->vol[ch] = val;
2080 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002081 } else if (init_only)
2082 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002083 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
2086/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002087 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002089static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090 hda_nid_t nid, int ch, int direction, int index,
2091 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 u32 parm;
2094
2095 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2096 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2097 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002098 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2099 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002100 ; /* set the zero value as a fake mute */
2101 else
2102 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2104}
2105
Takashi Iwaid5191e52009-11-16 14:58:17 +01002106/**
2107 * snd_hda_codec_amp_read - Read AMP value
2108 * @codec: HD-audio codec
2109 * @nid: NID to read the AMP value
2110 * @ch: channel (left=0 or right=1)
2111 * @direction: #HDA_INPUT or #HDA_OUTPUT
2112 * @index: the index value (only for input direction)
2113 *
2114 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
Takashi Iwai834be882006-03-01 14:16:17 +01002116int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2117 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002119 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002120 unsigned int val = 0;
2121
2122 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002123 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002124 if (info)
2125 val = info->vol[ch];
2126 mutex_unlock(&codec->hash_mutex);
2127 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002129EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Takashi Iwai280e57d2012-12-14 10:32:21 +01002131static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2132 int direction, int idx, int mask, int val,
2133 bool init_only)
2134{
2135 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002136 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002137 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002138
2139 if (snd_BUG_ON(mask & ~0xff))
2140 mask &= 0xff;
2141 val &= mask;
2142
2143 mutex_lock(&codec->hash_mutex);
2144 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2145 if (!info) {
2146 mutex_unlock(&codec->hash_mutex);
2147 return 0;
2148 }
2149 val |= info->vol[ch] & ~mask;
2150 if (info->vol[ch] == val) {
2151 mutex_unlock(&codec->hash_mutex);
2152 return 0;
2153 }
2154 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002155 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002156 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002157 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002158 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002159 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002160 return 1;
2161}
2162
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163/**
2164 * snd_hda_codec_amp_update - update the AMP value
2165 * @codec: HD-audio codec
2166 * @nid: NID to read the AMP value
2167 * @ch: channel (left=0 or right=1)
2168 * @direction: #HDA_INPUT or #HDA_OUTPUT
2169 * @idx: the index value (only for input direction)
2170 * @mask: bit mask to set
2171 * @val: the bits value to set
2172 *
2173 * Update the AMP value with a bit mask.
2174 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002175 */
Takashi Iwai834be882006-03-01 14:16:17 +01002176int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2177 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002179 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002181EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Takashi Iwaid5191e52009-11-16 14:58:17 +01002183/**
2184 * snd_hda_codec_amp_stereo - update the AMP stereo values
2185 * @codec: HD-audio codec
2186 * @nid: NID to read the AMP value
2187 * @direction: #HDA_INPUT or #HDA_OUTPUT
2188 * @idx: the index value (only for input direction)
2189 * @mask: bit mask to set
2190 * @val: the bits value to set
2191 *
2192 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2193 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002194 */
2195int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2196 int direction, int idx, int mask, int val)
2197{
2198 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002199
2200 if (snd_BUG_ON(mask & ~0xff))
2201 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002202 for (ch = 0; ch < 2; ch++)
2203 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2204 idx, mask, val);
2205 return ret;
2206}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002207EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002208
Takashi Iwai280e57d2012-12-14 10:32:21 +01002209/* Works like snd_hda_codec_amp_update() but it writes the value only at
2210 * the first access. If the amp was already initialized / updated beforehand,
2211 * this does nothing.
2212 */
2213int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2214 int dir, int idx, int mask, int val)
2215{
2216 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2217}
2218EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2219
2220int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2221 int dir, int idx, int mask, int val)
2222{
2223 int ch, ret = 0;
2224
2225 if (snd_BUG_ON(mask & ~0xff))
2226 mask &= 0xff;
2227 for (ch = 0; ch < 2; ch++)
2228 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2229 idx, mask, val);
2230 return ret;
2231}
2232EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2233
Takashi Iwaid5191e52009-11-16 14:58:17 +01002234/**
2235 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2236 * @codec: HD-audio codec
2237 *
2238 * Resume the all amp commands from the cache.
2239 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002240void snd_hda_codec_resume_amp(struct hda_codec *codec)
2241{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002242 int i;
2243
Takashi Iwaic370dd62012-12-13 18:30:04 +01002244 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002245 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002246 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2247 struct hda_amp_info *buffer;
2248 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002249 hda_nid_t nid;
2250 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002251 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002252
2253 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002254 if (!buffer->head.dirty)
2255 continue;
2256 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002257 info = *buffer;
2258 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002259 if (!key)
2260 continue;
2261 nid = key & 0xff;
2262 idx = (key >> 16) & 0xff;
2263 dir = (key >> 24) & 0xff;
2264 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002265 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002266 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002267 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002268 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2269 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002270 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002271 }
2272 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002273 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002274}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002275EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002277static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2278 unsigned int ofs)
2279{
2280 u32 caps = query_amp_caps(codec, nid, dir);
2281 /* get num steps */
2282 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2283 if (ofs < caps)
2284 caps -= ofs;
2285 return caps;
2286}
2287
Takashi Iwaid5191e52009-11-16 14:58:17 +01002288/**
2289 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2290 *
2291 * The control element is supposed to have the private_value field
2292 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002294int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2298 u16 nid = get_amp_nid(kcontrol);
2299 u8 chs = get_amp_channels(kcontrol);
2300 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002301 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002303 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2304 uinfo->count = chs == 3 ? 2 : 1;
2305 uinfo->value.integer.min = 0;
2306 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2307 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002308 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002309 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2310 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EINVAL;
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
2314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002315EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002317
2318static inline unsigned int
2319read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2320 int ch, int dir, int idx, unsigned int ofs)
2321{
2322 unsigned int val;
2323 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2324 val &= HDA_AMP_VOLMASK;
2325 if (val >= ofs)
2326 val -= ofs;
2327 else
2328 val = 0;
2329 return val;
2330}
2331
2332static inline int
2333update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2334 int ch, int dir, int idx, unsigned int ofs,
2335 unsigned int val)
2336{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002337 unsigned int maxval;
2338
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002339 if (val > 0)
2340 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002341 /* ofs = 0: raw max value */
2342 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002343 if (val > maxval)
2344 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002345 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2346 HDA_AMP_VOLMASK, val);
2347}
2348
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349/**
2350 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2351 *
2352 * The control element is supposed to have the private_value field
2353 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2354 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002355int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2359 hda_nid_t nid = get_amp_nid(kcontrol);
2360 int chs = get_amp_channels(kcontrol);
2361 int dir = get_amp_direction(kcontrol);
2362 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002363 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 long *valp = ucontrol->value.integer.value;
2365
2366 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002367 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002369 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
2371}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002372EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Takashi Iwaid5191e52009-11-16 14:58:17 +01002374/**
2375 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2376 *
2377 * The control element is supposed to have the private_value field
2378 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2379 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002380int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 hda_nid_t nid = get_amp_nid(kcontrol);
2385 int chs = get_amp_channels(kcontrol);
2386 int dir = get_amp_direction(kcontrol);
2387 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002388 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 long *valp = ucontrol->value.integer.value;
2390 int change = 0;
2391
Takashi Iwaicb53c622007-08-10 17:21:45 +02002392 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002393 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002394 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002395 valp++;
2396 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002397 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002398 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002399 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return change;
2401}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002402EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Takashi Iwaid5191e52009-11-16 14:58:17 +01002404/**
2405 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2406 *
2407 * The control element is supposed to have the private_value field
2408 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2409 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002410int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2411 unsigned int size, unsigned int __user *_tlv)
2412{
2413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2414 hda_nid_t nid = get_amp_nid(kcontrol);
2415 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002416 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002417 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002418 u32 caps, val1, val2;
2419
2420 if (size < 4 * sizeof(unsigned int))
2421 return -ENOMEM;
2422 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2424 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002425 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002426 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002427 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002428 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002429 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002430 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2431 return -EFAULT;
2432 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2433 return -EFAULT;
2434 if (put_user(val1, _tlv + 2))
2435 return -EFAULT;
2436 if (put_user(val2, _tlv + 3))
2437 return -EFAULT;
2438 return 0;
2439}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002440EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002441
Takashi Iwaid5191e52009-11-16 14:58:17 +01002442/**
2443 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2444 * @codec: HD-audio codec
2445 * @nid: NID of a reference widget
2446 * @dir: #HDA_INPUT or #HDA_OUTPUT
2447 * @tlv: TLV data to be stored, at least 4 elements
2448 *
2449 * Set (static) TLV data for a virtual master volume using the AMP caps
2450 * obtained from the reference NID.
2451 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002452 */
2453void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2454 unsigned int *tlv)
2455{
2456 u32 caps;
2457 int nums, step;
2458
2459 caps = query_amp_caps(codec, nid, dir);
2460 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2461 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2462 step = (step + 1) * 25;
2463 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2464 tlv[1] = 2 * sizeof(unsigned int);
2465 tlv[2] = -nums * step;
2466 tlv[3] = step;
2467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002468EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002469
2470/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002471static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002472find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002473{
2474 struct snd_ctl_elem_id id;
2475 memset(&id, 0, sizeof(id));
2476 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002477 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002478 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002479 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2480 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002481 strcpy(id.name, name);
2482 return snd_ctl_find_id(codec->bus->card, &id);
2483}
2484
Takashi Iwaid5191e52009-11-16 14:58:17 +01002485/**
2486 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2487 * @codec: HD-audio codec
2488 * @name: ctl id name string
2489 *
2490 * Get the control element with the given id string and IFACE_MIXER.
2491 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002492struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2493 const char *name)
2494{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002495 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002496}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002497EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002498
Takashi Iwaidcda5802012-10-12 17:24:51 +02002499static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002500 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002501{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002502 int i, idx;
2503 /* 16 ctlrs should be large enough */
2504 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2505 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002506 return idx;
2507 }
2508 return -EBUSY;
2509}
2510
Takashi Iwaid5191e52009-11-16 14:58:17 +01002511/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002512 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002513 * @codec: HD-audio codec
2514 * @nid: corresponding NID (optional)
2515 * @kctl: the control element to assign
2516 *
2517 * Add the given control element to an array inside the codec instance.
2518 * All control elements belonging to a codec are supposed to be added
2519 * by this function so that a proper clean-up works at the free or
2520 * reconfiguration time.
2521 *
2522 * If non-zero @nid is passed, the NID is assigned to the control element.
2523 * The assignment is shown in the codec proc file.
2524 *
2525 * snd_hda_ctl_add() checks the control subdev id field whether
2526 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002527 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2528 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002529 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002530int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2531 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002532{
2533 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002534 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002535 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002536
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002537 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002538 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002539 if (nid == 0)
2540 nid = get_amp_nid_(kctl->private_value);
2541 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002542 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2543 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002544 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002545 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002546 err = snd_ctl_add(codec->bus->card, kctl);
2547 if (err < 0)
2548 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002549 item = snd_array_new(&codec->mixers);
2550 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002551 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002552 item->kctl = kctl;
2553 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002554 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002555 return 0;
2556}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002557EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002558
Takashi Iwaid5191e52009-11-16 14:58:17 +01002559/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002560 * snd_hda_add_nid - Assign a NID to a control element
2561 * @codec: HD-audio codec
2562 * @nid: corresponding NID (optional)
2563 * @kctl: the control element to assign
2564 * @index: index to kctl
2565 *
2566 * Add the given control element to an array inside the codec instance.
2567 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2568 * NID:KCTL mapping - for example "Capture Source" selector.
2569 */
2570int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2571 unsigned int index, hda_nid_t nid)
2572{
2573 struct hda_nid_item *item;
2574
2575 if (nid > 0) {
2576 item = snd_array_new(&codec->nids);
2577 if (!item)
2578 return -ENOMEM;
2579 item->kctl = kctl;
2580 item->index = index;
2581 item->nid = nid;
2582 return 0;
2583 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002584 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2585 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002586 return -EINVAL;
2587}
2588EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2589
2590/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002591 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2592 * @codec: HD-audio codec
2593 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002594void snd_hda_ctls_clear(struct hda_codec *codec)
2595{
2596 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002597 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002598 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002599 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002600 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002601 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002602}
2603
Takashi Iwaia65d6292009-02-23 16:57:04 +01002604/* pseudo device locking
2605 * toggle card->shutdown to allow/disallow the device access (as a hack)
2606 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002607int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002608{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002609 struct snd_card *card = bus->card;
2610 struct hda_codec *codec;
2611
Takashi Iwaia65d6292009-02-23 16:57:04 +01002612 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002613 if (card->shutdown)
2614 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002615 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002616 if (!list_empty(&card->ctl_files))
2617 goto err_clear;
2618
2619 list_for_each_entry(codec, &bus->codec_list, list) {
2620 int pcm;
2621 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2622 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2623 if (!cpcm->pcm)
2624 continue;
2625 if (cpcm->pcm->streams[0].substream_opened ||
2626 cpcm->pcm->streams[1].substream_opened)
2627 goto err_clear;
2628 }
2629 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002630 spin_unlock(&card->files_lock);
2631 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002632
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002633 err_clear:
2634 card->shutdown = 0;
2635 err_unlock:
2636 spin_unlock(&card->files_lock);
2637 return -EINVAL;
2638}
2639EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2640
2641void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002642{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002643 struct snd_card *card = bus->card;
2644
2645 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002646 spin_lock(&card->files_lock);
2647 card->shutdown = 0;
2648 spin_unlock(&card->files_lock);
2649}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002650EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002651
Takashi Iwaid5191e52009-11-16 14:58:17 +01002652/**
2653 * snd_hda_codec_reset - Clear all objects assigned to the codec
2654 * @codec: HD-audio codec
2655 *
2656 * This frees the all PCM and control elements assigned to the codec, and
2657 * clears the caches and restores the pin default configurations.
2658 *
2659 * When a device is being used, it returns -EBSY. If successfully freed,
2660 * returns zero.
2661 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002662int snd_hda_codec_reset(struct hda_codec *codec)
2663{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002664 struct hda_bus *bus = codec->bus;
2665 struct snd_card *card = bus->card;
2666 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002667
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002668 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002669 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002670
2671 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002672 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002673#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002674 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002675 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002676#endif
2677 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002678 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002679 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002680 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002681 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002682 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002683 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002684 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002685 }
2686 if (codec->patch_ops.free)
2687 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002688 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002689 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002690 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002691 codec->spec = NULL;
2692 free_hda_cache(&codec->amp_cache);
2693 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002694 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2695 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002696 /* free only driver_pins so that init_pins + user_pins are restored */
2697 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002698 snd_array_free(&codec->cvt_setups);
2699 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002700 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002701 codec->num_pcms = 0;
2702 codec->pcm_info = NULL;
2703 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002704 codec->slave_dig_outs = NULL;
2705 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002706 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002707 module_put(codec->owner);
2708 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002709
2710 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002711 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002712 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002713}
2714
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002715typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2716
2717/* apply the function to all matching slave ctls in the mixer list */
2718static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002719 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002720{
2721 struct hda_nid_item *items;
2722 const char * const *s;
2723 int i, err;
2724
2725 items = codec->mixers.list;
2726 for (i = 0; i < codec->mixers.used; i++) {
2727 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002728 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002729 continue;
2730 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002731 char tmpname[sizeof(sctl->id.name)];
2732 const char *name = *s;
2733 if (suffix) {
2734 snprintf(tmpname, sizeof(tmpname), "%s %s",
2735 name, suffix);
2736 name = tmpname;
2737 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002738 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002739 err = func(data, sctl);
2740 if (err)
2741 return err;
2742 break;
2743 }
2744 }
2745 }
2746 return 0;
2747}
2748
2749static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2750{
2751 return 1;
2752}
2753
Takashi Iwai18478e82012-03-09 17:51:10 +01002754/* guess the value corresponding to 0dB */
Takashi Iwai485e3e02013-11-04 15:51:00 +01002755static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002756{
2757 int _tlv[4];
2758 const int *tlv = NULL;
2759 int val = -1;
2760
2761 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2762 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2763 mm_segment_t fs = get_fs();
2764 set_fs(get_ds());
2765 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2766 tlv = _tlv;
2767 set_fs(fs);
2768 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2769 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002770 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2771 int step = tlv[3];
2772 step &= ~TLV_DB_SCALE_MUTE;
2773 if (!step)
2774 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002775 if (*step_to_check && *step_to_check != step) {
2776 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2777 *step_to_check, step);
2778 return -1;
2779 }
2780 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002781 val = -tlv[2] / step;
2782 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002783 return val;
2784}
2785
2786/* call kctl->put with the given value(s) */
2787static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2788{
2789 struct snd_ctl_elem_value *ucontrol;
2790 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2791 if (!ucontrol)
2792 return -ENOMEM;
2793 ucontrol->value.integer.value[0] = val;
2794 ucontrol->value.integer.value[1] = val;
2795 kctl->put(kctl, ucontrol);
2796 kfree(ucontrol);
2797 return 0;
2798}
2799
2800/* initialize the slave volume with 0dB */
2801static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2802{
Takashi Iwai485e3e02013-11-04 15:51:00 +01002803 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002804 if (offset > 0)
2805 put_kctl_with_value(slave, offset);
2806 return 0;
2807}
2808
2809/* unmute the slave */
2810static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2811{
2812 return put_kctl_with_value(slave, 1);
2813}
2814
Takashi Iwaid5191e52009-11-16 14:58:17 +01002815/**
2816 * snd_hda_add_vmaster - create a virtual master control and add slaves
2817 * @codec: HD-audio codec
2818 * @name: vmaster control name
2819 * @tlv: TLV data (optional)
2820 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002821 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002822 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002823 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002824 *
2825 * Create a virtual master control with the given name. The TLV data
2826 * must be either NULL or a valid data.
2827 *
2828 * @slaves is a NULL-terminated array of strings, each of which is a
2829 * slave control name. All controls with these names are assigned to
2830 * the new virtual master control.
2831 *
2832 * This function returns zero if successful or a negative error code.
2833 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002834int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002835 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002836 const char *suffix, bool init_slave_vol,
2837 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002838{
2839 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002840 int err;
2841
Takashi Iwai29e58532012-03-12 12:25:03 +01002842 if (ctl_ret)
2843 *ctl_ret = NULL;
2844
Takashi Iwai9322ca52012-02-03 14:28:01 +01002845 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002846 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002847 snd_printdd("No slave found for %s\n", name);
2848 return 0;
2849 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002850 kctl = snd_ctl_make_virtual_master(name, tlv);
2851 if (!kctl)
2852 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002853 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002854 if (err < 0)
2855 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002856
Takashi Iwai9322ca52012-02-03 14:28:01 +01002857 err = map_slaves(codec, slaves, suffix,
2858 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002859 if (err < 0)
2860 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002861
2862 /* init with master mute & zero volume */
2863 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002864 if (init_slave_vol) {
2865 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002866 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002867 tlv ? init_slave_0dB : init_slave_unmute, &step);
2868 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002869
Takashi Iwai29e58532012-03-12 12:25:03 +01002870 if (ctl_ret)
2871 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002872 return 0;
2873}
Takashi Iwai18478e82012-03-09 17:51:10 +01002874EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002875
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002876/*
2877 * mute-LED control using vmaster
2878 */
2879static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_info *uinfo)
2881{
2882 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002883 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002884 };
2885 unsigned int index;
2886
2887 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2888 uinfo->count = 1;
2889 uinfo->value.enumerated.items = 3;
2890 index = uinfo->value.enumerated.item;
2891 if (index >= 3)
2892 index = 2;
2893 strcpy(uinfo->value.enumerated.name, texts[index]);
2894 return 0;
2895}
2896
2897static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_value *ucontrol)
2899{
2900 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2901 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2902 return 0;
2903}
2904
2905static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2906 struct snd_ctl_elem_value *ucontrol)
2907{
2908 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2909 unsigned int old_mode = hook->mute_mode;
2910
2911 hook->mute_mode = ucontrol->value.enumerated.item[0];
2912 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2913 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2914 if (old_mode == hook->mute_mode)
2915 return 0;
2916 snd_hda_sync_vmaster_hook(hook);
2917 return 1;
2918}
2919
2920static struct snd_kcontrol_new vmaster_mute_mode = {
2921 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2922 .name = "Mute-LED Mode",
2923 .info = vmaster_mute_mode_info,
2924 .get = vmaster_mute_mode_get,
2925 .put = vmaster_mute_mode_put,
2926};
2927
2928/*
2929 * Add a mute-LED hook with the given vmaster switch kctl
2930 * "Mute-LED Mode" control is automatically created and associated with
2931 * the given hook.
2932 */
2933int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002934 struct hda_vmaster_mute_hook *hook,
2935 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002936{
2937 struct snd_kcontrol *kctl;
2938
2939 if (!hook->hook || !hook->sw_kctl)
2940 return 0;
2941 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2942 hook->codec = codec;
2943 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002944 if (!expose_enum_ctl)
2945 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002946 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2947 if (!kctl)
2948 return -ENOMEM;
2949 return snd_hda_ctl_add(codec, 0, kctl);
2950}
2951EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2952
2953/*
2954 * Call the hook with the current value for synchronization
2955 * Should be called in init callback
2956 */
2957void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2958{
2959 if (!hook->hook || !hook->codec)
2960 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002961 /* don't call vmaster hook in the destructor since it might have
2962 * been already destroyed
2963 */
2964 if (hook->codec->bus->shutdown)
2965 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002966 switch (hook->mute_mode) {
2967 case HDA_VMUTE_FOLLOW_MASTER:
2968 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2969 break;
2970 default:
2971 hook->hook(hook->codec, hook->mute_mode);
2972 break;
2973 }
2974}
2975EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2976
2977
Takashi Iwaid5191e52009-11-16 14:58:17 +01002978/**
2979 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2980 *
2981 * The control element is supposed to have the private_value field
2982 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2983 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002984int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 int chs = get_amp_channels(kcontrol);
2988
2989 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2990 uinfo->count = chs == 3 ? 2 : 1;
2991 uinfo->value.integer.min = 0;
2992 uinfo->value.integer.max = 1;
2993 return 0;
2994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002995EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Takashi Iwaid5191e52009-11-16 14:58:17 +01002997/**
2998 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2999 *
3000 * The control element is supposed to have the private_value field
3001 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003003int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3007 hda_nid_t nid = get_amp_nid(kcontrol);
3008 int chs = get_amp_channels(kcontrol);
3009 int dir = get_amp_direction(kcontrol);
3010 int idx = get_amp_index(kcontrol);
3011 long *valp = ucontrol->value.integer.value;
3012
3013 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003014 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003015 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003017 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003018 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return 0;
3020}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003021EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Takashi Iwaid5191e52009-11-16 14:58:17 +01003023/**
3024 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3025 *
3026 * The control element is supposed to have the private_value field
3027 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3028 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003029int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3033 hda_nid_t nid = get_amp_nid(kcontrol);
3034 int chs = get_amp_channels(kcontrol);
3035 int dir = get_amp_direction(kcontrol);
3036 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 long *valp = ucontrol->value.integer.value;
3038 int change = 0;
3039
Takashi Iwaicb53c622007-08-10 17:21:45 +02003040 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003041 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003042 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003043 HDA_AMP_MUTE,
3044 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003045 valp++;
3046 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003047 if (chs & 2)
3048 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003049 HDA_AMP_MUTE,
3050 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003051 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003052 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return change;
3054}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003055EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057/*
Takashi Iwai985be542005-11-02 18:26:49 +01003058 * bound volume controls
3059 *
3060 * bind multiple volumes (# indices, from 0)
3061 */
3062
3063#define AMP_VAL_IDX_SHIFT 19
3064#define AMP_VAL_IDX_MASK (0x0f<<19)
3065
Takashi Iwaid5191e52009-11-16 14:58:17 +01003066/**
3067 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3068 *
3069 * The control element is supposed to have the private_value field
3070 * set up via HDA_BIND_MUTE*() macros.
3071 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003074{
3075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3076 unsigned long pval;
3077 int err;
3078
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003079 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003080 pval = kcontrol->private_value;
3081 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3082 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3083 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003084 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003085 return err;
3086}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003087EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003088
Takashi Iwaid5191e52009-11-16 14:58:17 +01003089/**
3090 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3091 *
3092 * The control element is supposed to have the private_value field
3093 * set up via HDA_BIND_MUTE*() macros.
3094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3096 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003097{
3098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3099 unsigned long pval;
3100 int i, indices, err = 0, change = 0;
3101
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003102 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003103 pval = kcontrol->private_value;
3104 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3105 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003106 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3107 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003108 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3109 if (err < 0)
3110 break;
3111 change |= err;
3112 }
3113 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003114 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003115 return err < 0 ? err : change;
3116}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003117EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003118
Takashi Iwaid5191e52009-11-16 14:58:17 +01003119/**
3120 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3121 *
3122 * The control element is supposed to have the private_value field
3123 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003124 */
3125int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_info *uinfo)
3127{
3128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3129 struct hda_bind_ctls *c;
3130 int err;
3131
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003132 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003133 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003134 kcontrol->private_value = *c->values;
3135 err = c->ops->info(kcontrol, uinfo);
3136 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003137 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003138 return err;
3139}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003140EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003141
Takashi Iwaid5191e52009-11-16 14:58:17 +01003142/**
3143 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3144 *
3145 * The control element is supposed to have the private_value field
3146 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3147 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003148int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3149 struct snd_ctl_elem_value *ucontrol)
3150{
3151 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3152 struct hda_bind_ctls *c;
3153 int err;
3154
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003155 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003156 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003157 kcontrol->private_value = *c->values;
3158 err = c->ops->get(kcontrol, ucontrol);
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;
3162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003163EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003164
Takashi Iwaid5191e52009-11-16 14:58:17 +01003165/**
3166 * snd_hda_mixer_bind_ctls_put - Put 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() or HDA_BIND_SW() macros.
3170 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003171int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3172 struct snd_ctl_elem_value *ucontrol)
3173{
3174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3175 struct hda_bind_ctls *c;
3176 unsigned long *vals;
3177 int err = 0, change = 0;
3178
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003179 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003180 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003181 for (vals = c->values; *vals; vals++) {
3182 kcontrol->private_value = *vals;
3183 err = c->ops->put(kcontrol, ucontrol);
3184 if (err < 0)
3185 break;
3186 change |= err;
3187 }
3188 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003189 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003190 return err < 0 ? err : change;
3191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003192EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003193
Takashi Iwaid5191e52009-11-16 14:58:17 +01003194/**
3195 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3196 *
3197 * The control element is supposed to have the private_value field
3198 * set up via HDA_BIND_VOL() macro.
3199 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003200int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3201 unsigned int size, unsigned int __user *tlv)
3202{
3203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3204 struct hda_bind_ctls *c;
3205 int err;
3206
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003207 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003208 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003209 kcontrol->private_value = *c->values;
3210 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3211 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003212 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003213 return err;
3214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003215EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003216
3217struct hda_ctl_ops snd_hda_bind_vol = {
3218 .info = snd_hda_mixer_amp_volume_info,
3219 .get = snd_hda_mixer_amp_volume_get,
3220 .put = snd_hda_mixer_amp_volume_put,
3221 .tlv = snd_hda_mixer_amp_tlv
3222};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003223EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003224
3225struct hda_ctl_ops snd_hda_bind_sw = {
3226 .info = snd_hda_mixer_amp_switch_info,
3227 .get = snd_hda_mixer_amp_switch_get,
3228 .put = snd_hda_mixer_amp_switch_put,
3229 .tlv = snd_hda_mixer_amp_tlv
3230};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003231EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003232
3233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 * SPDIF out controls
3235 */
3236
Takashi Iwai0ba21762007-04-16 11:29:14 +02003237static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3238 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
3240 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3241 uinfo->count = 1;
3242 return 0;
3243}
3244
Takashi Iwai0ba21762007-04-16 11:29:14 +02003245static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3246 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3249 IEC958_AES0_NONAUDIO |
3250 IEC958_AES0_CON_EMPHASIS_5015 |
3251 IEC958_AES0_CON_NOT_COPYRIGHT;
3252 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3253 IEC958_AES1_CON_ORIGINAL;
3254 return 0;
3255}
3256
Takashi Iwai0ba21762007-04-16 11:29:14 +02003257static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3258 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3261 IEC958_AES0_NONAUDIO |
3262 IEC958_AES0_PRO_EMPHASIS_5015;
3263 return 0;
3264}
3265
Takashi Iwai0ba21762007-04-16 11:29:14 +02003266static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003270 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003271 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003273 mutex_lock(&codec->spdif_mutex);
3274 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003275 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3276 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3277 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3278 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003279 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 return 0;
3282}
3283
3284/* convert from SPDIF status bits to HDA SPDIF bits
3285 * bit 0 (DigEn) is always set zero (to be filled later)
3286 */
3287static unsigned short convert_from_spdif_status(unsigned int sbits)
3288{
3289 unsigned short val = 0;
3290
3291 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003292 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003294 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003296 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3297 IEC958_AES0_PRO_EMPHASIS_5015)
3298 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003300 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3301 IEC958_AES0_CON_EMPHASIS_5015)
3302 val |= AC_DIG1_EMPHASIS;
3303 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3304 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003306 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3308 }
3309 return val;
3310}
3311
3312/* convert to SPDIF status bits from HDA SPDIF bits
3313 */
3314static unsigned int convert_to_spdif_status(unsigned short val)
3315{
3316 unsigned int sbits = 0;
3317
Takashi Iwai0ba21762007-04-16 11:29:14 +02003318 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003320 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 sbits |= IEC958_AES0_PROFESSIONAL;
3322 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003323 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3325 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003326 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003328 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003330 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3332 sbits |= val & (0x7f << 8);
3333 }
3334 return sbits;
3335}
3336
Takashi Iwai2f728532008-09-25 16:32:41 +02003337/* set digital convert verbs both for the given NID and its slaves */
3338static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3339 int verb, int val)
3340{
Takashi Iwaidda14412011-05-02 11:29:30 +02003341 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003342
Takashi Iwai9e976972008-11-25 08:17:20 +01003343 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003344 d = codec->slave_dig_outs;
3345 if (!d)
3346 return;
3347 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003348 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003349}
3350
3351static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3352 int dig1, int dig2)
3353{
3354 if (dig1 != -1)
3355 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3356 if (dig2 != -1)
3357 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3358}
3359
Takashi Iwai0ba21762007-04-16 11:29:14 +02003360static int snd_hda_spdif_default_put(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 Warren7c9359762011-06-01 11:14:17 -06003364 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003365 struct hda_spdif_out *spdif;
3366 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 unsigned short val;
3368 int change;
3369
Ingo Molnar62932df2006-01-16 16:34:20 +01003370 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003371 spdif = snd_array_elem(&codec->spdif_out, idx);
3372 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003373 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3375 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3376 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003377 val = convert_from_spdif_status(spdif->status);
3378 val |= spdif->ctls & 1;
3379 change = spdif->ctls != val;
3380 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003381 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003382 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003383 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return change;
3385}
3386
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003387#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Takashi Iwai0ba21762007-04-16 11:29:14 +02003389static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003393 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003394 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003396 mutex_lock(&codec->spdif_mutex);
3397 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003398 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003399 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return 0;
3401}
3402
Stephen Warren74b654c2011-06-01 11:14:18 -06003403static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3404 int dig1, int dig2)
3405{
3406 set_dig_out_convert(codec, nid, dig1, dig2);
3407 /* unmute amp switch (if any) */
3408 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3409 (dig1 & AC_DIG1_ENABLE))
3410 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3411 HDA_AMP_MUTE, 0);
3412}
3413
Takashi Iwai0ba21762007-04-16 11:29:14 +02003414static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003418 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003419 struct hda_spdif_out *spdif;
3420 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 unsigned short val;
3422 int change;
3423
Ingo Molnar62932df2006-01-16 16:34:20 +01003424 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003425 spdif = snd_array_elem(&codec->spdif_out, idx);
3426 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003427 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003430 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003431 spdif->ctls = val;
3432 if (change && nid != (u16)-1)
3433 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003434 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return change;
3436}
3437
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003438static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 {
3440 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3441 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003442 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 .info = snd_hda_spdif_mask_info,
3444 .get = snd_hda_spdif_cmask_get,
3445 },
3446 {
3447 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3448 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003449 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .info = snd_hda_spdif_mask_info,
3451 .get = snd_hda_spdif_pmask_get,
3452 },
3453 {
3454 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003455 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 .info = snd_hda_spdif_mask_info,
3457 .get = snd_hda_spdif_default_get,
3458 .put = snd_hda_spdif_default_put,
3459 },
3460 {
3461 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003462 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 .info = snd_hda_spdif_out_switch_info,
3464 .get = snd_hda_spdif_out_switch_get,
3465 .put = snd_hda_spdif_out_switch_put,
3466 },
3467 { } /* end */
3468};
3469
3470/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003471 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003473 * @associated_nid: NID that new ctls associated with
3474 * @cvt_nid: converter NID
3475 * @type: HDA_PCM_TYPE_*
3476 * Creates controls related with the digital output.
3477 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 *
3479 * Returns 0 if successful, or a negative error code.
3480 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003481int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3482 hda_nid_t associated_nid,
3483 hda_nid_t cvt_nid,
3484 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
3486 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003487 struct snd_kcontrol *kctl;
3488 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003489 int idx = 0;
3490 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003491 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003492 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003494 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003495 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003496 idx = spdif_index;
3497 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003498 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003499 /* suppose a single SPDIF device */
3500 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3501 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3502 if (!kctl)
3503 break;
3504 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003505 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003506 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003507 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003508 if (!bus->primary_dig_out_type)
3509 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003510
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003511 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003512 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003513 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3514 return -EBUSY;
3515 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003516 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003517 if (!spdif)
3518 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3520 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003521 if (!kctl)
3522 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003523 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003524 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003525 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003526 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return err;
3528 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003529 spdif->nid = cvt_nid;
3530 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003531 AC_VERB_GET_DIGI_CONVERT_1, 0);
3532 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return 0;
3534}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003535EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003537/* get the hda_spdif_out entry from the given NID
3538 * call within spdif_mutex lock
3539 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003540struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3541 hda_nid_t nid)
3542{
3543 int i;
3544 for (i = 0; i < codec->spdif_out.used; i++) {
3545 struct hda_spdif_out *spdif =
3546 snd_array_elem(&codec->spdif_out, i);
3547 if (spdif->nid == nid)
3548 return spdif;
3549 }
3550 return NULL;
3551}
3552EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3553
Stephen Warren74b654c2011-06-01 11:14:18 -06003554void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3555{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003556 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003557
3558 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003559 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003560 spdif->nid = (u16)-1;
3561 mutex_unlock(&codec->spdif_mutex);
3562}
3563EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3564
3565void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3566{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003567 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003568 unsigned short val;
3569
3570 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003571 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003572 if (spdif->nid != nid) {
3573 spdif->nid = nid;
3574 val = spdif->ctls;
3575 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3576 }
3577 mutex_unlock(&codec->spdif_mutex);
3578}
3579EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003582 * SPDIF sharing with analog output
3583 */
3584static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3585 struct snd_ctl_elem_value *ucontrol)
3586{
3587 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3588 ucontrol->value.integer.value[0] = mout->share_spdif;
3589 return 0;
3590}
3591
3592static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3593 struct snd_ctl_elem_value *ucontrol)
3594{
3595 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3596 mout->share_spdif = !!ucontrol->value.integer.value[0];
3597 return 0;
3598}
3599
3600static struct snd_kcontrol_new spdif_share_sw = {
3601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3602 .name = "IEC958 Default PCM Playback Switch",
3603 .info = snd_ctl_boolean_mono_info,
3604 .get = spdif_share_sw_get,
3605 .put = spdif_share_sw_put,
3606};
3607
Takashi Iwaid5191e52009-11-16 14:58:17 +01003608/**
3609 * snd_hda_create_spdif_share_sw - create Default PCM switch
3610 * @codec: the HDA codec
3611 * @mout: multi-out instance
3612 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003613int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3614 struct hda_multi_out *mout)
3615{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003616 struct snd_kcontrol *kctl;
3617
Takashi Iwai9a081602008-02-12 18:37:26 +01003618 if (!mout->dig_out_nid)
3619 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003620
3621 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3622 if (!kctl)
3623 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003624 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003625 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003626}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003627EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003628
3629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 * SPDIF input
3631 */
3632
3633#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3634
Takashi Iwai0ba21762007-04-16 11:29:14 +02003635static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3636 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637{
3638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3639
3640 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3641 return 0;
3642}
3643
Takashi Iwai0ba21762007-04-16 11:29:14 +02003644static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3645 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3648 hda_nid_t nid = kcontrol->private_value;
3649 unsigned int val = !!ucontrol->value.integer.value[0];
3650 int change;
3651
Ingo Molnar62932df2006-01-16 16:34:20 +01003652 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003654 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003656 snd_hda_codec_write_cache(codec, nid, 0,
3657 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003659 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return change;
3661}
3662
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3664 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3667 hda_nid_t nid = kcontrol->private_value;
3668 unsigned short val;
3669 unsigned int sbits;
3670
Andrew Paprocki3982d172007-12-19 12:13:44 +01003671 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 sbits = convert_to_spdif_status(val);
3673 ucontrol->value.iec958.status[0] = sbits;
3674 ucontrol->value.iec958.status[1] = sbits >> 8;
3675 ucontrol->value.iec958.status[2] = sbits >> 16;
3676 ucontrol->value.iec958.status[3] = sbits >> 24;
3677 return 0;
3678}
3679
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003680static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 {
3682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003683 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 .info = snd_hda_spdif_in_switch_info,
3685 .get = snd_hda_spdif_in_switch_get,
3686 .put = snd_hda_spdif_in_switch_put,
3687 },
3688 {
3689 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003691 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 .info = snd_hda_spdif_mask_info,
3693 .get = snd_hda_spdif_in_status_get,
3694 },
3695 { } /* end */
3696};
3697
3698/**
3699 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3700 * @codec: the HDA codec
3701 * @nid: audio in widget NID
3702 *
3703 * Creates controls related with the SPDIF input.
3704 * Called from each patch supporting the SPDIF in.
3705 *
3706 * Returns 0 if successful, or a negative error code.
3707 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003708int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
3710 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003711 struct snd_kcontrol *kctl;
3712 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003713 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Takashi Iwaidcda5802012-10-12 17:24:51 +02003715 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003716 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003717 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3718 return -EBUSY;
3719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3721 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003722 if (!kctl)
3723 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003725 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003726 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return err;
3728 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003729 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003730 snd_hda_codec_read(codec, nid, 0,
3731 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003732 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return 0;
3734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003735EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003737/*
3738 * command cache
3739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Takashi Iwaic370dd62012-12-13 18:30:04 +01003741/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003742#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3743#define get_cmd_cache_nid(key) ((key) & 0xff)
3744#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3745
3746/**
3747 * snd_hda_codec_write_cache - send a single command with caching
3748 * @codec: the HDA codec
3749 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003750 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003751 * @verb: the verb to send
3752 * @parm: the parameter for the verb
3753 *
3754 * Send a single command without waiting for response.
3755 *
3756 * Returns 0 if successful, or a negative error code.
3757 */
3758int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003759 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003760{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003761 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003762 struct hda_cache_head *c;
3763 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003764 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003765
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003766 cache_only = codec->cached_write;
3767 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003768 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003769 if (err < 0)
3770 return err;
3771 }
3772
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003773 /* parm may contain the verb stuff for get/set amp */
3774 verb = verb | (parm >> 8);
3775 parm &= 0xff;
3776 key = build_cmd_cache_key(nid, verb);
3777 mutex_lock(&codec->bus->cmd_mutex);
3778 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003779 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003780 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003781 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003782 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003783 mutex_unlock(&codec->bus->cmd_mutex);
3784 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003785}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003786EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003787
Takashi Iwaid5191e52009-11-16 14:58:17 +01003788/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003789 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3790 * @codec: the HDA codec
3791 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003792 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003793 * @verb: the verb to send
3794 * @parm: the parameter for the verb
3795 *
3796 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3797 * command if the parameter is already identical with the cached value.
3798 * If not, it sends the command and refreshes the cache.
3799 *
3800 * Returns 0 if successful, or a negative error code.
3801 */
3802int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003803 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003804{
3805 struct hda_cache_head *c;
3806 u32 key;
3807
3808 /* parm may contain the verb stuff for get/set amp */
3809 verb = verb | (parm >> 8);
3810 parm &= 0xff;
3811 key = build_cmd_cache_key(nid, verb);
3812 mutex_lock(&codec->bus->cmd_mutex);
3813 c = get_hash(&codec->cmd_cache, key);
3814 if (c && c->val == parm) {
3815 mutex_unlock(&codec->bus->cmd_mutex);
3816 return 0;
3817 }
3818 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003819 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003820}
3821EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3822
3823/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003824 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3825 * @codec: HD-audio codec
3826 *
3827 * Execute all verbs recorded in the command caches to resume.
3828 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003829void snd_hda_codec_resume_cache(struct hda_codec *codec)
3830{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003831 int i;
3832
Takashi Iwaic370dd62012-12-13 18:30:04 +01003833 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003834 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003835 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3836 struct hda_cache_head *buffer;
3837 u32 key;
3838
3839 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3840 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003841 if (!key)
3842 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003843 if (!buffer->dirty)
3844 continue;
3845 buffer->dirty = 0;
3846 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003847 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3848 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003849 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003850 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003851 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003852}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003853EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003854
3855/**
3856 * snd_hda_sequence_write_cache - sequence writes with caching
3857 * @codec: the HDA codec
3858 * @seq: VERB array to send
3859 *
3860 * Send the commands sequentially from the given array.
3861 * Thte commands are recorded on cache for power-save and resume.
3862 * The array must be terminated with NID=0.
3863 */
3864void snd_hda_sequence_write_cache(struct hda_codec *codec,
3865 const struct hda_verb *seq)
3866{
3867 for (; seq->nid; seq++)
3868 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3869 seq->param);
3870}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003871EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003872
Takashi Iwaidc870f32013-01-22 15:24:30 +01003873/**
3874 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3875 * @codec: HD-audio codec
3876 */
3877void snd_hda_codec_flush_cache(struct hda_codec *codec)
3878{
3879 snd_hda_codec_resume_amp(codec);
3880 snd_hda_codec_resume_cache(codec);
3881}
3882EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3883
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003884void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003885 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003886{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003887 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003888 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003889
Takashi Iwaicb53c622007-08-10 17:21:45 +02003890 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003891 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003892 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003893 if (!(wcaps & AC_WCAP_POWER))
3894 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003895 if (codec->power_filter) {
3896 state = codec->power_filter(codec, nid, power_state);
3897 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003898 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003899 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003900 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003901 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003902 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003903}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003904EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3905
3906/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003907 * supported power states check
3908 */
3909static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3910 unsigned int power_state)
3911{
3912 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3913
Mengdong Line037cb42012-08-10 14:11:58 +02003914 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003915 return false;
3916 if (sup & power_state)
3917 return true;
3918 else
3919 return false;
3920}
3921
3922/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003923 * wait until the state is reached, returns the current state
3924 */
3925static unsigned int hda_sync_power_state(struct hda_codec *codec,
3926 hda_nid_t fg,
3927 unsigned int power_state)
3928{
3929 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3930 unsigned int state, actual_state;
3931
3932 for (;;) {
3933 state = snd_hda_codec_read(codec, fg, 0,
3934 AC_VERB_GET_POWER_STATE, 0);
3935 if (state & AC_PWRST_ERROR)
3936 break;
3937 actual_state = (state >> 4) & 0x0f;
3938 if (actual_state == power_state)
3939 break;
3940 if (time_after_eq(jiffies, end_time))
3941 break;
3942 /* wait until the codec reachs to the target state */
3943 msleep(1);
3944 }
3945 return state;
3946}
3947
Takashi Iwai9419ab62013-01-24 17:23:35 +01003948/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003949unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3950 hda_nid_t nid,
3951 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003952{
3953 if (power_state == AC_PWRST_D3 &&
3954 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3955 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3956 int eapd = snd_hda_codec_read(codec, nid, 0,
3957 AC_VERB_GET_EAPD_BTLENABLE, 0);
3958 if (eapd & 0x02)
3959 return AC_PWRST_D0;
3960 }
3961 return power_state;
3962}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003963EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003964
Takashi Iwai432c6412012-08-28 09:59:20 -07003965/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003966 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003967 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003968static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003969 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003970{
Takashi Iwaid8193872012-08-31 07:54:38 -07003971 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003972 int count;
3973 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003974 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003975
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003976 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003977 if (power_state == AC_PWRST_D3) {
3978 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003979 msleep(codec->epss ? 10 : 100);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003980 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003981 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003982
3983 /* repeat power states setting at most 10 times*/
3984 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003985 if (codec->patch_ops.set_power_state)
3986 codec->patch_ops.set_power_state(codec, fg,
3987 power_state);
3988 else {
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003989 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003990 AC_VERB_SET_POWER_STATE,
3991 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003992 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003993 }
3994 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003995 if (!(state & AC_PWRST_ERROR))
3996 break;
3997 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003998
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003999 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004000}
Takashi Iwai54d17402005-11-21 16:33:22 +01004001
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004002/* sync power states of all widgets;
4003 * this is called at the end of codec parsing
4004 */
4005static void sync_power_up_states(struct hda_codec *codec)
4006{
4007 hda_nid_t nid = codec->start_nid;
4008 int i;
4009
Takashi Iwaiba615b82013-03-13 14:47:21 +01004010 /* don't care if no filter is used */
4011 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004012 return;
4013
4014 for (i = 0; i < codec->num_nodes; i++, nid++) {
4015 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004016 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004017 if (!(wcaps & AC_WCAP_POWER))
4018 continue;
4019 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4020 if (target == AC_PWRST_D0)
4021 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004022 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004023 snd_hda_codec_write(codec, nid, 0,
4024 AC_VERB_SET_POWER_STATE, target);
4025 }
4026}
4027
Takashi Iwai11aeff02008-07-30 15:01:46 +02004028#ifdef CONFIG_SND_HDA_HWDEP
4029/* execute additional init verbs */
4030static void hda_exec_init_verbs(struct hda_codec *codec)
4031{
4032 if (codec->init_verbs.list)
4033 snd_hda_sequence_write(codec, codec->init_verbs.list);
4034}
4035#else
4036static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4037#endif
4038
Takashi Iwai2a439522011-07-26 09:52:50 +02004039#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004040/*
4041 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004042 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004043 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004044static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004045{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004046 unsigned int state;
4047
Takashi Iwai989c3182012-11-19 14:14:58 +01004048 codec->in_pm = 1;
4049
Takashi Iwaicb53c622007-08-10 17:21:45 +02004050 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004051 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004052 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004053 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004054 /* Cancel delayed work if we aren't currently running from it. */
4055 if (!in_wq)
4056 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004057 spin_lock(&codec->power_lock);
4058 snd_hda_update_power_acct(codec);
4059 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004060 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004061 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004062 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004063 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004064 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004065 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004066}
4067
Takashi Iwaic370dd62012-12-13 18:30:04 +01004068/* mark all entries of cmd and amp caches dirty */
4069static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4070{
4071 int i;
4072 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4073 struct hda_cache_head *cmd;
4074 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4075 cmd->dirty = 1;
4076 }
4077 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4078 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004079 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004080 amp->head.dirty = 1;
4081 }
4082}
4083
Takashi Iwaicb53c622007-08-10 17:21:45 +02004084/*
4085 * kick up codec; used both from PM and power-save
4086 */
4087static void hda_call_codec_resume(struct hda_codec *codec)
4088{
Takashi Iwai989c3182012-11-19 14:14:58 +01004089 codec->in_pm = 1;
4090
Takashi Iwaic370dd62012-12-13 18:30:04 +01004091 hda_mark_cmd_cache_dirty(codec);
4092
Takashi Iwai7f308302012-05-08 16:52:23 +02004093 /* set as if powered on for avoiding re-entering the resume
4094 * in the resume / power-save sequence
4095 */
4096 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004097 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004098 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004099 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004100 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004101 if (codec->patch_ops.resume)
4102 codec->patch_ops.resume(codec);
4103 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004104 if (codec->patch_ops.init)
4105 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004106 snd_hda_codec_resume_amp(codec);
4107 snd_hda_codec_resume_cache(codec);
4108 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004109
4110 if (codec->jackpoll_interval)
4111 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004112 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004113 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004114
4115 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004116 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004117}
Takashi Iwai2a439522011-07-26 09:52:50 +02004118#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004119
Takashi Iwai54d17402005-11-21 16:33:22 +01004120
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121/**
4122 * snd_hda_build_controls - build mixer controls
4123 * @bus: the BUS
4124 *
4125 * Creates mixer controls for each codec included in the bus.
4126 *
4127 * Returns 0 if successful, otherwise a negative error code.
4128 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004129int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004131 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Takashi Iwai0ba21762007-04-16 11:29:14 +02004133 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004134 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004135 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004136 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004137 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004138 err = snd_hda_codec_reset(codec);
4139 if (err < 0) {
4140 printk(KERN_ERR
4141 "hda_codec: cannot revert codec\n");
4142 return err;
4143 }
4144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004146 return 0;
4147}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004148EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004150/*
4151 * add standard channel maps if not specified
4152 */
4153static int add_std_chmaps(struct hda_codec *codec)
4154{
4155 int i, str, err;
4156
4157 for (i = 0; i < codec->num_pcms; i++) {
4158 for (str = 0; str < 2; str++) {
4159 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4160 struct hda_pcm_stream *hinfo =
4161 &codec->pcm_info[i].stream[str];
4162 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004163 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004164
4165 if (codec->pcm_info[i].own_chmap)
4166 continue;
4167 if (!pcm || !hinfo->substreams)
4168 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004169 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4170 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004171 hinfo->channels_max,
4172 0, &chmap);
4173 if (err < 0)
4174 return err;
4175 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4176 }
4177 }
4178 return 0;
4179}
4180
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004181/* default channel maps for 2.1 speakers;
4182 * since HD-audio supports only stereo, odd number channels are omitted
4183 */
4184const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4185 { .channels = 2,
4186 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4187 { .channels = 4,
4188 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4189 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4190 { }
4191};
4192EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4193
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004194int snd_hda_codec_build_controls(struct hda_codec *codec)
4195{
4196 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004197 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004198 /* continue to initialize... */
4199 if (codec->patch_ops.init)
4200 err = codec->patch_ops.init(codec);
4201 if (!err && codec->patch_ops.build_controls)
4202 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004203 if (err < 0)
4204 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004205
4206 /* we create chmaps here instead of build_pcms */
4207 err = add_std_chmaps(codec);
4208 if (err < 0)
4209 return err;
4210
David Henningsson26a6cb62012-10-09 15:04:21 +02004211 if (codec->jackpoll_interval)
4212 hda_jackpoll_work(&codec->jackpoll_work.work);
4213 else
4214 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004215 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return 0;
4217}
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219/*
4220 * stream formats
4221 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004222struct hda_rate_tbl {
4223 unsigned int hz;
4224 unsigned int alsa_bits;
4225 unsigned int hda_fmt;
4226};
4227
Takashi Iwai92f10b32010-08-03 14:21:00 +02004228/* rate = base * mult / div */
4229#define HDA_RATE(base, mult, div) \
4230 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4231 (((div) - 1) << AC_FMT_DIV_SHIFT))
4232
Takashi Iwaibefdf312005-08-22 13:57:55 +02004233static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004235
4236 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004237 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4238 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4239 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4240 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4241 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4242 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4243 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4244 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4245 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4246 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4247 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004248#define AC_PAR_PCM_RATE_BITS 11
4249 /* up to bits 10, 384kHZ isn't supported properly */
4250
4251 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004252 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004253
Takashi Iwaibefdf312005-08-22 13:57:55 +02004254 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255};
4256
4257/**
4258 * snd_hda_calc_stream_format - calculate format bitset
4259 * @rate: the sample rate
4260 * @channels: the number of channels
4261 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4262 * @maxbps: the max. bps
4263 *
4264 * Calculate the format bitset from the given rate, channels and th PCM format.
4265 *
4266 * Return zero if invalid.
4267 */
4268unsigned int snd_hda_calc_stream_format(unsigned int rate,
4269 unsigned int channels,
4270 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004271 unsigned int maxbps,
4272 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
4274 int i;
4275 unsigned int val = 0;
4276
Takashi Iwaibefdf312005-08-22 13:57:55 +02004277 for (i = 0; rate_bits[i].hz; i++)
4278 if (rate_bits[i].hz == rate) {
4279 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 break;
4281 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004282 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 snd_printdd("invalid rate %d\n", rate);
4284 return 0;
4285 }
4286
4287 if (channels == 0 || channels > 8) {
4288 snd_printdd("invalid channels %d\n", channels);
4289 return 0;
4290 }
4291 val |= channels - 1;
4292
4293 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004294 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004295 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004296 break;
4297 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004298 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 case 20:
4301 case 24:
4302 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004303 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004304 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004306 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004308 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 break;
4310 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004311 snd_printdd("invalid format width %d\n",
4312 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return 0;
4314 }
4315
Anssi Hannula32c168c2010-08-03 13:28:57 +03004316 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004317 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return val;
4320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004321EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004323static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4324 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004325{
4326 unsigned int val = 0;
4327 if (nid != codec->afg &&
4328 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4329 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4330 if (!val || val == -1)
4331 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4332 if (!val || val == -1)
4333 return 0;
4334 return val;
4335}
4336
4337static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4338{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004339 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004340 get_pcm_param);
4341}
4342
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004343static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4344 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004345{
4346 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4347 if (!streams || streams == -1)
4348 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4349 if (!streams || streams == -1)
4350 return 0;
4351 return streams;
4352}
4353
4354static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4355{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004356 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004357 get_stream_param);
4358}
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360/**
4361 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4362 * @codec: the HDA codec
4363 * @nid: NID to query
4364 * @ratesp: the pointer to store the detected rate bitflags
4365 * @formatsp: the pointer to store the detected formats
4366 * @bpsp: the pointer to store the detected format widths
4367 *
4368 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4369 * or @bsps argument is ignored.
4370 *
4371 * Returns 0 if successful, otherwise a negative error code.
4372 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004373int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4375{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004376 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004378 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004379 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
4381 if (ratesp) {
4382 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004383 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004385 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004387 if (rates == 0) {
4388 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4389 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4390 nid, val,
4391 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4392 return -EIO;
4393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 *ratesp = rates;
4395 }
4396
4397 if (formatsp || bpsp) {
4398 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004399 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004401 streams = query_stream_param(codec, nid);
4402 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 bps = 0;
4406 if (streams & AC_SUPFMT_PCM) {
4407 if (val & AC_SUPPCM_BITS_8) {
4408 formats |= SNDRV_PCM_FMTBIT_U8;
4409 bps = 8;
4410 }
4411 if (val & AC_SUPPCM_BITS_16) {
4412 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4413 bps = 16;
4414 }
4415 if (wcaps & AC_WCAP_DIGITAL) {
4416 if (val & AC_SUPPCM_BITS_32)
4417 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4418 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4419 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4420 if (val & AC_SUPPCM_BITS_24)
4421 bps = 24;
4422 else if (val & AC_SUPPCM_BITS_20)
4423 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004424 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4425 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4427 if (val & AC_SUPPCM_BITS_32)
4428 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 else if (val & AC_SUPPCM_BITS_24)
4430 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004431 else if (val & AC_SUPPCM_BITS_20)
4432 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004435#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004436 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004438 if (!bps)
4439 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004440 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004441#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004442 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004443 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 /* temporary hack: we have still no proper support
4445 * for the direct AC3 stream...
4446 */
4447 formats |= SNDRV_PCM_FMTBIT_U8;
4448 bps = 8;
4449 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004450 if (formats == 0) {
4451 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4452 "(nid=0x%x, val=0x%x, ovrd=%i, "
4453 "streams=0x%x)\n",
4454 nid, val,
4455 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4456 streams);
4457 return -EIO;
4458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (formatsp)
4460 *formatsp = formats;
4461 if (bpsp)
4462 *bpsp = bps;
4463 }
4464
4465 return 0;
4466}
Stephen Warren384a48d2011-06-01 11:14:21 -06004467EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004470 * snd_hda_is_supported_format - Check the validity of the format
4471 * @codec: HD-audio codec
4472 * @nid: NID to check
4473 * @format: the HD-audio format value to check
4474 *
4475 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 *
4477 * Returns 1 if supported, 0 if not.
4478 */
4479int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4480 unsigned int format)
4481{
4482 int i;
4483 unsigned int val = 0, rate, stream;
4484
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004485 val = query_pcm_param(codec, nid);
4486 if (!val)
4487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004490 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004491 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (val & (1 << i))
4493 break;
4494 return 0;
4495 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004496 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return 0;
4498
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004499 stream = query_stream_param(codec, nid);
4500 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return 0;
4502
4503 if (stream & AC_SUPFMT_PCM) {
4504 switch (format & 0xf0) {
4505 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004506 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return 0;
4508 break;
4509 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004510 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return 0;
4512 break;
4513 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004514 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return 0;
4516 break;
4517 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004518 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return 0;
4520 break;
4521 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004522 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return 0;
4524 break;
4525 default:
4526 return 0;
4527 }
4528 } else {
4529 /* FIXME: check for float32 and AC3? */
4530 }
4531
4532 return 1;
4533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004534EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536/*
4537 * PCM stuff
4538 */
4539static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4540 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004541 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
4543 return 0;
4544}
4545
4546static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4547 struct hda_codec *codec,
4548 unsigned int stream_tag,
4549 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004550 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
4552 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4553 return 0;
4554}
4555
4556static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4557 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004558 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
Takashi Iwai888afa12008-03-18 09:57:50 +01004560 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return 0;
4562}
4563
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004564static int set_pcm_default_values(struct hda_codec *codec,
4565 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004567 int err;
4568
Takashi Iwai0ba21762007-04-16 11:29:14 +02004569 /* query support PCM information from the given NID */
4570 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004571 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004572 info->rates ? NULL : &info->rates,
4573 info->formats ? NULL : &info->formats,
4574 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004575 if (err < 0)
4576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
4578 if (info->ops.open == NULL)
4579 info->ops.open = hda_pcm_default_open_close;
4580 if (info->ops.close == NULL)
4581 info->ops.close = hda_pcm_default_open_close;
4582 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004583 if (snd_BUG_ON(!info->nid))
4584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 info->ops.prepare = hda_pcm_default_prepare;
4586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004588 if (snd_BUG_ON(!info->nid))
4589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 info->ops.cleanup = hda_pcm_default_cleanup;
4591 }
4592 return 0;
4593}
4594
Takashi Iwaieb541332010-08-06 13:48:11 +02004595/*
4596 * codec prepare/cleanup entries
4597 */
4598int snd_hda_codec_prepare(struct hda_codec *codec,
4599 struct hda_pcm_stream *hinfo,
4600 unsigned int stream,
4601 unsigned int format,
4602 struct snd_pcm_substream *substream)
4603{
4604 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004605 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004606 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4607 if (ret >= 0)
4608 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004609 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004610 return ret;
4611}
4612EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4613
4614void snd_hda_codec_cleanup(struct hda_codec *codec,
4615 struct hda_pcm_stream *hinfo,
4616 struct snd_pcm_substream *substream)
4617{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004618 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004619 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004620 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004621}
4622EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4623
Takashi Iwaid5191e52009-11-16 14:58:17 +01004624/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004625const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4626 "Audio", "SPDIF", "HDMI", "Modem"
4627};
4628
Takashi Iwai176d5332008-07-30 15:01:44 +02004629/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004630 * get the empty PCM device number to assign
4631 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004632static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004633{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004634 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004635 /* assigned to static slots up to dev#10; if more needed, assign
4636 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4637 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004638 static int audio_idx[HDA_PCM_NTYPES][5] = {
4639 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4640 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004641 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004642 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004643 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004644 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004645
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004646 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004647 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4648 return -EINVAL;
4649 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004650
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004651 for (i = 0; audio_idx[type][i] >= 0; i++) {
4652#ifndef CONFIG_SND_DYNAMIC_MINORS
4653 if (audio_idx[type][i] >= 8)
4654 break;
4655#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004656 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4657 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004658 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004659
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004660#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004661 /* non-fixed slots starting from 10 */
4662 for (i = 10; i < 32; i++) {
4663 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4664 return i;
4665 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004666#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004667
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004668 snd_printk(KERN_WARNING "Too many %s devices\n",
4669 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004670#ifndef CONFIG_SND_DYNAMIC_MINORS
4671 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4672#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004673 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004674}
4675
4676/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004677 * attach a new PCM stream
4678 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004679static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004680{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004681 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004682 struct hda_pcm_stream *info;
4683 int stream, err;
4684
Takashi Iwaib91f0802008-11-04 08:43:08 +01004685 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004686 return -EINVAL;
4687 for (stream = 0; stream < 2; stream++) {
4688 info = &pcm->stream[stream];
4689 if (info->substreams) {
4690 err = set_pcm_default_values(codec, info);
4691 if (err < 0)
4692 return err;
4693 }
4694 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004695 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004696}
4697
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004698/* assign all PCMs of the given codec */
4699int snd_hda_codec_build_pcms(struct hda_codec *codec)
4700{
4701 unsigned int pcm;
4702 int err;
4703
4704 if (!codec->num_pcms) {
4705 if (!codec->patch_ops.build_pcms)
4706 return 0;
4707 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004708 if (err < 0) {
4709 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004710 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004711 err = snd_hda_codec_reset(codec);
4712 if (err < 0) {
4713 printk(KERN_ERR
4714 "hda_codec: cannot revert codec\n");
4715 return err;
4716 }
4717 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004718 }
4719 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4720 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4721 int dev;
4722
4723 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004724 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004725
4726 if (!cpcm->pcm) {
4727 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4728 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004729 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004730 cpcm->device = dev;
4731 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004732 if (err < 0) {
4733 printk(KERN_ERR "hda_codec: cannot attach "
4734 "PCM stream %d for codec #%d\n",
4735 dev, codec->addr);
4736 continue; /* no fatal error */
4737 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004738 }
4739 }
4740 return 0;
4741}
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/**
4744 * snd_hda_build_pcms - build PCM information
4745 * @bus: the BUS
4746 *
4747 * Create PCM information for each codec included in the bus.
4748 *
4749 * The build_pcms codec patch is requested to set up codec->num_pcms and
4750 * codec->pcm_info properly. The array is referred by the top-level driver
4751 * to create its PCM instances.
4752 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4753 * callback.
4754 *
4755 * At least, substreams, channels_min and channels_max must be filled for
4756 * each stream. substreams = 0 indicates that the stream doesn't exist.
4757 * When rates and/or formats are zero, the supported values are queried
4758 * from the given nid. The nid is used also by the default ops.prepare
4759 * and ops.cleanup callbacks.
4760 *
4761 * The driver needs to call ops.open in its open callback. Similarly,
4762 * ops.close is supposed to be called in the close callback.
4763 * ops.prepare should be called in the prepare or hw_params callback
4764 * with the proper parameters for set up.
4765 * ops.cleanup should be called in hw_free for clean up of streams.
4766 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004767 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004769int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004771 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Takashi Iwai0ba21762007-04-16 11:29:14 +02004773 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004774 int err = snd_hda_codec_build_pcms(codec);
4775 if (err < 0)
4776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
4778 return 0;
4779}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004780EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782/**
4783 * snd_hda_check_board_config - compare the current codec with the config table
4784 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004785 * @num_configs: number of config enums
4786 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 * @tbl: configuration table, terminated by null entries
4788 *
4789 * Compares the modelname or PCI subsystem id of the current codec with the
4790 * given configuration table. If a matching entry is found, returns its
4791 * config value (supposed to be 0 or positive).
4792 *
4793 * If no entries are matching, the function returns a negative value.
4794 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004795int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004796 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004797 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004799 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004800 int i;
4801 for (i = 0; i < num_configs; i++) {
4802 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004803 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004804 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4805 "selected\n", models[i]);
4806 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
4808 }
4809 }
4810
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004811 if (!codec->bus->pci || !tbl)
4812 return -1;
4813
4814 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4815 if (!tbl)
4816 return -1;
4817 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004818#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004819 char tmp[10];
4820 const char *model = NULL;
4821 if (models)
4822 model = models[tbl->value];
4823 if (!model) {
4824 sprintf(tmp, "#%d", tbl->value);
4825 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004827 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4828 "for config %x:%x (%s)\n",
4829 model, tbl->subvendor, tbl->subdevice,
4830 (tbl->name ? tbl->name : "Unknown device"));
4831#endif
4832 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
4834 return -1;
4835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004836EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004839 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004840 subsystem ID with the
4841 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004842
4843 This is important for Gateway notebooks with SB450 HDA Audio
4844 where the vendor ID of the PCI device is:
4845 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4846 and the vendor/subvendor are found only at the codec.
4847
4848 * @codec: the HDA codec
4849 * @num_configs: number of config enums
4850 * @models: array of model name strings
4851 * @tbl: configuration table, terminated by null entries
4852 *
4853 * Compares the modelname or PCI subsystem id of the current codec with the
4854 * given configuration table. If a matching entry is found, returns its
4855 * config value (supposed to be 0 or positive).
4856 *
4857 * If no entries are matching, the function returns a negative value.
4858 */
4859int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004860 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004861 const struct snd_pci_quirk *tbl)
4862{
4863 const struct snd_pci_quirk *q;
4864
4865 /* Search for codec ID */
4866 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004867 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4868 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4869 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004870 break;
4871 }
4872
4873 if (!q->subvendor)
4874 return -1;
4875
4876 tbl = q;
4877
4878 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004879#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004880 char tmp[10];
4881 const char *model = NULL;
4882 if (models)
4883 model = models[tbl->value];
4884 if (!model) {
4885 sprintf(tmp, "#%d", tbl->value);
4886 model = tmp;
4887 }
4888 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4889 "for config %x:%x (%s)\n",
4890 model, tbl->subvendor, tbl->subdevice,
4891 (tbl->name ? tbl->name : "Unknown device"));
4892#endif
4893 return tbl->value;
4894 }
4895 return -1;
4896}
4897EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4898
4899/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 * snd_hda_add_new_ctls - create controls from the array
4901 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004902 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 *
4904 * This helper function creates and add new controls in the given array.
4905 * The array must be terminated with an empty entry as terminator.
4906 *
4907 * Returns 0 if successful, or a negative error code.
4908 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004909int snd_hda_add_new_ctls(struct hda_codec *codec,
4910 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004915 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004916 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004917 if (knew->iface == -1) /* skip this codec private value */
4918 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004919 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004920 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004921 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004922 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004923 if (addr > 0)
4924 kctl->id.device = addr;
4925 if (idx > 0)
4926 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004927 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004928 if (!err)
4929 break;
4930 /* try first with another device index corresponding to
4931 * the codec addr; if it still fails (or it's the
4932 * primary codec), then try another control index
4933 */
4934 if (!addr && codec->addr)
4935 addr = codec->addr;
4936 else if (!idx && !knew->index) {
4937 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004938 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004939 if (idx <= 0)
4940 return err;
4941 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004942 return err;
4943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
4945 return 0;
4946}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004947EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Takashi Iwai83012a72012-08-24 18:38:08 +02004949#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004950static void hda_power_work(struct work_struct *work)
4951{
4952 struct hda_codec *codec =
4953 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004954 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004955 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004956
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004957 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004958 if (codec->power_transition > 0) { /* during power-up sequence? */
4959 spin_unlock(&codec->power_lock);
4960 return;
4961 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004962 if (!codec->power_on || codec->power_count) {
4963 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004964 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004965 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004966 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004967 spin_unlock(&codec->power_lock);
4968
Dylan Reidd17344b2012-09-28 15:57:01 -07004969 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004970 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4971 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004972}
4973
4974static void hda_keep_power_on(struct hda_codec *codec)
4975{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004976 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004977 codec->power_count++;
4978 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004979 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004980 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004981 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004982}
4983
Takashi Iwaid5191e52009-11-16 14:58:17 +01004984/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004985void snd_hda_update_power_acct(struct hda_codec *codec)
4986{
4987 unsigned long delta = jiffies - codec->power_jiffies;
4988 if (codec->power_on)
4989 codec->power_on_acct += delta;
4990 else
4991 codec->power_off_acct += delta;
4992 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004993}
4994
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004995/* Transition to powered up, if wait_power_down then wait for a pending
4996 * transition to D3 to complete. A pending D3 transition is indicated
4997 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004998/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004999static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005000{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005001 /* Return if power_on or transitioning to power_on, unless currently
5002 * powering down. */
5003 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005004 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005005 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005006 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005007
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005008 cancel_delayed_work_sync(&codec->power_work);
5009
5010 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005011 /* If the power down delayed work was cancelled above before starting,
5012 * then there is no need to go through power up here.
5013 */
5014 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005015 if (codec->power_transition < 0)
5016 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005017 return;
5018 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005019
Takashi Iwaid66fee52011-08-02 15:39:31 +02005020 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005021 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005022 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005023 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005024 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005025 spin_unlock(&codec->power_lock);
5026
Takashi Iwaicb53c622007-08-10 17:21:45 +02005027 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005028
5029 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005030 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005031}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005032
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005033#define power_save(codec) \
5034 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005035
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005036/* Transition to powered down */
5037static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005038{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005039 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005040 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005041
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005042 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005043 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005044 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005045 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005046 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005047}
5048
5049/**
5050 * snd_hda_power_save - Power-up/down/sync the codec
5051 * @codec: HD-audio codec
5052 * @delta: the counter delta to change
5053 *
5054 * Change the power-up counter via @delta, and power up or down the hardware
5055 * appropriately. For the power-down, queue to the delayed action.
5056 * Passing zero to @delta means to synchronize the power state.
5057 */
5058void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5059{
5060 spin_lock(&codec->power_lock);
5061 codec->power_count += delta;
5062 trace_hda_power_count(codec);
5063 if (delta > 0)
5064 __snd_hda_power_up(codec, d3wait);
5065 else
5066 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005067 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005068}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005069EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005070
Takashi Iwaid5191e52009-11-16 14:58:17 +01005071/**
5072 * snd_hda_check_amp_list_power - Check the amp list and update the power
5073 * @codec: HD-audio codec
5074 * @check: the object containing an AMP list and the status
5075 * @nid: NID to check / update
5076 *
5077 * Check whether the given NID is in the amp list. If it's in the list,
5078 * check the current AMP status, and update the the power-status according
5079 * to the mute status.
5080 *
5081 * This function is supposed to be set or called from the check_power_status
5082 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005083 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005084int snd_hda_check_amp_list_power(struct hda_codec *codec,
5085 struct hda_loopback_check *check,
5086 hda_nid_t nid)
5087{
Takashi Iwai031024e2011-05-02 11:29:30 +02005088 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005089 int ch, v;
5090
5091 if (!check->amplist)
5092 return 0;
5093 for (p = check->amplist; p->nid; p++) {
5094 if (p->nid == nid)
5095 break;
5096 }
5097 if (!p->nid)
5098 return 0; /* nothing changed */
5099
5100 for (p = check->amplist; p->nid; p++) {
5101 for (ch = 0; ch < 2; ch++) {
5102 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5103 p->idx);
5104 if (!(v & HDA_AMP_MUTE) && v > 0) {
5105 if (!check->power_on) {
5106 check->power_on = 1;
5107 snd_hda_power_up(codec);
5108 }
5109 return 1;
5110 }
5111 }
5112 }
5113 if (check->power_on) {
5114 check->power_on = 0;
5115 snd_hda_power_down(codec);
5116 }
5117 return 0;
5118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005119EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005120
Mengdong Lin12edb892013-11-26 23:32:23 -05005121static void hda_suspend_work(struct work_struct *work)
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005122{
5123 struct hda_codec *codec =
Mengdong Lin12edb892013-11-26 23:32:23 -05005124 container_of(work, struct hda_codec, suspend_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005125
5126 hda_call_codec_suspend(codec, false);
5127}
Mengdong Lin12edb892013-11-26 23:32:23 -05005128
5129static void hda_resume_work(struct work_struct *work)
5130{
5131 struct hda_codec *codec =
5132 container_of(work, struct hda_codec, resume_work);
5133
5134 hda_call_codec_resume(codec);
5135}
Takashi Iwaicb53c622007-08-10 17:21:45 +02005136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005138/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005139 * Channel mode helper
5140 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005141
5142/**
5143 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5144 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005145int snd_hda_ch_mode_info(struct hda_codec *codec,
5146 struct snd_ctl_elem_info *uinfo,
5147 const struct hda_channel_mode *chmode,
5148 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005149{
5150 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5151 uinfo->count = 1;
5152 uinfo->value.enumerated.items = num_chmodes;
5153 if (uinfo->value.enumerated.item >= num_chmodes)
5154 uinfo->value.enumerated.item = num_chmodes - 1;
5155 sprintf(uinfo->value.enumerated.name, "%dch",
5156 chmode[uinfo->value.enumerated.item].channels);
5157 return 0;
5158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005159EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005160
Takashi Iwaid5191e52009-11-16 14:58:17 +01005161/**
5162 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5163 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005164int snd_hda_ch_mode_get(struct hda_codec *codec,
5165 struct snd_ctl_elem_value *ucontrol,
5166 const struct hda_channel_mode *chmode,
5167 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005168 int max_channels)
5169{
5170 int i;
5171
5172 for (i = 0; i < num_chmodes; i++) {
5173 if (max_channels == chmode[i].channels) {
5174 ucontrol->value.enumerated.item[0] = i;
5175 break;
5176 }
5177 }
5178 return 0;
5179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005180EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005181
Takashi Iwaid5191e52009-11-16 14:58:17 +01005182/**
5183 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5184 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005185int snd_hda_ch_mode_put(struct hda_codec *codec,
5186 struct snd_ctl_elem_value *ucontrol,
5187 const struct hda_channel_mode *chmode,
5188 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005189 int *max_channelsp)
5190{
5191 unsigned int mode;
5192
5193 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005194 if (mode >= num_chmodes)
5195 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005196 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005197 return 0;
5198 /* change the current channel setting */
5199 *max_channelsp = chmode[mode].channels;
5200 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005201 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005202 return 1;
5203}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005204EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206/*
5207 * input MUX helper
5208 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005209
5210/**
5211 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5212 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005213int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5214 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215{
5216 unsigned int index;
5217
5218 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5219 uinfo->count = 1;
5220 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005221 if (!imux->num_items)
5222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 index = uinfo->value.enumerated.item;
5224 if (index >= imux->num_items)
5225 index = imux->num_items - 1;
5226 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5227 return 0;
5228}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005229EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
Takashi Iwaid5191e52009-11-16 14:58:17 +01005231/**
5232 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5233 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005234int snd_hda_input_mux_put(struct hda_codec *codec,
5235 const struct hda_input_mux *imux,
5236 struct snd_ctl_elem_value *ucontrol,
5237 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 unsigned int *cur_val)
5239{
5240 unsigned int idx;
5241
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005242 if (!imux->num_items)
5243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 idx = ucontrol->value.enumerated.item[0];
5245 if (idx >= imux->num_items)
5246 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005247 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005249 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5250 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 *cur_val = idx;
5252 return 1;
5253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005254EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256
5257/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005258 * process kcontrol info callback of a simple string enum array
5259 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5260 */
5261int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5262 struct snd_ctl_elem_info *uinfo,
5263 int num_items, const char * const *texts)
5264{
5265 static const char * const texts_default[] = {
5266 "Disabled", "Enabled"
5267 };
5268
5269 if (!texts || !num_items) {
5270 num_items = 2;
5271 texts = texts_default;
5272 }
5273
5274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5275 uinfo->count = 1;
5276 uinfo->value.enumerated.items = num_items;
5277 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5278 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5279 strcpy(uinfo->value.enumerated.name,
5280 texts[uinfo->value.enumerated.item]);
5281 return 0;
5282}
5283EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5284
5285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 * Multi-channel / digital-out PCM helper functions
5287 */
5288
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005289/* setup SPDIF output stream */
5290static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5291 unsigned int stream_tag, unsigned int format)
5292{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005293 struct hda_spdif_out *spdif;
5294 unsigned int curr_fmt;
5295 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005296
Laurence Darby3bef1c32012-11-03 17:00:06 +00005297 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5298 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5299 AC_VERB_GET_STREAM_FORMAT, 0);
5300 reset = codec->spdif_status_reset &&
5301 (spdif->ctls & AC_DIG1_ENABLE) &&
5302 curr_fmt != format;
5303
5304 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5305 updated */
5306 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005307 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005308 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005309 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005310 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005311 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005312 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005313 for (d = codec->slave_dig_outs; *d; d++)
5314 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5315 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005316 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005317 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005318 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005319 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005320 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005321}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005322
Takashi Iwai2f728532008-09-25 16:32:41 +02005323static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5324{
5325 snd_hda_codec_cleanup_stream(codec, nid);
5326 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005327 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005328 for (d = codec->slave_dig_outs; *d; d++)
5329 snd_hda_codec_cleanup_stream(codec, *d);
5330 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005331}
5332
Takashi Iwaid5191e52009-11-16 14:58:17 +01005333/**
5334 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5335 * @bus: HD-audio bus
5336 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005337void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5338{
5339 struct hda_codec *codec;
5340
5341 if (!bus)
5342 return;
5343 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005344 if (hda_codec_is_power_on(codec) &&
5345 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005346 codec->patch_ops.reboot_notify(codec);
5347 }
5348}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005349EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005350
Takashi Iwaid5191e52009-11-16 14:58:17 +01005351/**
5352 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005354int snd_hda_multi_out_dig_open(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);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005358 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5359 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005360 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005362 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 return 0;
5364}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005365EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Takashi Iwaid5191e52009-11-16 14:58:17 +01005367/**
5368 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5369 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005370int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5371 struct hda_multi_out *mout,
5372 unsigned int stream_tag,
5373 unsigned int format,
5374 struct snd_pcm_substream *substream)
5375{
5376 mutex_lock(&codec->spdif_mutex);
5377 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5378 mutex_unlock(&codec->spdif_mutex);
5379 return 0;
5380}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005381EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005382
Takashi Iwaid5191e52009-11-16 14:58:17 +01005383/**
5384 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5385 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005386int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5387 struct hda_multi_out *mout)
5388{
5389 mutex_lock(&codec->spdif_mutex);
5390 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5391 mutex_unlock(&codec->spdif_mutex);
5392 return 0;
5393}
5394EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5395
Takashi Iwaid5191e52009-11-16 14:58:17 +01005396/**
5397 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005399int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5400 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Ingo Molnar62932df2006-01-16 16:34:20 +01005402 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005404 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 return 0;
5406}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005407EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Takashi Iwaid5191e52009-11-16 14:58:17 +01005409/**
5410 * snd_hda_multi_out_analog_open - open analog outputs
5411 *
5412 * Open analog outputs and set up the hw-constraints.
5413 * If the digital outputs can be opened as slave, open the digital
5414 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005416int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5417 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005418 struct snd_pcm_substream *substream,
5419 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
Takashi Iwai9a081602008-02-12 18:37:26 +01005421 struct snd_pcm_runtime *runtime = substream->runtime;
5422 runtime->hw.channels_max = mout->max_channels;
5423 if (mout->dig_out_nid) {
5424 if (!mout->analog_rates) {
5425 mout->analog_rates = hinfo->rates;
5426 mout->analog_formats = hinfo->formats;
5427 mout->analog_maxbps = hinfo->maxbps;
5428 } else {
5429 runtime->hw.rates = mout->analog_rates;
5430 runtime->hw.formats = mout->analog_formats;
5431 hinfo->maxbps = mout->analog_maxbps;
5432 }
5433 if (!mout->spdif_rates) {
5434 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5435 &mout->spdif_rates,
5436 &mout->spdif_formats,
5437 &mout->spdif_maxbps);
5438 }
5439 mutex_lock(&codec->spdif_mutex);
5440 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005441 if ((runtime->hw.rates & mout->spdif_rates) &&
5442 (runtime->hw.formats & mout->spdif_formats)) {
5443 runtime->hw.rates &= mout->spdif_rates;
5444 runtime->hw.formats &= mout->spdif_formats;
5445 if (mout->spdif_maxbps < hinfo->maxbps)
5446 hinfo->maxbps = mout->spdif_maxbps;
5447 } else {
5448 mout->share_spdif = 0;
5449 /* FIXME: need notify? */
5450 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005451 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005452 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5455 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5456}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005457EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Takashi Iwaid5191e52009-11-16 14:58:17 +01005459/**
5460 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5461 *
5462 * Set up the i/o for analog out.
5463 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005465int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5466 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 unsigned int stream_tag,
5468 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005469 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
Takashi Iwaidda14412011-05-02 11:29:30 +02005471 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005473 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 int i;
5475
Ingo Molnar62932df2006-01-16 16:34:20 +01005476 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005477 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005478 if (mout->dig_out_nid && mout->share_spdif &&
5479 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005481 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5482 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005483 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005485 setup_dig_out_stream(codec, mout->dig_out_nid,
5486 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 } else {
5488 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005489 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 }
5491 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005492 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
5494 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005495 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5496 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005497 if (!mout->no_share_stream &&
5498 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005500 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5501 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005502 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005503 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5504 if (!mout->no_share_stream && mout->hp_out_nid[i])
5505 snd_hda_codec_setup_stream(codec,
5506 mout->hp_out_nid[i],
5507 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 /* surrounds */
5510 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005511 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005512 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5513 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005514 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005515 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5516 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005518
5519 /* extra surrounds */
5520 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5521 int ch = 0;
5522 if (!mout->extra_out_nid[i])
5523 break;
5524 if (chs >= (i + 1) * 2)
5525 ch = i * 2;
5526 else if (!mout->no_share_stream)
5527 break;
5528 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5529 stream_tag, ch, format);
5530 }
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return 0;
5533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005534EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Takashi Iwaid5191e52009-11-16 14:58:17 +01005536/**
5537 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005539int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5540 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
Takashi Iwaidda14412011-05-02 11:29:30 +02005542 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 int i;
5544
5545 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005546 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005548 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005549 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5550 if (mout->hp_out_nid[i])
5551 snd_hda_codec_cleanup_stream(codec,
5552 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005553 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5554 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005555 snd_hda_codec_cleanup_stream(codec,
5556 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005557 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005559 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 mout->dig_out_used = 0;
5561 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005562 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 return 0;
5564}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005565EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Takashi Iwai47408602012-04-20 13:06:53 +02005567/**
5568 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5569 *
5570 * Guess the suitable VREF pin bits to be set as the pin-control value.
5571 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5572 */
5573unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5574{
5575 unsigned int pincap;
5576 unsigned int oldval;
5577 oldval = snd_hda_codec_read(codec, pin, 0,
5578 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5579 pincap = snd_hda_query_pin_caps(codec, pin);
5580 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5581 /* Exception: if the default pin setup is vref50, we give it priority */
5582 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5583 return AC_PINCTL_VREF_80;
5584 else if (pincap & AC_PINCAP_VREF_50)
5585 return AC_PINCTL_VREF_50;
5586 else if (pincap & AC_PINCAP_VREF_100)
5587 return AC_PINCTL_VREF_100;
5588 else if (pincap & AC_PINCAP_VREF_GRD)
5589 return AC_PINCTL_VREF_GRD;
5590 return AC_PINCTL_VREF_HIZ;
5591}
5592EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5593
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005594/* correct the pin ctl value for matching with the pin cap */
5595unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5596 hda_nid_t pin, unsigned int val)
5597{
5598 static unsigned int cap_lists[][2] = {
5599 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5600 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5601 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5602 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5603 };
5604 unsigned int cap;
5605
5606 if (!val)
5607 return 0;
5608 cap = snd_hda_query_pin_caps(codec, pin);
5609 if (!cap)
5610 return val; /* don't know what to do... */
5611
5612 if (val & AC_PINCTL_OUT_EN) {
5613 if (!(cap & AC_PINCAP_OUT))
5614 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5615 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5616 val &= ~AC_PINCTL_HP_EN;
5617 }
5618
5619 if (val & AC_PINCTL_IN_EN) {
5620 if (!(cap & AC_PINCAP_IN))
5621 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5622 else {
5623 unsigned int vcap, vref;
5624 int i;
5625 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5626 vref = val & AC_PINCTL_VREFEN;
5627 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5628 if (vref == cap_lists[i][0] &&
5629 !(vcap & cap_lists[i][1])) {
5630 if (i == ARRAY_SIZE(cap_lists) - 1)
5631 vref = AC_PINCTL_VREF_HIZ;
5632 else
5633 vref = cap_lists[i + 1][0];
5634 }
5635 }
5636 val &= ~AC_PINCTL_VREFEN;
5637 val |= vref;
5638 }
5639 }
5640
5641 return val;
5642}
5643EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5644
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005645int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5646 unsigned int val, bool cached)
5647{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005648 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005649 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005650 if (cached)
5651 return snd_hda_codec_update_cache(codec, pin, 0,
5652 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5653 else
5654 return snd_hda_codec_write(codec, pin, 0,
5655 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5656}
5657EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5658
Takashi Iwai990061c2010-09-09 22:08:44 +02005659/**
5660 * snd_hda_add_imux_item - Add an item to input_mux
5661 *
5662 * When the same label is used already in the existing items, the number
5663 * suffix is appended to the label. This label index number is stored
5664 * to type_idx when non-NULL pointer is given.
5665 */
Takashi Iwai10a20af72010-09-09 16:28:02 +02005666int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5667 int index, int *type_idx)
5668{
5669 int i, label_idx = 0;
5670 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5671 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5672 return -EINVAL;
5673 }
5674 for (i = 0; i < imux->num_items; i++) {
5675 if (!strncmp(label, imux->items[i].label, strlen(label)))
5676 label_idx++;
5677 }
5678 if (type_idx)
5679 *type_idx = label_idx;
5680 if (label_idx > 0)
5681 snprintf(imux->items[imux->num_items].label,
5682 sizeof(imux->items[imux->num_items].label),
5683 "%s %d", label, label_idx);
5684 else
5685 strlcpy(imux->items[imux->num_items].label, label,
5686 sizeof(imux->items[imux->num_items].label));
5687 imux->items[imux->num_items].index = index;
5688 imux->num_items++;
5689 return 0;
5690}
5691EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005692
Takashi Iwai4a471b72005-12-07 13:56:29 +01005693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694#ifdef CONFIG_PM
5695/*
5696 * power management
5697 */
5698
5699/**
5700 * snd_hda_suspend - suspend the codecs
5701 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 *
5703 * Returns 0 if successful.
5704 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005705int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005707 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Takashi Iwai0ba21762007-04-16 11:29:14 +02005709 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005710 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005711 if (hda_codec_is_power_on(codec)) {
5712 if (bus->num_codecs > 1)
Mengdong Lin12edb892013-11-26 23:32:23 -05005713 queue_work(bus->pm_wq, &codec->suspend_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005714 else
5715 hda_call_codec_suspend(codec, false);
5716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005718
5719 if (bus->num_codecs > 1)
5720 flush_workqueue(bus->pm_wq);
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 return 0;
5723}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005724EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726/**
5727 * snd_hda_resume - resume the codecs
5728 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 *
5730 * Returns 0 if successful.
5731 */
5732int snd_hda_resume(struct hda_bus *bus)
5733{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005734 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Takashi Iwai0ba21762007-04-16 11:29:14 +02005736 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005737 if (bus->num_codecs > 1)
5738 queue_work(bus->pm_wq, &codec->resume_work);
5739 else
5740 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005742
5743 if (bus->num_codecs > 1)
5744 flush_workqueue(bus->pm_wq);
5745
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 return 0;
5747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005748EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005749#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005750
5751/*
5752 * generic arrays
5753 */
5754
Takashi Iwaid5191e52009-11-16 14:58:17 +01005755/**
5756 * snd_array_new - get a new element from the given array
5757 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005758 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005759 * Get a new element from the given array. If it exceeds the
5760 * pre-allocated array size, re-allocate the array.
5761 *
5762 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005763 */
5764void *snd_array_new(struct snd_array *array)
5765{
Takashi Iwai12f17712012-10-10 08:59:14 +02005766 if (snd_BUG_ON(!array->elem_size))
5767 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005768 if (array->used >= array->alloced) {
5769 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005770 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005771 void *nlist;
5772 if (snd_BUG_ON(num >= 4096))
5773 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005774 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005775 if (!nlist)
5776 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005777 array->list = nlist;
5778 array->alloced = num;
5779 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005780 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005782EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005783
Takashi Iwaid5191e52009-11-16 14:58:17 +01005784/**
5785 * snd_array_free - free the given array elements
5786 * @array: the array object
5787 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005788void snd_array_free(struct snd_array *array)
5789{
5790 kfree(array->list);
5791 array->used = 0;
5792 array->alloced = 0;
5793 array->list = NULL;
5794}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005795EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005796
Takashi Iwaid5191e52009-11-16 14:58:17 +01005797/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005798 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5799 * @pcm: PCM caps bits
5800 * @buf: the string buffer to write
5801 * @buflen: the max buffer length
5802 *
5803 * used by hda_proc.c and hda_eld.c
5804 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005805void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5806{
5807 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5808 int i, j;
5809
5810 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5811 if (pcm & (AC_SUPPCM_BITS_8 << i))
5812 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5813
5814 buf[j] = '\0'; /* necessary when j == 0 */
5815}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005816EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005817
5818MODULE_DESCRIPTION("HDA codec core");
5819MODULE_LICENSE("GPL");