blob: 4c20277a683531fccf5be99f9995d438e961fe92 [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>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.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" },
Takashi Iwaidff86f82014-02-19 11:11:20 +010070 { 0x1af4, "QEMU" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010072 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020073 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {} /* terminator */
75};
76
Takashi Iwai1289e9e2008-11-27 15:47:11 +010077static DEFINE_MUTEX(preset_mutex);
78static LIST_HEAD(hda_preset_tables);
79
80int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81{
82 mutex_lock(&preset_mutex);
83 list_add_tail(&preset->list, &hda_preset_tables);
84 mutex_unlock(&preset_mutex);
85 return 0;
86}
Takashi Iwai2698ea92013-12-18 07:45:52 +010087EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010088
89int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90{
91 mutex_lock(&preset_mutex);
92 list_del(&preset->list);
93 mutex_unlock(&preset_mutex);
94 return 0;
95}
Takashi Iwai2698ea92013-12-18 07:45:52 +010096EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Takashi Iwai83012a72012-08-24 18:38:08 +020098#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010099#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200100static void hda_power_work(struct work_struct *work);
101static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200102#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100103
104static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700105{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100106 struct hda_bus *bus = codec->bus;
107
108 if ((power_up && codec->pm_up_notified) ||
109 (!power_up && !codec->pm_up_notified))
110 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700111 if (bus->ops.pm_notify)
112 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100113 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700114}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100115
Takashi Iwaicb53c622007-08-10 17:21:45 +0200116#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100117#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200118static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200119#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100120#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200121#endif
122
Takashi Iwaid5191e52009-11-16 14:58:17 +0100123/**
124 * snd_hda_get_jack_location - Give a location string of the jack
125 * @cfg: pin default config value
126 *
127 * Parse the pin default config value and returns the string of the
128 * jack location, e.g. "Rear", "Front", etc.
129 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130const char *snd_hda_get_jack_location(u32 cfg)
131{
132 static char *bases[7] = {
133 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
134 };
135 static unsigned char specials_idx[] = {
136 0x07, 0x08,
137 0x17, 0x18, 0x19,
138 0x37, 0x38
139 };
140 static char *specials[] = {
141 "Rear Panel", "Drive Bar",
142 "Riser", "HDMI", "ATAPI",
143 "Mobile-In", "Mobile-Out"
144 };
145 int i;
146 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
147 if ((cfg & 0x0f) < 7)
148 return bases[cfg & 0x0f];
149 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
150 if (cfg == specials_idx[i])
151 return specials[i];
152 }
153 return "UNKNOWN";
154}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100155EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400156
Takashi Iwaid5191e52009-11-16 14:58:17 +0100157/**
158 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
159 * @cfg: pin default config value
160 *
161 * Parse the pin default config value and returns the string of the
162 * jack connectivity, i.e. external or internal connection.
163 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400164const char *snd_hda_get_jack_connectivity(u32 cfg)
165{
166 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
167
168 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
169}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100170EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400171
Takashi Iwaid5191e52009-11-16 14:58:17 +0100172/**
173 * snd_hda_get_jack_type - Give a type string of the jack
174 * @cfg: pin default config value
175 *
176 * Parse the pin default config value and returns the string of the
177 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
178 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400179const char *snd_hda_get_jack_type(u32 cfg)
180{
181 static char *jack_types[16] = {
182 "Line Out", "Speaker", "HP Out", "CD",
183 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
184 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200185 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400186 };
187
188 return jack_types[(cfg & AC_DEFCFG_DEVICE)
189 >> AC_DEFCFG_DEVICE_SHIFT];
190}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100191EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400192
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193/*
194 * Compose a 32bit command word to be sent to the HD-audio controller
195 */
196static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200197make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100198 unsigned int verb, unsigned int parm)
199{
200 u32 val;
201
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200202 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200203 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100204 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200205 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800206 return ~0;
207 }
208
209 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100210 val |= (u32)nid << 20;
211 val |= verb << 8;
212 val |= parm;
213 return val;
214}
215
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216/*
217 * Send and receive a verb
218 */
219static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200220 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221{
222 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200223 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224
Wu Fengguang6430aee2009-07-17 16:49:19 +0800225 if (cmd == ~0)
226 return -1;
227
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200228 if (res)
229 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200230 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200231 snd_hda_power_up(codec);
232 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200233 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
234 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100235 for (;;) {
236 trace_hda_send_cmd(codec, cmd);
237 err = bus->ops.command(bus, cmd);
238 if (err != -EAGAIN)
239 break;
240 /* process pending verbs */
241 bus->ops.get_response(bus, codec->addr);
242 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800244 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200245 trace_hda_get_response(codec, *res);
246 }
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200247 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200248 mutex_unlock(&bus->cmd_mutex);
249 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100250 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200251 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100252 codec_dbg(codec,
253 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200254 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200255 bus->ops.bus_reset(bus);
256 }
257 goto again;
258 }
259 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100260 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200261 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200262 return err;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * snd_hda_codec_read - send a command and get the response
267 * @codec: the HDA codec
268 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200269 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @verb: the verb to send
271 * @parm: the parameter for the verb
272 *
273 * Send a single command and read the corresponding response.
274 *
275 * Returns the obtained response value, or -1 for an error.
276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200277unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200278 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 unsigned int verb, unsigned int parm)
280{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200281 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200282 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200283 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700284 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return res;
286}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100287EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * snd_hda_codec_write - send a single command without waiting for response
291 * @codec: the HDA codec
292 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200293 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * @verb: the verb to send
295 * @parm: the parameter for the verb
296 *
297 * Send a single command without waiting for response.
298 *
299 * Returns 0 if successful, or a negative error code.
300 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200301int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
302 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200304 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100305 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200306 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200307 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100309EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311/**
312 * snd_hda_sequence_write - sequence writes
313 * @codec: the HDA codec
314 * @seq: VERB array to send
315 *
316 * Send the commands sequentially from the given array.
317 * The array must be terminated with NID=0.
318 */
319void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
320{
321 for (; seq->nid; seq++)
322 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
323}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100324EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326/**
327 * snd_hda_get_sub_nodes - get the range of sub nodes
328 * @codec: the HDA codec
329 * @nid: NID to parse
330 * @start_id: the pointer to store the start NID
331 *
332 * Parse the NID and store the start NID of its sub-nodes.
333 * Returns the number of sub-nodes.
334 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200335int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
336 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned int parm;
339
340 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200341 if (parm == -1)
342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *start_id = (parm >> 16) & 0x7fff;
344 return (int)(parm & 0x7fff);
345}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100346EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348/* connection list element */
349struct hda_conn_list {
350 struct list_head list;
351 int len;
352 hda_nid_t nid;
353 hda_nid_t conns[0];
354};
355
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200356/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357static struct hda_conn_list *
358lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200359{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100360 struct hda_conn_list *p;
361 list_for_each_entry(p, &codec->conn_list, list) {
362 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200364 }
365 return NULL;
366}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100368static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
369 const hda_nid_t *list)
370{
371 struct hda_conn_list *p;
372
373 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
374 if (!p)
375 return -ENOMEM;
376 p->len = len;
377 p->nid = nid;
378 memcpy(p->conns, list, len * sizeof(hda_nid_t));
379 list_add(&p->list, &codec->conn_list);
380 return 0;
381}
382
383static void remove_conn_list(struct hda_codec *codec)
384{
385 while (!list_empty(&codec->conn_list)) {
386 struct hda_conn_list *p;
387 p = list_first_entry(&codec->conn_list, typeof(*p), list);
388 list_del(&p->list);
389 kfree(p);
390 }
391}
392
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200393/* read the connection and add to the cache */
394static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200395{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100396 hda_nid_t list[32];
397 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200398 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200399
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200400 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100401 if (len == -ENOSPC) {
402 len = snd_hda_get_num_raw_conns(codec, nid);
403 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
404 if (!result)
405 return -ENOMEM;
406 len = snd_hda_get_raw_connections(codec, nid, result, len);
407 }
408 if (len >= 0)
409 len = snd_hda_override_conn_list(codec, nid, len, result);
410 if (result != list)
411 kfree(result);
412 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200413}
Takashi Iwaidce20792011-06-24 14:10:28 +0200414
415/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100416 * snd_hda_get_conn_list - get connection list
417 * @codec: the HDA codec
418 * @nid: NID to parse
419 * @len: number of connection list entries
420 * @listp: the pointer to store NID list
421 *
422 * Parses the connection list of the given widget and stores the pointer
423 * to the list of NIDs.
424 *
425 * Returns the number of connections, or a negative error code.
426 *
427 * Note that the returned pointer isn't protected against the list
428 * modification. If snd_hda_override_conn_list() might be called
429 * concurrently, protect with a mutex appropriately.
430 */
431int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
432 const hda_nid_t **listp)
433{
434 bool added = false;
435
436 for (;;) {
437 int err;
438 const struct hda_conn_list *p;
439
440 /* if the connection-list is already cached, read it */
441 p = lookup_conn_list(codec, nid);
442 if (p) {
443 if (listp)
444 *listp = p->conns;
445 return p->len;
446 }
447 if (snd_BUG_ON(added))
448 return -EINVAL;
449
450 err = read_and_add_raw_conns(codec, nid);
451 if (err < 0)
452 return err;
453 added = true;
454 }
455}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100456EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100457
458/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200459 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * @codec: the HDA codec
461 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200462 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * @max_conns: max. number of connections to store
464 *
465 * Parses the connection list of the given widget and stores the list
466 * of NIDs.
467 *
468 * Returns the number of connections, or a negative error code.
469 */
470int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200471 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200472{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100473 const hda_nid_t *list;
474 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200475
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100476 if (len > 0 && conn_list) {
477 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100478 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200479 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200480 return -EINVAL;
481 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100482 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200483 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200484
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100485 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200486}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100487EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200488
Takashi Iwai4eea3092013-02-07 18:18:19 +0100489/* return CONNLIST_LEN parameter of the given widget */
490static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
491{
492 unsigned int wcaps = get_wcaps(codec, nid);
493 unsigned int parm;
494
495 if (!(wcaps & AC_WCAP_CONN_LIST) &&
496 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
497 return 0;
498
499 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
500 if (parm == -1)
501 parm = 0;
502 return parm;
503}
504
505int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
506{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100507 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100508}
509
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200510/**
511 * snd_hda_get_raw_connections - copy connection list without cache
512 * @codec: the HDA codec
513 * @nid: NID to parse
514 * @conn_list: connection list array
515 * @max_conns: max. number of connections to store
516 *
517 * Like snd_hda_get_connections(), copy the connection list but without
518 * checking through the connection-list cache.
519 * Currently called only from hda_proc.c, so not exported.
520 */
521int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
522 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100525 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100527 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100528 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Takashi Iwai4eea3092013-02-07 18:18:19 +0100530 parm = get_num_conns(codec, nid);
531 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200532 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (parm & AC_CLIST_LONG) {
535 /* long form */
536 shift = 16;
537 num_elems = 2;
538 } else {
539 /* short form */
540 shift = 8;
541 num_elems = 4;
542 }
543 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mask = (1 << (shift-1)) - 1;
545
Takashi Iwai0ba21762007-04-16 11:29:14 +0200546 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0; /* no connection */
548
549 if (conn_len == 1) {
550 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200551 parm = snd_hda_codec_read(codec, nid, 0,
552 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200553 if (parm == -1 && codec->bus->rirb_error)
554 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100555 if (conn_list)
556 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 1;
558 }
559
560 /* multi connection */
561 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100562 prev_nid = 0;
563 for (i = 0; i < conn_len; i++) {
564 int range_val;
565 hda_nid_t val, n;
566
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200567 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100568 parm = snd_hda_codec_read(codec, nid, 0,
569 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200570 if (parm == -1 && codec->bus->rirb_error)
571 return -EIO;
572 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200573 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100574 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100575 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100576 codec_dbg(codec,
577 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200578 nid, i, parm);
579 return 0;
580 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100581 parm >>= shift;
582 if (range_val) {
583 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200584 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100585 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200586 "invalid dep_range_val %x:%x\n",
587 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100591 if (conn_list) {
592 if (conns >= max_conns)
593 return -ENOSPC;
594 conn_list[conns] = n;
595 }
596 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 }
598 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100599 if (conn_list) {
600 if (conns >= max_conns)
601 return -ENOSPC;
602 conn_list[conns] = val;
603 }
604 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100606 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 return conns;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200612 * snd_hda_override_conn_list - add/modify the connection-list to cache
613 * @codec: the HDA codec
614 * @nid: NID to parse
615 * @len: number of connection list entries
616 * @list: the list of connection entries
617 *
618 * Add or modify the given connection-list to the cache. If the corresponding
619 * cache already exists, invalidate it and append a new one.
620 *
621 * Returns zero or a negative error code.
622 */
623int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
624 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100626 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200627
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100628 p = lookup_conn_list(codec, nid);
629 if (p) {
630 list_del(&p->list);
631 kfree(p);
632 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200633
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100634 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100636EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200637
638/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 * snd_hda_get_conn_index - get the connection index of the given NID
640 * @codec: the HDA codec
641 * @mux: NID containing the list
642 * @nid: NID to select
643 * @recursive: 1 when searching NID recursively, otherwise 0
644 *
645 * Parses the connection list of the widget @mux and checks whether the
646 * widget @nid is present. If it is, return the connection index.
647 * Otherwise it returns -1.
648 */
649int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
650 hda_nid_t nid, int recursive)
651{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100652 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200653 int i, nums;
654
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100655 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200656 for (i = 0; i < nums; i++)
657 if (conn[i] == nid)
658 return i;
659 if (!recursive)
660 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100661 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100662 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 return -1;
664 }
665 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200666 for (i = 0; i < nums; i++) {
667 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
668 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
669 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200670 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
671 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200672 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200673 return -1;
674}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100675EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Mengdong Linf1aa0682013-08-26 21:35:21 -0400677
678/* return DEVLIST_LEN parameter of the given widget */
679static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
680{
681 unsigned int wcaps = get_wcaps(codec, nid);
682 unsigned int parm;
683
684 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
685 get_wcaps_type(wcaps) != AC_WID_PIN)
686 return 0;
687
688 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
689 if (parm == -1 && codec->bus->rirb_error)
690 parm = 0;
691 return parm & AC_DEV_LIST_LEN_MASK;
692}
693
694/**
695 * snd_hda_get_devices - copy device list without cache
696 * @codec: the HDA codec
697 * @nid: NID of the pin to parse
698 * @dev_list: device list array
699 * @max_devices: max. number of devices to store
700 *
701 * Copy the device list. This info is dynamic and so not cached.
702 * Currently called only from hda_proc.c, so not exported.
703 */
704int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
705 u8 *dev_list, int max_devices)
706{
707 unsigned int parm;
708 int i, dev_len, devices;
709
710 parm = get_num_devices(codec, nid);
711 if (!parm) /* not multi-stream capable */
712 return 0;
713
714 dev_len = parm + 1;
715 dev_len = dev_len < max_devices ? dev_len : max_devices;
716
717 devices = 0;
718 while (devices < dev_len) {
719 parm = snd_hda_codec_read(codec, nid, 0,
720 AC_VERB_GET_DEVICE_LIST, devices);
721 if (parm == -1 && codec->bus->rirb_error)
722 break;
723
724 for (i = 0; i < 8; i++) {
725 dev_list[devices] = (u8)parm;
726 parm >>= 4;
727 devices++;
728 if (devices >= dev_len)
729 break;
730 }
731 }
732 return devices;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/**
736 * snd_hda_queue_unsol_event - add an unsolicited event to queue
737 * @bus: the BUS
738 * @res: unsolicited event (lower 32bit of RIRB entry)
739 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
740 *
741 * Adds the given event to the queue. The events are processed in
742 * the workqueue asynchronously. Call this function in the interrupt
743 * hanlder when RIRB receives an unsolicited event.
744 *
745 * Returns 0 if successful, or a negative error code.
746 */
747int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
748{
749 struct hda_bus_unsolicited *unsol;
750 unsigned int wp;
751
Wang YanQing2195b062013-05-07 11:27:33 +0800752 if (!bus || !bus->workq)
753 return 0;
754
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200755 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 unsol = bus->unsol;
757 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759
760 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
761 unsol->wp = wp;
762
763 wp <<= 1;
764 unsol->queue[wp] = res;
765 unsol->queue[wp + 1] = res_ex;
766
Takashi Iwai6acaed32009-01-12 10:09:24 +0100767 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 return 0;
770}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100771EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800774 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
David Howellsc4028952006-11-22 14:57:56 +0000776static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
David Howellsc4028952006-11-22 14:57:56 +0000778 struct hda_bus_unsolicited *unsol =
779 container_of(work, struct hda_bus_unsolicited, work);
780 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct hda_codec *codec;
782 unsigned int rp, caddr, res;
783
784 while (unsol->rp != unsol->wp) {
785 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
786 unsol->rp = rp;
787 rp <<= 1;
788 res = unsol->queue[rp];
789 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200790 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 continue;
792 codec = bus->caddr_tbl[caddr & 0x0f];
793 if (codec && codec->patch_ops.unsol_event)
794 codec->patch_ops.unsol_event(codec, res);
795 }
796}
797
798/*
799 * initialize unsolicited queue
800 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200801static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct hda_bus_unsolicited *unsol;
804
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100805 if (bus->unsol) /* already initialized */
806 return 0;
807
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200808 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200809 if (!unsol) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100810 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -ENOMEM;
812 }
David Howellsc4028952006-11-22 14:57:56 +0000813 INIT_WORK(&unsol->work, process_unsol_events);
814 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 bus->unsol = unsol;
816 return 0;
817}
818
819/*
820 * destructor
821 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100822static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200824 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100825 return;
826
827 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100828 if (bus->workq)
829 flush_workqueue(bus->workq);
830 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 kfree(bus->unsol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (bus->ops.private_free)
833 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100834 if (bus->workq)
835 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100840static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai2565c892014-02-19 11:41:09 +0100842 snd_hda_bus_free(device->device_data);
843 return 0;
844}
845
846static int snd_hda_bus_dev_disconnect(struct snd_device *device)
847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100849 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/**
854 * snd_hda_bus_new - create a HDA bus
855 * @card: the card entry
856 * @temp: the template for hda_bus information
857 * @busp: the pointer to store the created bus instance
858 *
859 * Returns 0 if successful, or a negative error code.
860 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100861int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200862 const struct hda_bus_template *temp,
863 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct hda_bus *bus;
866 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100867 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100868 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 .dev_free = snd_hda_bus_dev_free,
870 };
871
Takashi Iwaida3cec32008-08-08 17:12:14 +0200872 if (snd_BUG_ON(!temp))
873 return -EINVAL;
874 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (busp)
878 *busp = NULL;
879
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200880 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100882 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -ENOMEM;
884 }
885
886 bus->card = card;
887 bus->private_data = temp->private_data;
888 bus->pci = temp->pci;
889 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100890 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 bus->ops = temp->ops;
892
Ingo Molnar62932df2006-01-16 16:34:20 +0100893 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200894 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 INIT_LIST_HEAD(&bus->codec_list);
896
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100897 snprintf(bus->workq_name, sizeof(bus->workq_name),
898 "hd-audio%d", card->number);
899 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100900 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100901 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100902 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100903 kfree(bus);
904 return -ENOMEM;
905 }
906
Takashi Iwai0ba21762007-04-16 11:29:14 +0200907 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
908 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 snd_hda_bus_free(bus);
910 return err;
911 }
912 if (busp)
913 *busp = bus;
914 return 0;
915}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100916EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100918#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200919#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200920 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200921#else
922#define is_generic_config(codec) 0
923#endif
924
Takashi Iwai645f10c2008-11-28 15:07:37 +0100925#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100926#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
927#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100928#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100929#endif
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * find a matching codec preset
933 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200934static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200935find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100937 struct hda_codec_preset_list *tbl;
938 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200939 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100941 again:
942 mutex_lock(&preset_mutex);
943 list_for_each_entry(tbl, &hda_preset_tables, list) {
944 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100945 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100946 continue;
947 }
948 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100950 if (preset->afg && preset->afg != codec->afg)
951 continue;
952 if (preset->mfg && preset->mfg != codec->mfg)
953 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200954 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200956 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200957 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100958 preset->rev == codec->revision_id)) {
959 mutex_unlock(&preset_mutex);
960 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100964 module_put(tbl->owner);
965 }
966 mutex_unlock(&preset_mutex);
967
968 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
969 char name[32];
970 if (!mod_requested)
971 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
972 codec->vendor_id);
973 else
974 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
975 (codec->vendor_id >> 16) & 0xffff);
976 request_module(name);
977 mod_requested++;
978 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 return NULL;
981}
982
983/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200984 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200986static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 const struct hda_vendor_id *c;
989 const char *vendor = NULL;
990 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200991 char tmp[16];
992
993 if (codec->vendor_name)
994 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 for (c = hda_vendor_ids; c->id; c++) {
997 if (c->id == vendor_id) {
998 vendor = c->name;
999 break;
1000 }
1001 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001002 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sprintf(tmp, "Generic %04x", vendor_id);
1004 vendor = tmp;
1005 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001006 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1007 if (!codec->vendor_name)
1008 return -ENOMEM;
1009
1010 get_chip_name:
1011 if (codec->chip_name)
1012 return 0;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001015 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1016 else {
1017 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1018 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1019 }
1020 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001021 return -ENOMEM;
1022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001026 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001028static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001030 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 hda_nid_t nid;
1032
1033 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1034 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001035 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001036 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001037 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001038 case AC_GRP_AUDIO_FUNCTION:
1039 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001040 codec->afg_function_id = function_id & 0xff;
1041 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001042 break;
1043 case AC_GRP_MODEM_FUNCTION:
1044 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001045 codec->mfg_function_id = function_id & 0xff;
1046 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001047 break;
1048 default:
1049 break;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001055 * read widget caps for each widget and store in cache
1056 */
1057static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1058{
1059 int i;
1060 hda_nid_t nid;
1061
1062 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1063 &codec->start_nid);
1064 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001065 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001066 return -ENOMEM;
1067 nid = codec->start_nid;
1068 for (i = 0; i < codec->num_nodes; i++, nid++)
1069 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1070 AC_PAR_AUDIO_WIDGET_CAP);
1071 return 0;
1072}
1073
Takashi Iwai3be14142009-02-20 14:11:16 +01001074/* read all pin default configurations and save codec->init_pins */
1075static int read_pin_defaults(struct hda_codec *codec)
1076{
1077 int i;
1078 hda_nid_t nid = codec->start_nid;
1079
1080 for (i = 0; i < codec->num_nodes; i++, nid++) {
1081 struct hda_pincfg *pin;
1082 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001083 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001084 if (wid_type != AC_WID_PIN)
1085 continue;
1086 pin = snd_array_new(&codec->init_pins);
1087 if (!pin)
1088 return -ENOMEM;
1089 pin->nid = nid;
1090 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1091 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001092 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1093 AC_VERB_GET_PIN_WIDGET_CONTROL,
1094 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001095 }
1096 return 0;
1097}
1098
1099/* look up the given pin config list and return the item matching with NID */
1100static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1101 struct snd_array *array,
1102 hda_nid_t nid)
1103{
1104 int i;
1105 for (i = 0; i < array->used; i++) {
1106 struct hda_pincfg *pin = snd_array_elem(array, i);
1107 if (pin->nid == nid)
1108 return pin;
1109 }
1110 return NULL;
1111}
1112
Takashi Iwai3be14142009-02-20 14:11:16 +01001113/* set the current pin config value for the given NID.
1114 * the value is cached, and read via snd_hda_codec_get_pincfg()
1115 */
1116int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1117 hda_nid_t nid, unsigned int cfg)
1118{
1119 struct hda_pincfg *pin;
1120
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001121 /* the check below may be invalid when pins are added by a fixup
1122 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1123 * for now
1124 */
1125 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001126 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1127 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001128 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001129
Takashi Iwai3be14142009-02-20 14:11:16 +01001130 pin = look_up_pincfg(codec, list, nid);
1131 if (!pin) {
1132 pin = snd_array_new(list);
1133 if (!pin)
1134 return -ENOMEM;
1135 pin->nid = nid;
1136 }
1137 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001138 return 0;
1139}
1140
Takashi Iwaid5191e52009-11-16 14:58:17 +01001141/**
1142 * snd_hda_codec_set_pincfg - Override a pin default configuration
1143 * @codec: the HDA codec
1144 * @nid: NID to set the pin config
1145 * @cfg: the pin default config value
1146 *
1147 * Override a pin default configuration value in the cache.
1148 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1149 * priority than the real hardware value.
1150 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001151int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1152 hda_nid_t nid, unsigned int cfg)
1153{
Takashi Iwai346ff702009-02-23 09:42:57 +01001154 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001155}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001156EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001157
Takashi Iwaid5191e52009-11-16 14:58:17 +01001158/**
1159 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1160 * @codec: the HDA codec
1161 * @nid: NID to get the pin config
1162 *
1163 * Get the current pin config value of the given pin NID.
1164 * If the pincfg value is cached or overridden via sysfs or driver,
1165 * returns the cached value.
1166 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001167unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1168{
1169 struct hda_pincfg *pin;
1170
Takashi Iwai648a8d22014-02-25 10:38:13 +01001171#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001172 {
1173 unsigned int cfg = 0;
1174 mutex_lock(&codec->user_mutex);
1175 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1176 if (pin)
1177 cfg = pin->cfg;
1178 mutex_unlock(&codec->user_mutex);
1179 if (cfg)
1180 return cfg;
1181 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001182#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001183 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1184 if (pin)
1185 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001186 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1187 if (pin)
1188 return pin->cfg;
1189 return 0;
1190}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001191EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001192
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001193/* remember the current pinctl target value */
1194int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1195 unsigned int val)
1196{
1197 struct hda_pincfg *pin;
1198
1199 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1200 if (!pin)
1201 return -EINVAL;
1202 pin->target = val;
1203 return 0;
1204}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001205EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001206
1207/* return the current pinctl target value */
1208int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1209{
1210 struct hda_pincfg *pin;
1211
1212 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1213 if (!pin)
1214 return 0;
1215 return pin->target;
1216}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001217EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001218
Takashi Iwai92ee6162009-12-27 11:18:59 +01001219/**
1220 * snd_hda_shutup_pins - Shut up all pins
1221 * @codec: the HDA codec
1222 *
1223 * Clear all pin controls to shup up before suspend for avoiding click noise.
1224 * The controls aren't cached so that they can be resumed properly.
1225 */
1226void snd_hda_shutup_pins(struct hda_codec *codec)
1227{
1228 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001229 /* don't shut up pins when unloading the driver; otherwise it breaks
1230 * the default pin setup at the next load of the driver
1231 */
1232 if (codec->bus->shutdown)
1233 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001234 for (i = 0; i < codec->init_pins.used; i++) {
1235 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1236 /* use read here for syncing after issuing each verb */
1237 snd_hda_codec_read(codec, pin->nid, 0,
1238 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1239 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001240 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001241}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001242EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001243
Takashi Iwai2a439522011-07-26 09:52:50 +02001244#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001245/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1246static void restore_shutup_pins(struct hda_codec *codec)
1247{
1248 int i;
1249 if (!codec->pins_shutup)
1250 return;
1251 if (codec->bus->shutdown)
1252 return;
1253 for (i = 0; i < codec->init_pins.used; i++) {
1254 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1255 snd_hda_codec_write(codec, pin->nid, 0,
1256 AC_VERB_SET_PIN_WIDGET_CONTROL,
1257 pin->ctrl);
1258 }
1259 codec->pins_shutup = 0;
1260}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001261#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001262
David Henningsson26a6cb62012-10-09 15:04:21 +02001263static void hda_jackpoll_work(struct work_struct *work)
1264{
1265 struct hda_codec *codec =
1266 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001267
1268 snd_hda_jack_set_dirty_all(codec);
1269 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001270
1271 if (!codec->jackpoll_interval)
1272 return;
1273
David Henningsson26a6cb62012-10-09 15:04:21 +02001274 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1275 codec->jackpoll_interval);
1276}
1277
Takashi Iwai01751f52007-08-10 16:59:39 +02001278static void init_hda_cache(struct hda_cache_rec *cache,
1279 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001280static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001281
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001282/* release all pincfg lists */
1283static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001284{
Takashi Iwai346ff702009-02-23 09:42:57 +01001285 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001286#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001287 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001288#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001289 snd_array_free(&codec->init_pins);
1290}
1291
Takashi Iwai54d17402005-11-21 16:33:22 +01001292/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001293 * audio-converter setup caches
1294 */
1295struct hda_cvt_setup {
1296 hda_nid_t nid;
1297 u8 stream_tag;
1298 u8 channel_id;
1299 u16 format_id;
1300 unsigned char active; /* cvt is currently used */
1301 unsigned char dirty; /* setups should be cleared */
1302};
1303
1304/* get or create a cache entry for the given audio converter NID */
1305static struct hda_cvt_setup *
1306get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1307{
1308 struct hda_cvt_setup *p;
1309 int i;
1310
1311 for (i = 0; i < codec->cvt_setups.used; i++) {
1312 p = snd_array_elem(&codec->cvt_setups, i);
1313 if (p->nid == nid)
1314 return p;
1315 }
1316 p = snd_array_new(&codec->cvt_setups);
1317 if (p)
1318 p->nid = nid;
1319 return p;
1320}
1321
1322/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001323 * Dynamic symbol binding for the codec parsers
1324 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001325
1326#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001327 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001328
1329static void unload_parser(struct hda_codec *codec)
1330{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001331 if (codec->parser)
1332 symbol_put_addr(codec->parser);
1333 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001334}
1335
1336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * codec destructor
1338 */
1339static void snd_hda_codec_free(struct hda_codec *codec)
1340{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001341 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001343 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001344 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001345 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001346#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001347 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001348 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001351 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001352 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001353 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001354 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001355 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 codec->bus->caddr_tbl[codec->addr] = NULL;
1357 if (codec->patch_ops.free)
1358 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001359 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001360 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001361 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001362 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001363 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001364 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001365 kfree(codec->vendor_name);
1366 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001367 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001368 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001369 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001370 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001373static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1374 hda_nid_t fg, unsigned int power_state);
1375
Takashi Iwaid8193872012-08-31 07:54:38 -07001376static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001377 unsigned int power_state);
1378
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001379static int snd_hda_codec_dev_register(struct snd_device *device)
1380{
1381 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001382 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001383
Takashi Iwaid604b392014-02-28 13:42:09 +01001384 if (err < 0)
1385 return err;
1386 snd_hda_register_beep_device(codec);
1387 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001388}
1389
1390static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1391{
1392 struct hda_codec *codec = device->device_data;
1393
Takashi Iwaid604b392014-02-28 13:42:09 +01001394 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001395 device_del(&codec->dev);
1396 return 0;
1397}
1398
Takashi Iwai2565c892014-02-19 11:41:09 +01001399static int snd_hda_codec_dev_free(struct snd_device *device)
1400{
1401 snd_hda_codec_free(device->device_data);
1402 return 0;
1403}
1404
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001405/* just free the container */
1406static void snd_hda_codec_dev_release(struct device *dev)
1407{
1408 kfree(container_of(dev, struct hda_codec, dev));
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/**
1412 * snd_hda_codec_new - create a HDA codec
1413 * @bus: the bus to assign
1414 * @codec_addr: the codec address
1415 * @codecp: the pointer to store the generated codec
1416 *
1417 * Returns 0 if successful, or a negative error code.
1418 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001419int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001420 unsigned int codec_addr,
1421 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001424 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001425 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001427 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001428 .dev_register = snd_hda_codec_dev_register,
1429 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001430 .dev_free = snd_hda_codec_dev_free,
1431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Takashi Iwaida3cec32008-08-08 17:12:14 +02001433 if (snd_BUG_ON(!bus))
1434 return -EINVAL;
1435 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001439 dev_err(bus->card->dev,
1440 "address 0x%x is already occupied\n",
1441 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EBUSY;
1443 }
1444
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001445 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001447 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -ENOMEM;
1449 }
1450
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001451 device_initialize(&codec->dev);
1452 codec->dev.parent = &bus->card->card_dev;
1453 codec->dev.class = sound_class;
1454 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001455 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001456 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1457 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001458 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 codec->bus = bus;
1461 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001462 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001463 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001464 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001465 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001466 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001467 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1468 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001469 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001470 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001471 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001472 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001473 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001474 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001475 INIT_LIST_HEAD(&codec->conn_list);
1476
David Henningsson26a6cb62012-10-09 15:04:21 +02001477 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001478 codec->depop_delay = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Takashi Iwai83012a72012-08-24 18:38:08 +02001480#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001481 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001482 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1483 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1484 * the caller has to power down appropriatley after initialization
1485 * phase.
1486 */
1487 hda_keep_power_on(codec);
1488#endif
1489
Takashi Iwai648a8d22014-02-25 10:38:13 +01001490 snd_hda_sysfs_init(codec);
1491
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001492 if (codec->bus->modelname) {
1493 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1494 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001495 err = -ENODEV;
1496 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001497 }
1498 }
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001501 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 bus->caddr_tbl[codec_addr] = codec;
1504
Takashi Iwai0ba21762007-04-16 11:29:14 +02001505 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1506 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001507 if (codec->vendor_id == -1)
1508 /* read again, hopefully the access method was corrected
1509 * in the last read...
1510 */
1511 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1512 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001513 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 AC_PAR_SUBSYSTEM_ID);
1515 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1516 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001518 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001519 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001520 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001521 err = -ENODEV;
1522 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
Takashi Iwaid8193872012-08-31 07:54:38 -07001525 fg = codec->afg ? codec->afg : codec->mfg;
1526 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001527 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001528 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001529 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001530 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001531 err = read_pin_defaults(codec);
1532 if (err < 0)
1533 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001534
Takashi Iwai0ba21762007-04-16 11:29:14 +02001535 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001537 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001538 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001539 }
1540
Takashi Iwai83012a72012-08-24 18:38:08 +02001541#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001542 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001543 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001544#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001545 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001546 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001547#ifdef CONFIG_PM
1548 if (!codec->d3_stop_clk || !codec->epss)
1549 bus->power_keep_link_on = 1;
1550#endif
1551
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001552
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001553 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001554 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001555
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001556 snd_hda_codec_proc_new(codec);
1557
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001558 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001559
1560 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1561 codec->subsystem_id, codec->revision_id);
1562 snd_component_add(codec->bus->card, component);
1563
Takashi Iwai2565c892014-02-19 11:41:09 +01001564 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1565 if (err < 0)
1566 goto error;
1567
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001568 if (codecp)
1569 *codecp = codec;
1570 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001571
1572 error:
1573 snd_hda_codec_free(codec);
1574 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001575}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001576EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001577
Mengdong Lina15d05d2013-02-08 17:09:31 -05001578int snd_hda_codec_update_widgets(struct hda_codec *codec)
1579{
1580 hda_nid_t fg;
1581 int err;
1582
1583 /* Assume the function group node does not change,
1584 * only the widget nodes may change.
1585 */
1586 kfree(codec->wcaps);
1587 fg = codec->afg ? codec->afg : codec->mfg;
1588 err = read_widget_caps(codec, fg);
1589 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001590 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001591 return err;
1592 }
1593
1594 snd_array_free(&codec->init_pins);
1595 err = read_pin_defaults(codec);
1596
1597 return err;
1598}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001599EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001600
1601
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001602#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001603/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1604static bool is_likely_hdmi_codec(struct hda_codec *codec)
1605{
1606 hda_nid_t nid = codec->start_nid;
1607 int i;
1608
1609 for (i = 0; i < codec->num_nodes; i++, nid++) {
1610 unsigned int wcaps = get_wcaps(codec, nid);
1611 switch (get_wcaps_type(wcaps)) {
1612 case AC_WID_AUD_IN:
1613 return false; /* HDMI parser supports only HDMI out */
1614 case AC_WID_AUD_OUT:
1615 if (!(wcaps & AC_WCAP_DIGITAL))
1616 return false;
1617 break;
1618 }
1619 }
1620 return true;
1621}
1622#else
1623/* no HDMI codec parser support */
1624#define is_likely_hdmi_codec(codec) false
1625#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1626
Takashi Iwaid5191e52009-11-16 14:58:17 +01001627/**
1628 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1629 * @codec: the HDA codec
1630 *
1631 * Start parsing of the given codec tree and (re-)initialize the whole
1632 * patch instance.
1633 *
1634 * Returns 0 if successful or a negative error code.
1635 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001636int snd_hda_codec_configure(struct hda_codec *codec)
1637{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001638 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001639 int err;
1640
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001641 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001642 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001643 err = get_codec_name(codec);
1644 if (err < 0)
1645 return err;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001648 if (!is_generic_config(codec) && codec->preset)
1649 patch = codec->preset->patch;
1650 if (!patch) {
1651 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001652 if (is_likely_hdmi_codec(codec)) {
1653#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001654 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001655#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1656 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001657#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001658 }
1659 if (!patch) {
1660#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1661 patch = load_parser(codec, snd_hda_parse_generic_codec);
1662#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1663 patch = snd_hda_parse_generic_codec;
1664#endif
1665 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001666 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001667 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001668 return -ENODEV;
1669 }
Takashi Iwai82467612007-07-27 19:15:54 +02001670 }
1671
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001672 err = patch(codec);
1673 if (err < 0) {
1674 unload_parser(codec);
1675 return err;
1676 }
Takashi Iwai82467612007-07-27 19:15:54 +02001677
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001678 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001679 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001680 if (err < 0)
1681 return err;
1682 }
1683
Takashi Iwaif62faed2009-12-23 09:27:51 +01001684 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001685 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001686 snprintf(codec->bus->card->mixername,
1687 sizeof(codec->bus->card->mixername),
1688 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001691EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Takashi Iwaied360812012-08-08 17:12:52 +02001693/* update the stream-id if changed */
1694static void update_pcm_stream_id(struct hda_codec *codec,
1695 struct hda_cvt_setup *p, hda_nid_t nid,
1696 u32 stream_tag, int channel_id)
1697{
1698 unsigned int oldval, newval;
1699
1700 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1701 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1702 newval = (stream_tag << 4) | channel_id;
1703 if (oldval != newval)
1704 snd_hda_codec_write(codec, nid, 0,
1705 AC_VERB_SET_CHANNEL_STREAMID,
1706 newval);
1707 p->stream_tag = stream_tag;
1708 p->channel_id = channel_id;
1709 }
1710}
1711
1712/* update the format-id if changed */
1713static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1714 hda_nid_t nid, int format)
1715{
1716 unsigned int oldval;
1717
1718 if (p->format_id != format) {
1719 oldval = snd_hda_codec_read(codec, nid, 0,
1720 AC_VERB_GET_STREAM_FORMAT, 0);
1721 if (oldval != format) {
1722 msleep(1);
1723 snd_hda_codec_write(codec, nid, 0,
1724 AC_VERB_SET_STREAM_FORMAT,
1725 format);
1726 }
1727 p->format_id = format;
1728 }
1729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/**
1732 * snd_hda_codec_setup_stream - set up the codec for streaming
1733 * @codec: the CODEC to set up
1734 * @nid: the NID to set up
1735 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1736 * @channel_id: channel id to pass, zero based.
1737 * @format: stream format.
1738 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001739void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1740 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int channel_id, int format)
1742{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001743 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001744 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001745 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001746 int i;
1747
Takashi Iwai0ba21762007-04-16 11:29:14 +02001748 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001749 return;
1750
Takashi Iwai4e76a882014-02-25 12:21:03 +01001751 codec_dbg(codec,
1752 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1753 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001754 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001755 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001756 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001757
1758 if (codec->pcm_format_first)
1759 update_pcm_format(codec, p, nid, format);
1760 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1761 if (!codec->pcm_format_first)
1762 update_pcm_format(codec, p, nid, format);
1763
Takashi Iwaieb541332010-08-06 13:48:11 +02001764 p->active = 1;
1765 p->dirty = 0;
1766
1767 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001768 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001769 list_for_each_entry(c, &codec->bus->codec_list, list) {
1770 for (i = 0; i < c->cvt_setups.used; i++) {
1771 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001772 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001773 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001774 p->dirty = 1;
1775 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001778EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Takashi Iwaif0cea792010-08-13 11:56:53 +02001780static void really_cleanup_stream(struct hda_codec *codec,
1781 struct hda_cvt_setup *q);
1782
Takashi Iwaid5191e52009-11-16 14:58:17 +01001783/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001784 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001785 * @codec: the CODEC to clean up
1786 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001787 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001788 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001789void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1790 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001791{
Takashi Iwaieb541332010-08-06 13:48:11 +02001792 struct hda_cvt_setup *p;
1793
Takashi Iwai888afa12008-03-18 09:57:50 +01001794 if (!nid)
1795 return;
1796
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001797 if (codec->no_sticky_stream)
1798 do_now = 1;
1799
Takashi Iwai4e76a882014-02-25 12:21:03 +01001800 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001801 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001802 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001803 /* here we just clear the active flag when do_now isn't set;
1804 * actual clean-ups will be done later in
1805 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1806 */
1807 if (do_now)
1808 really_cleanup_stream(codec, p);
1809 else
1810 p->active = 0;
1811 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001812}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001813EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001814
Takashi Iwaieb541332010-08-06 13:48:11 +02001815static void really_cleanup_stream(struct hda_codec *codec,
1816 struct hda_cvt_setup *q)
1817{
1818 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001819 if (q->stream_tag || q->channel_id)
1820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1821 if (q->format_id)
1822 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1823);
Takashi Iwaieb541332010-08-06 13:48:11 +02001824 memset(q, 0, sizeof(*q));
1825 q->nid = nid;
1826}
1827
1828/* clean up the all conflicting obsolete streams */
1829static void purify_inactive_streams(struct hda_codec *codec)
1830{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001831 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001832 int i;
1833
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001834 list_for_each_entry(c, &codec->bus->codec_list, list) {
1835 for (i = 0; i < c->cvt_setups.used; i++) {
1836 struct hda_cvt_setup *p;
1837 p = snd_array_elem(&c->cvt_setups, i);
1838 if (p->dirty)
1839 really_cleanup_stream(c, p);
1840 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001841 }
1842}
1843
Takashi Iwai2a439522011-07-26 09:52:50 +02001844#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001845/* clean up all streams; called from suspend */
1846static void hda_cleanup_all_streams(struct hda_codec *codec)
1847{
1848 int i;
1849
1850 for (i = 0; i < codec->cvt_setups.used; i++) {
1851 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1852 if (p->stream_tag)
1853 really_cleanup_stream(codec, p);
1854 }
1855}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001856#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * amp access functions
1860 */
1861
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001862/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001863#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001864#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001865#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1866#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001868#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001871static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001872 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai01751f52007-08-10 16:59:39 +02001874 memset(cache, 0, sizeof(*cache));
1875 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001876 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001877}
1878
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001879static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001880{
Takashi Iwai603c4012008-07-30 15:01:44 +02001881 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001885static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai01751f52007-08-10 16:59:39 +02001887 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1888 u16 cur = cache->hash[idx];
1889 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001892 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (info->key == key)
1894 return info;
1895 cur = info->next;
1896 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001897 return NULL;
1898}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001900/* query the hash. allocate an entry if not found. */
1901static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1902 u32 key)
1903{
1904 struct hda_cache_head *info = get_hash(cache, key);
1905 if (!info) {
1906 u16 idx, cur;
1907 /* add a new hash entry */
1908 info = snd_array_new(&cache->buf);
1909 if (!info)
1910 return NULL;
1911 cur = snd_array_index(&cache->buf, info);
1912 info->key = key;
1913 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001914 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001915 idx = key % (u16)ARRAY_SIZE(cache->hash);
1916 info->next = cache->hash[idx];
1917 cache->hash[idx] = cur;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return info;
1920}
1921
Takashi Iwai01751f52007-08-10 16:59:39 +02001922/* query and allocate an amp hash entry */
1923static inline struct hda_amp_info *
1924get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1925{
1926 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1927}
1928
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001929/* overwrite the value with the key in the caps hash */
1930static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1931{
1932 struct hda_amp_info *info;
1933
1934 mutex_lock(&codec->hash_mutex);
1935 info = get_alloc_amp_hash(codec, key);
1936 if (!info) {
1937 mutex_unlock(&codec->hash_mutex);
1938 return -EINVAL;
1939 }
1940 info->amp_caps = val;
1941 info->head.val |= INFO_AMP_CAPS;
1942 mutex_unlock(&codec->hash_mutex);
1943 return 0;
1944}
1945
1946/* query the value from the caps hash; if not found, fetch the current
1947 * value from the given function and store in the hash
1948 */
1949static unsigned int
1950query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1951 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1952{
1953 struct hda_amp_info *info;
1954 unsigned int val;
1955
1956 mutex_lock(&codec->hash_mutex);
1957 info = get_alloc_amp_hash(codec, key);
1958 if (!info) {
1959 mutex_unlock(&codec->hash_mutex);
1960 return 0;
1961 }
1962 if (!(info->head.val & INFO_AMP_CAPS)) {
1963 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1964 val = func(codec, nid, dir);
1965 write_caps_hash(codec, key, val);
1966 } else {
1967 val = info->amp_caps;
1968 mutex_unlock(&codec->hash_mutex);
1969 }
1970 return val;
1971}
1972
1973static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1974 int direction)
1975{
1976 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1977 nid = codec->afg;
1978 return snd_hda_param_read(codec, nid,
1979 direction == HDA_OUTPUT ?
1980 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1981}
1982
Takashi Iwaid5191e52009-11-16 14:58:17 +01001983/**
1984 * query_amp_caps - query AMP capabilities
1985 * @codec: the HD-auio codec
1986 * @nid: the NID to query
1987 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1988 *
1989 * Query AMP capabilities for the given widget and direction.
1990 * Returns the obtained capability bits.
1991 *
1992 * When cap bits have been already read, this doesn't read again but
1993 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001995u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001997 return query_caps_hash(codec, nid, direction,
1998 HDA_HASH_KEY(nid, direction, 0),
1999 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002001EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Takashi Iwaid5191e52009-11-16 14:58:17 +01002003/**
2004 * snd_hda_override_amp_caps - Override the AMP capabilities
2005 * @codec: the CODEC to clean up
2006 * @nid: the NID to clean up
2007 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2008 * @caps: the capability bits to set
2009 *
2010 * Override the cached AMP caps bits value by the given one.
2011 * This function is useful if the driver needs to adjust the AMP ranges,
2012 * e.g. limit to 0dB, etc.
2013 *
2014 * Returns zero if successful or a negative error code.
2015 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002016int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2017 unsigned int caps)
2018{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002019 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002021EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002022
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002023static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2024 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002025{
2026 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2027}
2028
Takashi Iwaid5191e52009-11-16 14:58:17 +01002029/**
2030 * snd_hda_query_pin_caps - Query PIN capabilities
2031 * @codec: the HD-auio codec
2032 * @nid: the NID to query
2033 *
2034 * Query PIN capabilities for the given widget.
2035 * Returns the obtained capability bits.
2036 *
2037 * When cap bits have been already read, this doesn't read again but
2038 * returns the cached value.
2039 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002040u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2041{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002042 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002043 read_pin_cap);
2044}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002045EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002046
Wu Fengguang864f92b2009-11-18 12:38:02 +08002047/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002048 * snd_hda_override_pin_caps - Override the pin capabilities
2049 * @codec: the CODEC
2050 * @nid: the NID to override
2051 * @caps: the capability bits to set
2052 *
2053 * Override the cached PIN capabilitiy bits value by the given one.
2054 *
2055 * Returns zero if successful or a negative error code.
2056 */
2057int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2058 unsigned int caps)
2059{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002060 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002061}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002062EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002063
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002064/* read or sync the hash value with the current value;
2065 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002067static struct hda_amp_info *
2068update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002069 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002071 struct hda_amp_info *info;
2072 unsigned int parm, val = 0;
2073 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002075 retry:
2076 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2077 if (!info)
2078 return NULL;
2079 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2080 if (!val_read) {
2081 mutex_unlock(&codec->hash_mutex);
2082 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2083 parm |= direction == HDA_OUTPUT ?
2084 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2085 parm |= index;
2086 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002087 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088 val &= 0xff;
2089 val_read = true;
2090 mutex_lock(&codec->hash_mutex);
2091 goto retry;
2092 }
2093 info->vol[ch] = val;
2094 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002095 } else if (init_only)
2096 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002097 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
2100/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002101 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002103static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002104 hda_nid_t nid, int ch, int direction, int index,
2105 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 u32 parm;
2108
2109 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2110 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2111 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002112 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2113 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002114 ; /* set the zero value as a fake mute */
2115 else
2116 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2118}
2119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_codec_amp_read - Read AMP value
2122 * @codec: HD-audio codec
2123 * @nid: NID to read the AMP value
2124 * @ch: channel (left=0 or right=1)
2125 * @direction: #HDA_INPUT or #HDA_OUTPUT
2126 * @index: the index value (only for input direction)
2127 *
2128 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
Takashi Iwai834be882006-03-01 14:16:17 +01002130int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2131 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002134 unsigned int val = 0;
2135
2136 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002137 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002138 if (info)
2139 val = info->vol[ch];
2140 mutex_unlock(&codec->hash_mutex);
2141 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002143EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Takashi Iwai280e57d2012-12-14 10:32:21 +01002145static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2146 int direction, int idx, int mask, int val,
2147 bool init_only)
2148{
2149 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002150 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002151 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002152
2153 if (snd_BUG_ON(mask & ~0xff))
2154 mask &= 0xff;
2155 val &= mask;
2156
2157 mutex_lock(&codec->hash_mutex);
2158 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2159 if (!info) {
2160 mutex_unlock(&codec->hash_mutex);
2161 return 0;
2162 }
2163 val |= info->vol[ch] & ~mask;
2164 if (info->vol[ch] == val) {
2165 mutex_unlock(&codec->hash_mutex);
2166 return 0;
2167 }
2168 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002169 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002170 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002171 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002172 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002173 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002174 return 1;
2175}
2176
Takashi Iwaid5191e52009-11-16 14:58:17 +01002177/**
2178 * snd_hda_codec_amp_update - update the AMP value
2179 * @codec: HD-audio codec
2180 * @nid: NID to read the AMP value
2181 * @ch: channel (left=0 or right=1)
2182 * @direction: #HDA_INPUT or #HDA_OUTPUT
2183 * @idx: the index value (only for input direction)
2184 * @mask: bit mask to set
2185 * @val: the bits value to set
2186 *
2187 * Update the AMP value with a bit mask.
2188 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002189 */
Takashi Iwai834be882006-03-01 14:16:17 +01002190int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2191 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002193 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002195EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197/**
2198 * snd_hda_codec_amp_stereo - update the AMP stereo values
2199 * @codec: HD-audio codec
2200 * @nid: NID to read the AMP value
2201 * @direction: #HDA_INPUT or #HDA_OUTPUT
2202 * @idx: the index value (only for input direction)
2203 * @mask: bit mask to set
2204 * @val: the bits value to set
2205 *
2206 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2207 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002208 */
2209int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2210 int direction, int idx, int mask, int val)
2211{
2212 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002213
2214 if (snd_BUG_ON(mask & ~0xff))
2215 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002216 for (ch = 0; ch < 2; ch++)
2217 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2218 idx, mask, val);
2219 return ret;
2220}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002221EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002222
Takashi Iwai280e57d2012-12-14 10:32:21 +01002223/* Works like snd_hda_codec_amp_update() but it writes the value only at
2224 * the first access. If the amp was already initialized / updated beforehand,
2225 * this does nothing.
2226 */
2227int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2228 int dir, int idx, int mask, int val)
2229{
2230 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2231}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002232EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002233
2234int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2235 int dir, int idx, int mask, int val)
2236{
2237 int ch, ret = 0;
2238
2239 if (snd_BUG_ON(mask & ~0xff))
2240 mask &= 0xff;
2241 for (ch = 0; ch < 2; ch++)
2242 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2243 idx, mask, val);
2244 return ret;
2245}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002246EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002247
Takashi Iwaid5191e52009-11-16 14:58:17 +01002248/**
2249 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2250 * @codec: HD-audio codec
2251 *
2252 * Resume the all amp commands from the cache.
2253 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002254void snd_hda_codec_resume_amp(struct hda_codec *codec)
2255{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002256 int i;
2257
Takashi Iwaic370dd62012-12-13 18:30:04 +01002258 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002259 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002260 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2261 struct hda_amp_info *buffer;
2262 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002263 hda_nid_t nid;
2264 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002265 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002266
2267 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002268 if (!buffer->head.dirty)
2269 continue;
2270 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002271 info = *buffer;
2272 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002273 if (!key)
2274 continue;
2275 nid = key & 0xff;
2276 idx = (key >> 16) & 0xff;
2277 dir = (key >> 24) & 0xff;
2278 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002279 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002280 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002281 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002282 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2283 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002284 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002285 }
2286 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002287 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002288}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002289EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002291static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2292 unsigned int ofs)
2293{
2294 u32 caps = query_amp_caps(codec, nid, dir);
2295 /* get num steps */
2296 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2297 if (ofs < caps)
2298 caps -= ofs;
2299 return caps;
2300}
2301
Takashi Iwaid5191e52009-11-16 14:58:17 +01002302/**
2303 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2304 *
2305 * The control element is supposed to have the private_value field
2306 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2307 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002308int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2312 u16 nid = get_amp_nid(kcontrol);
2313 u8 chs = get_amp_channels(kcontrol);
2314 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002315 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002317 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2318 uinfo->count = chs == 3 ? 2 : 1;
2319 uinfo->value.integer.min = 0;
2320 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2321 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002322 codec_warn(codec,
2323 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2324 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -EINVAL;
2326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return 0;
2328}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002329EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002331
2332static inline unsigned int
2333read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2334 int ch, int dir, int idx, unsigned int ofs)
2335{
2336 unsigned int val;
2337 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2338 val &= HDA_AMP_VOLMASK;
2339 if (val >= ofs)
2340 val -= ofs;
2341 else
2342 val = 0;
2343 return val;
2344}
2345
2346static inline int
2347update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2348 int ch, int dir, int idx, unsigned int ofs,
2349 unsigned int val)
2350{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002351 unsigned int maxval;
2352
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002353 if (val > 0)
2354 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002355 /* ofs = 0: raw max value */
2356 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002357 if (val > maxval)
2358 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002359 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2360 HDA_AMP_VOLMASK, val);
2361}
2362
Takashi Iwaid5191e52009-11-16 14:58:17 +01002363/**
2364 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2365 *
2366 * The control element is supposed to have the private_value field
2367 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2368 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002369int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 hda_nid_t nid = get_amp_nid(kcontrol);
2374 int chs = get_amp_channels(kcontrol);
2375 int dir = get_amp_direction(kcontrol);
2376 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002377 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 long *valp = ucontrol->value.integer.value;
2379
2380 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002381 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002383 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
2385}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002386EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Takashi Iwaid5191e52009-11-16 14:58:17 +01002388/**
2389 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2390 *
2391 * The control element is supposed to have the private_value field
2392 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2393 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2398 hda_nid_t nid = get_amp_nid(kcontrol);
2399 int chs = get_amp_channels(kcontrol);
2400 int dir = get_amp_direction(kcontrol);
2401 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002402 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 long *valp = ucontrol->value.integer.value;
2404 int change = 0;
2405
Takashi Iwaicb53c622007-08-10 17:21:45 +02002406 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002407 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002408 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002409 valp++;
2410 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002411 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002412 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002413 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return change;
2415}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002416EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Takashi Iwaid5191e52009-11-16 14:58:17 +01002418/**
2419 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2420 *
2421 * The control element is supposed to have the private_value field
2422 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2423 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002424int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2425 unsigned int size, unsigned int __user *_tlv)
2426{
2427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2428 hda_nid_t nid = get_amp_nid(kcontrol);
2429 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002430 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002431 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002432 u32 caps, val1, val2;
2433
2434 if (size < 4 * sizeof(unsigned int))
2435 return -ENOMEM;
2436 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002437 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2438 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002439 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002440 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002441 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002442 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002443 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002444 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2445 return -EFAULT;
2446 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2447 return -EFAULT;
2448 if (put_user(val1, _tlv + 2))
2449 return -EFAULT;
2450 if (put_user(val2, _tlv + 3))
2451 return -EFAULT;
2452 return 0;
2453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002454EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002455
Takashi Iwaid5191e52009-11-16 14:58:17 +01002456/**
2457 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2458 * @codec: HD-audio codec
2459 * @nid: NID of a reference widget
2460 * @dir: #HDA_INPUT or #HDA_OUTPUT
2461 * @tlv: TLV data to be stored, at least 4 elements
2462 *
2463 * Set (static) TLV data for a virtual master volume using the AMP caps
2464 * obtained from the reference NID.
2465 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002466 */
2467void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2468 unsigned int *tlv)
2469{
2470 u32 caps;
2471 int nums, step;
2472
2473 caps = query_amp_caps(codec, nid, dir);
2474 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2475 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2476 step = (step + 1) * 25;
2477 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2478 tlv[1] = 2 * sizeof(unsigned int);
2479 tlv[2] = -nums * step;
2480 tlv[3] = step;
2481}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002482EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002483
2484/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002485static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002486find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002487{
2488 struct snd_ctl_elem_id id;
2489 memset(&id, 0, sizeof(id));
2490 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002491 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002492 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002493 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2494 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002495 strcpy(id.name, name);
2496 return snd_ctl_find_id(codec->bus->card, &id);
2497}
2498
Takashi Iwaid5191e52009-11-16 14:58:17 +01002499/**
2500 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2501 * @codec: HD-audio codec
2502 * @name: ctl id name string
2503 *
2504 * Get the control element with the given id string and IFACE_MIXER.
2505 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002506struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2507 const char *name)
2508{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002509 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002510}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002511EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002512
Takashi Iwaidcda5802012-10-12 17:24:51 +02002513static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002514 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002515{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002516 int i, idx;
2517 /* 16 ctlrs should be large enough */
2518 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2519 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002520 return idx;
2521 }
2522 return -EBUSY;
2523}
2524
Takashi Iwaid5191e52009-11-16 14:58:17 +01002525/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002526 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002527 * @codec: HD-audio codec
2528 * @nid: corresponding NID (optional)
2529 * @kctl: the control element to assign
2530 *
2531 * Add the given control element to an array inside the codec instance.
2532 * All control elements belonging to a codec are supposed to be added
2533 * by this function so that a proper clean-up works at the free or
2534 * reconfiguration time.
2535 *
2536 * If non-zero @nid is passed, the NID is assigned to the control element.
2537 * The assignment is shown in the codec proc file.
2538 *
2539 * snd_hda_ctl_add() checks the control subdev id field whether
2540 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002541 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2542 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002543 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002544int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2545 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002546{
2547 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002548 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002549 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002550
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002551 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002552 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002553 if (nid == 0)
2554 nid = get_amp_nid_(kctl->private_value);
2555 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002556 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2557 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002558 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002559 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002560 err = snd_ctl_add(codec->bus->card, kctl);
2561 if (err < 0)
2562 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002563 item = snd_array_new(&codec->mixers);
2564 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002565 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002566 item->kctl = kctl;
2567 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002568 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002569 return 0;
2570}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002571EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002572
Takashi Iwaid5191e52009-11-16 14:58:17 +01002573/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002574 * snd_hda_add_nid - Assign a NID to a control element
2575 * @codec: HD-audio codec
2576 * @nid: corresponding NID (optional)
2577 * @kctl: the control element to assign
2578 * @index: index to kctl
2579 *
2580 * Add the given control element to an array inside the codec instance.
2581 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2582 * NID:KCTL mapping - for example "Capture Source" selector.
2583 */
2584int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2585 unsigned int index, hda_nid_t nid)
2586{
2587 struct hda_nid_item *item;
2588
2589 if (nid > 0) {
2590 item = snd_array_new(&codec->nids);
2591 if (!item)
2592 return -ENOMEM;
2593 item->kctl = kctl;
2594 item->index = index;
2595 item->nid = nid;
2596 return 0;
2597 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002598 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2599 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002600 return -EINVAL;
2601}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002602EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002603
2604/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002605 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2606 * @codec: HD-audio codec
2607 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002608void snd_hda_ctls_clear(struct hda_codec *codec)
2609{
2610 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002611 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002612 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002613 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002614 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002615 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002616}
2617
Takashi Iwaia65d6292009-02-23 16:57:04 +01002618/* pseudo device locking
2619 * toggle card->shutdown to allow/disallow the device access (as a hack)
2620 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002621int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002622{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002623 struct snd_card *card = bus->card;
2624 struct hda_codec *codec;
2625
Takashi Iwaia65d6292009-02-23 16:57:04 +01002626 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002627 if (card->shutdown)
2628 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002629 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002630 if (!list_empty(&card->ctl_files))
2631 goto err_clear;
2632
2633 list_for_each_entry(codec, &bus->codec_list, list) {
2634 int pcm;
2635 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2636 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2637 if (!cpcm->pcm)
2638 continue;
2639 if (cpcm->pcm->streams[0].substream_opened ||
2640 cpcm->pcm->streams[1].substream_opened)
2641 goto err_clear;
2642 }
2643 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002644 spin_unlock(&card->files_lock);
2645 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002646
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002647 err_clear:
2648 card->shutdown = 0;
2649 err_unlock:
2650 spin_unlock(&card->files_lock);
2651 return -EINVAL;
2652}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002653EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002654
2655void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002656{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002657 struct snd_card *card = bus->card;
2658
2659 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002660 spin_lock(&card->files_lock);
2661 card->shutdown = 0;
2662 spin_unlock(&card->files_lock);
2663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002664EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002665
Takashi Iwaid5191e52009-11-16 14:58:17 +01002666/**
2667 * snd_hda_codec_reset - Clear all objects assigned to the codec
2668 * @codec: HD-audio codec
2669 *
2670 * This frees the all PCM and control elements assigned to the codec, and
2671 * clears the caches and restores the pin default configurations.
2672 *
2673 * When a device is being used, it returns -EBSY. If successfully freed,
2674 * returns zero.
2675 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002676int snd_hda_codec_reset(struct hda_codec *codec)
2677{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002678 struct hda_bus *bus = codec->bus;
2679 struct snd_card *card = bus->card;
2680 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002681
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002682 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002683 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002684
2685 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002686 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002687#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002688 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002689 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002690#endif
2691 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002692 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002693 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002694 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002695 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002696 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002697 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002698 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002699 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002700 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002701 if (codec->patch_ops.free)
2702 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002703 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002704 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002705 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002706 codec->spec = NULL;
2707 free_hda_cache(&codec->amp_cache);
2708 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002709 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2710 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002711 /* free only driver_pins so that init_pins + user_pins are restored */
2712 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002713 snd_array_free(&codec->cvt_setups);
2714 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002715 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002716 codec->num_pcms = 0;
2717 codec->pcm_info = NULL;
2718 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002719 codec->slave_dig_outs = NULL;
2720 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002721 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002722 module_put(codec->owner);
2723 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002724
2725 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002726 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002727 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002728}
2729
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002730typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2731
2732/* apply the function to all matching slave ctls in the mixer list */
2733static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002734 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002735{
2736 struct hda_nid_item *items;
2737 const char * const *s;
2738 int i, err;
2739
2740 items = codec->mixers.list;
2741 for (i = 0; i < codec->mixers.used; i++) {
2742 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002743 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002744 continue;
2745 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002746 char tmpname[sizeof(sctl->id.name)];
2747 const char *name = *s;
2748 if (suffix) {
2749 snprintf(tmpname, sizeof(tmpname), "%s %s",
2750 name, suffix);
2751 name = tmpname;
2752 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002753 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002754 err = func(data, sctl);
2755 if (err)
2756 return err;
2757 break;
2758 }
2759 }
2760 }
2761 return 0;
2762}
2763
2764static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2765{
2766 return 1;
2767}
2768
Takashi Iwai18478e82012-03-09 17:51:10 +01002769/* guess the value corresponding to 0dB */
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002770static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002771{
2772 int _tlv[4];
2773 const int *tlv = NULL;
2774 int val = -1;
2775
2776 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2777 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2778 mm_segment_t fs = get_fs();
2779 set_fs(get_ds());
2780 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2781 tlv = _tlv;
2782 set_fs(fs);
2783 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2784 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002785 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2786 int step = tlv[3];
2787 step &= ~TLV_DB_SCALE_MUTE;
2788 if (!step)
2789 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002790 if (*step_to_check && *step_to_check != step) {
2791 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002792- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002793 return -1;
2794 }
2795 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002796 val = -tlv[2] / step;
2797 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002798 return val;
2799}
2800
2801/* call kctl->put with the given value(s) */
2802static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2803{
2804 struct snd_ctl_elem_value *ucontrol;
2805 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2806 if (!ucontrol)
2807 return -ENOMEM;
2808 ucontrol->value.integer.value[0] = val;
2809 ucontrol->value.integer.value[1] = val;
2810 kctl->put(kctl, ucontrol);
2811 kfree(ucontrol);
2812 return 0;
2813}
2814
2815/* initialize the slave volume with 0dB */
2816static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2817{
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002818 int offset = get_kctl_0dB_offset(slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002819 if (offset > 0)
2820 put_kctl_with_value(slave, offset);
2821 return 0;
2822}
2823
2824/* unmute the slave */
2825static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2826{
2827 return put_kctl_with_value(slave, 1);
2828}
2829
Takashi Iwaid5191e52009-11-16 14:58:17 +01002830/**
2831 * snd_hda_add_vmaster - create a virtual master control and add slaves
2832 * @codec: HD-audio codec
2833 * @name: vmaster control name
2834 * @tlv: TLV data (optional)
2835 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002836 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002837 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002838 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002839 *
2840 * Create a virtual master control with the given name. The TLV data
2841 * must be either NULL or a valid data.
2842 *
2843 * @slaves is a NULL-terminated array of strings, each of which is a
2844 * slave control name. All controls with these names are assigned to
2845 * the new virtual master control.
2846 *
2847 * This function returns zero if successful or a negative error code.
2848 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002849int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002850 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002851 const char *suffix, bool init_slave_vol,
2852 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002853{
2854 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002855 int err;
2856
Takashi Iwai29e58532012-03-12 12:25:03 +01002857 if (ctl_ret)
2858 *ctl_ret = NULL;
2859
Takashi Iwai9322ca52012-02-03 14:28:01 +01002860 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002861 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002862 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002863 return 0;
2864 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002865 kctl = snd_ctl_make_virtual_master(name, tlv);
2866 if (!kctl)
2867 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002868 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002869 if (err < 0)
2870 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002871
Takashi Iwai9322ca52012-02-03 14:28:01 +01002872 err = map_slaves(codec, slaves, suffix,
2873 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002874 if (err < 0)
2875 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002876
2877 /* init with master mute & zero volume */
2878 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002879 if (init_slave_vol) {
2880 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002881 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002882 tlv ? init_slave_0dB : init_slave_unmute, &step);
2883 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002884
Takashi Iwai29e58532012-03-12 12:25:03 +01002885 if (ctl_ret)
2886 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002887 return 0;
2888}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002889EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002890
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002891/*
2892 * mute-LED control using vmaster
2893 */
2894static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_info *uinfo)
2896{
2897 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002898 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002899 };
2900 unsigned int index;
2901
2902 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2903 uinfo->count = 1;
2904 uinfo->value.enumerated.items = 3;
2905 index = uinfo->value.enumerated.item;
2906 if (index >= 3)
2907 index = 2;
2908 strcpy(uinfo->value.enumerated.name, texts[index]);
2909 return 0;
2910}
2911
2912static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2913 struct snd_ctl_elem_value *ucontrol)
2914{
2915 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2916 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2917 return 0;
2918}
2919
2920static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2922{
2923 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2924 unsigned int old_mode = hook->mute_mode;
2925
2926 hook->mute_mode = ucontrol->value.enumerated.item[0];
2927 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2928 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2929 if (old_mode == hook->mute_mode)
2930 return 0;
2931 snd_hda_sync_vmaster_hook(hook);
2932 return 1;
2933}
2934
2935static struct snd_kcontrol_new vmaster_mute_mode = {
2936 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2937 .name = "Mute-LED Mode",
2938 .info = vmaster_mute_mode_info,
2939 .get = vmaster_mute_mode_get,
2940 .put = vmaster_mute_mode_put,
2941};
2942
2943/*
2944 * Add a mute-LED hook with the given vmaster switch kctl
2945 * "Mute-LED Mode" control is automatically created and associated with
2946 * the given hook.
2947 */
2948int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002949 struct hda_vmaster_mute_hook *hook,
2950 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002951{
2952 struct snd_kcontrol *kctl;
2953
2954 if (!hook->hook || !hook->sw_kctl)
2955 return 0;
2956 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2957 hook->codec = codec;
2958 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002959 if (!expose_enum_ctl)
2960 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002961 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2962 if (!kctl)
2963 return -ENOMEM;
2964 return snd_hda_ctl_add(codec, 0, kctl);
2965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002966EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002967
2968/*
2969 * Call the hook with the current value for synchronization
2970 * Should be called in init callback
2971 */
2972void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2973{
2974 if (!hook->hook || !hook->codec)
2975 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002976 /* don't call vmaster hook in the destructor since it might have
2977 * been already destroyed
2978 */
2979 if (hook->codec->bus->shutdown)
2980 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002981 switch (hook->mute_mode) {
2982 case HDA_VMUTE_FOLLOW_MASTER:
2983 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2984 break;
2985 default:
2986 hook->hook(hook->codec, hook->mute_mode);
2987 break;
2988 }
2989}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002990EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002991
2992
Takashi Iwaid5191e52009-11-16 14:58:17 +01002993/**
2994 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2995 *
2996 * The control element is supposed to have the private_value field
2997 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2998 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
3002 int chs = get_amp_channels(kcontrol);
3003
3004 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3005 uinfo->count = chs == 3 ? 2 : 1;
3006 uinfo->value.integer.min = 0;
3007 uinfo->value.integer.max = 1;
3008 return 0;
3009}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003010EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Takashi Iwaid5191e52009-11-16 14:58:17 +01003012/**
3013 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3014 *
3015 * The control element is supposed to have the private_value field
3016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3019 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3022 hda_nid_t nid = get_amp_nid(kcontrol);
3023 int chs = get_amp_channels(kcontrol);
3024 int dir = get_amp_direction(kcontrol);
3025 int idx = get_amp_index(kcontrol);
3026 long *valp = ucontrol->value.integer.value;
3027
3028 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003029 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003030 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003033 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return 0;
3035}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003036EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Takashi Iwaid5191e52009-11-16 14:58:17 +01003038/**
3039 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3040 *
3041 * The control element is supposed to have the private_value field
3042 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3043 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003044int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3048 hda_nid_t nid = get_amp_nid(kcontrol);
3049 int chs = get_amp_channels(kcontrol);
3050 int dir = get_amp_direction(kcontrol);
3051 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 long *valp = ucontrol->value.integer.value;
3053 int change = 0;
3054
Takashi Iwaicb53c622007-08-10 17:21:45 +02003055 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003056 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003057 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003058 HDA_AMP_MUTE,
3059 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003060 valp++;
3061 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003062 if (chs & 2)
3063 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003064 HDA_AMP_MUTE,
3065 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003066 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003067 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return change;
3069}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003070EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072/*
Takashi Iwai985be542005-11-02 18:26:49 +01003073 * bound volume controls
3074 *
3075 * bind multiple volumes (# indices, from 0)
3076 */
3077
3078#define AMP_VAL_IDX_SHIFT 19
3079#define AMP_VAL_IDX_MASK (0x0f<<19)
3080
Takashi Iwaid5191e52009-11-16 14:58:17 +01003081/**
3082 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3083 *
3084 * The control element is supposed to have the private_value field
3085 * set up via HDA_BIND_MUTE*() macros.
3086 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3088 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003089{
3090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3091 unsigned long pval;
3092 int err;
3093
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003094 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003095 pval = kcontrol->private_value;
3096 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3097 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3098 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003099 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003100 return err;
3101}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003102EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003103
Takashi Iwaid5191e52009-11-16 14:58:17 +01003104/**
3105 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3106 *
3107 * The control element is supposed to have the private_value field
3108 * set up via HDA_BIND_MUTE*() macros.
3109 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003112{
3113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3114 unsigned long pval;
3115 int i, indices, err = 0, change = 0;
3116
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003117 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003118 pval = kcontrol->private_value;
3119 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3120 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003121 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3122 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003123 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3124 if (err < 0)
3125 break;
3126 change |= err;
3127 }
3128 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003129 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003130 return err < 0 ? err : change;
3131}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003132EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003133
Takashi Iwaid5191e52009-11-16 14:58:17 +01003134/**
3135 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3136 *
3137 * The control element is supposed to have the private_value field
3138 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003139 */
3140int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_info *uinfo)
3142{
3143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3144 struct hda_bind_ctls *c;
3145 int err;
3146
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003147 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003148 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003149 kcontrol->private_value = *c->values;
3150 err = c->ops->info(kcontrol, uinfo);
3151 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003152 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003153 return err;
3154}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003155EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003156
Takashi Iwaid5191e52009-11-16 14:58:17 +01003157/**
3158 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3159 *
3160 * The control element is supposed to have the private_value field
3161 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3162 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003163int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3164 struct snd_ctl_elem_value *ucontrol)
3165{
3166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3167 struct hda_bind_ctls *c;
3168 int err;
3169
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003170 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003171 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003172 kcontrol->private_value = *c->values;
3173 err = c->ops->get(kcontrol, ucontrol);
3174 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003175 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003176 return err;
3177}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003178EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003179
Takashi Iwaid5191e52009-11-16 14:58:17 +01003180/**
3181 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3182 *
3183 * The control element is supposed to have the private_value field
3184 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3185 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003186int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
3188{
3189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3190 struct hda_bind_ctls *c;
3191 unsigned long *vals;
3192 int err = 0, change = 0;
3193
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003194 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003195 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003196 for (vals = c->values; *vals; vals++) {
3197 kcontrol->private_value = *vals;
3198 err = c->ops->put(kcontrol, ucontrol);
3199 if (err < 0)
3200 break;
3201 change |= err;
3202 }
3203 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003204 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003205 return err < 0 ? err : change;
3206}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003207EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003208
Takashi Iwaid5191e52009-11-16 14:58:17 +01003209/**
3210 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3211 *
3212 * The control element is supposed to have the private_value field
3213 * set up via HDA_BIND_VOL() macro.
3214 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003215int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3216 unsigned int size, unsigned int __user *tlv)
3217{
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3219 struct hda_bind_ctls *c;
3220 int err;
3221
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003222 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003223 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003224 kcontrol->private_value = *c->values;
3225 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3226 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003227 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003228 return err;
3229}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003230EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003231
3232struct hda_ctl_ops snd_hda_bind_vol = {
3233 .info = snd_hda_mixer_amp_volume_info,
3234 .get = snd_hda_mixer_amp_volume_get,
3235 .put = snd_hda_mixer_amp_volume_put,
3236 .tlv = snd_hda_mixer_amp_tlv
3237};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003238EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003239
3240struct hda_ctl_ops snd_hda_bind_sw = {
3241 .info = snd_hda_mixer_amp_switch_info,
3242 .get = snd_hda_mixer_amp_switch_get,
3243 .put = snd_hda_mixer_amp_switch_put,
3244 .tlv = snd_hda_mixer_amp_tlv
3245};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003246EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003247
3248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 * SPDIF out controls
3250 */
3251
Takashi Iwai0ba21762007-04-16 11:29:14 +02003252static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3253 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3256 uinfo->count = 1;
3257 return 0;
3258}
3259
Takashi Iwai0ba21762007-04-16 11:29:14 +02003260static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3261 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3264 IEC958_AES0_NONAUDIO |
3265 IEC958_AES0_CON_EMPHASIS_5015 |
3266 IEC958_AES0_CON_NOT_COPYRIGHT;
3267 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3268 IEC958_AES1_CON_ORIGINAL;
3269 return 0;
3270}
3271
Takashi Iwai0ba21762007-04-16 11:29:14 +02003272static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3276 IEC958_AES0_NONAUDIO |
3277 IEC958_AES0_PRO_EMPHASIS_5015;
3278 return 0;
3279}
3280
Takashi Iwai0ba21762007-04-16 11:29:14 +02003281static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3282 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003285 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003286 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003288 mutex_lock(&codec->spdif_mutex);
3289 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003290 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3291 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3292 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3293 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003294 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 return 0;
3297}
3298
3299/* convert from SPDIF status bits to HDA SPDIF bits
3300 * bit 0 (DigEn) is always set zero (to be filled later)
3301 */
3302static unsigned short convert_from_spdif_status(unsigned int sbits)
3303{
3304 unsigned short val = 0;
3305
3306 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003307 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003309 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003311 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3312 IEC958_AES0_PRO_EMPHASIS_5015)
3313 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003315 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3316 IEC958_AES0_CON_EMPHASIS_5015)
3317 val |= AC_DIG1_EMPHASIS;
3318 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3319 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003321 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3323 }
3324 return val;
3325}
3326
3327/* convert to SPDIF status bits from HDA SPDIF bits
3328 */
3329static unsigned int convert_to_spdif_status(unsigned short val)
3330{
3331 unsigned int sbits = 0;
3332
Takashi Iwai0ba21762007-04-16 11:29:14 +02003333 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003335 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 sbits |= IEC958_AES0_PROFESSIONAL;
3337 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003338 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3340 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003341 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003345 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3347 sbits |= val & (0x7f << 8);
3348 }
3349 return sbits;
3350}
3351
Takashi Iwai2f728532008-09-25 16:32:41 +02003352/* set digital convert verbs both for the given NID and its slaves */
3353static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3354 int verb, int val)
3355{
Takashi Iwaidda14412011-05-02 11:29:30 +02003356 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003357
Takashi Iwai9e976972008-11-25 08:17:20 +01003358 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003359 d = codec->slave_dig_outs;
3360 if (!d)
3361 return;
3362 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003363 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003364}
3365
3366static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3367 int dig1, int dig2)
3368{
3369 if (dig1 != -1)
3370 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3371 if (dig2 != -1)
3372 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3373}
3374
Takashi Iwai0ba21762007-04-16 11:29:14 +02003375static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3376 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003379 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003380 struct hda_spdif_out *spdif;
3381 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 unsigned short val;
3383 int change;
3384
Ingo Molnar62932df2006-01-16 16:34:20 +01003385 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003386 spdif = snd_array_elem(&codec->spdif_out, idx);
3387 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003388 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3390 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3391 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003392 val = convert_from_spdif_status(spdif->status);
3393 val |= spdif->ctls & 1;
3394 change = spdif->ctls != val;
3395 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003396 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003397 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003398 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return change;
3400}
3401
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003402#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Takashi Iwai0ba21762007-04-16 11:29:14 +02003404static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003408 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003409 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003411 mutex_lock(&codec->spdif_mutex);
3412 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003413 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003414 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
3416}
3417
Stephen Warren74b654c2011-06-01 11:14:18 -06003418static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3419 int dig1, int dig2)
3420{
3421 set_dig_out_convert(codec, nid, dig1, dig2);
3422 /* unmute amp switch (if any) */
3423 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3424 (dig1 & AC_DIG1_ENABLE))
3425 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3426 HDA_AMP_MUTE, 0);
3427}
3428
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003433 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003434 struct hda_spdif_out *spdif;
3435 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 unsigned short val;
3437 int change;
3438
Ingo Molnar62932df2006-01-16 16:34:20 +01003439 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003440 spdif = snd_array_elem(&codec->spdif_out, idx);
3441 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003442 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003444 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003445 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003446 spdif->ctls = val;
3447 if (change && nid != (u16)-1)
3448 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003449 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return change;
3451}
3452
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003453static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 {
3455 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3456 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003457 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 .info = snd_hda_spdif_mask_info,
3459 .get = snd_hda_spdif_cmask_get,
3460 },
3461 {
3462 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3463 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003464 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 .info = snd_hda_spdif_mask_info,
3466 .get = snd_hda_spdif_pmask_get,
3467 },
3468 {
3469 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003470 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 .info = snd_hda_spdif_mask_info,
3472 .get = snd_hda_spdif_default_get,
3473 .put = snd_hda_spdif_default_put,
3474 },
3475 {
3476 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003477 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 .info = snd_hda_spdif_out_switch_info,
3479 .get = snd_hda_spdif_out_switch_get,
3480 .put = snd_hda_spdif_out_switch_put,
3481 },
3482 { } /* end */
3483};
3484
3485/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003486 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003488 * @associated_nid: NID that new ctls associated with
3489 * @cvt_nid: converter NID
3490 * @type: HDA_PCM_TYPE_*
3491 * Creates controls related with the digital output.
3492 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 *
3494 * Returns 0 if successful, or a negative error code.
3495 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003496int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3497 hda_nid_t associated_nid,
3498 hda_nid_t cvt_nid,
3499 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
3501 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003502 struct snd_kcontrol *kctl;
3503 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003504 int idx = 0;
3505 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003506 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003507 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003509 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003510 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003511 idx = spdif_index;
3512 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003513 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003514 /* suppose a single SPDIF device */
3515 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3516 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3517 if (!kctl)
3518 break;
3519 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003520 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003521 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003522 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003523 if (!bus->primary_dig_out_type)
3524 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003525
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003526 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003527 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003528 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003529 return -EBUSY;
3530 }
Stephen Warren7c935972011-06-01 11:14:17 -06003531 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003532 if (!spdif)
3533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3535 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003536 if (!kctl)
3537 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003538 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003539 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003540 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003541 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return err;
3543 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003544 spdif->nid = cvt_nid;
3545 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003546 AC_VERB_GET_DIGI_CONVERT_1, 0);
3547 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return 0;
3549}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003550EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003552/* get the hda_spdif_out entry from the given NID
3553 * call within spdif_mutex lock
3554 */
Stephen Warren7c935972011-06-01 11:14:17 -06003555struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3556 hda_nid_t nid)
3557{
3558 int i;
3559 for (i = 0; i < codec->spdif_out.used; i++) {
3560 struct hda_spdif_out *spdif =
3561 snd_array_elem(&codec->spdif_out, i);
3562 if (spdif->nid == nid)
3563 return spdif;
3564 }
3565 return NULL;
3566}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003567EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003568
Stephen Warren74b654c2011-06-01 11:14:18 -06003569void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3570{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003571 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003572
3573 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003574 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003575 spdif->nid = (u16)-1;
3576 mutex_unlock(&codec->spdif_mutex);
3577}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003578EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003579
3580void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3581{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003582 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003583 unsigned short val;
3584
3585 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003586 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003587 if (spdif->nid != nid) {
3588 spdif->nid = nid;
3589 val = spdif->ctls;
3590 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3591 }
3592 mutex_unlock(&codec->spdif_mutex);
3593}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003594EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003597 * SPDIF sharing with analog output
3598 */
3599static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3600 struct snd_ctl_elem_value *ucontrol)
3601{
3602 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3603 ucontrol->value.integer.value[0] = mout->share_spdif;
3604 return 0;
3605}
3606
3607static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3608 struct snd_ctl_elem_value *ucontrol)
3609{
3610 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3611 mout->share_spdif = !!ucontrol->value.integer.value[0];
3612 return 0;
3613}
3614
3615static struct snd_kcontrol_new spdif_share_sw = {
3616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3617 .name = "IEC958 Default PCM Playback Switch",
3618 .info = snd_ctl_boolean_mono_info,
3619 .get = spdif_share_sw_get,
3620 .put = spdif_share_sw_put,
3621};
3622
Takashi Iwaid5191e52009-11-16 14:58:17 +01003623/**
3624 * snd_hda_create_spdif_share_sw - create Default PCM switch
3625 * @codec: the HDA codec
3626 * @mout: multi-out instance
3627 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003628int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3629 struct hda_multi_out *mout)
3630{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003631 struct snd_kcontrol *kctl;
3632
Takashi Iwai9a081602008-02-12 18:37:26 +01003633 if (!mout->dig_out_nid)
3634 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003635
3636 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3637 if (!kctl)
3638 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003639 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003640 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003641}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003642EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003643
3644/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 * SPDIF input
3646 */
3647
3648#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3649
Takashi Iwai0ba21762007-04-16 11:29:14 +02003650static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3651 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
3653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3654
3655 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3656 return 0;
3657}
3658
Takashi Iwai0ba21762007-04-16 11:29:14 +02003659static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3660 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
3662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3663 hda_nid_t nid = kcontrol->private_value;
3664 unsigned int val = !!ucontrol->value.integer.value[0];
3665 int change;
3666
Ingo Molnar62932df2006-01-16 16:34:20 +01003667 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003669 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003671 snd_hda_codec_write_cache(codec, nid, 0,
3672 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003674 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return change;
3676}
3677
Takashi Iwai0ba21762007-04-16 11:29:14 +02003678static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3679 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
3681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3682 hda_nid_t nid = kcontrol->private_value;
3683 unsigned short val;
3684 unsigned int sbits;
3685
Andrew Paprocki3982d172007-12-19 12:13:44 +01003686 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 sbits = convert_to_spdif_status(val);
3688 ucontrol->value.iec958.status[0] = sbits;
3689 ucontrol->value.iec958.status[1] = sbits >> 8;
3690 ucontrol->value.iec958.status[2] = sbits >> 16;
3691 ucontrol->value.iec958.status[3] = sbits >> 24;
3692 return 0;
3693}
3694
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003695static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 {
3697 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003698 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 .info = snd_hda_spdif_in_switch_info,
3700 .get = snd_hda_spdif_in_switch_get,
3701 .put = snd_hda_spdif_in_switch_put,
3702 },
3703 {
3704 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3705 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003706 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .info = snd_hda_spdif_mask_info,
3708 .get = snd_hda_spdif_in_status_get,
3709 },
3710 { } /* end */
3711};
3712
3713/**
3714 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3715 * @codec: the HDA codec
3716 * @nid: audio in widget NID
3717 *
3718 * Creates controls related with the SPDIF input.
3719 * Called from each patch supporting the SPDIF in.
3720 *
3721 * Returns 0 if successful, or a negative error code.
3722 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003723int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003726 struct snd_kcontrol *kctl;
3727 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003728 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Takashi Iwaidcda5802012-10-12 17:24:51 +02003730 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003731 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003732 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003733 return -EBUSY;
3734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3736 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003737 if (!kctl)
3738 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003740 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003741 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return err;
3743 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003744 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003745 snd_hda_codec_read(codec, nid, 0,
3746 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003747 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return 0;
3749}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003750EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003752/*
3753 * command cache
3754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Takashi Iwaic370dd62012-12-13 18:30:04 +01003756/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003757#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3758#define get_cmd_cache_nid(key) ((key) & 0xff)
3759#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3760
3761/**
3762 * snd_hda_codec_write_cache - send a single command with caching
3763 * @codec: the HDA codec
3764 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003765 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003766 * @verb: the verb to send
3767 * @parm: the parameter for the verb
3768 *
3769 * Send a single command without waiting for response.
3770 *
3771 * Returns 0 if successful, or a negative error code.
3772 */
3773int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003774 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003775{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003776 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003777 struct hda_cache_head *c;
3778 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003779 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003780
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003781 cache_only = codec->cached_write;
3782 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003783 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003784 if (err < 0)
3785 return err;
3786 }
3787
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003788 /* parm may contain the verb stuff for get/set amp */
3789 verb = verb | (parm >> 8);
3790 parm &= 0xff;
3791 key = build_cmd_cache_key(nid, verb);
3792 mutex_lock(&codec->bus->cmd_mutex);
3793 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003794 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003795 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003796 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003797 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003798 mutex_unlock(&codec->bus->cmd_mutex);
3799 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003800}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003801EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003802
Takashi Iwaid5191e52009-11-16 14:58:17 +01003803/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003804 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3805 * @codec: the HDA codec
3806 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003807 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003808 * @verb: the verb to send
3809 * @parm: the parameter for the verb
3810 *
3811 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3812 * command if the parameter is already identical with the cached value.
3813 * If not, it sends the command and refreshes the cache.
3814 *
3815 * Returns 0 if successful, or a negative error code.
3816 */
3817int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003818 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003819{
3820 struct hda_cache_head *c;
3821 u32 key;
3822
3823 /* parm may contain the verb stuff for get/set amp */
3824 verb = verb | (parm >> 8);
3825 parm &= 0xff;
3826 key = build_cmd_cache_key(nid, verb);
3827 mutex_lock(&codec->bus->cmd_mutex);
3828 c = get_hash(&codec->cmd_cache, key);
3829 if (c && c->val == parm) {
3830 mutex_unlock(&codec->bus->cmd_mutex);
3831 return 0;
3832 }
3833 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003834 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003835}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003836EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003837
3838/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003839 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3840 * @codec: HD-audio codec
3841 *
3842 * Execute all verbs recorded in the command caches to resume.
3843 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003844void snd_hda_codec_resume_cache(struct hda_codec *codec)
3845{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003846 int i;
3847
Takashi Iwaic370dd62012-12-13 18:30:04 +01003848 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003849 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003850 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3851 struct hda_cache_head *buffer;
3852 u32 key;
3853
3854 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3855 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003856 if (!key)
3857 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003858 if (!buffer->dirty)
3859 continue;
3860 buffer->dirty = 0;
3861 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003862 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3863 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003864 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003865 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003866 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003867}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003868EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003869
3870/**
3871 * snd_hda_sequence_write_cache - sequence writes with caching
3872 * @codec: the HDA codec
3873 * @seq: VERB array to send
3874 *
3875 * Send the commands sequentially from the given array.
3876 * Thte commands are recorded on cache for power-save and resume.
3877 * The array must be terminated with NID=0.
3878 */
3879void snd_hda_sequence_write_cache(struct hda_codec *codec,
3880 const struct hda_verb *seq)
3881{
3882 for (; seq->nid; seq++)
3883 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3884 seq->param);
3885}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003886EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003887
Takashi Iwaidc870f32013-01-22 15:24:30 +01003888/**
3889 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3890 * @codec: HD-audio codec
3891 */
3892void snd_hda_codec_flush_cache(struct hda_codec *codec)
3893{
3894 snd_hda_codec_resume_amp(codec);
3895 snd_hda_codec_resume_cache(codec);
3896}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003897EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003898
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003899void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003900 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003901{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003902 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003903 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003904
Takashi Iwaicb53c622007-08-10 17:21:45 +02003905 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003906 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003907 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003908 if (!(wcaps & AC_WCAP_POWER))
3909 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003910 if (codec->power_filter) {
3911 state = codec->power_filter(codec, nid, power_state);
3912 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003913 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003914 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003915 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003916 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003917 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003918}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003919EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003920
3921/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003922 * supported power states check
3923 */
3924static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3925 unsigned int power_state)
3926{
3927 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3928
Mengdong Line037cb42012-08-10 14:11:58 +02003929 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003930 return false;
3931 if (sup & power_state)
3932 return true;
3933 else
3934 return false;
3935}
3936
3937/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003938 * wait until the state is reached, returns the current state
3939 */
3940static unsigned int hda_sync_power_state(struct hda_codec *codec,
3941 hda_nid_t fg,
3942 unsigned int power_state)
3943{
3944 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3945 unsigned int state, actual_state;
3946
3947 for (;;) {
3948 state = snd_hda_codec_read(codec, fg, 0,
3949 AC_VERB_GET_POWER_STATE, 0);
3950 if (state & AC_PWRST_ERROR)
3951 break;
3952 actual_state = (state >> 4) & 0x0f;
3953 if (actual_state == power_state)
3954 break;
3955 if (time_after_eq(jiffies, end_time))
3956 break;
3957 /* wait until the codec reachs to the target state */
3958 msleep(1);
3959 }
3960 return state;
3961}
3962
Takashi Iwai9419ab62013-01-24 17:23:35 +01003963/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003964unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3965 hda_nid_t nid,
3966 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003967{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003968 if (nid == codec->afg || nid == codec->mfg)
3969 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003970 if (power_state == AC_PWRST_D3 &&
3971 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3972 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3973 int eapd = snd_hda_codec_read(codec, nid, 0,
3974 AC_VERB_GET_EAPD_BTLENABLE, 0);
3975 if (eapd & 0x02)
3976 return AC_PWRST_D0;
3977 }
3978 return power_state;
3979}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003980EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003981
Takashi Iwai432c6412012-08-28 09:59:20 -07003982/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003983 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003984 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003985static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003986 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003987{
Takashi Iwaid8193872012-08-31 07:54:38 -07003988 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003989 int count;
3990 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003991 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003992
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003993 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003994 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003995 if (codec->depop_delay < 0)
3996 msleep(codec->epss ? 10 : 100);
3997 else if (codec->depop_delay > 0)
3998 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003999 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004000 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004001
4002 /* repeat power states setting at most 10 times*/
4003 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004004 if (codec->patch_ops.set_power_state)
4005 codec->patch_ops.set_power_state(codec, fg,
4006 power_state);
4007 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004008 state = power_state;
4009 if (codec->power_filter)
4010 state = codec->power_filter(codec, fg, state);
4011 if (state == power_state || power_state != AC_PWRST_D3)
4012 snd_hda_codec_read(codec, fg, flags,
4013 AC_VERB_SET_POWER_STATE,
4014 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004015 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004016 }
4017 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004018 if (!(state & AC_PWRST_ERROR))
4019 break;
4020 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004021
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004022 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004023}
Takashi Iwai54d17402005-11-21 16:33:22 +01004024
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004025/* sync power states of all widgets;
4026 * this is called at the end of codec parsing
4027 */
4028static void sync_power_up_states(struct hda_codec *codec)
4029{
4030 hda_nid_t nid = codec->start_nid;
4031 int i;
4032
Takashi Iwaiba615b82013-03-13 14:47:21 +01004033 /* don't care if no filter is used */
4034 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004035 return;
4036
4037 for (i = 0; i < codec->num_nodes; i++, nid++) {
4038 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004039 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004040 if (!(wcaps & AC_WCAP_POWER))
4041 continue;
4042 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4043 if (target == AC_PWRST_D0)
4044 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004045 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004046 snd_hda_codec_write(codec, nid, 0,
4047 AC_VERB_SET_POWER_STATE, target);
4048 }
4049}
4050
Takashi Iwai648a8d22014-02-25 10:38:13 +01004051#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004052/* execute additional init verbs */
4053static void hda_exec_init_verbs(struct hda_codec *codec)
4054{
4055 if (codec->init_verbs.list)
4056 snd_hda_sequence_write(codec, codec->init_verbs.list);
4057}
4058#else
4059static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4060#endif
4061
Takashi Iwai2a439522011-07-26 09:52:50 +02004062#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004063/*
4064 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004065 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004066 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004067static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004068{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004069 unsigned int state;
4070
Takashi Iwai989c3182012-11-19 14:14:58 +01004071 codec->in_pm = 1;
4072
Takashi Iwaicb53c622007-08-10 17:21:45 +02004073 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004074 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004075 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004076 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004077 /* Cancel delayed work if we aren't currently running from it. */
4078 if (!in_wq)
4079 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004080 spin_lock(&codec->power_lock);
4081 snd_hda_update_power_acct(codec);
4082 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004083 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004084 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004085 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004086 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004087 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004088 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004089}
4090
Takashi Iwaic370dd62012-12-13 18:30:04 +01004091/* mark all entries of cmd and amp caches dirty */
4092static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4093{
4094 int i;
4095 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4096 struct hda_cache_head *cmd;
4097 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4098 cmd->dirty = 1;
4099 }
4100 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4101 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004102 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004103 amp->head.dirty = 1;
4104 }
4105}
4106
Takashi Iwaicb53c622007-08-10 17:21:45 +02004107/*
4108 * kick up codec; used both from PM and power-save
4109 */
4110static void hda_call_codec_resume(struct hda_codec *codec)
4111{
Takashi Iwai989c3182012-11-19 14:14:58 +01004112 codec->in_pm = 1;
4113
Takashi Iwaic370dd62012-12-13 18:30:04 +01004114 hda_mark_cmd_cache_dirty(codec);
4115
Takashi Iwai7f308302012-05-08 16:52:23 +02004116 /* set as if powered on for avoiding re-entering the resume
4117 * in the resume / power-save sequence
4118 */
4119 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004120 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004121 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004122 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004123 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004124 if (codec->patch_ops.resume)
4125 codec->patch_ops.resume(codec);
4126 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004127 if (codec->patch_ops.init)
4128 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004129 snd_hda_codec_resume_amp(codec);
4130 snd_hda_codec_resume_cache(codec);
4131 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004132
4133 if (codec->jackpoll_interval)
4134 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004135 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004136 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004137
4138 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004139 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004140}
Takashi Iwai2a439522011-07-26 09:52:50 +02004141#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004142
Takashi Iwai54d17402005-11-21 16:33:22 +01004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144/**
4145 * snd_hda_build_controls - build mixer controls
4146 * @bus: the BUS
4147 *
4148 * Creates mixer controls for each codec included in the bus.
4149 *
4150 * Returns 0 if successful, otherwise a negative error code.
4151 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004152int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004154 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Takashi Iwai0ba21762007-04-16 11:29:14 +02004156 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004157 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004158 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004159 codec_err(codec,
4160 "cannot build controls for #%d (error %d)\n",
4161 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004162 err = snd_hda_codec_reset(codec);
4163 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004164 codec_err(codec,
4165 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004166 return err;
4167 }
4168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004170 return 0;
4171}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004172EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004174/*
4175 * add standard channel maps if not specified
4176 */
4177static int add_std_chmaps(struct hda_codec *codec)
4178{
4179 int i, str, err;
4180
4181 for (i = 0; i < codec->num_pcms; i++) {
4182 for (str = 0; str < 2; str++) {
4183 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4184 struct hda_pcm_stream *hinfo =
4185 &codec->pcm_info[i].stream[str];
4186 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004187 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004188
4189 if (codec->pcm_info[i].own_chmap)
4190 continue;
4191 if (!pcm || !hinfo->substreams)
4192 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004193 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4194 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004195 hinfo->channels_max,
4196 0, &chmap);
4197 if (err < 0)
4198 return err;
4199 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4200 }
4201 }
4202 return 0;
4203}
4204
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004205/* default channel maps for 2.1 speakers;
4206 * since HD-audio supports only stereo, odd number channels are omitted
4207 */
4208const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4209 { .channels = 2,
4210 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4211 { .channels = 4,
4212 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4213 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4214 { }
4215};
4216EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4217
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004218int snd_hda_codec_build_controls(struct hda_codec *codec)
4219{
4220 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004221 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004222 /* continue to initialize... */
4223 if (codec->patch_ops.init)
4224 err = codec->patch_ops.init(codec);
4225 if (!err && codec->patch_ops.build_controls)
4226 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004227 if (err < 0)
4228 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004229
4230 /* we create chmaps here instead of build_pcms */
4231 err = add_std_chmaps(codec);
4232 if (err < 0)
4233 return err;
4234
David Henningsson26a6cb62012-10-09 15:04:21 +02004235 if (codec->jackpoll_interval)
4236 hda_jackpoll_work(&codec->jackpoll_work.work);
4237 else
4238 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004239 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return 0;
4241}
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243/*
4244 * stream formats
4245 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004246struct hda_rate_tbl {
4247 unsigned int hz;
4248 unsigned int alsa_bits;
4249 unsigned int hda_fmt;
4250};
4251
Takashi Iwai92f10b32010-08-03 14:21:00 +02004252/* rate = base * mult / div */
4253#define HDA_RATE(base, mult, div) \
4254 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4255 (((div) - 1) << AC_FMT_DIV_SHIFT))
4256
Takashi Iwaibefdf312005-08-22 13:57:55 +02004257static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004259
4260 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004261 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4262 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4263 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4264 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4265 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4266 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4267 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4268 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4269 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4270 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4271 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004272#define AC_PAR_PCM_RATE_BITS 11
4273 /* up to bits 10, 384kHZ isn't supported properly */
4274
4275 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004276 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004277
Takashi Iwaibefdf312005-08-22 13:57:55 +02004278 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279};
4280
4281/**
4282 * snd_hda_calc_stream_format - calculate format bitset
4283 * @rate: the sample rate
4284 * @channels: the number of channels
4285 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4286 * @maxbps: the max. bps
4287 *
4288 * Calculate the format bitset from the given rate, channels and th PCM format.
4289 *
4290 * Return zero if invalid.
4291 */
4292unsigned int snd_hda_calc_stream_format(unsigned int rate,
4293 unsigned int channels,
4294 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004295 unsigned int maxbps,
4296 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 int i;
4299 unsigned int val = 0;
4300
Takashi Iwaibefdf312005-08-22 13:57:55 +02004301 for (i = 0; rate_bits[i].hz; i++)
4302 if (rate_bits[i].hz == rate) {
4303 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 break;
4305 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004306 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 snd_printdd("invalid rate %d\n", rate);
4308 return 0;
4309 }
4310
4311 if (channels == 0 || channels > 8) {
4312 snd_printdd("invalid channels %d\n", channels);
4313 return 0;
4314 }
4315 val |= channels - 1;
4316
4317 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004318 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004319 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004320 break;
4321 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004322 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 case 20:
4325 case 24:
4326 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004327 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004328 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004330 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004332 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 break;
4334 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004335 snd_printdd("invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004336 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return 0;
4338 }
4339
Anssi Hannula32c168c2010-08-03 13:28:57 +03004340 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004341 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return val;
4344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004345EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004347static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4348 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004349{
4350 unsigned int val = 0;
4351 if (nid != codec->afg &&
4352 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4353 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4354 if (!val || val == -1)
4355 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4356 if (!val || val == -1)
4357 return 0;
4358 return val;
4359}
4360
4361static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4362{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004363 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004364 get_pcm_param);
4365}
4366
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004367static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4368 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004369{
4370 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4371 if (!streams || streams == -1)
4372 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4373 if (!streams || streams == -1)
4374 return 0;
4375 return streams;
4376}
4377
4378static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4379{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004380 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004381 get_stream_param);
4382}
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384/**
4385 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4386 * @codec: the HDA codec
4387 * @nid: NID to query
4388 * @ratesp: the pointer to store the detected rate bitflags
4389 * @formatsp: the pointer to store the detected formats
4390 * @bpsp: the pointer to store the detected format widths
4391 *
4392 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4393 * or @bsps argument is ignored.
4394 *
4395 * Returns 0 if successful, otherwise a negative error code.
4396 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004397int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4399{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004400 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004402 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004403 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 if (ratesp) {
4406 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004407 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004409 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004411 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004412 codec_err(codec,
4413 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4414 nid, val,
4415 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004416 return -EIO;
4417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 *ratesp = rates;
4419 }
4420
4421 if (formatsp || bpsp) {
4422 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004423 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004425 streams = query_stream_param(codec, nid);
4426 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 bps = 0;
4430 if (streams & AC_SUPFMT_PCM) {
4431 if (val & AC_SUPPCM_BITS_8) {
4432 formats |= SNDRV_PCM_FMTBIT_U8;
4433 bps = 8;
4434 }
4435 if (val & AC_SUPPCM_BITS_16) {
4436 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4437 bps = 16;
4438 }
4439 if (wcaps & AC_WCAP_DIGITAL) {
4440 if (val & AC_SUPPCM_BITS_32)
4441 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4442 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4443 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4444 if (val & AC_SUPPCM_BITS_24)
4445 bps = 24;
4446 else if (val & AC_SUPPCM_BITS_20)
4447 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004448 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4449 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4451 if (val & AC_SUPPCM_BITS_32)
4452 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 else if (val & AC_SUPPCM_BITS_24)
4454 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004455 else if (val & AC_SUPPCM_BITS_20)
4456 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
4458 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004459#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004460 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004462 if (!bps)
4463 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004464 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004465#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004466 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004467 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 /* temporary hack: we have still no proper support
4469 * for the direct AC3 stream...
4470 */
4471 formats |= SNDRV_PCM_FMTBIT_U8;
4472 bps = 8;
4473 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004474 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004475 codec_err(codec,
4476 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4477 nid, val,
4478 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4479 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004480 return -EIO;
4481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (formatsp)
4483 *formatsp = formats;
4484 if (bpsp)
4485 *bpsp = bps;
4486 }
4487
4488 return 0;
4489}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004490EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004493 * snd_hda_is_supported_format - Check the validity of the format
4494 * @codec: HD-audio codec
4495 * @nid: NID to check
4496 * @format: the HD-audio format value to check
4497 *
4498 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 *
4500 * Returns 1 if supported, 0 if not.
4501 */
4502int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4503 unsigned int format)
4504{
4505 int i;
4506 unsigned int val = 0, rate, stream;
4507
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004508 val = query_pcm_param(codec, nid);
4509 if (!val)
4510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004513 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004514 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (val & (1 << i))
4516 break;
4517 return 0;
4518 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004519 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return 0;
4521
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004522 stream = query_stream_param(codec, nid);
4523 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return 0;
4525
4526 if (stream & AC_SUPFMT_PCM) {
4527 switch (format & 0xf0) {
4528 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004529 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return 0;
4531 break;
4532 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004533 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return 0;
4535 break;
4536 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004537 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return 0;
4539 break;
4540 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004541 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return 0;
4543 break;
4544 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004545 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 return 0;
4547 break;
4548 default:
4549 return 0;
4550 }
4551 } else {
4552 /* FIXME: check for float32 and AC3? */
4553 }
4554
4555 return 1;
4556}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004557EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559/*
4560 * PCM stuff
4561 */
4562static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4563 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004564 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
4566 return 0;
4567}
4568
4569static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4570 struct hda_codec *codec,
4571 unsigned int stream_tag,
4572 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004573 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4576 return 0;
4577}
4578
4579static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4580 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004581 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
Takashi Iwai888afa12008-03-18 09:57:50 +01004583 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 return 0;
4585}
4586
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004587static int set_pcm_default_values(struct hda_codec *codec,
4588 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004590 int err;
4591
Takashi Iwai0ba21762007-04-16 11:29:14 +02004592 /* query support PCM information from the given NID */
4593 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004594 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004595 info->rates ? NULL : &info->rates,
4596 info->formats ? NULL : &info->formats,
4597 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004598 if (err < 0)
4599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 }
4601 if (info->ops.open == NULL)
4602 info->ops.open = hda_pcm_default_open_close;
4603 if (info->ops.close == NULL)
4604 info->ops.close = hda_pcm_default_open_close;
4605 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004606 if (snd_BUG_ON(!info->nid))
4607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 info->ops.prepare = hda_pcm_default_prepare;
4609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004611 if (snd_BUG_ON(!info->nid))
4612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 info->ops.cleanup = hda_pcm_default_cleanup;
4614 }
4615 return 0;
4616}
4617
Takashi Iwaieb541332010-08-06 13:48:11 +02004618/*
4619 * codec prepare/cleanup entries
4620 */
4621int snd_hda_codec_prepare(struct hda_codec *codec,
4622 struct hda_pcm_stream *hinfo,
4623 unsigned int stream,
4624 unsigned int format,
4625 struct snd_pcm_substream *substream)
4626{
4627 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004628 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004629 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4630 if (ret >= 0)
4631 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004632 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004633 return ret;
4634}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004635EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004636
4637void snd_hda_codec_cleanup(struct hda_codec *codec,
4638 struct hda_pcm_stream *hinfo,
4639 struct snd_pcm_substream *substream)
4640{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004641 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004642 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004643 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004644}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004645EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004646
Takashi Iwaid5191e52009-11-16 14:58:17 +01004647/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004648const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4649 "Audio", "SPDIF", "HDMI", "Modem"
4650};
4651
Takashi Iwai176d5332008-07-30 15:01:44 +02004652/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004653 * get the empty PCM device number to assign
4654 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004655static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004656{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004657 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004658 /* assigned to static slots up to dev#10; if more needed, assign
4659 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4660 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004661 static int audio_idx[HDA_PCM_NTYPES][5] = {
4662 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4663 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004664 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004665 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004666 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004667 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004668
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004669 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004670 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004671 return -EINVAL;
4672 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004673
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004674 for (i = 0; audio_idx[type][i] >= 0; i++) {
4675#ifndef CONFIG_SND_DYNAMIC_MINORS
4676 if (audio_idx[type][i] >= 8)
4677 break;
4678#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004679 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4680 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004681 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004682
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004683#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004684 /* non-fixed slots starting from 10 */
4685 for (i = 10; i < 32; i++) {
4686 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4687 return i;
4688 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004689#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004690
Takashi Iwai4e76a882014-02-25 12:21:03 +01004691 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004692 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004693#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004694 dev_warn(bus->card->dev,
4695 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004696#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004697 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004698}
4699
4700/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004701 * attach a new PCM stream
4702 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004703static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004704{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004705 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004706 struct hda_pcm_stream *info;
4707 int stream, err;
4708
Takashi Iwaib91f0802008-11-04 08:43:08 +01004709 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004710 return -EINVAL;
4711 for (stream = 0; stream < 2; stream++) {
4712 info = &pcm->stream[stream];
4713 if (info->substreams) {
4714 err = set_pcm_default_values(codec, info);
4715 if (err < 0)
4716 return err;
4717 }
4718 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004719 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004720}
4721
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004722/* assign all PCMs of the given codec */
4723int snd_hda_codec_build_pcms(struct hda_codec *codec)
4724{
4725 unsigned int pcm;
4726 int err;
4727
4728 if (!codec->num_pcms) {
4729 if (!codec->patch_ops.build_pcms)
4730 return 0;
4731 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004732 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004733 codec_err(codec,
4734 "cannot build PCMs for #%d (error %d)\n",
4735 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004736 err = snd_hda_codec_reset(codec);
4737 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004738 codec_err(codec,
4739 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004740 return err;
4741 }
4742 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004743 }
4744 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4745 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4746 int dev;
4747
4748 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004749 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004750
4751 if (!cpcm->pcm) {
4752 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4753 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004754 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004755 cpcm->device = dev;
4756 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004757 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004758 codec_err(codec,
4759 "cannot attach PCM stream %d for codec #%d\n",
4760 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004761 continue; /* no fatal error */
4762 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004763 }
4764 }
4765 return 0;
4766}
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768/**
4769 * snd_hda_build_pcms - build PCM information
4770 * @bus: the BUS
4771 *
4772 * Create PCM information for each codec included in the bus.
4773 *
4774 * The build_pcms codec patch is requested to set up codec->num_pcms and
4775 * codec->pcm_info properly. The array is referred by the top-level driver
4776 * to create its PCM instances.
4777 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4778 * callback.
4779 *
4780 * At least, substreams, channels_min and channels_max must be filled for
4781 * each stream. substreams = 0 indicates that the stream doesn't exist.
4782 * When rates and/or formats are zero, the supported values are queried
4783 * from the given nid. The nid is used also by the default ops.prepare
4784 * and ops.cleanup callbacks.
4785 *
4786 * The driver needs to call ops.open in its open callback. Similarly,
4787 * ops.close is supposed to be called in the close callback.
4788 * ops.prepare should be called in the prepare or hw_params callback
4789 * with the proper parameters for set up.
4790 * ops.cleanup should be called in hw_free for clean up of streams.
4791 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004792 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004794int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004796 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Takashi Iwai0ba21762007-04-16 11:29:14 +02004798 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004799 int err = snd_hda_codec_build_pcms(codec);
4800 if (err < 0)
4801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 }
4803 return 0;
4804}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004805EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807/**
4808 * snd_hda_check_board_config - compare the current codec with the config table
4809 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004810 * @num_configs: number of config enums
4811 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 * @tbl: configuration table, terminated by null entries
4813 *
4814 * Compares the modelname or PCI subsystem id of the current codec with the
4815 * given configuration table. If a matching entry is found, returns its
4816 * config value (supposed to be 0 or positive).
4817 *
4818 * If no entries are matching, the function returns a negative value.
4819 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004820int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004821 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004822 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004824 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004825 int i;
4826 for (i = 0; i < num_configs; i++) {
4827 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004828 !strcmp(codec->modelname, models[i])) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004829 codec_info(codec, "model '%s' is selected\n",
4830 models[i]);
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004831 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
4833 }
4834 }
4835
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004836 if (!codec->bus->pci || !tbl)
4837 return -1;
4838
4839 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4840 if (!tbl)
4841 return -1;
4842 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004843#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004844 char tmp[10];
4845 const char *model = NULL;
4846 if (models)
4847 model = models[tbl->value];
4848 if (!model) {
4849 sprintf(tmp, "#%d", tbl->value);
4850 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01004852 codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
4853 model, tbl->subvendor, tbl->subdevice,
4854 (tbl->name ? tbl->name : "Unknown device"));
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004855#endif
4856 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
4858 return -1;
4859}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004860EXPORT_SYMBOL_GPL(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004863 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004864 subsystem ID with the
4865 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004866
4867 This is important for Gateway notebooks with SB450 HDA Audio
4868 where the vendor ID of the PCI device is:
4869 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4870 and the vendor/subvendor are found only at the codec.
4871
4872 * @codec: the HDA codec
4873 * @num_configs: number of config enums
4874 * @models: array of model name strings
4875 * @tbl: configuration table, terminated by null entries
4876 *
4877 * Compares the modelname or PCI subsystem id of the current codec with the
4878 * given configuration table. If a matching entry is found, returns its
4879 * config value (supposed to be 0 or positive).
4880 *
4881 * If no entries are matching, the function returns a negative value.
4882 */
4883int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004884 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004885 const struct snd_pci_quirk *tbl)
4886{
4887 const struct snd_pci_quirk *q;
4888
4889 /* Search for codec ID */
4890 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004891 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4892 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4893 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004894 break;
4895 }
4896
4897 if (!q->subvendor)
4898 return -1;
4899
4900 tbl = q;
4901
4902 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004903#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004904 char tmp[10];
4905 const char *model = NULL;
4906 if (models)
4907 model = models[tbl->value];
4908 if (!model) {
4909 sprintf(tmp, "#%d", tbl->value);
4910 model = tmp;
4911 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01004912 codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
4913 model, tbl->subvendor, tbl->subdevice,
4914 (tbl->name ? tbl->name : "Unknown device"));
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004915#endif
4916 return tbl->value;
4917 }
4918 return -1;
4919}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004920EXPORT_SYMBOL_GPL(snd_hda_check_board_codec_sid_config);
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004921
4922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 * snd_hda_add_new_ctls - create controls from the array
4924 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004925 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 *
4927 * This helper function creates and add new controls in the given array.
4928 * The array must be terminated with an empty entry as terminator.
4929 *
4930 * Returns 0 if successful, or a negative error code.
4931 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004932int snd_hda_add_new_ctls(struct hda_codec *codec,
4933 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004935 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004938 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004939 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004940 if (knew->iface == -1) /* skip this codec private value */
4941 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004942 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004943 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004944 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004945 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004946 if (addr > 0)
4947 kctl->id.device = addr;
4948 if (idx > 0)
4949 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004950 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004951 if (!err)
4952 break;
4953 /* try first with another device index corresponding to
4954 * the codec addr; if it still fails (or it's the
4955 * primary codec), then try another control index
4956 */
4957 if (!addr && codec->addr)
4958 addr = codec->addr;
4959 else if (!idx && !knew->index) {
4960 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004961 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004962 if (idx <= 0)
4963 return err;
4964 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004965 return err;
4966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
4968 return 0;
4969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004970EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Takashi Iwai83012a72012-08-24 18:38:08 +02004972#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004973static void hda_power_work(struct work_struct *work)
4974{
4975 struct hda_codec *codec =
4976 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004977 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004978 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004979
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004980 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004981 if (codec->power_transition > 0) { /* during power-up sequence? */
4982 spin_unlock(&codec->power_lock);
4983 return;
4984 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004985 if (!codec->power_on || codec->power_count) {
4986 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004987 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004988 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004989 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004990 spin_unlock(&codec->power_lock);
4991
Dylan Reidd17344b2012-09-28 15:57:01 -07004992 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004993 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4994 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004995}
4996
4997static void hda_keep_power_on(struct hda_codec *codec)
4998{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004999 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005000 codec->power_count++;
5001 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005002 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005003 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005004 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005005}
5006
Takashi Iwaid5191e52009-11-16 14:58:17 +01005007/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005008void snd_hda_update_power_acct(struct hda_codec *codec)
5009{
5010 unsigned long delta = jiffies - codec->power_jiffies;
5011 if (codec->power_on)
5012 codec->power_on_acct += delta;
5013 else
5014 codec->power_off_acct += delta;
5015 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005016}
5017
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005018/* Transition to powered up, if wait_power_down then wait for a pending
5019 * transition to D3 to complete. A pending D3 transition is indicated
5020 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005021/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005022static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005023{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005024 /* Return if power_on or transitioning to power_on, unless currently
5025 * powering down. */
5026 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005027 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005028 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005029 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005030
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005031 cancel_delayed_work_sync(&codec->power_work);
5032
5033 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005034 /* If the power down delayed work was cancelled above before starting,
5035 * then there is no need to go through power up here.
5036 */
5037 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005038 if (codec->power_transition < 0)
5039 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005040 return;
5041 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005042
Takashi Iwaid66fee52011-08-02 15:39:31 +02005043 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005044 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005045 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005046 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005047 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005048 spin_unlock(&codec->power_lock);
5049
Takashi Iwaicb53c622007-08-10 17:21:45 +02005050 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005051
5052 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005053 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005054}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005055
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005056#define power_save(codec) \
5057 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005058
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005059/* Transition to powered down */
5060static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005061{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005062 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005063 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005064
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005065 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005066 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005067 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005068 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005069 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005070}
5071
5072/**
5073 * snd_hda_power_save - Power-up/down/sync the codec
5074 * @codec: HD-audio codec
5075 * @delta: the counter delta to change
5076 *
5077 * Change the power-up counter via @delta, and power up or down the hardware
5078 * appropriately. For the power-down, queue to the delayed action.
5079 * Passing zero to @delta means to synchronize the power state.
5080 */
5081void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5082{
5083 spin_lock(&codec->power_lock);
5084 codec->power_count += delta;
5085 trace_hda_power_count(codec);
5086 if (delta > 0)
5087 __snd_hda_power_up(codec, d3wait);
5088 else
5089 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005090 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005091}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005092EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005093
Takashi Iwaid5191e52009-11-16 14:58:17 +01005094/**
5095 * snd_hda_check_amp_list_power - Check the amp list and update the power
5096 * @codec: HD-audio codec
5097 * @check: the object containing an AMP list and the status
5098 * @nid: NID to check / update
5099 *
5100 * Check whether the given NID is in the amp list. If it's in the list,
5101 * check the current AMP status, and update the the power-status according
5102 * to the mute status.
5103 *
5104 * This function is supposed to be set or called from the check_power_status
5105 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005106 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005107int snd_hda_check_amp_list_power(struct hda_codec *codec,
5108 struct hda_loopback_check *check,
5109 hda_nid_t nid)
5110{
Takashi Iwai031024e2011-05-02 11:29:30 +02005111 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005112 int ch, v;
5113
5114 if (!check->amplist)
5115 return 0;
5116 for (p = check->amplist; p->nid; p++) {
5117 if (p->nid == nid)
5118 break;
5119 }
5120 if (!p->nid)
5121 return 0; /* nothing changed */
5122
5123 for (p = check->amplist; p->nid; p++) {
5124 for (ch = 0; ch < 2; ch++) {
5125 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5126 p->idx);
5127 if (!(v & HDA_AMP_MUTE) && v > 0) {
5128 if (!check->power_on) {
5129 check->power_on = 1;
5130 snd_hda_power_up(codec);
5131 }
5132 return 1;
5133 }
5134 }
5135 }
5136 if (check->power_on) {
5137 check->power_on = 0;
5138 snd_hda_power_down(codec);
5139 }
5140 return 0;
5141}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005142EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005145/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005146 * Channel mode helper
5147 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005148
5149/**
5150 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5151 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005152int snd_hda_ch_mode_info(struct hda_codec *codec,
5153 struct snd_ctl_elem_info *uinfo,
5154 const struct hda_channel_mode *chmode,
5155 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005156{
5157 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5158 uinfo->count = 1;
5159 uinfo->value.enumerated.items = num_chmodes;
5160 if (uinfo->value.enumerated.item >= num_chmodes)
5161 uinfo->value.enumerated.item = num_chmodes - 1;
5162 sprintf(uinfo->value.enumerated.name, "%dch",
5163 chmode[uinfo->value.enumerated.item].channels);
5164 return 0;
5165}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005166EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005167
Takashi Iwaid5191e52009-11-16 14:58:17 +01005168/**
5169 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005171int snd_hda_ch_mode_get(struct hda_codec *codec,
5172 struct snd_ctl_elem_value *ucontrol,
5173 const struct hda_channel_mode *chmode,
5174 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005175 int max_channels)
5176{
5177 int i;
5178
5179 for (i = 0; i < num_chmodes; i++) {
5180 if (max_channels == chmode[i].channels) {
5181 ucontrol->value.enumerated.item[0] = i;
5182 break;
5183 }
5184 }
5185 return 0;
5186}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005187EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005188
Takashi Iwaid5191e52009-11-16 14:58:17 +01005189/**
5190 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5191 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005192int snd_hda_ch_mode_put(struct hda_codec *codec,
5193 struct snd_ctl_elem_value *ucontrol,
5194 const struct hda_channel_mode *chmode,
5195 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005196 int *max_channelsp)
5197{
5198 unsigned int mode;
5199
5200 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005201 if (mode >= num_chmodes)
5202 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005203 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005204 return 0;
5205 /* change the current channel setting */
5206 *max_channelsp = chmode[mode].channels;
5207 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005208 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005209 return 1;
5210}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005211EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213/*
5214 * input MUX helper
5215 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005216
5217/**
5218 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5219 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005220int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5221 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
5223 unsigned int index;
5224
5225 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5226 uinfo->count = 1;
5227 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005228 if (!imux->num_items)
5229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 index = uinfo->value.enumerated.item;
5231 if (index >= imux->num_items)
5232 index = imux->num_items - 1;
5233 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5234 return 0;
5235}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005236EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
Takashi Iwaid5191e52009-11-16 14:58:17 +01005238/**
5239 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5240 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005241int snd_hda_input_mux_put(struct hda_codec *codec,
5242 const struct hda_input_mux *imux,
5243 struct snd_ctl_elem_value *ucontrol,
5244 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 unsigned int *cur_val)
5246{
5247 unsigned int idx;
5248
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005249 if (!imux->num_items)
5250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 idx = ucontrol->value.enumerated.item[0];
5252 if (idx >= imux->num_items)
5253 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005254 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005256 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5257 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 *cur_val = idx;
5259 return 1;
5260}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005261EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
5263
5264/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005265 * process kcontrol info callback of a simple string enum array
5266 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5267 */
5268int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5269 struct snd_ctl_elem_info *uinfo,
5270 int num_items, const char * const *texts)
5271{
5272 static const char * const texts_default[] = {
5273 "Disabled", "Enabled"
5274 };
5275
5276 if (!texts || !num_items) {
5277 num_items = 2;
5278 texts = texts_default;
5279 }
5280
5281 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5282 uinfo->count = 1;
5283 uinfo->value.enumerated.items = num_items;
5284 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5285 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5286 strcpy(uinfo->value.enumerated.name,
5287 texts[uinfo->value.enumerated.item]);
5288 return 0;
5289}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005290EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005291
5292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 * Multi-channel / digital-out PCM helper functions
5294 */
5295
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005296/* setup SPDIF output stream */
5297static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5298 unsigned int stream_tag, unsigned int format)
5299{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005300 struct hda_spdif_out *spdif;
5301 unsigned int curr_fmt;
5302 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005303
Laurence Darby3bef1c32012-11-03 17:00:06 +00005304 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5305 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5306 AC_VERB_GET_STREAM_FORMAT, 0);
5307 reset = codec->spdif_status_reset &&
5308 (spdif->ctls & AC_DIG1_ENABLE) &&
5309 curr_fmt != format;
5310
5311 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5312 updated */
5313 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005314 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005315 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005316 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005317 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005318 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005319 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005320 for (d = codec->slave_dig_outs; *d; d++)
5321 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5322 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005323 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005324 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005325 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005326 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005327 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005328}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005329
Takashi Iwai2f728532008-09-25 16:32:41 +02005330static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5331{
5332 snd_hda_codec_cleanup_stream(codec, nid);
5333 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005334 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005335 for (d = codec->slave_dig_outs; *d; d++)
5336 snd_hda_codec_cleanup_stream(codec, *d);
5337 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005338}
5339
Takashi Iwaid5191e52009-11-16 14:58:17 +01005340/**
5341 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5342 * @bus: HD-audio bus
5343 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005344void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5345{
5346 struct hda_codec *codec;
5347
5348 if (!bus)
5349 return;
5350 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005351 if (hda_codec_is_power_on(codec) &&
5352 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005353 codec->patch_ops.reboot_notify(codec);
5354 }
5355}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005356EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005357
Takashi Iwaid5191e52009-11-16 14:58:17 +01005358/**
5359 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005361int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5362 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
Ingo Molnar62932df2006-01-16 16:34:20 +01005364 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005365 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5366 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005367 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005369 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return 0;
5371}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005372EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Takashi Iwaid5191e52009-11-16 14:58:17 +01005374/**
5375 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5376 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005377int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5378 struct hda_multi_out *mout,
5379 unsigned int stream_tag,
5380 unsigned int format,
5381 struct snd_pcm_substream *substream)
5382{
5383 mutex_lock(&codec->spdif_mutex);
5384 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5385 mutex_unlock(&codec->spdif_mutex);
5386 return 0;
5387}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005388EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005389
Takashi Iwaid5191e52009-11-16 14:58:17 +01005390/**
5391 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5392 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005393int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5394 struct hda_multi_out *mout)
5395{
5396 mutex_lock(&codec->spdif_mutex);
5397 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5398 mutex_unlock(&codec->spdif_mutex);
5399 return 0;
5400}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005401EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005402
Takashi Iwaid5191e52009-11-16 14:58:17 +01005403/**
5404 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005406int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5407 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Ingo Molnar62932df2006-01-16 16:34:20 +01005409 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005411 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return 0;
5413}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005414EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
Takashi Iwaid5191e52009-11-16 14:58:17 +01005416/**
5417 * snd_hda_multi_out_analog_open - open analog outputs
5418 *
5419 * Open analog outputs and set up the hw-constraints.
5420 * If the digital outputs can be opened as slave, open the digital
5421 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005423int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5424 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005425 struct snd_pcm_substream *substream,
5426 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
Takashi Iwai9a081602008-02-12 18:37:26 +01005428 struct snd_pcm_runtime *runtime = substream->runtime;
5429 runtime->hw.channels_max = mout->max_channels;
5430 if (mout->dig_out_nid) {
5431 if (!mout->analog_rates) {
5432 mout->analog_rates = hinfo->rates;
5433 mout->analog_formats = hinfo->formats;
5434 mout->analog_maxbps = hinfo->maxbps;
5435 } else {
5436 runtime->hw.rates = mout->analog_rates;
5437 runtime->hw.formats = mout->analog_formats;
5438 hinfo->maxbps = mout->analog_maxbps;
5439 }
5440 if (!mout->spdif_rates) {
5441 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5442 &mout->spdif_rates,
5443 &mout->spdif_formats,
5444 &mout->spdif_maxbps);
5445 }
5446 mutex_lock(&codec->spdif_mutex);
5447 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005448 if ((runtime->hw.rates & mout->spdif_rates) &&
5449 (runtime->hw.formats & mout->spdif_formats)) {
5450 runtime->hw.rates &= mout->spdif_rates;
5451 runtime->hw.formats &= mout->spdif_formats;
5452 if (mout->spdif_maxbps < hinfo->maxbps)
5453 hinfo->maxbps = mout->spdif_maxbps;
5454 } else {
5455 mout->share_spdif = 0;
5456 /* FIXME: need notify? */
5457 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005458 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005459 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5462 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5463}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005464EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Takashi Iwaid5191e52009-11-16 14:58:17 +01005466/**
5467 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5468 *
5469 * Set up the i/o for analog out.
5470 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005472int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5473 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 unsigned int stream_tag,
5475 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005476 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Takashi Iwaidda14412011-05-02 11:29:30 +02005478 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005480 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 int i;
5482
Ingo Molnar62932df2006-01-16 16:34:20 +01005483 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005484 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005485 if (mout->dig_out_nid && mout->share_spdif &&
5486 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005488 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5489 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005490 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005492 setup_dig_out_stream(codec, mout->dig_out_nid,
5493 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 } else {
5495 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005496 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
5498 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005499 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
5501 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005502 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5503 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005504 if (!mout->no_share_stream &&
5505 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005507 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5508 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005509 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005510 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5511 if (!mout->no_share_stream && mout->hp_out_nid[i])
5512 snd_hda_codec_setup_stream(codec,
5513 mout->hp_out_nid[i],
5514 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /* surrounds */
5517 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005518 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005519 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5520 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005521 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005522 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5523 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005525
5526 /* extra surrounds */
5527 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5528 int ch = 0;
5529 if (!mout->extra_out_nid[i])
5530 break;
5531 if (chs >= (i + 1) * 2)
5532 ch = i * 2;
5533 else if (!mout->no_share_stream)
5534 break;
5535 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5536 stream_tag, ch, format);
5537 }
5538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 return 0;
5540}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005541EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Takashi Iwaid5191e52009-11-16 14:58:17 +01005543/**
5544 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005546int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5547 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548{
Takashi Iwaidda14412011-05-02 11:29:30 +02005549 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 int i;
5551
5552 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005553 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005555 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005556 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5557 if (mout->hp_out_nid[i])
5558 snd_hda_codec_cleanup_stream(codec,
5559 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005560 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5561 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005562 snd_hda_codec_cleanup_stream(codec,
5563 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005564 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005566 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 mout->dig_out_used = 0;
5568 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005569 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return 0;
5571}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005572EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Takashi Iwai47408602012-04-20 13:06:53 +02005574/**
5575 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5576 *
5577 * Guess the suitable VREF pin bits to be set as the pin-control value.
5578 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5579 */
5580unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5581{
5582 unsigned int pincap;
5583 unsigned int oldval;
5584 oldval = snd_hda_codec_read(codec, pin, 0,
5585 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5586 pincap = snd_hda_query_pin_caps(codec, pin);
5587 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5588 /* Exception: if the default pin setup is vref50, we give it priority */
5589 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5590 return AC_PINCTL_VREF_80;
5591 else if (pincap & AC_PINCAP_VREF_50)
5592 return AC_PINCTL_VREF_50;
5593 else if (pincap & AC_PINCAP_VREF_100)
5594 return AC_PINCTL_VREF_100;
5595 else if (pincap & AC_PINCAP_VREF_GRD)
5596 return AC_PINCTL_VREF_GRD;
5597 return AC_PINCTL_VREF_HIZ;
5598}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005599EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005600
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005601/* correct the pin ctl value for matching with the pin cap */
5602unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5603 hda_nid_t pin, unsigned int val)
5604{
5605 static unsigned int cap_lists[][2] = {
5606 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5607 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5608 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5609 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5610 };
5611 unsigned int cap;
5612
5613 if (!val)
5614 return 0;
5615 cap = snd_hda_query_pin_caps(codec, pin);
5616 if (!cap)
5617 return val; /* don't know what to do... */
5618
5619 if (val & AC_PINCTL_OUT_EN) {
5620 if (!(cap & AC_PINCAP_OUT))
5621 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5622 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5623 val &= ~AC_PINCTL_HP_EN;
5624 }
5625
5626 if (val & AC_PINCTL_IN_EN) {
5627 if (!(cap & AC_PINCAP_IN))
5628 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5629 else {
5630 unsigned int vcap, vref;
5631 int i;
5632 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5633 vref = val & AC_PINCTL_VREFEN;
5634 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5635 if (vref == cap_lists[i][0] &&
5636 !(vcap & cap_lists[i][1])) {
5637 if (i == ARRAY_SIZE(cap_lists) - 1)
5638 vref = AC_PINCTL_VREF_HIZ;
5639 else
5640 vref = cap_lists[i + 1][0];
5641 }
5642 }
5643 val &= ~AC_PINCTL_VREFEN;
5644 val |= vref;
5645 }
5646 }
5647
5648 return val;
5649}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005650EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005651
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005652int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5653 unsigned int val, bool cached)
5654{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005655 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005656 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005657 if (cached)
5658 return snd_hda_codec_update_cache(codec, pin, 0,
5659 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5660 else
5661 return snd_hda_codec_write(codec, pin, 0,
5662 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005664EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005665
Takashi Iwai990061c2010-09-09 22:08:44 +02005666/**
5667 * snd_hda_add_imux_item - Add an item to input_mux
5668 *
5669 * When the same label is used already in the existing items, the number
5670 * suffix is appended to the label. This label index number is stored
5671 * to type_idx when non-NULL pointer is given.
5672 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005673int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5674 int index, int *type_idx)
5675{
5676 int i, label_idx = 0;
5677 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5678 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5679 return -EINVAL;
5680 }
5681 for (i = 0; i < imux->num_items; i++) {
5682 if (!strncmp(label, imux->items[i].label, strlen(label)))
5683 label_idx++;
5684 }
5685 if (type_idx)
5686 *type_idx = label_idx;
5687 if (label_idx > 0)
5688 snprintf(imux->items[imux->num_items].label,
5689 sizeof(imux->items[imux->num_items].label),
5690 "%s %d", label, label_idx);
5691 else
5692 strlcpy(imux->items[imux->num_items].label, label,
5693 sizeof(imux->items[imux->num_items].label));
5694 imux->items[imux->num_items].index = index;
5695 imux->num_items++;
5696 return 0;
5697}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005698EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005699
Takashi Iwai4a471b72005-12-07 13:56:29 +01005700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701#ifdef CONFIG_PM
5702/*
5703 * power management
5704 */
5705
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005706
5707static void hda_async_suspend(void *data, async_cookie_t cookie)
5708{
5709 hda_call_codec_suspend(data, false);
5710}
5711
5712static void hda_async_resume(void *data, async_cookie_t cookie)
5713{
5714 hda_call_codec_resume(data);
5715}
5716
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717/**
5718 * snd_hda_suspend - suspend the codecs
5719 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 *
5721 * Returns 0 if successful.
5722 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005723int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005725 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005726 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
Takashi Iwai0ba21762007-04-16 11:29:14 +02005728 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005729 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005730 if (hda_codec_is_power_on(codec)) {
5731 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005732 async_schedule_domain(hda_async_suspend, codec,
5733 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005734 else
5735 hda_call_codec_suspend(codec, false);
5736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005738
5739 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005740 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 return 0;
5743}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005744EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746/**
5747 * snd_hda_resume - resume the codecs
5748 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 *
5750 * Returns 0 if successful.
5751 */
5752int snd_hda_resume(struct hda_bus *bus)
5753{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005754 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005755 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Takashi Iwai0ba21762007-04-16 11:29:14 +02005757 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005758 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005759 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005760 else
5761 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005763
5764 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005765 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 return 0;
5768}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005769EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005770#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005771
5772/*
5773 * generic arrays
5774 */
5775
Takashi Iwaid5191e52009-11-16 14:58:17 +01005776/**
5777 * snd_array_new - get a new element from the given array
5778 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005779 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005780 * Get a new element from the given array. If it exceeds the
5781 * pre-allocated array size, re-allocate the array.
5782 *
5783 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005784 */
5785void *snd_array_new(struct snd_array *array)
5786{
Takashi Iwai12f17712012-10-10 08:59:14 +02005787 if (snd_BUG_ON(!array->elem_size))
5788 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005789 if (array->used >= array->alloced) {
5790 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005791 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005792 void *nlist;
5793 if (snd_BUG_ON(num >= 4096))
5794 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005795 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005796 if (!nlist)
5797 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005798 array->list = nlist;
5799 array->alloced = num;
5800 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005801 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005802}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005803EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005804
Takashi Iwaid5191e52009-11-16 14:58:17 +01005805/**
5806 * snd_array_free - free the given array elements
5807 * @array: the array object
5808 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005809void snd_array_free(struct snd_array *array)
5810{
5811 kfree(array->list);
5812 array->used = 0;
5813 array->alloced = 0;
5814 array->list = NULL;
5815}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005816EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005817
Takashi Iwaid5191e52009-11-16 14:58:17 +01005818/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005819 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5820 * @pcm: PCM caps bits
5821 * @buf: the string buffer to write
5822 * @buflen: the max buffer length
5823 *
5824 * used by hda_proc.c and hda_eld.c
5825 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005826void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5827{
5828 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5829 int i, j;
5830
5831 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5832 if (pcm & (AC_SUPPCM_BITS_8 << i))
5833 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5834
5835 buf[j] = '\0'; /* necessary when j == 0 */
5836}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005837EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005838
5839MODULE_DESCRIPTION("HDA codec core");
5840MODULE_LICENSE("GPL");