blob: 5b7622034eeef914ca42160f449de87aff8a6f26 [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 Iwai63e51fd72013-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 Iwai63e51fd72013-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 Warren7c935972011-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 Warren7c935972011-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);
Mengdong Lin7f132922013-11-29 01:48:45 -05001474 codec->depop_delay = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Takashi Iwai83012a72012-08-24 18:38:08 +02001476#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001477 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001478 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
Mengdong Lin12edb892013-11-26 23:32:23 -05001479 INIT_WORK(&codec->suspend_work, hda_suspend_work);
1480 INIT_WORK(&codec->resume_work, hda_resume_work);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001481 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1482 * the caller has to power down appropriatley after initialization
1483 * phase.
1484 */
1485 hda_keep_power_on(codec);
1486#endif
1487
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001488 if (codec->bus->modelname) {
1489 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1490 if (!codec->modelname) {
1491 snd_hda_codec_free(codec);
1492 return -ENODEV;
1493 }
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001497 bus->num_codecs++;
1498#ifdef CONFIG_PM
1499 workqueue_set_max_active(bus->pm_wq, bus->num_codecs);
1500#endif
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 bus->caddr_tbl[codec_addr] = codec;
1503
Takashi Iwai0ba21762007-04-16 11:29:14 +02001504 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1505 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001506 if (codec->vendor_id == -1)
1507 /* read again, hopefully the access method was corrected
1508 * in the last read...
1509 */
1510 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1511 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001512 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1513 AC_PAR_SUBSYSTEM_ID);
1514 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1515 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001517 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001518 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001519 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001520 err = -ENODEV;
1521 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
Takashi Iwaid8193872012-08-31 07:54:38 -07001524 fg = codec->afg ? codec->afg : codec->mfg;
1525 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001526 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001527 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001528 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001529 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001530 err = read_pin_defaults(codec);
1531 if (err < 0)
1532 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001533
Takashi Iwai0ba21762007-04-16 11:29:14 +02001534 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001535 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001536 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001538 }
1539
Takashi Iwai83012a72012-08-24 18:38:08 +02001540#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001541 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001542 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001543#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001544 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001545 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001546#ifdef CONFIG_PM
1547 if (!codec->d3_stop_clk || !codec->epss)
1548 bus->power_keep_link_on = 1;
1549#endif
1550
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001551
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001552 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001553 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001554
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001555 snd_hda_codec_proc_new(codec);
1556
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001557 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001558
1559 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1560 codec->subsystem_id, codec->revision_id);
1561 snd_component_add(codec->bus->card, component);
1562
1563 if (codecp)
1564 *codecp = codec;
1565 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001566
1567 error:
1568 snd_hda_codec_free(codec);
1569 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001571EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001572
Mengdong Lina15d05d2013-02-08 17:09:31 -05001573int snd_hda_codec_update_widgets(struct hda_codec *codec)
1574{
1575 hda_nid_t fg;
1576 int err;
1577
1578 /* Assume the function group node does not change,
1579 * only the widget nodes may change.
1580 */
1581 kfree(codec->wcaps);
1582 fg = codec->afg ? codec->afg : codec->mfg;
1583 err = read_widget_caps(codec, fg);
1584 if (err < 0) {
1585 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1586 return err;
1587 }
1588
1589 snd_array_free(&codec->init_pins);
1590 err = read_pin_defaults(codec);
1591
1592 return err;
1593}
1594EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1595
1596
Takashi Iwaif0639272013-11-18 12:07:29 +01001597#ifdef CONFIG_SND_HDA_CODEC_HDMI
1598/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1599static bool is_likely_hdmi_codec(struct hda_codec *codec)
1600{
1601 hda_nid_t nid = codec->start_nid;
1602 int i;
1603
1604 for (i = 0; i < codec->num_nodes; i++, nid++) {
1605 unsigned int wcaps = get_wcaps(codec, nid);
1606 switch (get_wcaps_type(wcaps)) {
1607 case AC_WID_AUD_IN:
1608 return false; /* HDMI parser supports only HDMI out */
1609 case AC_WID_AUD_OUT:
1610 if (!(wcaps & AC_WCAP_DIGITAL))
1611 return false;
1612 break;
1613 }
1614 }
1615 return true;
1616}
1617#else
1618/* no HDMI codec parser support */
1619#define is_likely_hdmi_codec(codec) false
1620#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1621
Takashi Iwaid5191e52009-11-16 14:58:17 +01001622/**
1623 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1624 * @codec: the HDA codec
1625 *
1626 * Start parsing of the given codec tree and (re-)initialize the whole
1627 * patch instance.
1628 *
1629 * Returns 0 if successful or a negative error code.
1630 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001631int snd_hda_codec_configure(struct hda_codec *codec)
1632{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001633 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001634 int err;
1635
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001636 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001637 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001638 err = get_codec_name(codec);
1639 if (err < 0)
1640 return err;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001643 if (!is_generic_config(codec) && codec->preset)
1644 patch = codec->preset->patch;
1645 if (!patch) {
1646 unload_parser(codec); /* to be sure */
Takashi Iwaif0639272013-11-18 12:07:29 +01001647 if (is_likely_hdmi_codec(codec))
1648 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001649#ifdef CONFIG_SND_HDA_GENERIC
1650 if (!patch)
1651 patch = load_parser(codec, snd_hda_parse_generic_codec);
1652#endif
1653 if (!patch) {
1654 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1655 return -ENODEV;
1656 }
Takashi Iwai82467612007-07-27 19:15:54 +02001657 }
1658
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001659 err = patch(codec);
1660 if (err < 0) {
1661 unload_parser(codec);
1662 return err;
1663 }
Takashi Iwai82467612007-07-27 19:15:54 +02001664
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001665 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001666 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001667 if (err < 0)
1668 return err;
1669 }
1670
Takashi Iwaif62faed2009-12-23 09:27:51 +01001671 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001672 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001673 snprintf(codec->bus->card->mixername,
1674 sizeof(codec->bus->card->mixername),
1675 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001678EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Takashi Iwaied360812012-08-08 17:12:52 +02001680/* update the stream-id if changed */
1681static void update_pcm_stream_id(struct hda_codec *codec,
1682 struct hda_cvt_setup *p, hda_nid_t nid,
1683 u32 stream_tag, int channel_id)
1684{
1685 unsigned int oldval, newval;
1686
1687 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1688 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1689 newval = (stream_tag << 4) | channel_id;
1690 if (oldval != newval)
1691 snd_hda_codec_write(codec, nid, 0,
1692 AC_VERB_SET_CHANNEL_STREAMID,
1693 newval);
1694 p->stream_tag = stream_tag;
1695 p->channel_id = channel_id;
1696 }
1697}
1698
1699/* update the format-id if changed */
1700static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1701 hda_nid_t nid, int format)
1702{
1703 unsigned int oldval;
1704
1705 if (p->format_id != format) {
1706 oldval = snd_hda_codec_read(codec, nid, 0,
1707 AC_VERB_GET_STREAM_FORMAT, 0);
1708 if (oldval != format) {
1709 msleep(1);
1710 snd_hda_codec_write(codec, nid, 0,
1711 AC_VERB_SET_STREAM_FORMAT,
1712 format);
1713 }
1714 p->format_id = format;
1715 }
1716}
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718/**
1719 * snd_hda_codec_setup_stream - set up the codec for streaming
1720 * @codec: the CODEC to set up
1721 * @nid: the NID to set up
1722 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1723 * @channel_id: channel id to pass, zero based.
1724 * @format: stream format.
1725 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001726void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1727 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int channel_id, int format)
1729{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001730 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001731 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001732 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001733 int i;
1734
Takashi Iwai0ba21762007-04-16 11:29:14 +02001735 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001736 return;
1737
Takashi Iwai0ba21762007-04-16 11:29:14 +02001738 snd_printdd("hda_codec_setup_stream: "
1739 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001741 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001742 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001743 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001744
1745 if (codec->pcm_format_first)
1746 update_pcm_format(codec, p, nid, format);
1747 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1748 if (!codec->pcm_format_first)
1749 update_pcm_format(codec, p, nid, format);
1750
Takashi Iwaieb541332010-08-06 13:48:11 +02001751 p->active = 1;
1752 p->dirty = 0;
1753
1754 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001755 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001756 list_for_each_entry(c, &codec->bus->codec_list, list) {
1757 for (i = 0; i < c->cvt_setups.used; i++) {
1758 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001759 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001760 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001761 p->dirty = 1;
1762 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001765EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Takashi Iwaif0cea792010-08-13 11:56:53 +02001767static void really_cleanup_stream(struct hda_codec *codec,
1768 struct hda_cvt_setup *q);
1769
Takashi Iwaid5191e52009-11-16 14:58:17 +01001770/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001771 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772 * @codec: the CODEC to clean up
1773 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001774 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001775 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001776void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1777 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001778{
Takashi Iwaieb541332010-08-06 13:48:11 +02001779 struct hda_cvt_setup *p;
1780
Takashi Iwai888afa12008-03-18 09:57:50 +01001781 if (!nid)
1782 return;
1783
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001784 if (codec->no_sticky_stream)
1785 do_now = 1;
1786
Takashi Iwai888afa12008-03-18 09:57:50 +01001787 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001788 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001789 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001790 /* here we just clear the active flag when do_now isn't set;
1791 * actual clean-ups will be done later in
1792 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1793 */
1794 if (do_now)
1795 really_cleanup_stream(codec, p);
1796 else
1797 p->active = 0;
1798 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001799}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001800EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001801
Takashi Iwaieb541332010-08-06 13:48:11 +02001802static void really_cleanup_stream(struct hda_codec *codec,
1803 struct hda_cvt_setup *q)
1804{
1805 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001806 if (q->stream_tag || q->channel_id)
1807 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1808 if (q->format_id)
1809 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1810);
Takashi Iwaieb541332010-08-06 13:48:11 +02001811 memset(q, 0, sizeof(*q));
1812 q->nid = nid;
1813}
1814
1815/* clean up the all conflicting obsolete streams */
1816static void purify_inactive_streams(struct hda_codec *codec)
1817{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001818 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001819 int i;
1820
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001821 list_for_each_entry(c, &codec->bus->codec_list, list) {
1822 for (i = 0; i < c->cvt_setups.used; i++) {
1823 struct hda_cvt_setup *p;
1824 p = snd_array_elem(&c->cvt_setups, i);
1825 if (p->dirty)
1826 really_cleanup_stream(c, p);
1827 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001828 }
1829}
1830
Takashi Iwai2a439522011-07-26 09:52:50 +02001831#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001832/* clean up all streams; called from suspend */
1833static void hda_cleanup_all_streams(struct hda_codec *codec)
1834{
1835 int i;
1836
1837 for (i = 0; i < codec->cvt_setups.used; i++) {
1838 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1839 if (p->stream_tag)
1840 really_cleanup_stream(codec, p);
1841 }
1842}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001843#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845/*
1846 * amp access functions
1847 */
1848
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001849/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001850#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001851#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001852#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1853#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001855#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001858static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001859 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Takashi Iwai01751f52007-08-10 16:59:39 +02001861 memset(cache, 0, sizeof(*cache));
1862 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001863 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001864}
1865
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001866static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001867{
Takashi Iwai603c4012008-07-30 15:01:44 +02001868 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001872static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai01751f52007-08-10 16:59:39 +02001874 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1875 u16 cur = cache->hash[idx];
1876 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001879 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (info->key == key)
1881 return info;
1882 cur = info->next;
1883 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001884 return NULL;
1885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001887/* query the hash. allocate an entry if not found. */
1888static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1889 u32 key)
1890{
1891 struct hda_cache_head *info = get_hash(cache, key);
1892 if (!info) {
1893 u16 idx, cur;
1894 /* add a new hash entry */
1895 info = snd_array_new(&cache->buf);
1896 if (!info)
1897 return NULL;
1898 cur = snd_array_index(&cache->buf, info);
1899 info->key = key;
1900 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001901 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001902 idx = key % (u16)ARRAY_SIZE(cache->hash);
1903 info->next = cache->hash[idx];
1904 cache->hash[idx] = cur;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return info;
1907}
1908
Takashi Iwai01751f52007-08-10 16:59:39 +02001909/* query and allocate an amp hash entry */
1910static inline struct hda_amp_info *
1911get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1912{
1913 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1914}
1915
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001916/* overwrite the value with the key in the caps hash */
1917static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1918{
1919 struct hda_amp_info *info;
1920
1921 mutex_lock(&codec->hash_mutex);
1922 info = get_alloc_amp_hash(codec, key);
1923 if (!info) {
1924 mutex_unlock(&codec->hash_mutex);
1925 return -EINVAL;
1926 }
1927 info->amp_caps = val;
1928 info->head.val |= INFO_AMP_CAPS;
1929 mutex_unlock(&codec->hash_mutex);
1930 return 0;
1931}
1932
1933/* query the value from the caps hash; if not found, fetch the current
1934 * value from the given function and store in the hash
1935 */
1936static unsigned int
1937query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1938 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1939{
1940 struct hda_amp_info *info;
1941 unsigned int val;
1942
1943 mutex_lock(&codec->hash_mutex);
1944 info = get_alloc_amp_hash(codec, key);
1945 if (!info) {
1946 mutex_unlock(&codec->hash_mutex);
1947 return 0;
1948 }
1949 if (!(info->head.val & INFO_AMP_CAPS)) {
1950 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1951 val = func(codec, nid, dir);
1952 write_caps_hash(codec, key, val);
1953 } else {
1954 val = info->amp_caps;
1955 mutex_unlock(&codec->hash_mutex);
1956 }
1957 return val;
1958}
1959
1960static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1961 int direction)
1962{
1963 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1964 nid = codec->afg;
1965 return snd_hda_param_read(codec, nid,
1966 direction == HDA_OUTPUT ?
1967 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1968}
1969
Takashi Iwaid5191e52009-11-16 14:58:17 +01001970/**
1971 * query_amp_caps - query AMP capabilities
1972 * @codec: the HD-auio codec
1973 * @nid: the NID to query
1974 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1975 *
1976 * Query AMP capabilities for the given widget and direction.
1977 * Returns the obtained capability bits.
1978 *
1979 * When cap bits have been already read, this doesn't read again but
1980 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001982u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001984 return query_caps_hash(codec, nid, direction,
1985 HDA_HASH_KEY(nid, direction, 0),
1986 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001988EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Takashi Iwaid5191e52009-11-16 14:58:17 +01001990/**
1991 * snd_hda_override_amp_caps - Override the AMP capabilities
1992 * @codec: the CODEC to clean up
1993 * @nid: the NID to clean up
1994 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1995 * @caps: the capability bits to set
1996 *
1997 * Override the cached AMP caps bits value by the given one.
1998 * This function is useful if the driver needs to adjust the AMP ranges,
1999 * e.g. limit to 0dB, etc.
2000 *
2001 * Returns zero if successful or a negative error code.
2002 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002003int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2004 unsigned int caps)
2005{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002006 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002007}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002008EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002009
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002010static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2011 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002012{
2013 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2014}
2015
Takashi Iwaid5191e52009-11-16 14:58:17 +01002016/**
2017 * snd_hda_query_pin_caps - Query PIN capabilities
2018 * @codec: the HD-auio codec
2019 * @nid: the NID to query
2020 *
2021 * Query PIN capabilities for the given widget.
2022 * Returns the obtained capability bits.
2023 *
2024 * When cap bits have been already read, this doesn't read again but
2025 * returns the cached value.
2026 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002027u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2028{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002029 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002030 read_pin_cap);
2031}
Takashi Iwai1327a322009-03-23 13:07:47 +01002032EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
2033
Wu Fengguang864f92b2009-11-18 12:38:02 +08002034/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002035 * snd_hda_override_pin_caps - Override the pin capabilities
2036 * @codec: the CODEC
2037 * @nid: the NID to override
2038 * @caps: the capability bits to set
2039 *
2040 * Override the cached PIN capabilitiy bits value by the given one.
2041 *
2042 * Returns zero if successful or a negative error code.
2043 */
2044int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2045 unsigned int caps)
2046{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002047 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002048}
2049EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2050
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002051/* read or sync the hash value with the current value;
2052 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002054static struct hda_amp_info *
2055update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002056 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002058 struct hda_amp_info *info;
2059 unsigned int parm, val = 0;
2060 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002062 retry:
2063 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2064 if (!info)
2065 return NULL;
2066 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2067 if (!val_read) {
2068 mutex_unlock(&codec->hash_mutex);
2069 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2070 parm |= direction == HDA_OUTPUT ?
2071 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2072 parm |= index;
2073 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002074 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002075 val &= 0xff;
2076 val_read = true;
2077 mutex_lock(&codec->hash_mutex);
2078 goto retry;
2079 }
2080 info->vol[ch] = val;
2081 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002082 } else if (init_only)
2083 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002084 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002090static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002091 hda_nid_t nid, int ch, int direction, int index,
2092 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 u32 parm;
2095
2096 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2097 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2098 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002099 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2100 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002101 ; /* set the zero value as a fake mute */
2102 else
2103 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2105}
2106
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107/**
2108 * snd_hda_codec_amp_read - Read AMP value
2109 * @codec: HD-audio codec
2110 * @nid: NID to read the AMP value
2111 * @ch: channel (left=0 or right=1)
2112 * @direction: #HDA_INPUT or #HDA_OUTPUT
2113 * @index: the index value (only for input direction)
2114 *
2115 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
Takashi Iwai834be882006-03-01 14:16:17 +01002117int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2118 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002120 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002121 unsigned int val = 0;
2122
2123 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002124 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002125 if (info)
2126 val = info->vol[ch];
2127 mutex_unlock(&codec->hash_mutex);
2128 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002130EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Takashi Iwai280e57d2012-12-14 10:32:21 +01002132static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2133 int direction, int idx, int mask, int val,
2134 bool init_only)
2135{
2136 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002137 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002138 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002139
2140 if (snd_BUG_ON(mask & ~0xff))
2141 mask &= 0xff;
2142 val &= mask;
2143
2144 mutex_lock(&codec->hash_mutex);
2145 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2146 if (!info) {
2147 mutex_unlock(&codec->hash_mutex);
2148 return 0;
2149 }
2150 val |= info->vol[ch] & ~mask;
2151 if (info->vol[ch] == val) {
2152 mutex_unlock(&codec->hash_mutex);
2153 return 0;
2154 }
2155 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002156 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002157 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002158 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002159 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002160 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002161 return 1;
2162}
2163
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164/**
2165 * snd_hda_codec_amp_update - update the AMP value
2166 * @codec: HD-audio codec
2167 * @nid: NID to read the AMP value
2168 * @ch: channel (left=0 or right=1)
2169 * @direction: #HDA_INPUT or #HDA_OUTPUT
2170 * @idx: the index value (only for input direction)
2171 * @mask: bit mask to set
2172 * @val: the bits value to set
2173 *
2174 * Update the AMP value with a bit mask.
2175 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002176 */
Takashi Iwai834be882006-03-01 14:16:17 +01002177int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2178 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002180 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002182EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Takashi Iwaid5191e52009-11-16 14:58:17 +01002184/**
2185 * snd_hda_codec_amp_stereo - update the AMP stereo values
2186 * @codec: HD-audio codec
2187 * @nid: NID to read the AMP value
2188 * @direction: #HDA_INPUT or #HDA_OUTPUT
2189 * @idx: the index value (only for input direction)
2190 * @mask: bit mask to set
2191 * @val: the bits value to set
2192 *
2193 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2194 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002195 */
2196int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2197 int direction, int idx, int mask, int val)
2198{
2199 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002200
2201 if (snd_BUG_ON(mask & ~0xff))
2202 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002203 for (ch = 0; ch < 2; ch++)
2204 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2205 idx, mask, val);
2206 return ret;
2207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002208EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002209
Takashi Iwai280e57d2012-12-14 10:32:21 +01002210/* Works like snd_hda_codec_amp_update() but it writes the value only at
2211 * the first access. If the amp was already initialized / updated beforehand,
2212 * this does nothing.
2213 */
2214int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2215 int dir, int idx, int mask, int val)
2216{
2217 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2218}
2219EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2220
2221int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2222 int dir, int idx, int mask, int val)
2223{
2224 int ch, ret = 0;
2225
2226 if (snd_BUG_ON(mask & ~0xff))
2227 mask &= 0xff;
2228 for (ch = 0; ch < 2; ch++)
2229 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2230 idx, mask, val);
2231 return ret;
2232}
2233EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2234
Takashi Iwaid5191e52009-11-16 14:58:17 +01002235/**
2236 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2237 * @codec: HD-audio codec
2238 *
2239 * Resume the all amp commands from the cache.
2240 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002241void snd_hda_codec_resume_amp(struct hda_codec *codec)
2242{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002243 int i;
2244
Takashi Iwaic370dd62012-12-13 18:30:04 +01002245 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002246 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002247 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2248 struct hda_amp_info *buffer;
2249 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002250 hda_nid_t nid;
2251 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002252 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002253
2254 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002255 if (!buffer->head.dirty)
2256 continue;
2257 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002258 info = *buffer;
2259 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002260 if (!key)
2261 continue;
2262 nid = key & 0xff;
2263 idx = (key >> 16) & 0xff;
2264 dir = (key >> 24) & 0xff;
2265 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002266 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002267 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002268 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002269 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2270 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002271 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002272 }
2273 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002274 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002276EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002278static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2279 unsigned int ofs)
2280{
2281 u32 caps = query_amp_caps(codec, nid, dir);
2282 /* get num steps */
2283 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2284 if (ofs < caps)
2285 caps -= ofs;
2286 return caps;
2287}
2288
Takashi Iwaid5191e52009-11-16 14:58:17 +01002289/**
2290 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2291 *
2292 * The control element is supposed to have the private_value field
2293 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2294 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002295int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2299 u16 nid = get_amp_nid(kcontrol);
2300 u8 chs = get_amp_channels(kcontrol);
2301 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002302 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002304 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2305 uinfo->count = chs == 3 ? 2 : 1;
2306 uinfo->value.integer.min = 0;
2307 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2308 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002309 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002310 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2311 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return -EINVAL;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002316EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002318
2319static inline unsigned int
2320read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2321 int ch, int dir, int idx, unsigned int ofs)
2322{
2323 unsigned int val;
2324 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2325 val &= HDA_AMP_VOLMASK;
2326 if (val >= ofs)
2327 val -= ofs;
2328 else
2329 val = 0;
2330 return val;
2331}
2332
2333static inline int
2334update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2335 int ch, int dir, int idx, unsigned int ofs,
2336 unsigned int val)
2337{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002338 unsigned int maxval;
2339
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002340 if (val > 0)
2341 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002342 /* ofs = 0: raw max value */
2343 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002344 if (val > maxval)
2345 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002346 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2347 HDA_AMP_VOLMASK, val);
2348}
2349
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350/**
2351 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2352 *
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2355 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002356int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 hda_nid_t nid = get_amp_nid(kcontrol);
2361 int chs = get_amp_channels(kcontrol);
2362 int dir = get_amp_direction(kcontrol);
2363 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002364 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 long *valp = ucontrol->value.integer.value;
2366
2367 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002368 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002370 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002373EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Takashi Iwaid5191e52009-11-16 14:58:17 +01002375/**
2376 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2377 *
2378 * The control element is supposed to have the private_value field
2379 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2380 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002381int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2385 hda_nid_t nid = get_amp_nid(kcontrol);
2386 int chs = get_amp_channels(kcontrol);
2387 int dir = get_amp_direction(kcontrol);
2388 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002389 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 long *valp = ucontrol->value.integer.value;
2391 int change = 0;
2392
Takashi Iwaicb53c622007-08-10 17:21:45 +02002393 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002394 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002395 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002396 valp++;
2397 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002398 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002399 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002400 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return change;
2402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002403EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Takashi Iwaid5191e52009-11-16 14:58:17 +01002405/**
2406 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2407 *
2408 * The control element is supposed to have the private_value field
2409 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2410 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002411int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2412 unsigned int size, unsigned int __user *_tlv)
2413{
2414 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2415 hda_nid_t nid = get_amp_nid(kcontrol);
2416 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002417 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002418 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002419 u32 caps, val1, val2;
2420
2421 if (size < 4 * sizeof(unsigned int))
2422 return -ENOMEM;
2423 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002424 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2425 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002426 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002427 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002428 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002429 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002430 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002431 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2432 return -EFAULT;
2433 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2434 return -EFAULT;
2435 if (put_user(val1, _tlv + 2))
2436 return -EFAULT;
2437 if (put_user(val2, _tlv + 3))
2438 return -EFAULT;
2439 return 0;
2440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002441EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002442
Takashi Iwaid5191e52009-11-16 14:58:17 +01002443/**
2444 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2445 * @codec: HD-audio codec
2446 * @nid: NID of a reference widget
2447 * @dir: #HDA_INPUT or #HDA_OUTPUT
2448 * @tlv: TLV data to be stored, at least 4 elements
2449 *
2450 * Set (static) TLV data for a virtual master volume using the AMP caps
2451 * obtained from the reference NID.
2452 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002453 */
2454void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2455 unsigned int *tlv)
2456{
2457 u32 caps;
2458 int nums, step;
2459
2460 caps = query_amp_caps(codec, nid, dir);
2461 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2462 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2463 step = (step + 1) * 25;
2464 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2465 tlv[1] = 2 * sizeof(unsigned int);
2466 tlv[2] = -nums * step;
2467 tlv[3] = step;
2468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002469EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002470
2471/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002472static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002473find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002474{
2475 struct snd_ctl_elem_id id;
2476 memset(&id, 0, sizeof(id));
2477 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002478 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002479 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002480 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2481 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002482 strcpy(id.name, name);
2483 return snd_ctl_find_id(codec->bus->card, &id);
2484}
2485
Takashi Iwaid5191e52009-11-16 14:58:17 +01002486/**
2487 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2488 * @codec: HD-audio codec
2489 * @name: ctl id name string
2490 *
2491 * Get the control element with the given id string and IFACE_MIXER.
2492 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002493struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2494 const char *name)
2495{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002496 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002497}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002498EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002499
Takashi Iwaidcda5802012-10-12 17:24:51 +02002500static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002501 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002502{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002503 int i, idx;
2504 /* 16 ctlrs should be large enough */
2505 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2506 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002507 return idx;
2508 }
2509 return -EBUSY;
2510}
2511
Takashi Iwaid5191e52009-11-16 14:58:17 +01002512/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002513 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002514 * @codec: HD-audio codec
2515 * @nid: corresponding NID (optional)
2516 * @kctl: the control element to assign
2517 *
2518 * Add the given control element to an array inside the codec instance.
2519 * All control elements belonging to a codec are supposed to be added
2520 * by this function so that a proper clean-up works at the free or
2521 * reconfiguration time.
2522 *
2523 * If non-zero @nid is passed, the NID is assigned to the control element.
2524 * The assignment is shown in the codec proc file.
2525 *
2526 * snd_hda_ctl_add() checks the control subdev id field whether
2527 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002528 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2529 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002530 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002531int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2532 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002533{
2534 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002535 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002536 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002537
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002538 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002539 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002540 if (nid == 0)
2541 nid = get_amp_nid_(kctl->private_value);
2542 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002543 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2544 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002545 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002546 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002547 err = snd_ctl_add(codec->bus->card, kctl);
2548 if (err < 0)
2549 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002550 item = snd_array_new(&codec->mixers);
2551 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002552 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002553 item->kctl = kctl;
2554 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002555 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002556 return 0;
2557}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002558EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002559
Takashi Iwaid5191e52009-11-16 14:58:17 +01002560/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002561 * snd_hda_add_nid - Assign a NID to a control element
2562 * @codec: HD-audio codec
2563 * @nid: corresponding NID (optional)
2564 * @kctl: the control element to assign
2565 * @index: index to kctl
2566 *
2567 * Add the given control element to an array inside the codec instance.
2568 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2569 * NID:KCTL mapping - for example "Capture Source" selector.
2570 */
2571int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2572 unsigned int index, hda_nid_t nid)
2573{
2574 struct hda_nid_item *item;
2575
2576 if (nid > 0) {
2577 item = snd_array_new(&codec->nids);
2578 if (!item)
2579 return -ENOMEM;
2580 item->kctl = kctl;
2581 item->index = index;
2582 item->nid = nid;
2583 return 0;
2584 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002585 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2586 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002587 return -EINVAL;
2588}
2589EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2590
2591/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002592 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2593 * @codec: HD-audio codec
2594 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002595void snd_hda_ctls_clear(struct hda_codec *codec)
2596{
2597 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002598 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002599 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002600 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002601 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002602 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002603}
2604
Takashi Iwaia65d6292009-02-23 16:57:04 +01002605/* pseudo device locking
2606 * toggle card->shutdown to allow/disallow the device access (as a hack)
2607 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002608int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002609{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002610 struct snd_card *card = bus->card;
2611 struct hda_codec *codec;
2612
Takashi Iwaia65d6292009-02-23 16:57:04 +01002613 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002614 if (card->shutdown)
2615 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002616 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002617 if (!list_empty(&card->ctl_files))
2618 goto err_clear;
2619
2620 list_for_each_entry(codec, &bus->codec_list, list) {
2621 int pcm;
2622 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2623 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2624 if (!cpcm->pcm)
2625 continue;
2626 if (cpcm->pcm->streams[0].substream_opened ||
2627 cpcm->pcm->streams[1].substream_opened)
2628 goto err_clear;
2629 }
2630 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002631 spin_unlock(&card->files_lock);
2632 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002633
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002634 err_clear:
2635 card->shutdown = 0;
2636 err_unlock:
2637 spin_unlock(&card->files_lock);
2638 return -EINVAL;
2639}
2640EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2641
2642void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002643{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002644 struct snd_card *card = bus->card;
2645
2646 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002647 spin_lock(&card->files_lock);
2648 card->shutdown = 0;
2649 spin_unlock(&card->files_lock);
2650}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002651EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002652
Takashi Iwaid5191e52009-11-16 14:58:17 +01002653/**
2654 * snd_hda_codec_reset - Clear all objects assigned to the codec
2655 * @codec: HD-audio codec
2656 *
2657 * This frees the all PCM and control elements assigned to the codec, and
2658 * clears the caches and restores the pin default configurations.
2659 *
2660 * When a device is being used, it returns -EBSY. If successfully freed,
2661 * returns zero.
2662 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002663int snd_hda_codec_reset(struct hda_codec *codec)
2664{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002665 struct hda_bus *bus = codec->bus;
2666 struct snd_card *card = bus->card;
2667 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002668
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002669 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002670 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002671
2672 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002673 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002674#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002675 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002676 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002677#endif
2678 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002679 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002680 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002681 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002682 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002683 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002684 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002685 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002686 }
2687 if (codec->patch_ops.free)
2688 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002689 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002690 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002691 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002692 codec->spec = NULL;
2693 free_hda_cache(&codec->amp_cache);
2694 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002695 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2696 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002697 /* free only driver_pins so that init_pins + user_pins are restored */
2698 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002699 snd_array_free(&codec->cvt_setups);
2700 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002701 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002702 codec->num_pcms = 0;
2703 codec->pcm_info = NULL;
2704 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002705 codec->slave_dig_outs = NULL;
2706 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002707 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002708 module_put(codec->owner);
2709 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002710
2711 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002712 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002713 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002714}
2715
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002716typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2717
2718/* apply the function to all matching slave ctls in the mixer list */
2719static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002720 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002721{
2722 struct hda_nid_item *items;
2723 const char * const *s;
2724 int i, err;
2725
2726 items = codec->mixers.list;
2727 for (i = 0; i < codec->mixers.used; i++) {
2728 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002729 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002730 continue;
2731 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002732 char tmpname[sizeof(sctl->id.name)];
2733 const char *name = *s;
2734 if (suffix) {
2735 snprintf(tmpname, sizeof(tmpname), "%s %s",
2736 name, suffix);
2737 name = tmpname;
2738 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002739 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002740 err = func(data, sctl);
2741 if (err)
2742 return err;
2743 break;
2744 }
2745 }
2746 }
2747 return 0;
2748}
2749
2750static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2751{
2752 return 1;
2753}
2754
Takashi Iwai18478e82012-03-09 17:51:10 +01002755/* guess the value corresponding to 0dB */
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002756static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002757{
2758 int _tlv[4];
2759 const int *tlv = NULL;
2760 int val = -1;
2761
2762 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2763 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2764 mm_segment_t fs = get_fs();
2765 set_fs(get_ds());
2766 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2767 tlv = _tlv;
2768 set_fs(fs);
2769 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2770 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002771 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2772 int step = tlv[3];
2773 step &= ~TLV_DB_SCALE_MUTE;
2774 if (!step)
2775 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002776 if (*step_to_check && *step_to_check != step) {
2777 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2778 *step_to_check, step);
2779 return -1;
2780 }
2781 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002782 val = -tlv[2] / step;
2783 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002784 return val;
2785}
2786
2787/* call kctl->put with the given value(s) */
2788static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2789{
2790 struct snd_ctl_elem_value *ucontrol;
2791 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2792 if (!ucontrol)
2793 return -ENOMEM;
2794 ucontrol->value.integer.value[0] = val;
2795 ucontrol->value.integer.value[1] = val;
2796 kctl->put(kctl, ucontrol);
2797 kfree(ucontrol);
2798 return 0;
2799}
2800
2801/* initialize the slave volume with 0dB */
2802static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2803{
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002804 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002805 if (offset > 0)
2806 put_kctl_with_value(slave, offset);
2807 return 0;
2808}
2809
2810/* unmute the slave */
2811static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2812{
2813 return put_kctl_with_value(slave, 1);
2814}
2815
Takashi Iwaid5191e52009-11-16 14:58:17 +01002816/**
2817 * snd_hda_add_vmaster - create a virtual master control and add slaves
2818 * @codec: HD-audio codec
2819 * @name: vmaster control name
2820 * @tlv: TLV data (optional)
2821 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002822 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002823 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002824 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002825 *
2826 * Create a virtual master control with the given name. The TLV data
2827 * must be either NULL or a valid data.
2828 *
2829 * @slaves is a NULL-terminated array of strings, each of which is a
2830 * slave control name. All controls with these names are assigned to
2831 * the new virtual master control.
2832 *
2833 * This function returns zero if successful or a negative error code.
2834 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002835int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002836 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002837 const char *suffix, bool init_slave_vol,
2838 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002839{
2840 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002841 int err;
2842
Takashi Iwai29e58532012-03-12 12:25:03 +01002843 if (ctl_ret)
2844 *ctl_ret = NULL;
2845
Takashi Iwai9322ca52012-02-03 14:28:01 +01002846 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002847 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002848 snd_printdd("No slave found for %s\n", name);
2849 return 0;
2850 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002851 kctl = snd_ctl_make_virtual_master(name, tlv);
2852 if (!kctl)
2853 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002854 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002855 if (err < 0)
2856 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002857
Takashi Iwai9322ca52012-02-03 14:28:01 +01002858 err = map_slaves(codec, slaves, suffix,
2859 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002860 if (err < 0)
2861 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002862
2863 /* init with master mute & zero volume */
2864 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002865 if (init_slave_vol) {
2866 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002867 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002868 tlv ? init_slave_0dB : init_slave_unmute, &step);
2869 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002870
Takashi Iwai29e58532012-03-12 12:25:03 +01002871 if (ctl_ret)
2872 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002873 return 0;
2874}
Takashi Iwai18478e82012-03-09 17:51:10 +01002875EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002876
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002877/*
2878 * mute-LED control using vmaster
2879 */
2880static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
2882{
2883 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002884 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002885 };
2886 unsigned int index;
2887
2888 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2889 uinfo->count = 1;
2890 uinfo->value.enumerated.items = 3;
2891 index = uinfo->value.enumerated.item;
2892 if (index >= 3)
2893 index = 2;
2894 strcpy(uinfo->value.enumerated.name, texts[index]);
2895 return 0;
2896}
2897
2898static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
2901 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2902 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2903 return 0;
2904}
2905
2906static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_value *ucontrol)
2908{
2909 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2910 unsigned int old_mode = hook->mute_mode;
2911
2912 hook->mute_mode = ucontrol->value.enumerated.item[0];
2913 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2914 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2915 if (old_mode == hook->mute_mode)
2916 return 0;
2917 snd_hda_sync_vmaster_hook(hook);
2918 return 1;
2919}
2920
2921static struct snd_kcontrol_new vmaster_mute_mode = {
2922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2923 .name = "Mute-LED Mode",
2924 .info = vmaster_mute_mode_info,
2925 .get = vmaster_mute_mode_get,
2926 .put = vmaster_mute_mode_put,
2927};
2928
2929/*
2930 * Add a mute-LED hook with the given vmaster switch kctl
2931 * "Mute-LED Mode" control is automatically created and associated with
2932 * the given hook.
2933 */
2934int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002935 struct hda_vmaster_mute_hook *hook,
2936 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002937{
2938 struct snd_kcontrol *kctl;
2939
2940 if (!hook->hook || !hook->sw_kctl)
2941 return 0;
2942 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2943 hook->codec = codec;
2944 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002945 if (!expose_enum_ctl)
2946 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002947 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2948 if (!kctl)
2949 return -ENOMEM;
2950 return snd_hda_ctl_add(codec, 0, kctl);
2951}
2952EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2953
2954/*
2955 * Call the hook with the current value for synchronization
2956 * Should be called in init callback
2957 */
2958void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2959{
2960 if (!hook->hook || !hook->codec)
2961 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002962 /* don't call vmaster hook in the destructor since it might have
2963 * been already destroyed
2964 */
2965 if (hook->codec->bus->shutdown)
2966 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002967 switch (hook->mute_mode) {
2968 case HDA_VMUTE_FOLLOW_MASTER:
2969 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2970 break;
2971 default:
2972 hook->hook(hook->codec, hook->mute_mode);
2973 break;
2974 }
2975}
2976EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2977
2978
Takashi Iwaid5191e52009-11-16 14:58:17 +01002979/**
2980 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2981 *
2982 * The control element is supposed to have the private_value field
2983 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2984 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 int chs = get_amp_channels(kcontrol);
2989
2990 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2991 uinfo->count = chs == 3 ? 2 : 1;
2992 uinfo->value.integer.min = 0;
2993 uinfo->value.integer.max = 1;
2994 return 0;
2995}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002996EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Takashi Iwaid5191e52009-11-16 14:58:17 +01002998/**
2999 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3000 *
3001 * The control element is supposed to have the private_value field
3002 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3003 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003004int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3008 hda_nid_t nid = get_amp_nid(kcontrol);
3009 int chs = get_amp_channels(kcontrol);
3010 int dir = get_amp_direction(kcontrol);
3011 int idx = get_amp_index(kcontrol);
3012 long *valp = ucontrol->value.integer.value;
3013
3014 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003016 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003019 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
3021}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003022EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Takashi Iwaid5191e52009-11-16 14:58:17 +01003024/**
3025 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3026 *
3027 * The control element is supposed to have the private_value field
3028 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3029 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003030int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3031 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3034 hda_nid_t nid = get_amp_nid(kcontrol);
3035 int chs = get_amp_channels(kcontrol);
3036 int dir = get_amp_direction(kcontrol);
3037 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 long *valp = ucontrol->value.integer.value;
3039 int change = 0;
3040
Takashi Iwaicb53c622007-08-10 17:21:45 +02003041 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003042 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003043 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003044 HDA_AMP_MUTE,
3045 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003046 valp++;
3047 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003048 if (chs & 2)
3049 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003050 HDA_AMP_MUTE,
3051 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003052 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003053 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return change;
3055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003056EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058/*
Takashi Iwai985be542005-11-02 18:26:49 +01003059 * bound volume controls
3060 *
3061 * bind multiple volumes (# indices, from 0)
3062 */
3063
3064#define AMP_VAL_IDX_SHIFT 19
3065#define AMP_VAL_IDX_MASK (0x0f<<19)
3066
Takashi Iwaid5191e52009-11-16 14:58:17 +01003067/**
3068 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3069 *
3070 * The control element is supposed to have the private_value field
3071 * set up via HDA_BIND_MUTE*() macros.
3072 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003073int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3074 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003075{
3076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3077 unsigned long pval;
3078 int err;
3079
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003080 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003081 pval = kcontrol->private_value;
3082 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3083 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3084 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003085 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003086 return err;
3087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003088EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003089
Takashi Iwaid5191e52009-11-16 14:58:17 +01003090/**
3091 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3092 *
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_BIND_MUTE*() macros.
3095 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003096int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003098{
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 unsigned long pval;
3101 int i, indices, err = 0, change = 0;
3102
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003103 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003104 pval = kcontrol->private_value;
3105 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3106 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3108 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003109 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3110 if (err < 0)
3111 break;
3112 change |= err;
3113 }
3114 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003115 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003116 return err < 0 ? err : change;
3117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003118EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003119
Takashi Iwaid5191e52009-11-16 14:58:17 +01003120/**
3121 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3122 *
3123 * The control element is supposed to have the private_value field
3124 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003125 */
3126int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3127 struct snd_ctl_elem_info *uinfo)
3128{
3129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3130 struct hda_bind_ctls *c;
3131 int err;
3132
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003133 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003134 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003135 kcontrol->private_value = *c->values;
3136 err = c->ops->info(kcontrol, uinfo);
3137 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003138 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003139 return err;
3140}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003141EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003142
Takashi Iwaid5191e52009-11-16 14:58:17 +01003143/**
3144 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3145 *
3146 * The control element is supposed to have the private_value field
3147 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3148 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003149int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3150 struct snd_ctl_elem_value *ucontrol)
3151{
3152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3153 struct hda_bind_ctls *c;
3154 int err;
3155
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003156 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003157 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003158 kcontrol->private_value = *c->values;
3159 err = c->ops->get(kcontrol, ucontrol);
3160 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003161 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003162 return err;
3163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003164EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003165
Takashi Iwaid5191e52009-11-16 14:58:17 +01003166/**
3167 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3168 *
3169 * The control element is supposed to have the private_value field
3170 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3171 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003172int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3173 struct snd_ctl_elem_value *ucontrol)
3174{
3175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3176 struct hda_bind_ctls *c;
3177 unsigned long *vals;
3178 int err = 0, change = 0;
3179
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003180 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003181 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003182 for (vals = c->values; *vals; vals++) {
3183 kcontrol->private_value = *vals;
3184 err = c->ops->put(kcontrol, ucontrol);
3185 if (err < 0)
3186 break;
3187 change |= err;
3188 }
3189 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003190 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003191 return err < 0 ? err : change;
3192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003193EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003194
Takashi Iwaid5191e52009-11-16 14:58:17 +01003195/**
3196 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3197 *
3198 * The control element is supposed to have the private_value field
3199 * set up via HDA_BIND_VOL() macro.
3200 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003201int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3202 unsigned int size, unsigned int __user *tlv)
3203{
3204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3205 struct hda_bind_ctls *c;
3206 int err;
3207
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003208 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003209 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003210 kcontrol->private_value = *c->values;
3211 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3212 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003213 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003214 return err;
3215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003216EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003217
3218struct hda_ctl_ops snd_hda_bind_vol = {
3219 .info = snd_hda_mixer_amp_volume_info,
3220 .get = snd_hda_mixer_amp_volume_get,
3221 .put = snd_hda_mixer_amp_volume_put,
3222 .tlv = snd_hda_mixer_amp_tlv
3223};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003224EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003225
3226struct hda_ctl_ops snd_hda_bind_sw = {
3227 .info = snd_hda_mixer_amp_switch_info,
3228 .get = snd_hda_mixer_amp_switch_get,
3229 .put = snd_hda_mixer_amp_switch_put,
3230 .tlv = snd_hda_mixer_amp_tlv
3231};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003232EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003233
3234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * SPDIF out controls
3236 */
3237
Takashi Iwai0ba21762007-04-16 11:29:14 +02003238static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3239 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
3241 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3242 uinfo->count = 1;
3243 return 0;
3244}
3245
Takashi Iwai0ba21762007-04-16 11:29:14 +02003246static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
3249 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3250 IEC958_AES0_NONAUDIO |
3251 IEC958_AES0_CON_EMPHASIS_5015 |
3252 IEC958_AES0_CON_NOT_COPYRIGHT;
3253 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3254 IEC958_AES1_CON_ORIGINAL;
3255 return 0;
3256}
3257
Takashi Iwai0ba21762007-04-16 11:29:14 +02003258static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3259 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3262 IEC958_AES0_NONAUDIO |
3263 IEC958_AES0_PRO_EMPHASIS_5015;
3264 return 0;
3265}
3266
Takashi Iwai0ba21762007-04-16 11:29:14 +02003267static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3268 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003271 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003272 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003274 mutex_lock(&codec->spdif_mutex);
3275 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003276 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3277 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3278 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3279 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003280 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 return 0;
3283}
3284
3285/* convert from SPDIF status bits to HDA SPDIF bits
3286 * bit 0 (DigEn) is always set zero (to be filled later)
3287 */
3288static unsigned short convert_from_spdif_status(unsigned int sbits)
3289{
3290 unsigned short val = 0;
3291
3292 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003293 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003295 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003297 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3298 IEC958_AES0_PRO_EMPHASIS_5015)
3299 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003301 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3302 IEC958_AES0_CON_EMPHASIS_5015)
3303 val |= AC_DIG1_EMPHASIS;
3304 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3305 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003307 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3309 }
3310 return val;
3311}
3312
3313/* convert to SPDIF status bits from HDA SPDIF bits
3314 */
3315static unsigned int convert_to_spdif_status(unsigned short val)
3316{
3317 unsigned int sbits = 0;
3318
Takashi Iwai0ba21762007-04-16 11:29:14 +02003319 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003321 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 sbits |= IEC958_AES0_PROFESSIONAL;
3323 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003324 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3326 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003327 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003329 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3333 sbits |= val & (0x7f << 8);
3334 }
3335 return sbits;
3336}
3337
Takashi Iwai2f728532008-09-25 16:32:41 +02003338/* set digital convert verbs both for the given NID and its slaves */
3339static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3340 int verb, int val)
3341{
Takashi Iwaidda14412011-05-02 11:29:30 +02003342 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003343
Takashi Iwai9e976972008-11-25 08:17:20 +01003344 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003345 d = codec->slave_dig_outs;
3346 if (!d)
3347 return;
3348 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003349 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003350}
3351
3352static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3353 int dig1, int dig2)
3354{
3355 if (dig1 != -1)
3356 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3357 if (dig2 != -1)
3358 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3359}
3360
Takashi Iwai0ba21762007-04-16 11:29:14 +02003361static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3362 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
3364 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003365 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003366 struct hda_spdif_out *spdif;
3367 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 unsigned short val;
3369 int change;
3370
Ingo Molnar62932df2006-01-16 16:34:20 +01003371 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003372 spdif = snd_array_elem(&codec->spdif_out, idx);
3373 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003374 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3376 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3377 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003378 val = convert_from_spdif_status(spdif->status);
3379 val |= spdif->ctls & 1;
3380 change = spdif->ctls != val;
3381 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003382 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003383 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003384 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return change;
3386}
3387
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003388#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Takashi Iwai0ba21762007-04-16 11:29:14 +02003390static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3391 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003394 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003395 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003397 mutex_lock(&codec->spdif_mutex);
3398 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003399 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003400 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return 0;
3402}
3403
Stephen Warren74b654c2011-06-01 11:14:18 -06003404static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3405 int dig1, int dig2)
3406{
3407 set_dig_out_convert(codec, nid, dig1, dig2);
3408 /* unmute amp switch (if any) */
3409 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3410 (dig1 & AC_DIG1_ENABLE))
3411 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3412 HDA_AMP_MUTE, 0);
3413}
3414
Takashi Iwai0ba21762007-04-16 11:29:14 +02003415static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3416 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
3418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003419 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003420 struct hda_spdif_out *spdif;
3421 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 unsigned short val;
3423 int change;
3424
Ingo Molnar62932df2006-01-16 16:34:20 +01003425 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003426 spdif = snd_array_elem(&codec->spdif_out, idx);
3427 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003428 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003430 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003431 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003432 spdif->ctls = val;
3433 if (change && nid != (u16)-1)
3434 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003435 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return change;
3437}
3438
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003439static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 {
3441 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003443 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 .info = snd_hda_spdif_mask_info,
3445 .get = snd_hda_spdif_cmask_get,
3446 },
3447 {
3448 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3449 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003450 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 .info = snd_hda_spdif_mask_info,
3452 .get = snd_hda_spdif_pmask_get,
3453 },
3454 {
3455 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003456 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 .info = snd_hda_spdif_mask_info,
3458 .get = snd_hda_spdif_default_get,
3459 .put = snd_hda_spdif_default_put,
3460 },
3461 {
3462 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003463 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 .info = snd_hda_spdif_out_switch_info,
3465 .get = snd_hda_spdif_out_switch_get,
3466 .put = snd_hda_spdif_out_switch_put,
3467 },
3468 { } /* end */
3469};
3470
3471/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003472 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003474 * @associated_nid: NID that new ctls associated with
3475 * @cvt_nid: converter NID
3476 * @type: HDA_PCM_TYPE_*
3477 * Creates controls related with the digital output.
3478 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 *
3480 * Returns 0 if successful, or a negative error code.
3481 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003482int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3483 hda_nid_t associated_nid,
3484 hda_nid_t cvt_nid,
3485 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
3487 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003488 struct snd_kcontrol *kctl;
3489 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003490 int idx = 0;
3491 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003492 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003493 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003495 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003496 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003497 idx = spdif_index;
3498 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003499 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003500 /* suppose a single SPDIF device */
3501 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3502 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3503 if (!kctl)
3504 break;
3505 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003506 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003507 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003508 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003509 if (!bus->primary_dig_out_type)
3510 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003511
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003512 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003513 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003514 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3515 return -EBUSY;
3516 }
Stephen Warren7c935972011-06-01 11:14:17 -06003517 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003518 if (!spdif)
3519 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3521 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003522 if (!kctl)
3523 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003524 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003525 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003526 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003527 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return err;
3529 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003530 spdif->nid = cvt_nid;
3531 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003532 AC_VERB_GET_DIGI_CONVERT_1, 0);
3533 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return 0;
3535}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003536EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003538/* get the hda_spdif_out entry from the given NID
3539 * call within spdif_mutex lock
3540 */
Stephen Warren7c935972011-06-01 11:14:17 -06003541struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3542 hda_nid_t nid)
3543{
3544 int i;
3545 for (i = 0; i < codec->spdif_out.used; i++) {
3546 struct hda_spdif_out *spdif =
3547 snd_array_elem(&codec->spdif_out, i);
3548 if (spdif->nid == nid)
3549 return spdif;
3550 }
3551 return NULL;
3552}
3553EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3554
Stephen Warren74b654c2011-06-01 11:14:18 -06003555void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3556{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003557 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003558
3559 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003560 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003561 spdif->nid = (u16)-1;
3562 mutex_unlock(&codec->spdif_mutex);
3563}
3564EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3565
3566void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3567{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003568 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003569 unsigned short val;
3570
3571 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003572 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003573 if (spdif->nid != nid) {
3574 spdif->nid = nid;
3575 val = spdif->ctls;
3576 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3577 }
3578 mutex_unlock(&codec->spdif_mutex);
3579}
3580EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003583 * SPDIF sharing with analog output
3584 */
3585static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3586 struct snd_ctl_elem_value *ucontrol)
3587{
3588 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3589 ucontrol->value.integer.value[0] = mout->share_spdif;
3590 return 0;
3591}
3592
3593static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3594 struct snd_ctl_elem_value *ucontrol)
3595{
3596 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3597 mout->share_spdif = !!ucontrol->value.integer.value[0];
3598 return 0;
3599}
3600
3601static struct snd_kcontrol_new spdif_share_sw = {
3602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3603 .name = "IEC958 Default PCM Playback Switch",
3604 .info = snd_ctl_boolean_mono_info,
3605 .get = spdif_share_sw_get,
3606 .put = spdif_share_sw_put,
3607};
3608
Takashi Iwaid5191e52009-11-16 14:58:17 +01003609/**
3610 * snd_hda_create_spdif_share_sw - create Default PCM switch
3611 * @codec: the HDA codec
3612 * @mout: multi-out instance
3613 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003614int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3615 struct hda_multi_out *mout)
3616{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003617 struct snd_kcontrol *kctl;
3618
Takashi Iwai9a081602008-02-12 18:37:26 +01003619 if (!mout->dig_out_nid)
3620 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003621
3622 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3623 if (!kctl)
3624 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003625 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003626 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003628EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003629
3630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 * SPDIF input
3632 */
3633
3634#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3635
Takashi Iwai0ba21762007-04-16 11:29:14 +02003636static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3637 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
3639 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3640
3641 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3642 return 0;
3643}
3644
Takashi Iwai0ba21762007-04-16 11:29:14 +02003645static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3646 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3649 hda_nid_t nid = kcontrol->private_value;
3650 unsigned int val = !!ucontrol->value.integer.value[0];
3651 int change;
3652
Ingo Molnar62932df2006-01-16 16:34:20 +01003653 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003655 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003657 snd_hda_codec_write_cache(codec, nid, 0,
3658 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003660 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return change;
3662}
3663
Takashi Iwai0ba21762007-04-16 11:29:14 +02003664static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3665 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
3667 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3668 hda_nid_t nid = kcontrol->private_value;
3669 unsigned short val;
3670 unsigned int sbits;
3671
Andrew Paprocki3982d172007-12-19 12:13:44 +01003672 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 sbits = convert_to_spdif_status(val);
3674 ucontrol->value.iec958.status[0] = sbits;
3675 ucontrol->value.iec958.status[1] = sbits >> 8;
3676 ucontrol->value.iec958.status[2] = sbits >> 16;
3677 ucontrol->value.iec958.status[3] = sbits >> 24;
3678 return 0;
3679}
3680
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003681static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 {
3683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003684 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 .info = snd_hda_spdif_in_switch_info,
3686 .get = snd_hda_spdif_in_switch_get,
3687 .put = snd_hda_spdif_in_switch_put,
3688 },
3689 {
3690 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003692 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 .info = snd_hda_spdif_mask_info,
3694 .get = snd_hda_spdif_in_status_get,
3695 },
3696 { } /* end */
3697};
3698
3699/**
3700 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3701 * @codec: the HDA codec
3702 * @nid: audio in widget NID
3703 *
3704 * Creates controls related with the SPDIF input.
3705 * Called from each patch supporting the SPDIF in.
3706 *
3707 * Returns 0 if successful, or a negative error code.
3708 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003709int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
3711 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003712 struct snd_kcontrol *kctl;
3713 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003714 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Takashi Iwaidcda5802012-10-12 17:24:51 +02003716 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003717 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003718 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3719 return -EBUSY;
3720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3722 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003723 if (!kctl)
3724 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003726 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003727 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 return err;
3729 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003730 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003731 snd_hda_codec_read(codec, nid, 0,
3732 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003733 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 return 0;
3735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003736EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003738/*
3739 * command cache
3740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Takashi Iwaic370dd62012-12-13 18:30:04 +01003742/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003743#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3744#define get_cmd_cache_nid(key) ((key) & 0xff)
3745#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3746
3747/**
3748 * snd_hda_codec_write_cache - send a single command with caching
3749 * @codec: the HDA codec
3750 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003751 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003752 * @verb: the verb to send
3753 * @parm: the parameter for the verb
3754 *
3755 * Send a single command without waiting for response.
3756 *
3757 * Returns 0 if successful, or a negative error code.
3758 */
3759int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003760 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003761{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003762 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003763 struct hda_cache_head *c;
3764 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003765 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003766
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003767 cache_only = codec->cached_write;
3768 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003769 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003770 if (err < 0)
3771 return err;
3772 }
3773
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003774 /* parm may contain the verb stuff for get/set amp */
3775 verb = verb | (parm >> 8);
3776 parm &= 0xff;
3777 key = build_cmd_cache_key(nid, verb);
3778 mutex_lock(&codec->bus->cmd_mutex);
3779 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003780 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003781 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003782 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003783 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003784 mutex_unlock(&codec->bus->cmd_mutex);
3785 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003786}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003787EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003788
Takashi Iwaid5191e52009-11-16 14:58:17 +01003789/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003790 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3791 * @codec: the HDA codec
3792 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003793 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003794 * @verb: the verb to send
3795 * @parm: the parameter for the verb
3796 *
3797 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3798 * command if the parameter is already identical with the cached value.
3799 * If not, it sends the command and refreshes the cache.
3800 *
3801 * Returns 0 if successful, or a negative error code.
3802 */
3803int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003804 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003805{
3806 struct hda_cache_head *c;
3807 u32 key;
3808
3809 /* parm may contain the verb stuff for get/set amp */
3810 verb = verb | (parm >> 8);
3811 parm &= 0xff;
3812 key = build_cmd_cache_key(nid, verb);
3813 mutex_lock(&codec->bus->cmd_mutex);
3814 c = get_hash(&codec->cmd_cache, key);
3815 if (c && c->val == parm) {
3816 mutex_unlock(&codec->bus->cmd_mutex);
3817 return 0;
3818 }
3819 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003820 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003821}
3822EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3823
3824/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003825 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3826 * @codec: HD-audio codec
3827 *
3828 * Execute all verbs recorded in the command caches to resume.
3829 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003830void snd_hda_codec_resume_cache(struct hda_codec *codec)
3831{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003832 int i;
3833
Takashi Iwaic370dd62012-12-13 18:30:04 +01003834 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003835 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003836 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3837 struct hda_cache_head *buffer;
3838 u32 key;
3839
3840 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3841 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003842 if (!key)
3843 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003844 if (!buffer->dirty)
3845 continue;
3846 buffer->dirty = 0;
3847 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003848 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3849 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003850 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003851 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003852 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003854EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003855
3856/**
3857 * snd_hda_sequence_write_cache - sequence writes with caching
3858 * @codec: the HDA codec
3859 * @seq: VERB array to send
3860 *
3861 * Send the commands sequentially from the given array.
3862 * Thte commands are recorded on cache for power-save and resume.
3863 * The array must be terminated with NID=0.
3864 */
3865void snd_hda_sequence_write_cache(struct hda_codec *codec,
3866 const struct hda_verb *seq)
3867{
3868 for (; seq->nid; seq++)
3869 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3870 seq->param);
3871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003872EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003873
Takashi Iwaidc870f32013-01-22 15:24:30 +01003874/**
3875 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3876 * @codec: HD-audio codec
3877 */
3878void snd_hda_codec_flush_cache(struct hda_codec *codec)
3879{
3880 snd_hda_codec_resume_amp(codec);
3881 snd_hda_codec_resume_cache(codec);
3882}
3883EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3884
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003885void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003886 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003887{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003888 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003889 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003890
Takashi Iwaicb53c622007-08-10 17:21:45 +02003891 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003892 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003893 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003894 if (!(wcaps & AC_WCAP_POWER))
3895 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003896 if (codec->power_filter) {
3897 state = codec->power_filter(codec, nid, power_state);
3898 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003899 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003900 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003901 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003902 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003903 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003904}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003905EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3906
3907/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003908 * supported power states check
3909 */
3910static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3911 unsigned int power_state)
3912{
3913 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3914
Mengdong Line037cb42012-08-10 14:11:58 +02003915 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003916 return false;
3917 if (sup & power_state)
3918 return true;
3919 else
3920 return false;
3921}
3922
3923/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003924 * wait until the state is reached, returns the current state
3925 */
3926static unsigned int hda_sync_power_state(struct hda_codec *codec,
3927 hda_nid_t fg,
3928 unsigned int power_state)
3929{
3930 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3931 unsigned int state, actual_state;
3932
3933 for (;;) {
3934 state = snd_hda_codec_read(codec, fg, 0,
3935 AC_VERB_GET_POWER_STATE, 0);
3936 if (state & AC_PWRST_ERROR)
3937 break;
3938 actual_state = (state >> 4) & 0x0f;
3939 if (actual_state == power_state)
3940 break;
3941 if (time_after_eq(jiffies, end_time))
3942 break;
3943 /* wait until the codec reachs to the target state */
3944 msleep(1);
3945 }
3946 return state;
3947}
3948
Takashi Iwai9419ab62013-01-24 17:23:35 +01003949/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003950unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3951 hda_nid_t nid,
3952 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003953{
3954 if (power_state == AC_PWRST_D3 &&
3955 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3956 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3957 int eapd = snd_hda_codec_read(codec, nid, 0,
3958 AC_VERB_GET_EAPD_BTLENABLE, 0);
3959 if (eapd & 0x02)
3960 return AC_PWRST_D0;
3961 }
3962 return power_state;
3963}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003964EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003965
Takashi Iwai432c6412012-08-28 09:59:20 -07003966/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003967 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003968 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003969static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003970 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003971{
Takashi Iwaid8193872012-08-31 07:54:38 -07003972 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003973 int count;
3974 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003975 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003976
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003977 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003978 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003979 if (codec->depop_delay < 0)
3980 msleep(codec->epss ? 10 : 100);
3981 else if (codec->depop_delay > 0)
3982 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003983 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003984 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003985
3986 /* repeat power states setting at most 10 times*/
3987 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003988 if (codec->patch_ops.set_power_state)
3989 codec->patch_ops.set_power_state(codec, fg,
3990 power_state);
3991 else {
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003992 snd_hda_codec_read(codec, fg, flags,
Takashi Iwai432c6412012-08-28 09:59:20 -07003993 AC_VERB_SET_POWER_STATE,
3994 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003995 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003996 }
3997 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003998 if (!(state & AC_PWRST_ERROR))
3999 break;
4000 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004001
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004002 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004003}
Takashi Iwai54d17402005-11-21 16:33:22 +01004004
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004005/* sync power states of all widgets;
4006 * this is called at the end of codec parsing
4007 */
4008static void sync_power_up_states(struct hda_codec *codec)
4009{
4010 hda_nid_t nid = codec->start_nid;
4011 int i;
4012
Takashi Iwaiba615b82013-03-13 14:47:21 +01004013 /* don't care if no filter is used */
4014 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004015 return;
4016
4017 for (i = 0; i < codec->num_nodes; i++, nid++) {
4018 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004019 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004020 if (!(wcaps & AC_WCAP_POWER))
4021 continue;
4022 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4023 if (target == AC_PWRST_D0)
4024 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004025 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004026 snd_hda_codec_write(codec, nid, 0,
4027 AC_VERB_SET_POWER_STATE, target);
4028 }
4029}
4030
Takashi Iwai11aeff02008-07-30 15:01:46 +02004031#ifdef CONFIG_SND_HDA_HWDEP
4032/* execute additional init verbs */
4033static void hda_exec_init_verbs(struct hda_codec *codec)
4034{
4035 if (codec->init_verbs.list)
4036 snd_hda_sequence_write(codec, codec->init_verbs.list);
4037}
4038#else
4039static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4040#endif
4041
Takashi Iwai2a439522011-07-26 09:52:50 +02004042#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004043/*
4044 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004045 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004046 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004047static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004048{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004049 unsigned int state;
4050
Takashi Iwai989c3182012-11-19 14:14:58 +01004051 codec->in_pm = 1;
4052
Takashi Iwaicb53c622007-08-10 17:21:45 +02004053 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004054 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004055 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004056 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004057 /* Cancel delayed work if we aren't currently running from it. */
4058 if (!in_wq)
4059 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004060 spin_lock(&codec->power_lock);
4061 snd_hda_update_power_acct(codec);
4062 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004063 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004064 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004065 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004066 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004067 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004068 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004069}
4070
Takashi Iwaic370dd62012-12-13 18:30:04 +01004071/* mark all entries of cmd and amp caches dirty */
4072static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4073{
4074 int i;
4075 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4076 struct hda_cache_head *cmd;
4077 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4078 cmd->dirty = 1;
4079 }
4080 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4081 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004082 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004083 amp->head.dirty = 1;
4084 }
4085}
4086
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087/*
4088 * kick up codec; used both from PM and power-save
4089 */
4090static void hda_call_codec_resume(struct hda_codec *codec)
4091{
Takashi Iwai989c3182012-11-19 14:14:58 +01004092 codec->in_pm = 1;
4093
Takashi Iwaic370dd62012-12-13 18:30:04 +01004094 hda_mark_cmd_cache_dirty(codec);
4095
Takashi Iwai7f308302012-05-08 16:52:23 +02004096 /* set as if powered on for avoiding re-entering the resume
4097 * in the resume / power-save sequence
4098 */
4099 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004100 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004101 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004102 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004103 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004104 if (codec->patch_ops.resume)
4105 codec->patch_ops.resume(codec);
4106 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004107 if (codec->patch_ops.init)
4108 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004109 snd_hda_codec_resume_amp(codec);
4110 snd_hda_codec_resume_cache(codec);
4111 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004112
4113 if (codec->jackpoll_interval)
4114 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004115 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004116 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004117
4118 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004119 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004120}
Takashi Iwai2a439522011-07-26 09:52:50 +02004121#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004122
Takashi Iwai54d17402005-11-21 16:33:22 +01004123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124/**
4125 * snd_hda_build_controls - build mixer controls
4126 * @bus: the BUS
4127 *
4128 * Creates mixer controls for each codec included in the bus.
4129 *
4130 * Returns 0 if successful, otherwise a negative error code.
4131 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004132int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004134 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Takashi Iwai0ba21762007-04-16 11:29:14 +02004136 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004137 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004138 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01004139 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004140 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004141 err = snd_hda_codec_reset(codec);
4142 if (err < 0) {
4143 printk(KERN_ERR
4144 "hda_codec: cannot revert codec\n");
4145 return err;
4146 }
4147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004149 return 0;
4150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004151EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004153/*
4154 * add standard channel maps if not specified
4155 */
4156static int add_std_chmaps(struct hda_codec *codec)
4157{
4158 int i, str, err;
4159
4160 for (i = 0; i < codec->num_pcms; i++) {
4161 for (str = 0; str < 2; str++) {
4162 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4163 struct hda_pcm_stream *hinfo =
4164 &codec->pcm_info[i].stream[str];
4165 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004166 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004167
4168 if (codec->pcm_info[i].own_chmap)
4169 continue;
4170 if (!pcm || !hinfo->substreams)
4171 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004172 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4173 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004174 hinfo->channels_max,
4175 0, &chmap);
4176 if (err < 0)
4177 return err;
4178 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4179 }
4180 }
4181 return 0;
4182}
4183
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004184/* default channel maps for 2.1 speakers;
4185 * since HD-audio supports only stereo, odd number channels are omitted
4186 */
4187const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4188 { .channels = 2,
4189 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4190 { .channels = 4,
4191 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4192 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4193 { }
4194};
4195EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4196
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004197int snd_hda_codec_build_controls(struct hda_codec *codec)
4198{
4199 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004200 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004201 /* continue to initialize... */
4202 if (codec->patch_ops.init)
4203 err = codec->patch_ops.init(codec);
4204 if (!err && codec->patch_ops.build_controls)
4205 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004206 if (err < 0)
4207 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004208
4209 /* we create chmaps here instead of build_pcms */
4210 err = add_std_chmaps(codec);
4211 if (err < 0)
4212 return err;
4213
David Henningsson26a6cb62012-10-09 15:04:21 +02004214 if (codec->jackpoll_interval)
4215 hda_jackpoll_work(&codec->jackpoll_work.work);
4216 else
4217 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004218 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return 0;
4220}
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222/*
4223 * stream formats
4224 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004225struct hda_rate_tbl {
4226 unsigned int hz;
4227 unsigned int alsa_bits;
4228 unsigned int hda_fmt;
4229};
4230
Takashi Iwai92f10b32010-08-03 14:21:00 +02004231/* rate = base * mult / div */
4232#define HDA_RATE(base, mult, div) \
4233 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4234 (((div) - 1) << AC_FMT_DIV_SHIFT))
4235
Takashi Iwaibefdf312005-08-22 13:57:55 +02004236static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004238
4239 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004240 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4241 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4242 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4243 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4244 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4245 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4246 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4247 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4248 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4249 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4250 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004251#define AC_PAR_PCM_RATE_BITS 11
4252 /* up to bits 10, 384kHZ isn't supported properly */
4253
4254 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004255 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004256
Takashi Iwaibefdf312005-08-22 13:57:55 +02004257 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258};
4259
4260/**
4261 * snd_hda_calc_stream_format - calculate format bitset
4262 * @rate: the sample rate
4263 * @channels: the number of channels
4264 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4265 * @maxbps: the max. bps
4266 *
4267 * Calculate the format bitset from the given rate, channels and th PCM format.
4268 *
4269 * Return zero if invalid.
4270 */
4271unsigned int snd_hda_calc_stream_format(unsigned int rate,
4272 unsigned int channels,
4273 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004274 unsigned int maxbps,
4275 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 int i;
4278 unsigned int val = 0;
4279
Takashi Iwaibefdf312005-08-22 13:57:55 +02004280 for (i = 0; rate_bits[i].hz; i++)
4281 if (rate_bits[i].hz == rate) {
4282 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 break;
4284 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004285 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 snd_printdd("invalid rate %d\n", rate);
4287 return 0;
4288 }
4289
4290 if (channels == 0 || channels > 8) {
4291 snd_printdd("invalid channels %d\n", channels);
4292 return 0;
4293 }
4294 val |= channels - 1;
4295
4296 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004297 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004298 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004299 break;
4300 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004301 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 case 20:
4304 case 24:
4305 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004306 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004307 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004309 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004311 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 break;
4313 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004314 snd_printdd("invalid format width %d\n",
4315 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return 0;
4317 }
4318
Anssi Hannula32c168c2010-08-03 13:28:57 +03004319 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004320 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return val;
4323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004324EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004326static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4327 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004328{
4329 unsigned int val = 0;
4330 if (nid != codec->afg &&
4331 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4332 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4333 if (!val || val == -1)
4334 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4335 if (!val || val == -1)
4336 return 0;
4337 return val;
4338}
4339
4340static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4341{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004342 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004343 get_pcm_param);
4344}
4345
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004346static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4347 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004348{
4349 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4350 if (!streams || streams == -1)
4351 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4352 if (!streams || streams == -1)
4353 return 0;
4354 return streams;
4355}
4356
4357static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4358{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004359 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004360 get_stream_param);
4361}
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363/**
4364 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4365 * @codec: the HDA codec
4366 * @nid: NID to query
4367 * @ratesp: the pointer to store the detected rate bitflags
4368 * @formatsp: the pointer to store the detected formats
4369 * @bpsp: the pointer to store the detected format widths
4370 *
4371 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4372 * or @bsps argument is ignored.
4373 *
4374 * Returns 0 if successful, otherwise a negative error code.
4375 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004376int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4378{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004379 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004381 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004382 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 if (ratesp) {
4385 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004386 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004388 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004390 if (rates == 0) {
4391 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4392 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4393 nid, val,
4394 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4395 return -EIO;
4396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 *ratesp = rates;
4398 }
4399
4400 if (formatsp || bpsp) {
4401 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004402 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004404 streams = query_stream_param(codec, nid);
4405 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
4408 bps = 0;
4409 if (streams & AC_SUPFMT_PCM) {
4410 if (val & AC_SUPPCM_BITS_8) {
4411 formats |= SNDRV_PCM_FMTBIT_U8;
4412 bps = 8;
4413 }
4414 if (val & AC_SUPPCM_BITS_16) {
4415 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4416 bps = 16;
4417 }
4418 if (wcaps & AC_WCAP_DIGITAL) {
4419 if (val & AC_SUPPCM_BITS_32)
4420 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4421 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4422 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4423 if (val & AC_SUPPCM_BITS_24)
4424 bps = 24;
4425 else if (val & AC_SUPPCM_BITS_20)
4426 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004427 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4428 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4430 if (val & AC_SUPPCM_BITS_32)
4431 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 else if (val & AC_SUPPCM_BITS_24)
4433 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004434 else if (val & AC_SUPPCM_BITS_20)
4435 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
4437 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004438#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004439 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004441 if (!bps)
4442 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004443 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004444#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004445 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004446 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 /* temporary hack: we have still no proper support
4448 * for the direct AC3 stream...
4449 */
4450 formats |= SNDRV_PCM_FMTBIT_U8;
4451 bps = 8;
4452 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004453 if (formats == 0) {
4454 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4455 "(nid=0x%x, val=0x%x, ovrd=%i, "
4456 "streams=0x%x)\n",
4457 nid, val,
4458 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4459 streams);
4460 return -EIO;
4461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (formatsp)
4463 *formatsp = formats;
4464 if (bpsp)
4465 *bpsp = bps;
4466 }
4467
4468 return 0;
4469}
Stephen Warren384a48d2011-06-01 11:14:21 -06004470EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
4472/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004473 * snd_hda_is_supported_format - Check the validity of the format
4474 * @codec: HD-audio codec
4475 * @nid: NID to check
4476 * @format: the HD-audio format value to check
4477 *
4478 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 *
4480 * Returns 1 if supported, 0 if not.
4481 */
4482int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4483 unsigned int format)
4484{
4485 int i;
4486 unsigned int val = 0, rate, stream;
4487
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004488 val = query_pcm_param(codec, nid);
4489 if (!val)
4490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004493 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004494 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (val & (1 << i))
4496 break;
4497 return 0;
4498 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004499 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return 0;
4501
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004502 stream = query_stream_param(codec, nid);
4503 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return 0;
4505
4506 if (stream & AC_SUPFMT_PCM) {
4507 switch (format & 0xf0) {
4508 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004509 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return 0;
4511 break;
4512 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004513 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return 0;
4515 break;
4516 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004517 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 return 0;
4519 break;
4520 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004521 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return 0;
4523 break;
4524 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004525 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return 0;
4527 break;
4528 default:
4529 return 0;
4530 }
4531 } else {
4532 /* FIXME: check for float32 and AC3? */
4533 }
4534
4535 return 1;
4536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004537EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539/*
4540 * PCM stuff
4541 */
4542static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4543 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004544 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
4546 return 0;
4547}
4548
4549static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 unsigned int stream_tag,
4552 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004553 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4556 return 0;
4557}
4558
4559static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4560 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004561 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Takashi Iwai888afa12008-03-18 09:57:50 +01004563 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return 0;
4565}
4566
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004567static int set_pcm_default_values(struct hda_codec *codec,
4568 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004570 int err;
4571
Takashi Iwai0ba21762007-04-16 11:29:14 +02004572 /* query support PCM information from the given NID */
4573 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004574 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004575 info->rates ? NULL : &info->rates,
4576 info->formats ? NULL : &info->formats,
4577 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004578 if (err < 0)
4579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 }
4581 if (info->ops.open == NULL)
4582 info->ops.open = hda_pcm_default_open_close;
4583 if (info->ops.close == NULL)
4584 info->ops.close = hda_pcm_default_open_close;
4585 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004586 if (snd_BUG_ON(!info->nid))
4587 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 info->ops.prepare = hda_pcm_default_prepare;
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004591 if (snd_BUG_ON(!info->nid))
4592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 info->ops.cleanup = hda_pcm_default_cleanup;
4594 }
4595 return 0;
4596}
4597
Takashi Iwaieb541332010-08-06 13:48:11 +02004598/*
4599 * codec prepare/cleanup entries
4600 */
4601int snd_hda_codec_prepare(struct hda_codec *codec,
4602 struct hda_pcm_stream *hinfo,
4603 unsigned int stream,
4604 unsigned int format,
4605 struct snd_pcm_substream *substream)
4606{
4607 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004608 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004609 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4610 if (ret >= 0)
4611 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004612 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004613 return ret;
4614}
4615EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4616
4617void snd_hda_codec_cleanup(struct hda_codec *codec,
4618 struct hda_pcm_stream *hinfo,
4619 struct snd_pcm_substream *substream)
4620{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004621 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004622 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004623 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004624}
4625EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4626
Takashi Iwaid5191e52009-11-16 14:58:17 +01004627/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004628const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4629 "Audio", "SPDIF", "HDMI", "Modem"
4630};
4631
Takashi Iwai176d5332008-07-30 15:01:44 +02004632/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004633 * get the empty PCM device number to assign
4634 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004635static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004636{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004637 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004638 /* assigned to static slots up to dev#10; if more needed, assign
4639 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4640 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004641 static int audio_idx[HDA_PCM_NTYPES][5] = {
4642 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4643 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004644 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004645 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004646 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004647 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004648
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004649 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004650 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4651 return -EINVAL;
4652 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004653
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004654 for (i = 0; audio_idx[type][i] >= 0; i++) {
4655#ifndef CONFIG_SND_DYNAMIC_MINORS
4656 if (audio_idx[type][i] >= 8)
4657 break;
4658#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004659 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4660 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004661 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004662
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004663#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004664 /* non-fixed slots starting from 10 */
4665 for (i = 10; i < 32; i++) {
4666 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4667 return i;
4668 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004669#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004670
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004671 snd_printk(KERN_WARNING "Too many %s devices\n",
4672 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004673#ifndef CONFIG_SND_DYNAMIC_MINORS
4674 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4675#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004676 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004677}
4678
4679/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004680 * attach a new PCM stream
4681 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004682static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004683{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004684 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004685 struct hda_pcm_stream *info;
4686 int stream, err;
4687
Takashi Iwaib91f0802008-11-04 08:43:08 +01004688 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004689 return -EINVAL;
4690 for (stream = 0; stream < 2; stream++) {
4691 info = &pcm->stream[stream];
4692 if (info->substreams) {
4693 err = set_pcm_default_values(codec, info);
4694 if (err < 0)
4695 return err;
4696 }
4697 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004698 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004699}
4700
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004701/* assign all PCMs of the given codec */
4702int snd_hda_codec_build_pcms(struct hda_codec *codec)
4703{
4704 unsigned int pcm;
4705 int err;
4706
4707 if (!codec->num_pcms) {
4708 if (!codec->patch_ops.build_pcms)
4709 return 0;
4710 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004711 if (err < 0) {
4712 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004713 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004714 err = snd_hda_codec_reset(codec);
4715 if (err < 0) {
4716 printk(KERN_ERR
4717 "hda_codec: cannot revert codec\n");
4718 return err;
4719 }
4720 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004721 }
4722 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4723 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4724 int dev;
4725
4726 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004727 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004728
4729 if (!cpcm->pcm) {
4730 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4731 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004732 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004733 cpcm->device = dev;
4734 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004735 if (err < 0) {
4736 printk(KERN_ERR "hda_codec: cannot attach "
4737 "PCM stream %d for codec #%d\n",
4738 dev, codec->addr);
4739 continue; /* no fatal error */
4740 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004741 }
4742 }
4743 return 0;
4744}
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746/**
4747 * snd_hda_build_pcms - build PCM information
4748 * @bus: the BUS
4749 *
4750 * Create PCM information for each codec included in the bus.
4751 *
4752 * The build_pcms codec patch is requested to set up codec->num_pcms and
4753 * codec->pcm_info properly. The array is referred by the top-level driver
4754 * to create its PCM instances.
4755 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4756 * callback.
4757 *
4758 * At least, substreams, channels_min and channels_max must be filled for
4759 * each stream. substreams = 0 indicates that the stream doesn't exist.
4760 * When rates and/or formats are zero, the supported values are queried
4761 * from the given nid. The nid is used also by the default ops.prepare
4762 * and ops.cleanup callbacks.
4763 *
4764 * The driver needs to call ops.open in its open callback. Similarly,
4765 * ops.close is supposed to be called in the close callback.
4766 * ops.prepare should be called in the prepare or hw_params callback
4767 * with the proper parameters for set up.
4768 * ops.cleanup should be called in hw_free for clean up of streams.
4769 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004770 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004772int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004774 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Takashi Iwai0ba21762007-04-16 11:29:14 +02004776 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004777 int err = snd_hda_codec_build_pcms(codec);
4778 if (err < 0)
4779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
4781 return 0;
4782}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004783EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785/**
4786 * snd_hda_check_board_config - compare the current codec with the config table
4787 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004788 * @num_configs: number of config enums
4789 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 * @tbl: configuration table, terminated by null entries
4791 *
4792 * Compares the modelname or PCI subsystem id of the current codec with the
4793 * given configuration table. If a matching entry is found, returns its
4794 * config value (supposed to be 0 or positive).
4795 *
4796 * If no entries are matching, the function returns a negative value.
4797 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004798int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004799 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004800 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004802 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004803 int i;
4804 for (i = 0; i < num_configs; i++) {
4805 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004806 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004807 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4808 "selected\n", models[i]);
4809 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 }
4811 }
4812 }
4813
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004814 if (!codec->bus->pci || !tbl)
4815 return -1;
4816
4817 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4818 if (!tbl)
4819 return -1;
4820 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004821#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004822 char tmp[10];
4823 const char *model = NULL;
4824 if (models)
4825 model = models[tbl->value];
4826 if (!model) {
4827 sprintf(tmp, "#%d", tbl->value);
4828 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004830 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4831 "for config %x:%x (%s)\n",
4832 model, tbl->subvendor, tbl->subdevice,
4833 (tbl->name ? tbl->name : "Unknown device"));
4834#endif
4835 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
4837 return -1;
4838}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004839EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004842 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004843 subsystem ID with the
4844 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004845
4846 This is important for Gateway notebooks with SB450 HDA Audio
4847 where the vendor ID of the PCI device is:
4848 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4849 and the vendor/subvendor are found only at the codec.
4850
4851 * @codec: the HDA codec
4852 * @num_configs: number of config enums
4853 * @models: array of model name strings
4854 * @tbl: configuration table, terminated by null entries
4855 *
4856 * Compares the modelname or PCI subsystem id of the current codec with the
4857 * given configuration table. If a matching entry is found, returns its
4858 * config value (supposed to be 0 or positive).
4859 *
4860 * If no entries are matching, the function returns a negative value.
4861 */
4862int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004863 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004864 const struct snd_pci_quirk *tbl)
4865{
4866 const struct snd_pci_quirk *q;
4867
4868 /* Search for codec ID */
4869 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004870 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4871 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4872 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004873 break;
4874 }
4875
4876 if (!q->subvendor)
4877 return -1;
4878
4879 tbl = q;
4880
4881 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004882#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004883 char tmp[10];
4884 const char *model = NULL;
4885 if (models)
4886 model = models[tbl->value];
4887 if (!model) {
4888 sprintf(tmp, "#%d", tbl->value);
4889 model = tmp;
4890 }
4891 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4892 "for config %x:%x (%s)\n",
4893 model, tbl->subvendor, tbl->subdevice,
4894 (tbl->name ? tbl->name : "Unknown device"));
4895#endif
4896 return tbl->value;
4897 }
4898 return -1;
4899}
4900EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4901
4902/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 * snd_hda_add_new_ctls - create controls from the array
4904 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004905 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 *
4907 * This helper function creates and add new controls in the given array.
4908 * The array must be terminated with an empty entry as terminator.
4909 *
4910 * Returns 0 if successful, or a negative error code.
4911 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004912int snd_hda_add_new_ctls(struct hda_codec *codec,
4913 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004915 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004918 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004919 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004920 if (knew->iface == -1) /* skip this codec private value */
4921 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004922 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004923 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004924 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004925 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004926 if (addr > 0)
4927 kctl->id.device = addr;
4928 if (idx > 0)
4929 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004930 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004931 if (!err)
4932 break;
4933 /* try first with another device index corresponding to
4934 * the codec addr; if it still fails (or it's the
4935 * primary codec), then try another control index
4936 */
4937 if (!addr && codec->addr)
4938 addr = codec->addr;
4939 else if (!idx && !knew->index) {
4940 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004941 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004942 if (idx <= 0)
4943 return err;
4944 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004945 return err;
4946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
4948 return 0;
4949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004950EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Takashi Iwai83012a72012-08-24 18:38:08 +02004952#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004953static void hda_power_work(struct work_struct *work)
4954{
4955 struct hda_codec *codec =
4956 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004957 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004958 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004959
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004960 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004961 if (codec->power_transition > 0) { /* during power-up sequence? */
4962 spin_unlock(&codec->power_lock);
4963 return;
4964 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004965 if (!codec->power_on || codec->power_count) {
4966 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004967 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004968 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004969 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004970 spin_unlock(&codec->power_lock);
4971
Dylan Reidd17344b2012-09-28 15:57:01 -07004972 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004973 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4974 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004975}
4976
4977static void hda_keep_power_on(struct hda_codec *codec)
4978{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004979 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004980 codec->power_count++;
4981 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004982 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004983 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004984 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004985}
4986
Takashi Iwaid5191e52009-11-16 14:58:17 +01004987/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004988void snd_hda_update_power_acct(struct hda_codec *codec)
4989{
4990 unsigned long delta = jiffies - codec->power_jiffies;
4991 if (codec->power_on)
4992 codec->power_on_acct += delta;
4993 else
4994 codec->power_off_acct += delta;
4995 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004996}
4997
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004998/* Transition to powered up, if wait_power_down then wait for a pending
4999 * transition to D3 to complete. A pending D3 transition is indicated
5000 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005001/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005002static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005003{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005004 /* Return if power_on or transitioning to power_on, unless currently
5005 * powering down. */
5006 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005007 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005008 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005009 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005010
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005011 cancel_delayed_work_sync(&codec->power_work);
5012
5013 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005014 /* If the power down delayed work was cancelled above before starting,
5015 * then there is no need to go through power up here.
5016 */
5017 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005018 if (codec->power_transition < 0)
5019 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005020 return;
5021 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005022
Takashi Iwaid66fee52011-08-02 15:39:31 +02005023 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005024 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005025 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005026 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005027 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005028 spin_unlock(&codec->power_lock);
5029
Takashi Iwaicb53c622007-08-10 17:21:45 +02005030 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005031
5032 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005033 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005034}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005035
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005036#define power_save(codec) \
5037 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005038
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005039/* Transition to powered down */
5040static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005041{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005042 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005043 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005044
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005045 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005046 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005047 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005048 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005049 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005050}
5051
5052/**
5053 * snd_hda_power_save - Power-up/down/sync the codec
5054 * @codec: HD-audio codec
5055 * @delta: the counter delta to change
5056 *
5057 * Change the power-up counter via @delta, and power up or down the hardware
5058 * appropriately. For the power-down, queue to the delayed action.
5059 * Passing zero to @delta means to synchronize the power state.
5060 */
5061void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5062{
5063 spin_lock(&codec->power_lock);
5064 codec->power_count += delta;
5065 trace_hda_power_count(codec);
5066 if (delta > 0)
5067 __snd_hda_power_up(codec, d3wait);
5068 else
5069 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005070 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005071}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005072EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005073
Takashi Iwaid5191e52009-11-16 14:58:17 +01005074/**
5075 * snd_hda_check_amp_list_power - Check the amp list and update the power
5076 * @codec: HD-audio codec
5077 * @check: the object containing an AMP list and the status
5078 * @nid: NID to check / update
5079 *
5080 * Check whether the given NID is in the amp list. If it's in the list,
5081 * check the current AMP status, and update the the power-status according
5082 * to the mute status.
5083 *
5084 * This function is supposed to be set or called from the check_power_status
5085 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005086 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005087int snd_hda_check_amp_list_power(struct hda_codec *codec,
5088 struct hda_loopback_check *check,
5089 hda_nid_t nid)
5090{
Takashi Iwai031024e2011-05-02 11:29:30 +02005091 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005092 int ch, v;
5093
5094 if (!check->amplist)
5095 return 0;
5096 for (p = check->amplist; p->nid; p++) {
5097 if (p->nid == nid)
5098 break;
5099 }
5100 if (!p->nid)
5101 return 0; /* nothing changed */
5102
5103 for (p = check->amplist; p->nid; p++) {
5104 for (ch = 0; ch < 2; ch++) {
5105 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5106 p->idx);
5107 if (!(v & HDA_AMP_MUTE) && v > 0) {
5108 if (!check->power_on) {
5109 check->power_on = 1;
5110 snd_hda_power_up(codec);
5111 }
5112 return 1;
5113 }
5114 }
5115 }
5116 if (check->power_on) {
5117 check->power_on = 0;
5118 snd_hda_power_down(codec);
5119 }
5120 return 0;
5121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005122EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005123
Mengdong Lin12edb892013-11-26 23:32:23 -05005124static void hda_suspend_work(struct work_struct *work)
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005125{
5126 struct hda_codec *codec =
Mengdong Lin12edb892013-11-26 23:32:23 -05005127 container_of(work, struct hda_codec, suspend_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005128
5129 hda_call_codec_suspend(codec, false);
5130}
Mengdong Lin12edb892013-11-26 23:32:23 -05005131
5132static void hda_resume_work(struct work_struct *work)
5133{
5134 struct hda_codec *codec =
5135 container_of(work, struct hda_codec, resume_work);
5136
5137 hda_call_codec_resume(codec);
5138}
Takashi Iwaicb53c622007-08-10 17:21:45 +02005139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005141/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005142 * Channel mode helper
5143 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005144
5145/**
5146 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5147 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005148int snd_hda_ch_mode_info(struct hda_codec *codec,
5149 struct snd_ctl_elem_info *uinfo,
5150 const struct hda_channel_mode *chmode,
5151 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005152{
5153 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5154 uinfo->count = 1;
5155 uinfo->value.enumerated.items = num_chmodes;
5156 if (uinfo->value.enumerated.item >= num_chmodes)
5157 uinfo->value.enumerated.item = num_chmodes - 1;
5158 sprintf(uinfo->value.enumerated.name, "%dch",
5159 chmode[uinfo->value.enumerated.item].channels);
5160 return 0;
5161}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005162EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005163
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164/**
5165 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5166 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005167int snd_hda_ch_mode_get(struct hda_codec *codec,
5168 struct snd_ctl_elem_value *ucontrol,
5169 const struct hda_channel_mode *chmode,
5170 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005171 int max_channels)
5172{
5173 int i;
5174
5175 for (i = 0; i < num_chmodes; i++) {
5176 if (max_channels == chmode[i].channels) {
5177 ucontrol->value.enumerated.item[0] = i;
5178 break;
5179 }
5180 }
5181 return 0;
5182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005183EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005184
Takashi Iwaid5191e52009-11-16 14:58:17 +01005185/**
5186 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5187 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005188int snd_hda_ch_mode_put(struct hda_codec *codec,
5189 struct snd_ctl_elem_value *ucontrol,
5190 const struct hda_channel_mode *chmode,
5191 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005192 int *max_channelsp)
5193{
5194 unsigned int mode;
5195
5196 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005197 if (mode >= num_chmodes)
5198 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005199 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005200 return 0;
5201 /* change the current channel setting */
5202 *max_channelsp = chmode[mode].channels;
5203 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005204 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005205 return 1;
5206}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005207EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209/*
5210 * input MUX helper
5211 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005212
5213/**
5214 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5215 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005216int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5217 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
5219 unsigned int index;
5220
5221 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5222 uinfo->count = 1;
5223 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005224 if (!imux->num_items)
5225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 index = uinfo->value.enumerated.item;
5227 if (index >= imux->num_items)
5228 index = imux->num_items - 1;
5229 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5230 return 0;
5231}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005232EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Takashi Iwaid5191e52009-11-16 14:58:17 +01005234/**
5235 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5236 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005237int snd_hda_input_mux_put(struct hda_codec *codec,
5238 const struct hda_input_mux *imux,
5239 struct snd_ctl_elem_value *ucontrol,
5240 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 unsigned int *cur_val)
5242{
5243 unsigned int idx;
5244
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005245 if (!imux->num_items)
5246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 idx = ucontrol->value.enumerated.item[0];
5248 if (idx >= imux->num_items)
5249 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005250 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005252 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5253 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 *cur_val = idx;
5255 return 1;
5256}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005257EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
5259
5260/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005261 * process kcontrol info callback of a simple string enum array
5262 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5263 */
5264int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5265 struct snd_ctl_elem_info *uinfo,
5266 int num_items, const char * const *texts)
5267{
5268 static const char * const texts_default[] = {
5269 "Disabled", "Enabled"
5270 };
5271
5272 if (!texts || !num_items) {
5273 num_items = 2;
5274 texts = texts_default;
5275 }
5276
5277 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5278 uinfo->count = 1;
5279 uinfo->value.enumerated.items = num_items;
5280 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5281 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5282 strcpy(uinfo->value.enumerated.name,
5283 texts[uinfo->value.enumerated.item]);
5284 return 0;
5285}
5286EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5287
5288/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 * Multi-channel / digital-out PCM helper functions
5290 */
5291
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005292/* setup SPDIF output stream */
5293static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5294 unsigned int stream_tag, unsigned int format)
5295{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005296 struct hda_spdif_out *spdif;
5297 unsigned int curr_fmt;
5298 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005299
Laurence Darby3bef1c32012-11-03 17:00:06 +00005300 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5301 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5302 AC_VERB_GET_STREAM_FORMAT, 0);
5303 reset = codec->spdif_status_reset &&
5304 (spdif->ctls & AC_DIG1_ENABLE) &&
5305 curr_fmt != format;
5306
5307 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5308 updated */
5309 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005310 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005311 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005312 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005313 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005314 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005315 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005316 for (d = codec->slave_dig_outs; *d; d++)
5317 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5318 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005319 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005320 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005321 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005322 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005323 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005324}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005325
Takashi Iwai2f728532008-09-25 16:32:41 +02005326static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5327{
5328 snd_hda_codec_cleanup_stream(codec, nid);
5329 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005330 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005331 for (d = codec->slave_dig_outs; *d; d++)
5332 snd_hda_codec_cleanup_stream(codec, *d);
5333 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005334}
5335
Takashi Iwaid5191e52009-11-16 14:58:17 +01005336/**
5337 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5338 * @bus: HD-audio bus
5339 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005340void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5341{
5342 struct hda_codec *codec;
5343
5344 if (!bus)
5345 return;
5346 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005347 if (hda_codec_is_power_on(codec) &&
5348 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005349 codec->patch_ops.reboot_notify(codec);
5350 }
5351}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005352EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005353
Takashi Iwaid5191e52009-11-16 14:58:17 +01005354/**
5355 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005357int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5358 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
Ingo Molnar62932df2006-01-16 16:34:20 +01005360 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005361 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5362 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005363 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005365 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return 0;
5367}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005368EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Takashi Iwaid5191e52009-11-16 14:58:17 +01005370/**
5371 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5372 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005373int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5374 struct hda_multi_out *mout,
5375 unsigned int stream_tag,
5376 unsigned int format,
5377 struct snd_pcm_substream *substream)
5378{
5379 mutex_lock(&codec->spdif_mutex);
5380 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5381 mutex_unlock(&codec->spdif_mutex);
5382 return 0;
5383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005384EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005385
Takashi Iwaid5191e52009-11-16 14:58:17 +01005386/**
5387 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5388 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005389int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5390 struct hda_multi_out *mout)
5391{
5392 mutex_lock(&codec->spdif_mutex);
5393 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5394 mutex_unlock(&codec->spdif_mutex);
5395 return 0;
5396}
5397EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5398
Takashi Iwaid5191e52009-11-16 14:58:17 +01005399/**
5400 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005402int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5403 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
Ingo Molnar62932df2006-01-16 16:34:20 +01005405 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005407 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return 0;
5409}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005410EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Takashi Iwaid5191e52009-11-16 14:58:17 +01005412/**
5413 * snd_hda_multi_out_analog_open - open analog outputs
5414 *
5415 * Open analog outputs and set up the hw-constraints.
5416 * If the digital outputs can be opened as slave, open the digital
5417 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005419int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5420 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005421 struct snd_pcm_substream *substream,
5422 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Takashi Iwai9a081602008-02-12 18:37:26 +01005424 struct snd_pcm_runtime *runtime = substream->runtime;
5425 runtime->hw.channels_max = mout->max_channels;
5426 if (mout->dig_out_nid) {
5427 if (!mout->analog_rates) {
5428 mout->analog_rates = hinfo->rates;
5429 mout->analog_formats = hinfo->formats;
5430 mout->analog_maxbps = hinfo->maxbps;
5431 } else {
5432 runtime->hw.rates = mout->analog_rates;
5433 runtime->hw.formats = mout->analog_formats;
5434 hinfo->maxbps = mout->analog_maxbps;
5435 }
5436 if (!mout->spdif_rates) {
5437 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5438 &mout->spdif_rates,
5439 &mout->spdif_formats,
5440 &mout->spdif_maxbps);
5441 }
5442 mutex_lock(&codec->spdif_mutex);
5443 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005444 if ((runtime->hw.rates & mout->spdif_rates) &&
5445 (runtime->hw.formats & mout->spdif_formats)) {
5446 runtime->hw.rates &= mout->spdif_rates;
5447 runtime->hw.formats &= mout->spdif_formats;
5448 if (mout->spdif_maxbps < hinfo->maxbps)
5449 hinfo->maxbps = mout->spdif_maxbps;
5450 } else {
5451 mout->share_spdif = 0;
5452 /* FIXME: need notify? */
5453 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005454 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005455 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5458 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5459}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005460EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Takashi Iwaid5191e52009-11-16 14:58:17 +01005462/**
5463 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5464 *
5465 * Set up the i/o for analog out.
5466 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005468int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5469 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 unsigned int stream_tag,
5471 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005472 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
Takashi Iwaidda14412011-05-02 11:29:30 +02005474 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005476 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 int i;
5478
Ingo Molnar62932df2006-01-16 16:34:20 +01005479 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005480 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005481 if (mout->dig_out_nid && mout->share_spdif &&
5482 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005484 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5485 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005486 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005488 setup_dig_out_stream(codec, mout->dig_out_nid,
5489 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 } else {
5491 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005492 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
5494 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005495 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005498 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5499 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005500 if (!mout->no_share_stream &&
5501 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005503 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5504 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005505 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005506 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5507 if (!mout->no_share_stream && mout->hp_out_nid[i])
5508 snd_hda_codec_setup_stream(codec,
5509 mout->hp_out_nid[i],
5510 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /* surrounds */
5513 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005514 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005515 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5516 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005517 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005518 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5519 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005521
5522 /* extra surrounds */
5523 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5524 int ch = 0;
5525 if (!mout->extra_out_nid[i])
5526 break;
5527 if (chs >= (i + 1) * 2)
5528 ch = i * 2;
5529 else if (!mout->no_share_stream)
5530 break;
5531 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5532 stream_tag, ch, format);
5533 }
5534
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return 0;
5536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005537EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Takashi Iwaid5191e52009-11-16 14:58:17 +01005539/**
5540 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005542int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5543 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
Takashi Iwaidda14412011-05-02 11:29:30 +02005545 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 int i;
5547
5548 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005549 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005551 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005552 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5553 if (mout->hp_out_nid[i])
5554 snd_hda_codec_cleanup_stream(codec,
5555 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005556 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5557 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005558 snd_hda_codec_cleanup_stream(codec,
5559 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005560 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005562 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 mout->dig_out_used = 0;
5564 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005565 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return 0;
5567}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005568EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
Takashi Iwai47408602012-04-20 13:06:53 +02005570/**
5571 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5572 *
5573 * Guess the suitable VREF pin bits to be set as the pin-control value.
5574 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5575 */
5576unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5577{
5578 unsigned int pincap;
5579 unsigned int oldval;
5580 oldval = snd_hda_codec_read(codec, pin, 0,
5581 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5582 pincap = snd_hda_query_pin_caps(codec, pin);
5583 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5584 /* Exception: if the default pin setup is vref50, we give it priority */
5585 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5586 return AC_PINCTL_VREF_80;
5587 else if (pincap & AC_PINCAP_VREF_50)
5588 return AC_PINCTL_VREF_50;
5589 else if (pincap & AC_PINCAP_VREF_100)
5590 return AC_PINCTL_VREF_100;
5591 else if (pincap & AC_PINCAP_VREF_GRD)
5592 return AC_PINCTL_VREF_GRD;
5593 return AC_PINCTL_VREF_HIZ;
5594}
5595EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5596
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005597/* correct the pin ctl value for matching with the pin cap */
5598unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5599 hda_nid_t pin, unsigned int val)
5600{
5601 static unsigned int cap_lists[][2] = {
5602 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5603 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5604 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5605 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5606 };
5607 unsigned int cap;
5608
5609 if (!val)
5610 return 0;
5611 cap = snd_hda_query_pin_caps(codec, pin);
5612 if (!cap)
5613 return val; /* don't know what to do... */
5614
5615 if (val & AC_PINCTL_OUT_EN) {
5616 if (!(cap & AC_PINCAP_OUT))
5617 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5618 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5619 val &= ~AC_PINCTL_HP_EN;
5620 }
5621
5622 if (val & AC_PINCTL_IN_EN) {
5623 if (!(cap & AC_PINCAP_IN))
5624 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5625 else {
5626 unsigned int vcap, vref;
5627 int i;
5628 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5629 vref = val & AC_PINCTL_VREFEN;
5630 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5631 if (vref == cap_lists[i][0] &&
5632 !(vcap & cap_lists[i][1])) {
5633 if (i == ARRAY_SIZE(cap_lists) - 1)
5634 vref = AC_PINCTL_VREF_HIZ;
5635 else
5636 vref = cap_lists[i + 1][0];
5637 }
5638 }
5639 val &= ~AC_PINCTL_VREFEN;
5640 val |= vref;
5641 }
5642 }
5643
5644 return val;
5645}
5646EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5647
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005648int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5649 unsigned int val, bool cached)
5650{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005651 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005652 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005653 if (cached)
5654 return snd_hda_codec_update_cache(codec, pin, 0,
5655 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5656 else
5657 return snd_hda_codec_write(codec, pin, 0,
5658 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5659}
5660EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5661
Takashi Iwai990061c2010-09-09 22:08:44 +02005662/**
5663 * snd_hda_add_imux_item - Add an item to input_mux
5664 *
5665 * When the same label is used already in the existing items, the number
5666 * suffix is appended to the label. This label index number is stored
5667 * to type_idx when non-NULL pointer is given.
5668 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005669int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5670 int index, int *type_idx)
5671{
5672 int i, label_idx = 0;
5673 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5674 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5675 return -EINVAL;
5676 }
5677 for (i = 0; i < imux->num_items; i++) {
5678 if (!strncmp(label, imux->items[i].label, strlen(label)))
5679 label_idx++;
5680 }
5681 if (type_idx)
5682 *type_idx = label_idx;
5683 if (label_idx > 0)
5684 snprintf(imux->items[imux->num_items].label,
5685 sizeof(imux->items[imux->num_items].label),
5686 "%s %d", label, label_idx);
5687 else
5688 strlcpy(imux->items[imux->num_items].label, label,
5689 sizeof(imux->items[imux->num_items].label));
5690 imux->items[imux->num_items].index = index;
5691 imux->num_items++;
5692 return 0;
5693}
5694EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005695
Takashi Iwai4a471b72005-12-07 13:56:29 +01005696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697#ifdef CONFIG_PM
5698/*
5699 * power management
5700 */
5701
5702/**
5703 * snd_hda_suspend - suspend the codecs
5704 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 *
5706 * Returns 0 if successful.
5707 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005708int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005710 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
Takashi Iwai0ba21762007-04-16 11:29:14 +02005712 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005713 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005714 if (hda_codec_is_power_on(codec)) {
5715 if (bus->num_codecs > 1)
Mengdong Lin12edb892013-11-26 23:32:23 -05005716 queue_work(bus->pm_wq, &codec->suspend_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005717 else
5718 hda_call_codec_suspend(codec, false);
5719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005721
5722 if (bus->num_codecs > 1)
5723 flush_workqueue(bus->pm_wq);
5724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 return 0;
5726}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005727EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729/**
5730 * snd_hda_resume - resume the codecs
5731 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 *
5733 * Returns 0 if successful.
5734 */
5735int snd_hda_resume(struct hda_bus *bus)
5736{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005737 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
Takashi Iwai0ba21762007-04-16 11:29:14 +02005739 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005740 if (bus->num_codecs > 1)
5741 queue_work(bus->pm_wq, &codec->resume_work);
5742 else
5743 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005745
5746 if (bus->num_codecs > 1)
5747 flush_workqueue(bus->pm_wq);
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return 0;
5750}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005751EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005752#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005753
5754/*
5755 * generic arrays
5756 */
5757
Takashi Iwaid5191e52009-11-16 14:58:17 +01005758/**
5759 * snd_array_new - get a new element from the given array
5760 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005761 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005762 * Get a new element from the given array. If it exceeds the
5763 * pre-allocated array size, re-allocate the array.
5764 *
5765 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005766 */
5767void *snd_array_new(struct snd_array *array)
5768{
Takashi Iwai12f17712012-10-10 08:59:14 +02005769 if (snd_BUG_ON(!array->elem_size))
5770 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005771 if (array->used >= array->alloced) {
5772 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005773 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005774 void *nlist;
5775 if (snd_BUG_ON(num >= 4096))
5776 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005777 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005778 if (!nlist)
5779 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005780 array->list = nlist;
5781 array->alloced = num;
5782 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005783 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005785EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005786
Takashi Iwaid5191e52009-11-16 14:58:17 +01005787/**
5788 * snd_array_free - free the given array elements
5789 * @array: the array object
5790 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005791void snd_array_free(struct snd_array *array)
5792{
5793 kfree(array->list);
5794 array->used = 0;
5795 array->alloced = 0;
5796 array->list = NULL;
5797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005798EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005799
Takashi Iwaid5191e52009-11-16 14:58:17 +01005800/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005801 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5802 * @pcm: PCM caps bits
5803 * @buf: the string buffer to write
5804 * @buflen: the max buffer length
5805 *
5806 * used by hda_proc.c and hda_eld.c
5807 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005808void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5809{
5810 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5811 int i, j;
5812
5813 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5814 if (pcm & (AC_SUPPCM_BITS_8 << i))
5815 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5816
5817 buf[j] = '\0'; /* necessary when j == 0 */
5818}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005819EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005820
5821MODULE_DESCRIPTION("HDA codec core");
5822MODULE_LICENSE("GPL");