blob: 259fbeaa37bd35b2b887dbe6c08fc0ce540bdf24 [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 Iwai63e51fd2013-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 Iwai63e51fd2013-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;
David Henningssonf5662e12014-07-22 14:09:34 +02001479 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwai83012a72012-08-24 18:38:08 +02001481#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001482 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001483 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1484 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1485 * the caller has to power down appropriatley after initialization
1486 * phase.
1487 */
1488 hda_keep_power_on(codec);
1489#endif
1490
Takashi Iwai648a8d22014-02-25 10:38:13 +01001491 snd_hda_sysfs_init(codec);
1492
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001493 if (codec->bus->modelname) {
1494 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1495 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001496 err = -ENODEV;
1497 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001498 }
1499 }
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001502 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 bus->caddr_tbl[codec_addr] = codec;
1505
Takashi Iwai0ba21762007-04-16 11:29:14 +02001506 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1507 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001508 if (codec->vendor_id == -1)
1509 /* read again, hopefully the access method was corrected
1510 * in the last read...
1511 */
1512 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1513 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001514 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1515 AC_PAR_SUBSYSTEM_ID);
1516 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1517 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001519 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001520 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001521 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001522 err = -ENODEV;
1523 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Takashi Iwaid8193872012-08-31 07:54:38 -07001526 fg = codec->afg ? codec->afg : codec->mfg;
1527 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001528 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001529 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001530 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001531 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001532 err = read_pin_defaults(codec);
1533 if (err < 0)
1534 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001535
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001538 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001539 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001540 }
1541
Takashi Iwai83012a72012-08-24 18:38:08 +02001542#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001543 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001544 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001545#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001546 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001547 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001548#ifdef CONFIG_PM
1549 if (!codec->d3_stop_clk || !codec->epss)
1550 bus->power_keep_link_on = 1;
1551#endif
1552
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001553
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001554 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001555 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001556
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001557 snd_hda_codec_proc_new(codec);
1558
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001559 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001560
1561 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1562 codec->subsystem_id, codec->revision_id);
1563 snd_component_add(codec->bus->card, component);
1564
Takashi Iwai2565c892014-02-19 11:41:09 +01001565 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1566 if (err < 0)
1567 goto error;
1568
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001569 if (codecp)
1570 *codecp = codec;
1571 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001572
1573 error:
1574 snd_hda_codec_free(codec);
1575 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001576}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001577EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001578
Mengdong Lina15d05d2013-02-08 17:09:31 -05001579int snd_hda_codec_update_widgets(struct hda_codec *codec)
1580{
1581 hda_nid_t fg;
1582 int err;
1583
1584 /* Assume the function group node does not change,
1585 * only the widget nodes may change.
1586 */
1587 kfree(codec->wcaps);
1588 fg = codec->afg ? codec->afg : codec->mfg;
1589 err = read_widget_caps(codec, fg);
1590 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001591 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001592 return err;
1593 }
1594
1595 snd_array_free(&codec->init_pins);
1596 err = read_pin_defaults(codec);
1597
1598 return err;
1599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001600EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001601
1602
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001603#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001604/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1605static bool is_likely_hdmi_codec(struct hda_codec *codec)
1606{
1607 hda_nid_t nid = codec->start_nid;
1608 int i;
1609
1610 for (i = 0; i < codec->num_nodes; i++, nid++) {
1611 unsigned int wcaps = get_wcaps(codec, nid);
1612 switch (get_wcaps_type(wcaps)) {
1613 case AC_WID_AUD_IN:
1614 return false; /* HDMI parser supports only HDMI out */
1615 case AC_WID_AUD_OUT:
1616 if (!(wcaps & AC_WCAP_DIGITAL))
1617 return false;
1618 break;
1619 }
1620 }
1621 return true;
1622}
1623#else
1624/* no HDMI codec parser support */
1625#define is_likely_hdmi_codec(codec) false
1626#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1627
Takashi Iwaid5191e52009-11-16 14:58:17 +01001628/**
1629 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1630 * @codec: the HDA codec
1631 *
1632 * Start parsing of the given codec tree and (re-)initialize the whole
1633 * patch instance.
1634 *
1635 * Returns 0 if successful or a negative error code.
1636 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001637int snd_hda_codec_configure(struct hda_codec *codec)
1638{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001639 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001640 int err;
1641
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001642 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001643 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001644 err = get_codec_name(codec);
1645 if (err < 0)
1646 return err;
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001649 if (!is_generic_config(codec) && codec->preset)
1650 patch = codec->preset->patch;
1651 if (!patch) {
1652 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001653 if (is_likely_hdmi_codec(codec)) {
1654#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001655 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001656#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1657 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001658#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001659 }
1660 if (!patch) {
1661#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1662 patch = load_parser(codec, snd_hda_parse_generic_codec);
1663#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1664 patch = snd_hda_parse_generic_codec;
1665#endif
1666 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001667 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001668 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001669 return -ENODEV;
1670 }
Takashi Iwai82467612007-07-27 19:15:54 +02001671 }
1672
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001673 err = patch(codec);
1674 if (err < 0) {
1675 unload_parser(codec);
1676 return err;
1677 }
Takashi Iwai82467612007-07-27 19:15:54 +02001678
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001679 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001680 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001681 if (err < 0)
1682 return err;
1683 }
1684
Takashi Iwaif62faed2009-12-23 09:27:51 +01001685 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001686 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001687 snprintf(codec->bus->card->mixername,
1688 sizeof(codec->bus->card->mixername),
1689 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001692EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Takashi Iwaied360812012-08-08 17:12:52 +02001694/* update the stream-id if changed */
1695static void update_pcm_stream_id(struct hda_codec *codec,
1696 struct hda_cvt_setup *p, hda_nid_t nid,
1697 u32 stream_tag, int channel_id)
1698{
1699 unsigned int oldval, newval;
1700
1701 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1702 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1703 newval = (stream_tag << 4) | channel_id;
1704 if (oldval != newval)
1705 snd_hda_codec_write(codec, nid, 0,
1706 AC_VERB_SET_CHANNEL_STREAMID,
1707 newval);
1708 p->stream_tag = stream_tag;
1709 p->channel_id = channel_id;
1710 }
1711}
1712
1713/* update the format-id if changed */
1714static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1715 hda_nid_t nid, int format)
1716{
1717 unsigned int oldval;
1718
1719 if (p->format_id != format) {
1720 oldval = snd_hda_codec_read(codec, nid, 0,
1721 AC_VERB_GET_STREAM_FORMAT, 0);
1722 if (oldval != format) {
1723 msleep(1);
1724 snd_hda_codec_write(codec, nid, 0,
1725 AC_VERB_SET_STREAM_FORMAT,
1726 format);
1727 }
1728 p->format_id = format;
1729 }
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/**
1733 * snd_hda_codec_setup_stream - set up the codec for streaming
1734 * @codec: the CODEC to set up
1735 * @nid: the NID to set up
1736 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1737 * @channel_id: channel id to pass, zero based.
1738 * @format: stream format.
1739 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001740void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1741 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int channel_id, int format)
1743{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001744 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001745 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001746 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001747 int i;
1748
Takashi Iwai0ba21762007-04-16 11:29:14 +02001749 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001750 return;
1751
Takashi Iwai4e76a882014-02-25 12:21:03 +01001752 codec_dbg(codec,
1753 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1754 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001755 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001756 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001757 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001758
1759 if (codec->pcm_format_first)
1760 update_pcm_format(codec, p, nid, format);
1761 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1762 if (!codec->pcm_format_first)
1763 update_pcm_format(codec, p, nid, format);
1764
Takashi Iwaieb541332010-08-06 13:48:11 +02001765 p->active = 1;
1766 p->dirty = 0;
1767
1768 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001769 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001770 list_for_each_entry(c, &codec->bus->codec_list, list) {
1771 for (i = 0; i < c->cvt_setups.used; i++) {
1772 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001773 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001774 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001775 p->dirty = 1;
1776 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001779EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Takashi Iwaif0cea792010-08-13 11:56:53 +02001781static void really_cleanup_stream(struct hda_codec *codec,
1782 struct hda_cvt_setup *q);
1783
Takashi Iwaid5191e52009-11-16 14:58:17 +01001784/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001785 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001786 * @codec: the CODEC to clean up
1787 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001788 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001789 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001790void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1791 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001792{
Takashi Iwaieb541332010-08-06 13:48:11 +02001793 struct hda_cvt_setup *p;
1794
Takashi Iwai888afa12008-03-18 09:57:50 +01001795 if (!nid)
1796 return;
1797
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001798 if (codec->no_sticky_stream)
1799 do_now = 1;
1800
Takashi Iwai4e76a882014-02-25 12:21:03 +01001801 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001802 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001803 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001804 /* here we just clear the active flag when do_now isn't set;
1805 * actual clean-ups will be done later in
1806 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1807 */
1808 if (do_now)
1809 really_cleanup_stream(codec, p);
1810 else
1811 p->active = 0;
1812 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001813}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001814EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001815
Takashi Iwaieb541332010-08-06 13:48:11 +02001816static void really_cleanup_stream(struct hda_codec *codec,
1817 struct hda_cvt_setup *q)
1818{
1819 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001820 if (q->stream_tag || q->channel_id)
1821 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1822 if (q->format_id)
1823 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1824);
Takashi Iwaieb541332010-08-06 13:48:11 +02001825 memset(q, 0, sizeof(*q));
1826 q->nid = nid;
1827}
1828
1829/* clean up the all conflicting obsolete streams */
1830static void purify_inactive_streams(struct hda_codec *codec)
1831{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001832 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001833 int i;
1834
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001835 list_for_each_entry(c, &codec->bus->codec_list, list) {
1836 for (i = 0; i < c->cvt_setups.used; i++) {
1837 struct hda_cvt_setup *p;
1838 p = snd_array_elem(&c->cvt_setups, i);
1839 if (p->dirty)
1840 really_cleanup_stream(c, p);
1841 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001842 }
1843}
1844
Takashi Iwai2a439522011-07-26 09:52:50 +02001845#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001846/* clean up all streams; called from suspend */
1847static void hda_cleanup_all_streams(struct hda_codec *codec)
1848{
1849 int i;
1850
1851 for (i = 0; i < codec->cvt_setups.used; i++) {
1852 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1853 if (p->stream_tag)
1854 really_cleanup_stream(codec, p);
1855 }
1856}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001857#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859/*
1860 * amp access functions
1861 */
1862
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001863/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001864#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001865#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001866#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1867#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001869#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001872static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001873 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Takashi Iwai01751f52007-08-10 16:59:39 +02001875 memset(cache, 0, sizeof(*cache));
1876 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001877 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001878}
1879
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001880static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001881{
Takashi Iwai603c4012008-07-30 15:01:44 +02001882 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001886static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Takashi Iwai01751f52007-08-10 16:59:39 +02001888 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1889 u16 cur = cache->hash[idx];
1890 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001893 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (info->key == key)
1895 return info;
1896 cur = info->next;
1897 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001898 return NULL;
1899}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001901/* query the hash. allocate an entry if not found. */
1902static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1903 u32 key)
1904{
1905 struct hda_cache_head *info = get_hash(cache, key);
1906 if (!info) {
1907 u16 idx, cur;
1908 /* add a new hash entry */
1909 info = snd_array_new(&cache->buf);
1910 if (!info)
1911 return NULL;
1912 cur = snd_array_index(&cache->buf, info);
1913 info->key = key;
1914 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001915 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001916 idx = key % (u16)ARRAY_SIZE(cache->hash);
1917 info->next = cache->hash[idx];
1918 cache->hash[idx] = cur;
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return info;
1921}
1922
Takashi Iwai01751f52007-08-10 16:59:39 +02001923/* query and allocate an amp hash entry */
1924static inline struct hda_amp_info *
1925get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1926{
1927 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1928}
1929
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001930/* overwrite the value with the key in the caps hash */
1931static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1932{
1933 struct hda_amp_info *info;
1934
1935 mutex_lock(&codec->hash_mutex);
1936 info = get_alloc_amp_hash(codec, key);
1937 if (!info) {
1938 mutex_unlock(&codec->hash_mutex);
1939 return -EINVAL;
1940 }
1941 info->amp_caps = val;
1942 info->head.val |= INFO_AMP_CAPS;
1943 mutex_unlock(&codec->hash_mutex);
1944 return 0;
1945}
1946
1947/* query the value from the caps hash; if not found, fetch the current
1948 * value from the given function and store in the hash
1949 */
1950static unsigned int
1951query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1952 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1953{
1954 struct hda_amp_info *info;
1955 unsigned int val;
1956
1957 mutex_lock(&codec->hash_mutex);
1958 info = get_alloc_amp_hash(codec, key);
1959 if (!info) {
1960 mutex_unlock(&codec->hash_mutex);
1961 return 0;
1962 }
1963 if (!(info->head.val & INFO_AMP_CAPS)) {
1964 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1965 val = func(codec, nid, dir);
1966 write_caps_hash(codec, key, val);
1967 } else {
1968 val = info->amp_caps;
1969 mutex_unlock(&codec->hash_mutex);
1970 }
1971 return val;
1972}
1973
1974static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1975 int direction)
1976{
1977 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1978 nid = codec->afg;
1979 return snd_hda_param_read(codec, nid,
1980 direction == HDA_OUTPUT ?
1981 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1982}
1983
Takashi Iwaid5191e52009-11-16 14:58:17 +01001984/**
1985 * query_amp_caps - query AMP capabilities
1986 * @codec: the HD-auio codec
1987 * @nid: the NID to query
1988 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1989 *
1990 * Query AMP capabilities for the given widget and direction.
1991 * Returns the obtained capability bits.
1992 *
1993 * When cap bits have been already read, this doesn't read again but
1994 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001996u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001998 return query_caps_hash(codec, nid, direction,
1999 HDA_HASH_KEY(nid, direction, 0),
2000 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002002EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Takashi Iwaid5191e52009-11-16 14:58:17 +01002004/**
David Henningsson861a04e2014-09-23 10:38:17 +02002005 * snd_hda_check_amp_caps - query AMP capabilities
2006 * @codec: the HD-audio codec
2007 * @nid: the NID to query
2008 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2009 *
2010 * Check whether the widget has the given amp capability for the direction.
2011 */
2012bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2013 int dir, unsigned int bits)
2014{
2015 if (!nid)
2016 return false;
2017 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2018 if (query_amp_caps(codec, nid, dir) & bits)
2019 return true;
2020 return false;
2021}
2022EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2023
2024/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002025 * snd_hda_override_amp_caps - Override the AMP capabilities
2026 * @codec: the CODEC to clean up
2027 * @nid: the NID to clean up
2028 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2029 * @caps: the capability bits to set
2030 *
2031 * Override the cached AMP caps bits value by the given one.
2032 * This function is useful if the driver needs to adjust the AMP ranges,
2033 * e.g. limit to 0dB, etc.
2034 *
2035 * Returns zero if successful or a negative error code.
2036 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002037int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2038 unsigned int caps)
2039{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002040 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002041}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002042EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002043
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002044static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2045 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002046{
2047 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2048}
2049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
2051 * snd_hda_query_pin_caps - Query PIN capabilities
2052 * @codec: the HD-auio codec
2053 * @nid: the NID to query
2054 *
2055 * Query PIN capabilities for the given widget.
2056 * Returns the obtained capability bits.
2057 *
2058 * When cap bits have been already read, this doesn't read again but
2059 * returns the cached value.
2060 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002061u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2062{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002063 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002064 read_pin_cap);
2065}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002066EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002067
Wu Fengguang864f92b2009-11-18 12:38:02 +08002068/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002069 * snd_hda_override_pin_caps - Override the pin capabilities
2070 * @codec: the CODEC
2071 * @nid: the NID to override
2072 * @caps: the capability bits to set
2073 *
2074 * Override the cached PIN capabilitiy bits value by the given one.
2075 *
2076 * Returns zero if successful or a negative error code.
2077 */
2078int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2079 unsigned int caps)
2080{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002081 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002083EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002084
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002085/* read or sync the hash value with the current value;
2086 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088static struct hda_amp_info *
2089update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002090 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002092 struct hda_amp_info *info;
2093 unsigned int parm, val = 0;
2094 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002096 retry:
2097 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2098 if (!info)
2099 return NULL;
2100 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2101 if (!val_read) {
2102 mutex_unlock(&codec->hash_mutex);
2103 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2104 parm |= direction == HDA_OUTPUT ?
2105 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2106 parm |= index;
2107 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002109 val &= 0xff;
2110 val_read = true;
2111 mutex_lock(&codec->hash_mutex);
2112 goto retry;
2113 }
2114 info->vol[ch] = val;
2115 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002116 } else if (init_only)
2117 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002118 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002122 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002124static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002125 hda_nid_t nid, int ch, int direction, int index,
2126 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 u32 parm;
2129
2130 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2131 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2132 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002133 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2134 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002135 ; /* set the zero value as a fake mute */
2136 else
2137 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2139}
2140
Takashi Iwaid5191e52009-11-16 14:58:17 +01002141/**
2142 * snd_hda_codec_amp_read - Read AMP value
2143 * @codec: HD-audio codec
2144 * @nid: NID to read the AMP value
2145 * @ch: channel (left=0 or right=1)
2146 * @direction: #HDA_INPUT or #HDA_OUTPUT
2147 * @index: the index value (only for input direction)
2148 *
2149 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
Takashi Iwai834be882006-03-01 14:16:17 +01002151int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2152 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002155 unsigned int val = 0;
2156
2157 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002158 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002159 if (info)
2160 val = info->vol[ch];
2161 mutex_unlock(&codec->hash_mutex);
2162 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002164EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Takashi Iwai280e57d2012-12-14 10:32:21 +01002166static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2167 int direction, int idx, int mask, int val,
2168 bool init_only)
2169{
2170 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002171 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002172 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002173
2174 if (snd_BUG_ON(mask & ~0xff))
2175 mask &= 0xff;
2176 val &= mask;
2177
2178 mutex_lock(&codec->hash_mutex);
2179 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2180 if (!info) {
2181 mutex_unlock(&codec->hash_mutex);
2182 return 0;
2183 }
2184 val |= info->vol[ch] & ~mask;
2185 if (info->vol[ch] == val) {
2186 mutex_unlock(&codec->hash_mutex);
2187 return 0;
2188 }
2189 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002190 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002191 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002192 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002193 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002194 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002195 return 1;
2196}
2197
Takashi Iwaid5191e52009-11-16 14:58:17 +01002198/**
2199 * snd_hda_codec_amp_update - update the AMP value
2200 * @codec: HD-audio codec
2201 * @nid: NID to read the AMP value
2202 * @ch: channel (left=0 or right=1)
2203 * @direction: #HDA_INPUT or #HDA_OUTPUT
2204 * @idx: the index value (only for input direction)
2205 * @mask: bit mask to set
2206 * @val: the bits value to set
2207 *
2208 * Update the AMP value with a bit mask.
2209 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002210 */
Takashi Iwai834be882006-03-01 14:16:17 +01002211int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2212 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002214 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002216EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
2219 * snd_hda_codec_amp_stereo - update the AMP stereo values
2220 * @codec: HD-audio codec
2221 * @nid: NID to read the AMP value
2222 * @direction: #HDA_INPUT or #HDA_OUTPUT
2223 * @idx: the index value (only for input direction)
2224 * @mask: bit mask to set
2225 * @val: the bits value to set
2226 *
2227 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2228 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002229 */
2230int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2231 int direction, int idx, int mask, int val)
2232{
2233 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002234
2235 if (snd_BUG_ON(mask & ~0xff))
2236 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002237 for (ch = 0; ch < 2; ch++)
2238 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2239 idx, mask, val);
2240 return ret;
2241}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002242EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002243
Takashi Iwai280e57d2012-12-14 10:32:21 +01002244/* Works like snd_hda_codec_amp_update() but it writes the value only at
2245 * the first access. If the amp was already initialized / updated beforehand,
2246 * this does nothing.
2247 */
2248int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2249 int dir, int idx, int mask, int val)
2250{
2251 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2252}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002253EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002254
2255int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2256 int dir, int idx, int mask, int val)
2257{
2258 int ch, ret = 0;
2259
2260 if (snd_BUG_ON(mask & ~0xff))
2261 mask &= 0xff;
2262 for (ch = 0; ch < 2; ch++)
2263 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2264 idx, mask, val);
2265 return ret;
2266}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002267EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002268
Takashi Iwaid5191e52009-11-16 14:58:17 +01002269/**
2270 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2271 * @codec: HD-audio codec
2272 *
2273 * Resume the all amp commands from the cache.
2274 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002275void snd_hda_codec_resume_amp(struct hda_codec *codec)
2276{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002277 int i;
2278
Takashi Iwaic370dd62012-12-13 18:30:04 +01002279 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002280 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002281 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2282 struct hda_amp_info *buffer;
2283 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002284 hda_nid_t nid;
2285 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002286 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002287
2288 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002289 if (!buffer->head.dirty)
2290 continue;
2291 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002292 info = *buffer;
2293 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002294 if (!key)
2295 continue;
2296 nid = key & 0xff;
2297 idx = (key >> 16) & 0xff;
2298 dir = (key >> 24) & 0xff;
2299 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002300 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002301 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002302 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002303 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2304 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002305 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002306 }
2307 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002308 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002309}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002310EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002312static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2313 unsigned int ofs)
2314{
2315 u32 caps = query_amp_caps(codec, nid, dir);
2316 /* get num steps */
2317 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2318 if (ofs < caps)
2319 caps -= ofs;
2320 return caps;
2321}
2322
Takashi Iwaid5191e52009-11-16 14:58:17 +01002323/**
2324 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2325 *
2326 * The control element is supposed to have the private_value field
2327 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2328 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002329int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 u16 nid = get_amp_nid(kcontrol);
2334 u8 chs = get_amp_channels(kcontrol);
2335 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002336 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002338 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2339 uinfo->count = chs == 3 ? 2 : 1;
2340 uinfo->value.integer.min = 0;
2341 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2342 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002343 codec_warn(codec,
2344 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2345 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EINVAL;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002350EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002352
2353static inline unsigned int
2354read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2355 int ch, int dir, int idx, unsigned int ofs)
2356{
2357 unsigned int val;
2358 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2359 val &= HDA_AMP_VOLMASK;
2360 if (val >= ofs)
2361 val -= ofs;
2362 else
2363 val = 0;
2364 return val;
2365}
2366
2367static inline int
2368update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2369 int ch, int dir, int idx, unsigned int ofs,
2370 unsigned int val)
2371{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002372 unsigned int maxval;
2373
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002374 if (val > 0)
2375 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002376 /* ofs = 0: raw max value */
2377 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002378 if (val > maxval)
2379 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002380 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2381 HDA_AMP_VOLMASK, val);
2382}
2383
Takashi Iwaid5191e52009-11-16 14:58:17 +01002384/**
2385 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2386 *
2387 * The control element is supposed to have the private_value field
2388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2389 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002390int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 hda_nid_t nid = get_amp_nid(kcontrol);
2395 int chs = get_amp_channels(kcontrol);
2396 int dir = get_amp_direction(kcontrol);
2397 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002398 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 long *valp = ucontrol->value.integer.value;
2400
2401 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002402 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002404 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return 0;
2406}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002407EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Takashi Iwaid5191e52009-11-16 14:58:17 +01002409/**
2410 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2411 *
2412 * The control element is supposed to have the private_value field
2413 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2414 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002415int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2419 hda_nid_t nid = get_amp_nid(kcontrol);
2420 int chs = get_amp_channels(kcontrol);
2421 int dir = get_amp_direction(kcontrol);
2422 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002423 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 long *valp = ucontrol->value.integer.value;
2425 int change = 0;
2426
Takashi Iwaicb53c622007-08-10 17:21:45 +02002427 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002428 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002429 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002430 valp++;
2431 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002432 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002433 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002434 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return change;
2436}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002437EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Takashi Iwaid5191e52009-11-16 14:58:17 +01002439/**
2440 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2441 *
2442 * The control element is supposed to have the private_value field
2443 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2444 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002445int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2446 unsigned int size, unsigned int __user *_tlv)
2447{
2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449 hda_nid_t nid = get_amp_nid(kcontrol);
2450 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002451 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002452 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002453 u32 caps, val1, val2;
2454
2455 if (size < 4 * sizeof(unsigned int))
2456 return -ENOMEM;
2457 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002458 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2459 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002460 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002461 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002462 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002463 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002464 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002465 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2466 return -EFAULT;
2467 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2468 return -EFAULT;
2469 if (put_user(val1, _tlv + 2))
2470 return -EFAULT;
2471 if (put_user(val2, _tlv + 3))
2472 return -EFAULT;
2473 return 0;
2474}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002475EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002476
Takashi Iwaid5191e52009-11-16 14:58:17 +01002477/**
2478 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2479 * @codec: HD-audio codec
2480 * @nid: NID of a reference widget
2481 * @dir: #HDA_INPUT or #HDA_OUTPUT
2482 * @tlv: TLV data to be stored, at least 4 elements
2483 *
2484 * Set (static) TLV data for a virtual master volume using the AMP caps
2485 * obtained from the reference NID.
2486 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002487 */
2488void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2489 unsigned int *tlv)
2490{
2491 u32 caps;
2492 int nums, step;
2493
2494 caps = query_amp_caps(codec, nid, dir);
2495 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2496 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2497 step = (step + 1) * 25;
2498 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2499 tlv[1] = 2 * sizeof(unsigned int);
2500 tlv[2] = -nums * step;
2501 tlv[3] = step;
2502}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002503EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002504
2505/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002506static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002507find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002508{
2509 struct snd_ctl_elem_id id;
2510 memset(&id, 0, sizeof(id));
2511 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002512 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002513 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002514 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2515 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002516 strcpy(id.name, name);
2517 return snd_ctl_find_id(codec->bus->card, &id);
2518}
2519
Takashi Iwaid5191e52009-11-16 14:58:17 +01002520/**
2521 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2522 * @codec: HD-audio codec
2523 * @name: ctl id name string
2524 *
2525 * Get the control element with the given id string and IFACE_MIXER.
2526 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002527struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2528 const char *name)
2529{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002530 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002531}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002532EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002533
Takashi Iwaidcda5802012-10-12 17:24:51 +02002534static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002535 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002536{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002537 int i, idx;
2538 /* 16 ctlrs should be large enough */
2539 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2540 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002541 return idx;
2542 }
2543 return -EBUSY;
2544}
2545
Takashi Iwaid5191e52009-11-16 14:58:17 +01002546/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002547 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002548 * @codec: HD-audio codec
2549 * @nid: corresponding NID (optional)
2550 * @kctl: the control element to assign
2551 *
2552 * Add the given control element to an array inside the codec instance.
2553 * All control elements belonging to a codec are supposed to be added
2554 * by this function so that a proper clean-up works at the free or
2555 * reconfiguration time.
2556 *
2557 * If non-zero @nid is passed, the NID is assigned to the control element.
2558 * The assignment is shown in the codec proc file.
2559 *
2560 * snd_hda_ctl_add() checks the control subdev id field whether
2561 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002562 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2563 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002564 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002565int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2566 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002567{
2568 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002569 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002570 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002571
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002572 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002573 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002574 if (nid == 0)
2575 nid = get_amp_nid_(kctl->private_value);
2576 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002577 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2578 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002579 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002580 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002581 err = snd_ctl_add(codec->bus->card, kctl);
2582 if (err < 0)
2583 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002584 item = snd_array_new(&codec->mixers);
2585 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002586 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002587 item->kctl = kctl;
2588 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002589 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002590 return 0;
2591}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002592EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002593
Takashi Iwaid5191e52009-11-16 14:58:17 +01002594/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002595 * snd_hda_add_nid - Assign a NID to a control element
2596 * @codec: HD-audio codec
2597 * @nid: corresponding NID (optional)
2598 * @kctl: the control element to assign
2599 * @index: index to kctl
2600 *
2601 * Add the given control element to an array inside the codec instance.
2602 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2603 * NID:KCTL mapping - for example "Capture Source" selector.
2604 */
2605int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2606 unsigned int index, hda_nid_t nid)
2607{
2608 struct hda_nid_item *item;
2609
2610 if (nid > 0) {
2611 item = snd_array_new(&codec->nids);
2612 if (!item)
2613 return -ENOMEM;
2614 item->kctl = kctl;
2615 item->index = index;
2616 item->nid = nid;
2617 return 0;
2618 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002619 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2620 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002621 return -EINVAL;
2622}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002623EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002624
2625/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002626 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2627 * @codec: HD-audio codec
2628 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002629void snd_hda_ctls_clear(struct hda_codec *codec)
2630{
2631 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002632 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002633 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002634 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002635 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002636 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002637}
2638
Takashi Iwaia65d6292009-02-23 16:57:04 +01002639/* pseudo device locking
2640 * toggle card->shutdown to allow/disallow the device access (as a hack)
2641 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002642int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002643{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002644 struct snd_card *card = bus->card;
2645 struct hda_codec *codec;
2646
Takashi Iwaia65d6292009-02-23 16:57:04 +01002647 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002648 if (card->shutdown)
2649 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002650 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002651 if (!list_empty(&card->ctl_files))
2652 goto err_clear;
2653
2654 list_for_each_entry(codec, &bus->codec_list, list) {
2655 int pcm;
2656 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2657 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2658 if (!cpcm->pcm)
2659 continue;
2660 if (cpcm->pcm->streams[0].substream_opened ||
2661 cpcm->pcm->streams[1].substream_opened)
2662 goto err_clear;
2663 }
2664 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002665 spin_unlock(&card->files_lock);
2666 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002667
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002668 err_clear:
2669 card->shutdown = 0;
2670 err_unlock:
2671 spin_unlock(&card->files_lock);
2672 return -EINVAL;
2673}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002674EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002675
2676void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002677{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002678 struct snd_card *card = bus->card;
2679
2680 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002681 spin_lock(&card->files_lock);
2682 card->shutdown = 0;
2683 spin_unlock(&card->files_lock);
2684}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002685EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002686
Takashi Iwaid5191e52009-11-16 14:58:17 +01002687/**
2688 * snd_hda_codec_reset - Clear all objects assigned to the codec
2689 * @codec: HD-audio codec
2690 *
2691 * This frees the all PCM and control elements assigned to the codec, and
2692 * clears the caches and restores the pin default configurations.
2693 *
2694 * When a device is being used, it returns -EBSY. If successfully freed,
2695 * returns zero.
2696 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002697int snd_hda_codec_reset(struct hda_codec *codec)
2698{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002699 struct hda_bus *bus = codec->bus;
2700 struct snd_card *card = bus->card;
2701 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002702
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002703 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002704 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002705
2706 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002707 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002708#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002709 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002710 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002711#endif
2712 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002713 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002714 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002715 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002716 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002717 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002718 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002719 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002720 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002721 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002722 if (codec->patch_ops.free)
2723 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002724 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002725 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002726 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002727 codec->spec = NULL;
2728 free_hda_cache(&codec->amp_cache);
2729 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002730 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2731 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002732 /* free only driver_pins so that init_pins + user_pins are restored */
2733 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002734 snd_array_free(&codec->cvt_setups);
2735 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002736 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002737 codec->num_pcms = 0;
2738 codec->pcm_info = NULL;
2739 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002740 codec->slave_dig_outs = NULL;
2741 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002742 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002743 module_put(codec->owner);
2744 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002745
2746 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002747 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002748 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002749}
2750
Takashi Iwai6194b992014-06-06 18:12:16 +02002751typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002752
2753/* apply the function to all matching slave ctls in the mixer list */
2754static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002755 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002756{
2757 struct hda_nid_item *items;
2758 const char * const *s;
2759 int i, err;
2760
2761 items = codec->mixers.list;
2762 for (i = 0; i < codec->mixers.used; i++) {
2763 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002764 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002765 continue;
2766 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002767 char tmpname[sizeof(sctl->id.name)];
2768 const char *name = *s;
2769 if (suffix) {
2770 snprintf(tmpname, sizeof(tmpname), "%s %s",
2771 name, suffix);
2772 name = tmpname;
2773 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002774 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002775 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002776 if (err)
2777 return err;
2778 break;
2779 }
2780 }
2781 }
2782 return 0;
2783}
2784
Takashi Iwai6194b992014-06-06 18:12:16 +02002785static int check_slave_present(struct hda_codec *codec,
2786 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002787{
2788 return 1;
2789}
2790
Takashi Iwai18478e82012-03-09 17:51:10 +01002791/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002792static int get_kctl_0dB_offset(struct hda_codec *codec,
2793 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002794{
2795 int _tlv[4];
2796 const int *tlv = NULL;
2797 int val = -1;
2798
2799 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2800 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2801 mm_segment_t fs = get_fs();
2802 set_fs(get_ds());
2803 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2804 tlv = _tlv;
2805 set_fs(fs);
2806 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2807 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002808 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2809 int step = tlv[3];
2810 step &= ~TLV_DB_SCALE_MUTE;
2811 if (!step)
2812 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002813 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002814 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002815- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002816 return -1;
2817 }
2818 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002819 val = -tlv[2] / step;
2820 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002821 return val;
2822}
2823
2824/* call kctl->put with the given value(s) */
2825static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2826{
2827 struct snd_ctl_elem_value *ucontrol;
2828 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2829 if (!ucontrol)
2830 return -ENOMEM;
2831 ucontrol->value.integer.value[0] = val;
2832 ucontrol->value.integer.value[1] = val;
2833 kctl->put(kctl, ucontrol);
2834 kfree(ucontrol);
2835 return 0;
2836}
2837
2838/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002839static int init_slave_0dB(struct hda_codec *codec,
2840 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002841{
Takashi Iwai6194b992014-06-06 18:12:16 +02002842 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002843 if (offset > 0)
2844 put_kctl_with_value(slave, offset);
2845 return 0;
2846}
2847
2848/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002849static int init_slave_unmute(struct hda_codec *codec,
2850 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002851{
2852 return put_kctl_with_value(slave, 1);
2853}
2854
Takashi Iwaie8750942014-06-30 14:02:39 +02002855static int add_slave(struct hda_codec *codec,
2856 void *data, struct snd_kcontrol *slave)
2857{
2858 return snd_ctl_add_slave(data, slave);
2859}
2860
Takashi Iwaid5191e52009-11-16 14:58:17 +01002861/**
2862 * snd_hda_add_vmaster - create a virtual master control and add slaves
2863 * @codec: HD-audio codec
2864 * @name: vmaster control name
2865 * @tlv: TLV data (optional)
2866 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002867 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002868 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002869 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002870 *
2871 * Create a virtual master control with the given name. The TLV data
2872 * must be either NULL or a valid data.
2873 *
2874 * @slaves is a NULL-terminated array of strings, each of which is a
2875 * slave control name. All controls with these names are assigned to
2876 * the new virtual master control.
2877 *
2878 * This function returns zero if successful or a negative error code.
2879 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002880int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002881 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002882 const char *suffix, bool init_slave_vol,
2883 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002884{
2885 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002886 int err;
2887
Takashi Iwai29e58532012-03-12 12:25:03 +01002888 if (ctl_ret)
2889 *ctl_ret = NULL;
2890
Takashi Iwai9322ca52012-02-03 14:28:01 +01002891 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002892 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002893 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002894 return 0;
2895 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002896 kctl = snd_ctl_make_virtual_master(name, tlv);
2897 if (!kctl)
2898 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002899 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002900 if (err < 0)
2901 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002902
Takashi Iwaie8750942014-06-30 14:02:39 +02002903 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002904 if (err < 0)
2905 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002906
2907 /* init with master mute & zero volume */
2908 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002909 if (init_slave_vol) {
2910 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002911 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002912 tlv ? init_slave_0dB : init_slave_unmute, &step);
2913 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002914
Takashi Iwai29e58532012-03-12 12:25:03 +01002915 if (ctl_ret)
2916 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002917 return 0;
2918}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002919EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002920
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002921/*
2922 * mute-LED control using vmaster
2923 */
2924static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_info *uinfo)
2926{
2927 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002928 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002929 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002930
Takashi Iwai3ff72212014-10-20 18:17:28 +02002931 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002932}
2933
2934static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2936{
2937 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2938 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2939 return 0;
2940}
2941
2942static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_value *ucontrol)
2944{
2945 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2946 unsigned int old_mode = hook->mute_mode;
2947
2948 hook->mute_mode = ucontrol->value.enumerated.item[0];
2949 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2950 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2951 if (old_mode == hook->mute_mode)
2952 return 0;
2953 snd_hda_sync_vmaster_hook(hook);
2954 return 1;
2955}
2956
2957static struct snd_kcontrol_new vmaster_mute_mode = {
2958 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2959 .name = "Mute-LED Mode",
2960 .info = vmaster_mute_mode_info,
2961 .get = vmaster_mute_mode_get,
2962 .put = vmaster_mute_mode_put,
2963};
2964
2965/*
2966 * Add a mute-LED hook with the given vmaster switch kctl
2967 * "Mute-LED Mode" control is automatically created and associated with
2968 * the given hook.
2969 */
2970int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002971 struct hda_vmaster_mute_hook *hook,
2972 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002973{
2974 struct snd_kcontrol *kctl;
2975
2976 if (!hook->hook || !hook->sw_kctl)
2977 return 0;
2978 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2979 hook->codec = codec;
2980 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002981 if (!expose_enum_ctl)
2982 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002983 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2984 if (!kctl)
2985 return -ENOMEM;
2986 return snd_hda_ctl_add(codec, 0, kctl);
2987}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002988EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002989
2990/*
2991 * Call the hook with the current value for synchronization
2992 * Should be called in init callback
2993 */
2994void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2995{
2996 if (!hook->hook || !hook->codec)
2997 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002998 /* don't call vmaster hook in the destructor since it might have
2999 * been already destroyed
3000 */
3001 if (hook->codec->bus->shutdown)
3002 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003003 switch (hook->mute_mode) {
3004 case HDA_VMUTE_FOLLOW_MASTER:
3005 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3006 break;
3007 default:
3008 hook->hook(hook->codec, hook->mute_mode);
3009 break;
3010 }
3011}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003012EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003013
3014
Takashi Iwaid5191e52009-11-16 14:58:17 +01003015/**
3016 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3017 *
3018 * The control element is supposed to have the private_value field
3019 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3020 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3022 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 int chs = get_amp_channels(kcontrol);
3025
3026 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3027 uinfo->count = chs == 3 ? 2 : 1;
3028 uinfo->value.integer.min = 0;
3029 uinfo->value.integer.max = 1;
3030 return 0;
3031}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003032EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Takashi Iwaid5191e52009-11-16 14:58:17 +01003034/**
3035 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3036 *
3037 * The control element is supposed to have the private_value field
3038 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003040int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
3043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3044 hda_nid_t nid = get_amp_nid(kcontrol);
3045 int chs = get_amp_channels(kcontrol);
3046 int dir = get_amp_direction(kcontrol);
3047 int idx = get_amp_index(kcontrol);
3048 long *valp = ucontrol->value.integer.value;
3049
3050 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003051 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003052 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003055 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 0;
3057}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003058EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Takashi Iwaid5191e52009-11-16 14:58:17 +01003060/**
3061 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3062 *
3063 * The control element is supposed to have the private_value field
3064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3070 hda_nid_t nid = get_amp_nid(kcontrol);
3071 int chs = get_amp_channels(kcontrol);
3072 int dir = get_amp_direction(kcontrol);
3073 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 long *valp = ucontrol->value.integer.value;
3075 int change = 0;
3076
Takashi Iwaicb53c622007-08-10 17:21:45 +02003077 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003078 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003079 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003080 HDA_AMP_MUTE,
3081 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003082 valp++;
3083 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003084 if (chs & 2)
3085 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003086 HDA_AMP_MUTE,
3087 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003088 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003089 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return change;
3091}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003092EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094/*
Takashi Iwai985be542005-11-02 18:26:49 +01003095 * bound volume controls
3096 *
3097 * bind multiple volumes (# indices, from 0)
3098 */
3099
3100#define AMP_VAL_IDX_SHIFT 19
3101#define AMP_VAL_IDX_MASK (0x0f<<19)
3102
Takashi Iwaid5191e52009-11-16 14:58:17 +01003103/**
3104 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3105 *
3106 * The control element is supposed to have the private_value field
3107 * set up via HDA_BIND_MUTE*() macros.
3108 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3110 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003111{
3112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3113 unsigned long pval;
3114 int err;
3115
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003116 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003117 pval = kcontrol->private_value;
3118 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3119 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3120 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003121 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003122 return err;
3123}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003124EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003125
Takashi Iwaid5191e52009-11-16 14:58:17 +01003126/**
3127 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3128 *
3129 * The control element is supposed to have the private_value field
3130 * set up via HDA_BIND_MUTE*() macros.
3131 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003132int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3133 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003134{
3135 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3136 unsigned long pval;
3137 int i, indices, err = 0, change = 0;
3138
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003139 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003140 pval = kcontrol->private_value;
3141 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3142 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3144 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003145 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3146 if (err < 0)
3147 break;
3148 change |= err;
3149 }
3150 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003151 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003152 return err < 0 ? err : change;
3153}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003154EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003155
Takashi Iwaid5191e52009-11-16 14:58:17 +01003156/**
3157 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3158 *
3159 * The control element is supposed to have the private_value field
3160 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003161 */
3162int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_info *uinfo)
3164{
3165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3166 struct hda_bind_ctls *c;
3167 int err;
3168
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003169 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003170 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003171 kcontrol->private_value = *c->values;
3172 err = c->ops->info(kcontrol, uinfo);
3173 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003174 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003175 return err;
3176}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003177EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003178
Takashi Iwaid5191e52009-11-16 14:58:17 +01003179/**
3180 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3181 *
3182 * The control element is supposed to have the private_value field
3183 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3184 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003185int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_value *ucontrol)
3187{
3188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3189 struct hda_bind_ctls *c;
3190 int err;
3191
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003192 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003193 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003194 kcontrol->private_value = *c->values;
3195 err = c->ops->get(kcontrol, ucontrol);
3196 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003197 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003198 return err;
3199}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003200EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003201
Takashi Iwaid5191e52009-11-16 14:58:17 +01003202/**
3203 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3204 *
3205 * The control element is supposed to have the private_value field
3206 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3207 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003208int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3212 struct hda_bind_ctls *c;
3213 unsigned long *vals;
3214 int err = 0, change = 0;
3215
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003216 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003217 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003218 for (vals = c->values; *vals; vals++) {
3219 kcontrol->private_value = *vals;
3220 err = c->ops->put(kcontrol, ucontrol);
3221 if (err < 0)
3222 break;
3223 change |= err;
3224 }
3225 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003226 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003227 return err < 0 ? err : change;
3228}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003229EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003230
Takashi Iwaid5191e52009-11-16 14:58:17 +01003231/**
3232 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3233 *
3234 * The control element is supposed to have the private_value field
3235 * set up via HDA_BIND_VOL() macro.
3236 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003237int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3238 unsigned int size, unsigned int __user *tlv)
3239{
3240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3241 struct hda_bind_ctls *c;
3242 int err;
3243
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003244 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003245 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003246 kcontrol->private_value = *c->values;
3247 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3248 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003249 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003250 return err;
3251}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003252EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003253
3254struct hda_ctl_ops snd_hda_bind_vol = {
3255 .info = snd_hda_mixer_amp_volume_info,
3256 .get = snd_hda_mixer_amp_volume_get,
3257 .put = snd_hda_mixer_amp_volume_put,
3258 .tlv = snd_hda_mixer_amp_tlv
3259};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003260EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003261
3262struct hda_ctl_ops snd_hda_bind_sw = {
3263 .info = snd_hda_mixer_amp_switch_info,
3264 .get = snd_hda_mixer_amp_switch_get,
3265 .put = snd_hda_mixer_amp_switch_put,
3266 .tlv = snd_hda_mixer_amp_tlv
3267};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003268EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003269
3270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 * SPDIF out controls
3272 */
3273
Takashi Iwai0ba21762007-04-16 11:29:14 +02003274static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3278 uinfo->count = 1;
3279 return 0;
3280}
3281
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3286 IEC958_AES0_NONAUDIO |
3287 IEC958_AES0_CON_EMPHASIS_5015 |
3288 IEC958_AES0_CON_NOT_COPYRIGHT;
3289 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3290 IEC958_AES1_CON_ORIGINAL;
3291 return 0;
3292}
3293
Takashi Iwai0ba21762007-04-16 11:29:14 +02003294static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3298 IEC958_AES0_NONAUDIO |
3299 IEC958_AES0_PRO_EMPHASIS_5015;
3300 return 0;
3301}
3302
Takashi Iwai0ba21762007-04-16 11:29:14 +02003303static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003307 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003308 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003310 mutex_lock(&codec->spdif_mutex);
3311 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003312 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3313 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3314 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3315 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003316 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 return 0;
3319}
3320
3321/* convert from SPDIF status bits to HDA SPDIF bits
3322 * bit 0 (DigEn) is always set zero (to be filled later)
3323 */
3324static unsigned short convert_from_spdif_status(unsigned int sbits)
3325{
3326 unsigned short val = 0;
3327
3328 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003329 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003333 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3334 IEC958_AES0_PRO_EMPHASIS_5015)
3335 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003337 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3338 IEC958_AES0_CON_EMPHASIS_5015)
3339 val |= AC_DIG1_EMPHASIS;
3340 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3341 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3345 }
3346 return val;
3347}
3348
3349/* convert to SPDIF status bits from HDA SPDIF bits
3350 */
3351static unsigned int convert_to_spdif_status(unsigned short val)
3352{
3353 unsigned int sbits = 0;
3354
Takashi Iwai0ba21762007-04-16 11:29:14 +02003355 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003357 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 sbits |= IEC958_AES0_PROFESSIONAL;
3359 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003360 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3362 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003363 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003365 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003367 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3369 sbits |= val & (0x7f << 8);
3370 }
3371 return sbits;
3372}
3373
Takashi Iwai2f728532008-09-25 16:32:41 +02003374/* set digital convert verbs both for the given NID and its slaves */
3375static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3376 int verb, int val)
3377{
Takashi Iwaidda14412011-05-02 11:29:30 +02003378 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003379
Takashi Iwai9e976972008-11-25 08:17:20 +01003380 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003381 d = codec->slave_dig_outs;
3382 if (!d)
3383 return;
3384 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003385 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003386}
3387
3388static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3389 int dig1, int dig2)
3390{
3391 if (dig1 != -1)
3392 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3393 if (dig2 != -1)
3394 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3395}
3396
Takashi Iwai0ba21762007-04-16 11:29:14 +02003397static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3398 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003401 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003402 struct hda_spdif_out *spdif;
3403 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 unsigned short val;
3405 int change;
3406
Ingo Molnar62932df2006-01-16 16:34:20 +01003407 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003408 spdif = snd_array_elem(&codec->spdif_out, idx);
3409 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003410 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3412 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3413 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003414 val = convert_from_spdif_status(spdif->status);
3415 val |= spdif->ctls & 1;
3416 change = spdif->ctls != val;
3417 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003418 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003419 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003420 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return change;
3422}
3423
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003424#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Takashi Iwai0ba21762007-04-16 11:29:14 +02003426static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
3429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003430 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003431 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003433 mutex_lock(&codec->spdif_mutex);
3434 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003435 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003436 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return 0;
3438}
3439
Stephen Warren74b654c2011-06-01 11:14:18 -06003440static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3441 int dig1, int dig2)
3442{
3443 set_dig_out_convert(codec, nid, dig1, dig2);
3444 /* unmute amp switch (if any) */
3445 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3446 (dig1 & AC_DIG1_ENABLE))
3447 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3448 HDA_AMP_MUTE, 0);
3449}
3450
Takashi Iwai0ba21762007-04-16 11:29:14 +02003451static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3452 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
3454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003455 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003456 struct hda_spdif_out *spdif;
3457 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 unsigned short val;
3459 int change;
3460
Ingo Molnar62932df2006-01-16 16:34:20 +01003461 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003462 spdif = snd_array_elem(&codec->spdif_out, idx);
3463 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003464 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003466 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003467 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003468 spdif->ctls = val;
3469 if (change && nid != (u16)-1)
3470 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003471 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return change;
3473}
3474
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003475static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 {
3477 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3478 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003479 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .info = snd_hda_spdif_mask_info,
3481 .get = snd_hda_spdif_cmask_get,
3482 },
3483 {
3484 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003486 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 .info = snd_hda_spdif_mask_info,
3488 .get = snd_hda_spdif_pmask_get,
3489 },
3490 {
3491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003492 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 .info = snd_hda_spdif_mask_info,
3494 .get = snd_hda_spdif_default_get,
3495 .put = snd_hda_spdif_default_put,
3496 },
3497 {
3498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003499 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 .info = snd_hda_spdif_out_switch_info,
3501 .get = snd_hda_spdif_out_switch_get,
3502 .put = snd_hda_spdif_out_switch_put,
3503 },
3504 { } /* end */
3505};
3506
3507/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003508 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003510 * @associated_nid: NID that new ctls associated with
3511 * @cvt_nid: converter NID
3512 * @type: HDA_PCM_TYPE_*
3513 * Creates controls related with the digital output.
3514 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 *
3516 * Returns 0 if successful, or a negative error code.
3517 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003518int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3519 hda_nid_t associated_nid,
3520 hda_nid_t cvt_nid,
3521 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
3523 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003524 struct snd_kcontrol *kctl;
3525 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003526 int idx = 0;
3527 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003528 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003529 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003531 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003532 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003533 idx = spdif_index;
3534 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003535 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003536 /* suppose a single SPDIF device */
3537 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3538 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3539 if (!kctl)
3540 break;
3541 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003542 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003543 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003544 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003545 if (!bus->primary_dig_out_type)
3546 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003547
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003548 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003549 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003550 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003551 return -EBUSY;
3552 }
Stephen Warren7c935972011-06-01 11:14:17 -06003553 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003554 if (!spdif)
3555 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3557 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003558 if (!kctl)
3559 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003560 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003561 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003562 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return err;
3565 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003566 spdif->nid = cvt_nid;
3567 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003568 AC_VERB_GET_DIGI_CONVERT_1, 0);
3569 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003572EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003574/* get the hda_spdif_out entry from the given NID
3575 * call within spdif_mutex lock
3576 */
Stephen Warren7c935972011-06-01 11:14:17 -06003577struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3578 hda_nid_t nid)
3579{
3580 int i;
3581 for (i = 0; i < codec->spdif_out.used; i++) {
3582 struct hda_spdif_out *spdif =
3583 snd_array_elem(&codec->spdif_out, i);
3584 if (spdif->nid == nid)
3585 return spdif;
3586 }
3587 return NULL;
3588}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003589EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003590
Stephen Warren74b654c2011-06-01 11:14:18 -06003591void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3592{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003593 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003594
3595 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003596 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003597 spdif->nid = (u16)-1;
3598 mutex_unlock(&codec->spdif_mutex);
3599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003600EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003601
3602void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3603{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003604 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003605 unsigned short val;
3606
3607 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003608 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003609 if (spdif->nid != nid) {
3610 spdif->nid = nid;
3611 val = spdif->ctls;
3612 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3613 }
3614 mutex_unlock(&codec->spdif_mutex);
3615}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003616EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003619 * SPDIF sharing with analog output
3620 */
3621static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3622 struct snd_ctl_elem_value *ucontrol)
3623{
3624 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3625 ucontrol->value.integer.value[0] = mout->share_spdif;
3626 return 0;
3627}
3628
3629static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3630 struct snd_ctl_elem_value *ucontrol)
3631{
3632 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3633 mout->share_spdif = !!ucontrol->value.integer.value[0];
3634 return 0;
3635}
3636
3637static struct snd_kcontrol_new spdif_share_sw = {
3638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3639 .name = "IEC958 Default PCM Playback Switch",
3640 .info = snd_ctl_boolean_mono_info,
3641 .get = spdif_share_sw_get,
3642 .put = spdif_share_sw_put,
3643};
3644
Takashi Iwaid5191e52009-11-16 14:58:17 +01003645/**
3646 * snd_hda_create_spdif_share_sw - create Default PCM switch
3647 * @codec: the HDA codec
3648 * @mout: multi-out instance
3649 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003650int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3651 struct hda_multi_out *mout)
3652{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003653 struct snd_kcontrol *kctl;
3654
Takashi Iwai9a081602008-02-12 18:37:26 +01003655 if (!mout->dig_out_nid)
3656 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003657
3658 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3659 if (!kctl)
3660 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003661 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003662 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003664EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003665
3666/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 * SPDIF input
3668 */
3669
3670#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3671
Takashi Iwai0ba21762007-04-16 11:29:14 +02003672static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3673 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3676
3677 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3678 return 0;
3679}
3680
Takashi Iwai0ba21762007-04-16 11:29:14 +02003681static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3682 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3685 hda_nid_t nid = kcontrol->private_value;
3686 unsigned int val = !!ucontrol->value.integer.value[0];
3687 int change;
3688
Ingo Molnar62932df2006-01-16 16:34:20 +01003689 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003691 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003693 snd_hda_codec_write_cache(codec, nid, 0,
3694 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003696 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return change;
3698}
3699
Takashi Iwai0ba21762007-04-16 11:29:14 +02003700static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3701 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
3703 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3704 hda_nid_t nid = kcontrol->private_value;
3705 unsigned short val;
3706 unsigned int sbits;
3707
Andrew Paprocki3982d172007-12-19 12:13:44 +01003708 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 sbits = convert_to_spdif_status(val);
3710 ucontrol->value.iec958.status[0] = sbits;
3711 ucontrol->value.iec958.status[1] = sbits >> 8;
3712 ucontrol->value.iec958.status[2] = sbits >> 16;
3713 ucontrol->value.iec958.status[3] = sbits >> 24;
3714 return 0;
3715}
3716
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003717static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 {
3719 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003720 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 .info = snd_hda_spdif_in_switch_info,
3722 .get = snd_hda_spdif_in_switch_get,
3723 .put = snd_hda_spdif_in_switch_put,
3724 },
3725 {
3726 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003728 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 .info = snd_hda_spdif_mask_info,
3730 .get = snd_hda_spdif_in_status_get,
3731 },
3732 { } /* end */
3733};
3734
3735/**
3736 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3737 * @codec: the HDA codec
3738 * @nid: audio in widget NID
3739 *
3740 * Creates controls related with the SPDIF input.
3741 * Called from each patch supporting the SPDIF in.
3742 *
3743 * Returns 0 if successful, or a negative error code.
3744 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003745int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
3747 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003748 struct snd_kcontrol *kctl;
3749 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003750 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Takashi Iwaidcda5802012-10-12 17:24:51 +02003752 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003753 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003754 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003755 return -EBUSY;
3756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3758 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003759 if (!kctl)
3760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003762 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003763 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return err;
3765 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003766 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003767 snd_hda_codec_read(codec, nid, 0,
3768 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003769 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return 0;
3771}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003772EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003774/*
3775 * command cache
3776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Takashi Iwaic370dd62012-12-13 18:30:04 +01003778/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003779#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3780#define get_cmd_cache_nid(key) ((key) & 0xff)
3781#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3782
3783/**
3784 * snd_hda_codec_write_cache - send a single command with caching
3785 * @codec: the HDA codec
3786 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003787 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003788 * @verb: the verb to send
3789 * @parm: the parameter for the verb
3790 *
3791 * Send a single command without waiting for response.
3792 *
3793 * Returns 0 if successful, or a negative error code.
3794 */
3795int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003796 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003797{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003798 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003799 struct hda_cache_head *c;
3800 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003801 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003802
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003803 cache_only = codec->cached_write;
3804 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003805 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003806 if (err < 0)
3807 return err;
3808 }
3809
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003810 /* parm may contain the verb stuff for get/set amp */
3811 verb = verb | (parm >> 8);
3812 parm &= 0xff;
3813 key = build_cmd_cache_key(nid, verb);
3814 mutex_lock(&codec->bus->cmd_mutex);
3815 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003816 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003817 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003818 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003819 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003820 mutex_unlock(&codec->bus->cmd_mutex);
3821 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003822}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003823EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003824
Takashi Iwaid5191e52009-11-16 14:58:17 +01003825/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003826 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3827 * @codec: the HDA codec
3828 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003829 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003830 * @verb: the verb to send
3831 * @parm: the parameter for the verb
3832 *
3833 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3834 * command if the parameter is already identical with the cached value.
3835 * If not, it sends the command and refreshes the cache.
3836 *
3837 * Returns 0 if successful, or a negative error code.
3838 */
3839int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003840 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003841{
3842 struct hda_cache_head *c;
3843 u32 key;
3844
3845 /* parm may contain the verb stuff for get/set amp */
3846 verb = verb | (parm >> 8);
3847 parm &= 0xff;
3848 key = build_cmd_cache_key(nid, verb);
3849 mutex_lock(&codec->bus->cmd_mutex);
3850 c = get_hash(&codec->cmd_cache, key);
3851 if (c && c->val == parm) {
3852 mutex_unlock(&codec->bus->cmd_mutex);
3853 return 0;
3854 }
3855 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003856 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003857}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003858EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003859
3860/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003861 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3862 * @codec: HD-audio codec
3863 *
3864 * Execute all verbs recorded in the command caches to resume.
3865 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003866void snd_hda_codec_resume_cache(struct hda_codec *codec)
3867{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003868 int i;
3869
Takashi Iwaic370dd62012-12-13 18:30:04 +01003870 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003871 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003872 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3873 struct hda_cache_head *buffer;
3874 u32 key;
3875
3876 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3877 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003878 if (!key)
3879 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003880 if (!buffer->dirty)
3881 continue;
3882 buffer->dirty = 0;
3883 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003884 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3885 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003886 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003887 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003888 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003889}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003890EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003891
3892/**
3893 * snd_hda_sequence_write_cache - sequence writes with caching
3894 * @codec: the HDA codec
3895 * @seq: VERB array to send
3896 *
3897 * Send the commands sequentially from the given array.
3898 * Thte commands are recorded on cache for power-save and resume.
3899 * The array must be terminated with NID=0.
3900 */
3901void snd_hda_sequence_write_cache(struct hda_codec *codec,
3902 const struct hda_verb *seq)
3903{
3904 for (; seq->nid; seq++)
3905 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3906 seq->param);
3907}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003908EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003909
Takashi Iwaidc870f32013-01-22 15:24:30 +01003910/**
3911 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3912 * @codec: HD-audio codec
3913 */
3914void snd_hda_codec_flush_cache(struct hda_codec *codec)
3915{
3916 snd_hda_codec_resume_amp(codec);
3917 snd_hda_codec_resume_cache(codec);
3918}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003919EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003920
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003921void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003922 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003923{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003924 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003925 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003926
Takashi Iwaicb53c622007-08-10 17:21:45 +02003927 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003928 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003929 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003930 if (!(wcaps & AC_WCAP_POWER))
3931 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003932 if (codec->power_filter) {
3933 state = codec->power_filter(codec, nid, power_state);
3934 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003935 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003936 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003937 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003938 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003939 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003940}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003941EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003942
3943/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003944 * supported power states check
3945 */
3946static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3947 unsigned int power_state)
3948{
3949 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3950
Mengdong Line037cb42012-08-10 14:11:58 +02003951 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003952 return false;
3953 if (sup & power_state)
3954 return true;
3955 else
3956 return false;
3957}
3958
3959/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003960 * wait until the state is reached, returns the current state
3961 */
3962static unsigned int hda_sync_power_state(struct hda_codec *codec,
3963 hda_nid_t fg,
3964 unsigned int power_state)
3965{
3966 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3967 unsigned int state, actual_state;
3968
3969 for (;;) {
3970 state = snd_hda_codec_read(codec, fg, 0,
3971 AC_VERB_GET_POWER_STATE, 0);
3972 if (state & AC_PWRST_ERROR)
3973 break;
3974 actual_state = (state >> 4) & 0x0f;
3975 if (actual_state == power_state)
3976 break;
3977 if (time_after_eq(jiffies, end_time))
3978 break;
3979 /* wait until the codec reachs to the target state */
3980 msleep(1);
3981 }
3982 return state;
3983}
3984
Takashi Iwai9419ab62013-01-24 17:23:35 +01003985/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003986unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3987 hda_nid_t nid,
3988 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003989{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003990 if (nid == codec->afg || nid == codec->mfg)
3991 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003992 if (power_state == AC_PWRST_D3 &&
3993 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3994 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3995 int eapd = snd_hda_codec_read(codec, nid, 0,
3996 AC_VERB_GET_EAPD_BTLENABLE, 0);
3997 if (eapd & 0x02)
3998 return AC_PWRST_D0;
3999 }
4000 return power_state;
4001}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004002EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004003
Takashi Iwai432c6412012-08-28 09:59:20 -07004004/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004005 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004006 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004007static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004008 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004009{
Takashi Iwaid8193872012-08-31 07:54:38 -07004010 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004011 int count;
4012 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004013 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004014
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004015 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004016 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004017 if (codec->depop_delay < 0)
4018 msleep(codec->epss ? 10 : 100);
4019 else if (codec->depop_delay > 0)
4020 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004021 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004022 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004023
4024 /* repeat power states setting at most 10 times*/
4025 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004026 if (codec->patch_ops.set_power_state)
4027 codec->patch_ops.set_power_state(codec, fg,
4028 power_state);
4029 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004030 state = power_state;
4031 if (codec->power_filter)
4032 state = codec->power_filter(codec, fg, state);
4033 if (state == power_state || power_state != AC_PWRST_D3)
4034 snd_hda_codec_read(codec, fg, flags,
4035 AC_VERB_SET_POWER_STATE,
4036 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004037 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004038 }
4039 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004040 if (!(state & AC_PWRST_ERROR))
4041 break;
4042 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004043
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004044 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004045}
Takashi Iwai54d17402005-11-21 16:33:22 +01004046
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004047/* sync power states of all widgets;
4048 * this is called at the end of codec parsing
4049 */
4050static void sync_power_up_states(struct hda_codec *codec)
4051{
4052 hda_nid_t nid = codec->start_nid;
4053 int i;
4054
Takashi Iwaiba615b82013-03-13 14:47:21 +01004055 /* don't care if no filter is used */
4056 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004057 return;
4058
4059 for (i = 0; i < codec->num_nodes; i++, nid++) {
4060 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004061 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004062 if (!(wcaps & AC_WCAP_POWER))
4063 continue;
4064 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4065 if (target == AC_PWRST_D0)
4066 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004067 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004068 snd_hda_codec_write(codec, nid, 0,
4069 AC_VERB_SET_POWER_STATE, target);
4070 }
4071}
4072
Takashi Iwai648a8d22014-02-25 10:38:13 +01004073#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004074/* execute additional init verbs */
4075static void hda_exec_init_verbs(struct hda_codec *codec)
4076{
4077 if (codec->init_verbs.list)
4078 snd_hda_sequence_write(codec, codec->init_verbs.list);
4079}
4080#else
4081static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4082#endif
4083
Takashi Iwai2a439522011-07-26 09:52:50 +02004084#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004085/*
4086 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004087 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004088 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004089static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004090{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004091 unsigned int state;
4092
Takashi Iwai989c3182012-11-19 14:14:58 +01004093 codec->in_pm = 1;
4094
Takashi Iwaicb53c622007-08-10 17:21:45 +02004095 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004096 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004097 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004098 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004099 /* Cancel delayed work if we aren't currently running from it. */
4100 if (!in_wq)
4101 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004102 spin_lock(&codec->power_lock);
4103 snd_hda_update_power_acct(codec);
4104 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004105 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004106 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004107 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004108 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004109 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004110 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004111}
4112
Takashi Iwaic370dd62012-12-13 18:30:04 +01004113/* mark all entries of cmd and amp caches dirty */
4114static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4115{
4116 int i;
4117 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4118 struct hda_cache_head *cmd;
4119 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4120 cmd->dirty = 1;
4121 }
4122 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4123 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004124 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004125 amp->head.dirty = 1;
4126 }
4127}
4128
Takashi Iwaicb53c622007-08-10 17:21:45 +02004129/*
4130 * kick up codec; used both from PM and power-save
4131 */
4132static void hda_call_codec_resume(struct hda_codec *codec)
4133{
Takashi Iwai989c3182012-11-19 14:14:58 +01004134 codec->in_pm = 1;
4135
Takashi Iwaic370dd62012-12-13 18:30:04 +01004136 hda_mark_cmd_cache_dirty(codec);
4137
Takashi Iwai7f308302012-05-08 16:52:23 +02004138 /* set as if powered on for avoiding re-entering the resume
4139 * in the resume / power-save sequence
4140 */
4141 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004142 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004143 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004144 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004145 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004146 if (codec->patch_ops.resume)
4147 codec->patch_ops.resume(codec);
4148 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004149 if (codec->patch_ops.init)
4150 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004151 snd_hda_codec_resume_amp(codec);
4152 snd_hda_codec_resume_cache(codec);
4153 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004154
4155 if (codec->jackpoll_interval)
4156 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004157 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004158 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004159
4160 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004161 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004162}
Takashi Iwai2a439522011-07-26 09:52:50 +02004163#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004164
Takashi Iwai54d17402005-11-21 16:33:22 +01004165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166/**
4167 * snd_hda_build_controls - build mixer controls
4168 * @bus: the BUS
4169 *
4170 * Creates mixer controls for each codec included in the bus.
4171 *
4172 * Returns 0 if successful, otherwise a negative error code.
4173 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004174int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004176 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Takashi Iwai0ba21762007-04-16 11:29:14 +02004178 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004179 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004180 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004181 codec_err(codec,
4182 "cannot build controls for #%d (error %d)\n",
4183 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004184 err = snd_hda_codec_reset(codec);
4185 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004186 codec_err(codec,
4187 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004188 return err;
4189 }
4190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004192 return 0;
4193}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004194EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004196/*
4197 * add standard channel maps if not specified
4198 */
4199static int add_std_chmaps(struct hda_codec *codec)
4200{
4201 int i, str, err;
4202
4203 for (i = 0; i < codec->num_pcms; i++) {
4204 for (str = 0; str < 2; str++) {
4205 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4206 struct hda_pcm_stream *hinfo =
4207 &codec->pcm_info[i].stream[str];
4208 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004209 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004210
4211 if (codec->pcm_info[i].own_chmap)
4212 continue;
4213 if (!pcm || !hinfo->substreams)
4214 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004215 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4216 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004217 hinfo->channels_max,
4218 0, &chmap);
4219 if (err < 0)
4220 return err;
4221 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4222 }
4223 }
4224 return 0;
4225}
4226
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004227/* default channel maps for 2.1 speakers;
4228 * since HD-audio supports only stereo, odd number channels are omitted
4229 */
4230const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4231 { .channels = 2,
4232 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4233 { .channels = 4,
4234 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4235 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4236 { }
4237};
4238EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4239
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004240int snd_hda_codec_build_controls(struct hda_codec *codec)
4241{
4242 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004243 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004244 /* continue to initialize... */
4245 if (codec->patch_ops.init)
4246 err = codec->patch_ops.init(codec);
4247 if (!err && codec->patch_ops.build_controls)
4248 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004249 if (err < 0)
4250 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004251
4252 /* we create chmaps here instead of build_pcms */
4253 err = add_std_chmaps(codec);
4254 if (err < 0)
4255 return err;
4256
David Henningsson26a6cb62012-10-09 15:04:21 +02004257 if (codec->jackpoll_interval)
4258 hda_jackpoll_work(&codec->jackpoll_work.work);
4259 else
4260 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004261 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return 0;
4263}
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265/*
4266 * stream formats
4267 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004268struct hda_rate_tbl {
4269 unsigned int hz;
4270 unsigned int alsa_bits;
4271 unsigned int hda_fmt;
4272};
4273
Takashi Iwai92f10b32010-08-03 14:21:00 +02004274/* rate = base * mult / div */
4275#define HDA_RATE(base, mult, div) \
4276 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4277 (((div) - 1) << AC_FMT_DIV_SHIFT))
4278
Takashi Iwaibefdf312005-08-22 13:57:55 +02004279static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004281
4282 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004283 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4284 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4285 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4286 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4287 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4288 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4289 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4290 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4291 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4292 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4293 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004294#define AC_PAR_PCM_RATE_BITS 11
4295 /* up to bits 10, 384kHZ isn't supported properly */
4296
4297 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004298 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004299
Takashi Iwaibefdf312005-08-22 13:57:55 +02004300 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301};
4302
4303/**
4304 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004305 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 * @rate: the sample rate
4307 * @channels: the number of channels
4308 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4309 * @maxbps: the max. bps
4310 *
4311 * Calculate the format bitset from the given rate, channels and th PCM format.
4312 *
4313 * Return zero if invalid.
4314 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004315unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4316 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 unsigned int channels,
4318 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004319 unsigned int maxbps,
4320 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
4322 int i;
4323 unsigned int val = 0;
4324
Takashi Iwaibefdf312005-08-22 13:57:55 +02004325 for (i = 0; rate_bits[i].hz; i++)
4326 if (rate_bits[i].hz == rate) {
4327 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 break;
4329 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004330 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004331 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return 0;
4333 }
4334
4335 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004336 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return 0;
4338 }
4339 val |= channels - 1;
4340
4341 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004342 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004343 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004344 break;
4345 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004346 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 case 20:
4349 case 24:
4350 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004351 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004352 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004354 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004356 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 break;
4358 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004359 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004360 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return 0;
4362 }
4363
Anssi Hannula32c168c2010-08-03 13:28:57 +03004364 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004365 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 return val;
4368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004369EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004371static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4372 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004373{
4374 unsigned int val = 0;
4375 if (nid != codec->afg &&
4376 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4377 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4378 if (!val || val == -1)
4379 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4380 if (!val || val == -1)
4381 return 0;
4382 return val;
4383}
4384
4385static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4386{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004387 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004388 get_pcm_param);
4389}
4390
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004391static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4392 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004393{
4394 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4395 if (!streams || streams == -1)
4396 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4397 if (!streams || streams == -1)
4398 return 0;
4399 return streams;
4400}
4401
4402static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4403{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004404 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004405 get_stream_param);
4406}
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408/**
4409 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4410 * @codec: the HDA codec
4411 * @nid: NID to query
4412 * @ratesp: the pointer to store the detected rate bitflags
4413 * @formatsp: the pointer to store the detected formats
4414 * @bpsp: the pointer to store the detected format widths
4415 *
4416 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4417 * or @bsps argument is ignored.
4418 *
4419 * Returns 0 if successful, otherwise a negative error code.
4420 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004421int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4423{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004424 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004426 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004427 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 if (ratesp) {
4430 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004431 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004433 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004435 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004436 codec_err(codec,
4437 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4438 nid, val,
4439 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004440 return -EIO;
4441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 *ratesp = rates;
4443 }
4444
4445 if (formatsp || bpsp) {
4446 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004447 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004449 streams = query_stream_param(codec, nid);
4450 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 bps = 0;
4454 if (streams & AC_SUPFMT_PCM) {
4455 if (val & AC_SUPPCM_BITS_8) {
4456 formats |= SNDRV_PCM_FMTBIT_U8;
4457 bps = 8;
4458 }
4459 if (val & AC_SUPPCM_BITS_16) {
4460 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4461 bps = 16;
4462 }
4463 if (wcaps & AC_WCAP_DIGITAL) {
4464 if (val & AC_SUPPCM_BITS_32)
4465 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4466 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4467 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4468 if (val & AC_SUPPCM_BITS_24)
4469 bps = 24;
4470 else if (val & AC_SUPPCM_BITS_20)
4471 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004472 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4473 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4475 if (val & AC_SUPPCM_BITS_32)
4476 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 else if (val & AC_SUPPCM_BITS_24)
4478 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004479 else if (val & AC_SUPPCM_BITS_20)
4480 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004483#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004484 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004486 if (!bps)
4487 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004488 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004489#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004490 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004491 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 /* temporary hack: we have still no proper support
4493 * for the direct AC3 stream...
4494 */
4495 formats |= SNDRV_PCM_FMTBIT_U8;
4496 bps = 8;
4497 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004498 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004499 codec_err(codec,
4500 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4501 nid, val,
4502 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4503 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004504 return -EIO;
4505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 if (formatsp)
4507 *formatsp = formats;
4508 if (bpsp)
4509 *bpsp = bps;
4510 }
4511
4512 return 0;
4513}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004514EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004517 * snd_hda_is_supported_format - Check the validity of the format
4518 * @codec: HD-audio codec
4519 * @nid: NID to check
4520 * @format: the HD-audio format value to check
4521 *
4522 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 *
4524 * Returns 1 if supported, 0 if not.
4525 */
4526int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4527 unsigned int format)
4528{
4529 int i;
4530 unsigned int val = 0, rate, stream;
4531
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004532 val = query_pcm_param(codec, nid);
4533 if (!val)
4534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004537 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004538 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (val & (1 << i))
4540 break;
4541 return 0;
4542 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004543 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return 0;
4545
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004546 stream = query_stream_param(codec, nid);
4547 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 return 0;
4549
4550 if (stream & AC_SUPFMT_PCM) {
4551 switch (format & 0xf0) {
4552 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004553 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 return 0;
4555 break;
4556 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004557 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return 0;
4559 break;
4560 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004561 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 return 0;
4563 break;
4564 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004565 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return 0;
4567 break;
4568 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004569 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return 0;
4571 break;
4572 default:
4573 return 0;
4574 }
4575 } else {
4576 /* FIXME: check for float32 and AC3? */
4577 }
4578
4579 return 1;
4580}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004581EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583/*
4584 * PCM stuff
4585 */
4586static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4587 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004588 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
4590 return 0;
4591}
4592
4593static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 unsigned int stream_tag,
4596 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004597 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4600 return 0;
4601}
4602
4603static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4604 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004605 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Takashi Iwai888afa12008-03-18 09:57:50 +01004607 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return 0;
4609}
4610
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004611static int set_pcm_default_values(struct hda_codec *codec,
4612 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004614 int err;
4615
Takashi Iwai0ba21762007-04-16 11:29:14 +02004616 /* query support PCM information from the given NID */
4617 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004618 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004619 info->rates ? NULL : &info->rates,
4620 info->formats ? NULL : &info->formats,
4621 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004622 if (err < 0)
4623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625 if (info->ops.open == NULL)
4626 info->ops.open = hda_pcm_default_open_close;
4627 if (info->ops.close == NULL)
4628 info->ops.close = hda_pcm_default_open_close;
4629 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004630 if (snd_BUG_ON(!info->nid))
4631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 info->ops.prepare = hda_pcm_default_prepare;
4633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004635 if (snd_BUG_ON(!info->nid))
4636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 info->ops.cleanup = hda_pcm_default_cleanup;
4638 }
4639 return 0;
4640}
4641
Takashi Iwaieb541332010-08-06 13:48:11 +02004642/*
4643 * codec prepare/cleanup entries
4644 */
4645int snd_hda_codec_prepare(struct hda_codec *codec,
4646 struct hda_pcm_stream *hinfo,
4647 unsigned int stream,
4648 unsigned int format,
4649 struct snd_pcm_substream *substream)
4650{
4651 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004652 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004653 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4654 if (ret >= 0)
4655 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004656 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004657 return ret;
4658}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004659EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004660
4661void snd_hda_codec_cleanup(struct hda_codec *codec,
4662 struct hda_pcm_stream *hinfo,
4663 struct snd_pcm_substream *substream)
4664{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004665 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004666 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004667 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004668}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004669EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004670
Takashi Iwaid5191e52009-11-16 14:58:17 +01004671/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004672const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4673 "Audio", "SPDIF", "HDMI", "Modem"
4674};
4675
Takashi Iwai176d5332008-07-30 15:01:44 +02004676/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004677 * get the empty PCM device number to assign
4678 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004679static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004680{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004681 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004682 /* assigned to static slots up to dev#10; if more needed, assign
4683 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4684 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004685 static int audio_idx[HDA_PCM_NTYPES][5] = {
4686 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4687 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004688 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004689 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004690 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004691 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004692
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004693 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004694 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004695 return -EINVAL;
4696 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004697
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004698 for (i = 0; audio_idx[type][i] >= 0; i++) {
4699#ifndef CONFIG_SND_DYNAMIC_MINORS
4700 if (audio_idx[type][i] >= 8)
4701 break;
4702#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004703 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4704 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004705 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004706
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004707#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004708 /* non-fixed slots starting from 10 */
4709 for (i = 10; i < 32; i++) {
4710 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4711 return i;
4712 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004713#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004714
Takashi Iwai4e76a882014-02-25 12:21:03 +01004715 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004716 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004717#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004718 dev_warn(bus->card->dev,
4719 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004720#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004721 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004722}
4723
4724/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004725 * attach a new PCM stream
4726 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004727static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004728{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004729 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004730 struct hda_pcm_stream *info;
4731 int stream, err;
4732
Takashi Iwaib91f0802008-11-04 08:43:08 +01004733 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004734 return -EINVAL;
4735 for (stream = 0; stream < 2; stream++) {
4736 info = &pcm->stream[stream];
4737 if (info->substreams) {
4738 err = set_pcm_default_values(codec, info);
4739 if (err < 0)
4740 return err;
4741 }
4742 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004743 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004744}
4745
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004746/* assign all PCMs of the given codec */
4747int snd_hda_codec_build_pcms(struct hda_codec *codec)
4748{
4749 unsigned int pcm;
4750 int err;
4751
4752 if (!codec->num_pcms) {
4753 if (!codec->patch_ops.build_pcms)
4754 return 0;
4755 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004756 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004757 codec_err(codec,
4758 "cannot build PCMs for #%d (error %d)\n",
4759 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004760 err = snd_hda_codec_reset(codec);
4761 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004762 codec_err(codec,
4763 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004764 return err;
4765 }
4766 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004767 }
4768 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4769 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4770 int dev;
4771
4772 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004773 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004774
4775 if (!cpcm->pcm) {
4776 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4777 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004778 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004779 cpcm->device = dev;
4780 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004781 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004782 codec_err(codec,
4783 "cannot attach PCM stream %d for codec #%d\n",
4784 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004785 continue; /* no fatal error */
4786 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004787 }
4788 }
4789 return 0;
4790}
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792/**
4793 * snd_hda_build_pcms - build PCM information
4794 * @bus: the BUS
4795 *
4796 * Create PCM information for each codec included in the bus.
4797 *
4798 * The build_pcms codec patch is requested to set up codec->num_pcms and
4799 * codec->pcm_info properly. The array is referred by the top-level driver
4800 * to create its PCM instances.
4801 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4802 * callback.
4803 *
4804 * At least, substreams, channels_min and channels_max must be filled for
4805 * each stream. substreams = 0 indicates that the stream doesn't exist.
4806 * When rates and/or formats are zero, the supported values are queried
4807 * from the given nid. The nid is used also by the default ops.prepare
4808 * and ops.cleanup callbacks.
4809 *
4810 * The driver needs to call ops.open in its open callback. Similarly,
4811 * ops.close is supposed to be called in the close callback.
4812 * ops.prepare should be called in the prepare or hw_params callback
4813 * with the proper parameters for set up.
4814 * ops.cleanup should be called in hw_free for clean up of streams.
4815 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004816 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004818int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004820 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Takashi Iwai0ba21762007-04-16 11:29:14 +02004822 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004823 int err = snd_hda_codec_build_pcms(codec);
4824 if (err < 0)
4825 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 }
4827 return 0;
4828}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004829EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 * snd_hda_add_new_ctls - create controls from the array
4833 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004834 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 *
4836 * This helper function creates and add new controls in the given array.
4837 * The array must be terminated with an empty entry as terminator.
4838 *
4839 * Returns 0 if successful, or a negative error code.
4840 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004841int snd_hda_add_new_ctls(struct hda_codec *codec,
4842 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004844 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
4846 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004847 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004848 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004849 if (knew->iface == -1) /* skip this codec private value */
4850 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004851 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004852 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004853 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004854 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004855 if (addr > 0)
4856 kctl->id.device = addr;
4857 if (idx > 0)
4858 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004859 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004860 if (!err)
4861 break;
4862 /* try first with another device index corresponding to
4863 * the codec addr; if it still fails (or it's the
4864 * primary codec), then try another control index
4865 */
4866 if (!addr && codec->addr)
4867 addr = codec->addr;
4868 else if (!idx && !knew->index) {
4869 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004870 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004871 if (idx <= 0)
4872 return err;
4873 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004874 return err;
4875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 }
4877 return 0;
4878}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004879EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Takashi Iwai83012a72012-08-24 18:38:08 +02004881#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004882static void hda_power_work(struct work_struct *work)
4883{
4884 struct hda_codec *codec =
4885 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004886 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004887 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004888
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004889 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004890 if (codec->power_transition > 0) { /* during power-up sequence? */
4891 spin_unlock(&codec->power_lock);
4892 return;
4893 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004894 if (!codec->power_on || codec->power_count) {
4895 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004896 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004897 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004898 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004899 spin_unlock(&codec->power_lock);
4900
Dylan Reidd17344b2012-09-28 15:57:01 -07004901 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004902 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4903 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004904}
4905
4906static void hda_keep_power_on(struct hda_codec *codec)
4907{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004908 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004909 codec->power_count++;
4910 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004911 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004912 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004913 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004914}
4915
Takashi Iwaid5191e52009-11-16 14:58:17 +01004916/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004917void snd_hda_update_power_acct(struct hda_codec *codec)
4918{
4919 unsigned long delta = jiffies - codec->power_jiffies;
4920 if (codec->power_on)
4921 codec->power_on_acct += delta;
4922 else
4923 codec->power_off_acct += delta;
4924 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004925}
4926
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004927/* Transition to powered up, if wait_power_down then wait for a pending
4928 * transition to D3 to complete. A pending D3 transition is indicated
4929 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004930/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004931static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004932{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004933 /* Return if power_on or transitioning to power_on, unless currently
4934 * powering down. */
4935 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004936 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004937 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004938 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004939
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004940 cancel_delayed_work_sync(&codec->power_work);
4941
4942 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004943 /* If the power down delayed work was cancelled above before starting,
4944 * then there is no need to go through power up here.
4945 */
4946 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004947 if (codec->power_transition < 0)
4948 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004949 return;
4950 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004951
Takashi Iwaid66fee52011-08-02 15:39:31 +02004952 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004953 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004954 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004955 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004956 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004957 spin_unlock(&codec->power_lock);
4958
Takashi Iwaicb53c622007-08-10 17:21:45 +02004959 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004960
4961 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004962 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004963}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004964
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004965#define power_save(codec) \
4966 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004967
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004968/* Transition to powered down */
4969static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004970{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004971 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004972 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004973
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004974 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004975 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004976 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004977 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004978 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004979}
4980
4981/**
4982 * snd_hda_power_save - Power-up/down/sync the codec
4983 * @codec: HD-audio codec
4984 * @delta: the counter delta to change
4985 *
4986 * Change the power-up counter via @delta, and power up or down the hardware
4987 * appropriately. For the power-down, queue to the delayed action.
4988 * Passing zero to @delta means to synchronize the power state.
4989 */
4990void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4991{
4992 spin_lock(&codec->power_lock);
4993 codec->power_count += delta;
4994 trace_hda_power_count(codec);
4995 if (delta > 0)
4996 __snd_hda_power_up(codec, d3wait);
4997 else
4998 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004999 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005000}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005001EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005002
Takashi Iwaid5191e52009-11-16 14:58:17 +01005003/**
5004 * snd_hda_check_amp_list_power - Check the amp list and update the power
5005 * @codec: HD-audio codec
5006 * @check: the object containing an AMP list and the status
5007 * @nid: NID to check / update
5008 *
5009 * Check whether the given NID is in the amp list. If it's in the list,
5010 * check the current AMP status, and update the the power-status according
5011 * to the mute status.
5012 *
5013 * This function is supposed to be set or called from the check_power_status
5014 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005015 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005016int snd_hda_check_amp_list_power(struct hda_codec *codec,
5017 struct hda_loopback_check *check,
5018 hda_nid_t nid)
5019{
Takashi Iwai031024e2011-05-02 11:29:30 +02005020 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005021 int ch, v;
5022
5023 if (!check->amplist)
5024 return 0;
5025 for (p = check->amplist; p->nid; p++) {
5026 if (p->nid == nid)
5027 break;
5028 }
5029 if (!p->nid)
5030 return 0; /* nothing changed */
5031
5032 for (p = check->amplist; p->nid; p++) {
5033 for (ch = 0; ch < 2; ch++) {
5034 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5035 p->idx);
5036 if (!(v & HDA_AMP_MUTE) && v > 0) {
5037 if (!check->power_on) {
5038 check->power_on = 1;
5039 snd_hda_power_up(codec);
5040 }
5041 return 1;
5042 }
5043 }
5044 }
5045 if (check->power_on) {
5046 check->power_on = 0;
5047 snd_hda_power_down(codec);
5048 }
5049 return 0;
5050}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005051EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005052#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005054/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005055 * Channel mode helper
5056 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005057
5058/**
5059 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5060 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005061int snd_hda_ch_mode_info(struct hda_codec *codec,
5062 struct snd_ctl_elem_info *uinfo,
5063 const struct hda_channel_mode *chmode,
5064 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005065{
5066 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5067 uinfo->count = 1;
5068 uinfo->value.enumerated.items = num_chmodes;
5069 if (uinfo->value.enumerated.item >= num_chmodes)
5070 uinfo->value.enumerated.item = num_chmodes - 1;
5071 sprintf(uinfo->value.enumerated.name, "%dch",
5072 chmode[uinfo->value.enumerated.item].channels);
5073 return 0;
5074}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005075EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005076
Takashi Iwaid5191e52009-11-16 14:58:17 +01005077/**
5078 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5079 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005080int snd_hda_ch_mode_get(struct hda_codec *codec,
5081 struct snd_ctl_elem_value *ucontrol,
5082 const struct hda_channel_mode *chmode,
5083 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005084 int max_channels)
5085{
5086 int i;
5087
5088 for (i = 0; i < num_chmodes; i++) {
5089 if (max_channels == chmode[i].channels) {
5090 ucontrol->value.enumerated.item[0] = i;
5091 break;
5092 }
5093 }
5094 return 0;
5095}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005096EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005097
Takashi Iwaid5191e52009-11-16 14:58:17 +01005098/**
5099 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5100 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005101int snd_hda_ch_mode_put(struct hda_codec *codec,
5102 struct snd_ctl_elem_value *ucontrol,
5103 const struct hda_channel_mode *chmode,
5104 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005105 int *max_channelsp)
5106{
5107 unsigned int mode;
5108
5109 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005110 if (mode >= num_chmodes)
5111 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005112 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005113 return 0;
5114 /* change the current channel setting */
5115 *max_channelsp = chmode[mode].channels;
5116 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005117 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005118 return 1;
5119}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005120EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122/*
5123 * input MUX helper
5124 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005125
5126/**
5127 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5128 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005129int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5130 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131{
5132 unsigned int index;
5133
5134 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5135 uinfo->count = 1;
5136 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005137 if (!imux->num_items)
5138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 index = uinfo->value.enumerated.item;
5140 if (index >= imux->num_items)
5141 index = imux->num_items - 1;
5142 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5143 return 0;
5144}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005145EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Takashi Iwaid5191e52009-11-16 14:58:17 +01005147/**
5148 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5149 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005150int snd_hda_input_mux_put(struct hda_codec *codec,
5151 const struct hda_input_mux *imux,
5152 struct snd_ctl_elem_value *ucontrol,
5153 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 unsigned int *cur_val)
5155{
5156 unsigned int idx;
5157
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005158 if (!imux->num_items)
5159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 idx = ucontrol->value.enumerated.item[0];
5161 if (idx >= imux->num_items)
5162 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005163 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005165 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5166 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 *cur_val = idx;
5168 return 1;
5169}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005170EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
5172
5173/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005174 * process kcontrol info callback of a simple string enum array
5175 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5176 */
5177int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5178 struct snd_ctl_elem_info *uinfo,
5179 int num_items, const char * const *texts)
5180{
5181 static const char * const texts_default[] = {
5182 "Disabled", "Enabled"
5183 };
5184
5185 if (!texts || !num_items) {
5186 num_items = 2;
5187 texts = texts_default;
5188 }
5189
Takashi Iwai3ff72212014-10-20 18:17:28 +02005190 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005191}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005192EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005193
5194/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 * Multi-channel / digital-out PCM helper functions
5196 */
5197
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005198/* setup SPDIF output stream */
5199static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5200 unsigned int stream_tag, unsigned int format)
5201{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005202 struct hda_spdif_out *spdif;
5203 unsigned int curr_fmt;
5204 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005205
Laurence Darby3bef1c32012-11-03 17:00:06 +00005206 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5207 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5208 AC_VERB_GET_STREAM_FORMAT, 0);
5209 reset = codec->spdif_status_reset &&
5210 (spdif->ctls & AC_DIG1_ENABLE) &&
5211 curr_fmt != format;
5212
5213 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5214 updated */
5215 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005216 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005217 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005218 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005219 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005220 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005221 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005222 for (d = codec->slave_dig_outs; *d; d++)
5223 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5224 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005225 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005226 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005227 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005228 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005229 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005230}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005231
Takashi Iwai2f728532008-09-25 16:32:41 +02005232static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5233{
5234 snd_hda_codec_cleanup_stream(codec, nid);
5235 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005236 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005237 for (d = codec->slave_dig_outs; *d; d++)
5238 snd_hda_codec_cleanup_stream(codec, *d);
5239 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005240}
5241
Takashi Iwaid5191e52009-11-16 14:58:17 +01005242/**
5243 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5244 * @bus: HD-audio bus
5245 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005246void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5247{
5248 struct hda_codec *codec;
5249
5250 if (!bus)
5251 return;
5252 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005253 if (hda_codec_is_power_on(codec) &&
5254 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005255 codec->patch_ops.reboot_notify(codec);
5256 }
5257}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005258EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005259
Takashi Iwaid5191e52009-11-16 14:58:17 +01005260/**
5261 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005263int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5264 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265{
Ingo Molnar62932df2006-01-16 16:34:20 +01005266 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005267 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5268 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005269 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005271 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 return 0;
5273}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005274EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Takashi Iwaid5191e52009-11-16 14:58:17 +01005276/**
5277 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5278 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005279int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5280 struct hda_multi_out *mout,
5281 unsigned int stream_tag,
5282 unsigned int format,
5283 struct snd_pcm_substream *substream)
5284{
5285 mutex_lock(&codec->spdif_mutex);
5286 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5287 mutex_unlock(&codec->spdif_mutex);
5288 return 0;
5289}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005290EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005291
Takashi Iwaid5191e52009-11-16 14:58:17 +01005292/**
5293 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5294 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005295int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5296 struct hda_multi_out *mout)
5297{
5298 mutex_lock(&codec->spdif_mutex);
5299 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5300 mutex_unlock(&codec->spdif_mutex);
5301 return 0;
5302}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005303EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005304
Takashi Iwaid5191e52009-11-16 14:58:17 +01005305/**
5306 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005308int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5309 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310{
Ingo Molnar62932df2006-01-16 16:34:20 +01005311 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005313 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 return 0;
5315}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005316EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
Takashi Iwaid5191e52009-11-16 14:58:17 +01005318/**
5319 * snd_hda_multi_out_analog_open - open analog outputs
5320 *
5321 * Open analog outputs and set up the hw-constraints.
5322 * If the digital outputs can be opened as slave, open the digital
5323 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005325int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5326 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005327 struct snd_pcm_substream *substream,
5328 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
Takashi Iwai9a081602008-02-12 18:37:26 +01005330 struct snd_pcm_runtime *runtime = substream->runtime;
5331 runtime->hw.channels_max = mout->max_channels;
5332 if (mout->dig_out_nid) {
5333 if (!mout->analog_rates) {
5334 mout->analog_rates = hinfo->rates;
5335 mout->analog_formats = hinfo->formats;
5336 mout->analog_maxbps = hinfo->maxbps;
5337 } else {
5338 runtime->hw.rates = mout->analog_rates;
5339 runtime->hw.formats = mout->analog_formats;
5340 hinfo->maxbps = mout->analog_maxbps;
5341 }
5342 if (!mout->spdif_rates) {
5343 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5344 &mout->spdif_rates,
5345 &mout->spdif_formats,
5346 &mout->spdif_maxbps);
5347 }
5348 mutex_lock(&codec->spdif_mutex);
5349 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005350 if ((runtime->hw.rates & mout->spdif_rates) &&
5351 (runtime->hw.formats & mout->spdif_formats)) {
5352 runtime->hw.rates &= mout->spdif_rates;
5353 runtime->hw.formats &= mout->spdif_formats;
5354 if (mout->spdif_maxbps < hinfo->maxbps)
5355 hinfo->maxbps = mout->spdif_maxbps;
5356 } else {
5357 mout->share_spdif = 0;
5358 /* FIXME: need notify? */
5359 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005360 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005361 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5364 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5365}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005366EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Takashi Iwaid5191e52009-11-16 14:58:17 +01005368/**
5369 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5370 *
5371 * Set up the i/o for analog out.
5372 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005374int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5375 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 unsigned int stream_tag,
5377 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005378 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379{
Takashi Iwaidda14412011-05-02 11:29:30 +02005380 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005382 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 int i;
5384
Ingo Molnar62932df2006-01-16 16:34:20 +01005385 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005386 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005387 if (mout->dig_out_nid && mout->share_spdif &&
5388 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005390 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5391 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005392 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005394 setup_dig_out_stream(codec, mout->dig_out_nid,
5395 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 } else {
5397 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005398 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 }
5400 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005401 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
5403 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005404 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5405 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005406 if (!mout->no_share_stream &&
5407 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005409 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5410 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005411 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005412 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5413 if (!mout->no_share_stream && mout->hp_out_nid[i])
5414 snd_hda_codec_setup_stream(codec,
5415 mout->hp_out_nid[i],
5416 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 /* surrounds */
5419 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005420 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005421 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5422 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005423 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005424 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5425 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005427
5428 /* extra surrounds */
5429 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5430 int ch = 0;
5431 if (!mout->extra_out_nid[i])
5432 break;
5433 if (chs >= (i + 1) * 2)
5434 ch = i * 2;
5435 else if (!mout->no_share_stream)
5436 break;
5437 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5438 stream_tag, ch, format);
5439 }
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 return 0;
5442}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005443EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Takashi Iwaid5191e52009-11-16 14:58:17 +01005445/**
5446 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005448int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5449 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Takashi Iwaidda14412011-05-02 11:29:30 +02005451 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 int i;
5453
5454 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005455 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005457 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005458 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5459 if (mout->hp_out_nid[i])
5460 snd_hda_codec_cleanup_stream(codec,
5461 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005462 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5463 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005464 snd_hda_codec_cleanup_stream(codec,
5465 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005466 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005468 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 mout->dig_out_used = 0;
5470 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005471 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 return 0;
5473}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005474EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Takashi Iwai47408602012-04-20 13:06:53 +02005476/**
5477 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5478 *
5479 * Guess the suitable VREF pin bits to be set as the pin-control value.
5480 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5481 */
5482unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5483{
5484 unsigned int pincap;
5485 unsigned int oldval;
5486 oldval = snd_hda_codec_read(codec, pin, 0,
5487 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5488 pincap = snd_hda_query_pin_caps(codec, pin);
5489 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5490 /* Exception: if the default pin setup is vref50, we give it priority */
5491 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5492 return AC_PINCTL_VREF_80;
5493 else if (pincap & AC_PINCAP_VREF_50)
5494 return AC_PINCTL_VREF_50;
5495 else if (pincap & AC_PINCAP_VREF_100)
5496 return AC_PINCTL_VREF_100;
5497 else if (pincap & AC_PINCAP_VREF_GRD)
5498 return AC_PINCTL_VREF_GRD;
5499 return AC_PINCTL_VREF_HIZ;
5500}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005501EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005502
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005503/* correct the pin ctl value for matching with the pin cap */
5504unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5505 hda_nid_t pin, unsigned int val)
5506{
5507 static unsigned int cap_lists[][2] = {
5508 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5509 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5510 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5511 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5512 };
5513 unsigned int cap;
5514
5515 if (!val)
5516 return 0;
5517 cap = snd_hda_query_pin_caps(codec, pin);
5518 if (!cap)
5519 return val; /* don't know what to do... */
5520
5521 if (val & AC_PINCTL_OUT_EN) {
5522 if (!(cap & AC_PINCAP_OUT))
5523 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5524 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5525 val &= ~AC_PINCTL_HP_EN;
5526 }
5527
5528 if (val & AC_PINCTL_IN_EN) {
5529 if (!(cap & AC_PINCAP_IN))
5530 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5531 else {
5532 unsigned int vcap, vref;
5533 int i;
5534 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5535 vref = val & AC_PINCTL_VREFEN;
5536 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5537 if (vref == cap_lists[i][0] &&
5538 !(vcap & cap_lists[i][1])) {
5539 if (i == ARRAY_SIZE(cap_lists) - 1)
5540 vref = AC_PINCTL_VREF_HIZ;
5541 else
5542 vref = cap_lists[i + 1][0];
5543 }
5544 }
5545 val &= ~AC_PINCTL_VREFEN;
5546 val |= vref;
5547 }
5548 }
5549
5550 return val;
5551}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005552EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005553
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005554int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5555 unsigned int val, bool cached)
5556{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005557 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005558 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005559 if (cached)
5560 return snd_hda_codec_update_cache(codec, pin, 0,
5561 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5562 else
5563 return snd_hda_codec_write(codec, pin, 0,
5564 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5565}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005566EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005567
Takashi Iwai990061c2010-09-09 22:08:44 +02005568/**
5569 * snd_hda_add_imux_item - Add an item to input_mux
5570 *
5571 * When the same label is used already in the existing items, the number
5572 * suffix is appended to the label. This label index number is stored
5573 * to type_idx when non-NULL pointer is given.
5574 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005575int snd_hda_add_imux_item(struct hda_codec *codec,
5576 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005577 int index, int *type_idx)
5578{
5579 int i, label_idx = 0;
5580 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005581 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005582 return -EINVAL;
5583 }
5584 for (i = 0; i < imux->num_items; i++) {
5585 if (!strncmp(label, imux->items[i].label, strlen(label)))
5586 label_idx++;
5587 }
5588 if (type_idx)
5589 *type_idx = label_idx;
5590 if (label_idx > 0)
5591 snprintf(imux->items[imux->num_items].label,
5592 sizeof(imux->items[imux->num_items].label),
5593 "%s %d", label, label_idx);
5594 else
5595 strlcpy(imux->items[imux->num_items].label, label,
5596 sizeof(imux->items[imux->num_items].label));
5597 imux->items[imux->num_items].index = index;
5598 imux->num_items++;
5599 return 0;
5600}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005601EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005602
Takashi Iwai4a471b72005-12-07 13:56:29 +01005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604#ifdef CONFIG_PM
5605/*
5606 * power management
5607 */
5608
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005609
5610static void hda_async_suspend(void *data, async_cookie_t cookie)
5611{
5612 hda_call_codec_suspend(data, false);
5613}
5614
5615static void hda_async_resume(void *data, async_cookie_t cookie)
5616{
5617 hda_call_codec_resume(data);
5618}
5619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620/**
5621 * snd_hda_suspend - suspend the codecs
5622 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 *
5624 * Returns 0 if successful.
5625 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005626int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005628 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005629 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Takashi Iwai0ba21762007-04-16 11:29:14 +02005631 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005632 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005633 if (hda_codec_is_power_on(codec)) {
5634 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005635 async_schedule_domain(hda_async_suspend, codec,
5636 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005637 else
5638 hda_call_codec_suspend(codec, false);
5639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005641
5642 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005643 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 return 0;
5646}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005647EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
5649/**
5650 * snd_hda_resume - resume the codecs
5651 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 *
5653 * Returns 0 if successful.
5654 */
5655int snd_hda_resume(struct hda_bus *bus)
5656{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005657 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005658 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Takashi Iwai0ba21762007-04-16 11:29:14 +02005660 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005661 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005662 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005663 else
5664 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005666
5667 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005668 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 return 0;
5671}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005672EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005673#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005674
5675/*
5676 * generic arrays
5677 */
5678
Takashi Iwaid5191e52009-11-16 14:58:17 +01005679/**
5680 * snd_array_new - get a new element from the given array
5681 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005682 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005683 * Get a new element from the given array. If it exceeds the
5684 * pre-allocated array size, re-allocate the array.
5685 *
5686 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005687 */
5688void *snd_array_new(struct snd_array *array)
5689{
Takashi Iwai12f17712012-10-10 08:59:14 +02005690 if (snd_BUG_ON(!array->elem_size))
5691 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005692 if (array->used >= array->alloced) {
5693 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005694 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005695 void *nlist;
5696 if (snd_BUG_ON(num >= 4096))
5697 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005698 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005699 if (!nlist)
5700 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005701 array->list = nlist;
5702 array->alloced = num;
5703 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005704 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005705}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005706EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005707
Takashi Iwaid5191e52009-11-16 14:58:17 +01005708/**
5709 * snd_array_free - free the given array elements
5710 * @array: the array object
5711 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005712void snd_array_free(struct snd_array *array)
5713{
5714 kfree(array->list);
5715 array->used = 0;
5716 array->alloced = 0;
5717 array->list = NULL;
5718}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005719EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005720
Takashi Iwaid5191e52009-11-16 14:58:17 +01005721/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005722 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5723 * @pcm: PCM caps bits
5724 * @buf: the string buffer to write
5725 * @buflen: the max buffer length
5726 *
5727 * used by hda_proc.c and hda_eld.c
5728 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005729void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5730{
5731 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5732 int i, j;
5733
5734 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5735 if (pcm & (AC_SUPPCM_BITS_8 << i))
5736 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5737
5738 buf[j] = '\0'; /* necessary when j == 0 */
5739}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005740EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005741
5742MODULE_DESCRIPTION("HDA codec core");
5743MODULE_LICENSE("GPL");