blob: e15254204c72a4b1e34a04ad10d8a2fc83b1e1ca [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
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100419 * @listp: the pointer to store NID list
420 *
421 * Parses the connection list of the given widget and stores the pointer
422 * to the list of NIDs.
423 *
424 * Returns the number of connections, or a negative error code.
425 *
426 * Note that the returned pointer isn't protected against the list
427 * modification. If snd_hda_override_conn_list() might be called
428 * concurrently, protect with a mutex appropriately.
429 */
430int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
431 const hda_nid_t **listp)
432{
433 bool added = false;
434
435 for (;;) {
436 int err;
437 const struct hda_conn_list *p;
438
439 /* if the connection-list is already cached, read it */
440 p = lookup_conn_list(codec, nid);
441 if (p) {
442 if (listp)
443 *listp = p->conns;
444 return p->len;
445 }
446 if (snd_BUG_ON(added))
447 return -EINVAL;
448
449 err = read_and_add_raw_conns(codec, nid);
450 if (err < 0)
451 return err;
452 added = true;
453 }
454}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100455EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100456
457/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200458 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * @codec: the HDA codec
460 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200461 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * @max_conns: max. number of connections to store
463 *
464 * Parses the connection list of the given widget and stores the list
465 * of NIDs.
466 *
467 * Returns the number of connections, or a negative error code.
468 */
469int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200471{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 const hda_nid_t *list;
473 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 if (len > 0 && conn_list) {
476 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100477 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200478 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200479 return -EINVAL;
480 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100481 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200482 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200483
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100484 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200485}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100486EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200487
Takashi Iwai4eea3092013-02-07 18:18:19 +0100488/* return CONNLIST_LEN parameter of the given widget */
489static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
490{
491 unsigned int wcaps = get_wcaps(codec, nid);
492 unsigned int parm;
493
494 if (!(wcaps & AC_WCAP_CONN_LIST) &&
495 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
496 return 0;
497
498 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
499 if (parm == -1)
500 parm = 0;
501 return parm;
502}
503
504int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
505{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100506 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100507}
508
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200509/**
510 * snd_hda_get_raw_connections - copy connection list without cache
511 * @codec: the HDA codec
512 * @nid: NID to parse
513 * @conn_list: connection list array
514 * @max_conns: max. number of connections to store
515 *
516 * Like snd_hda_get_connections(), copy the connection list but without
517 * checking through the connection-list cache.
518 * Currently called only from hda_proc.c, so not exported.
519 */
520int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
521 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100524 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100526 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100527 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Takashi Iwai4eea3092013-02-07 18:18:19 +0100529 parm = get_num_conns(codec, nid);
530 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200531 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (parm & AC_CLIST_LONG) {
534 /* long form */
535 shift = 16;
536 num_elems = 2;
537 } else {
538 /* short form */
539 shift = 8;
540 num_elems = 4;
541 }
542 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 mask = (1 << (shift-1)) - 1;
544
Takashi Iwai0ba21762007-04-16 11:29:14 +0200545 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0; /* no connection */
547
548 if (conn_len == 1) {
549 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200550 parm = snd_hda_codec_read(codec, nid, 0,
551 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200552 if (parm == -1 && codec->bus->rirb_error)
553 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100554 if (conn_list)
555 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 1;
557 }
558
559 /* multi connection */
560 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100561 prev_nid = 0;
562 for (i = 0; i < conn_len; i++) {
563 int range_val;
564 hda_nid_t val, n;
565
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200566 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100567 parm = snd_hda_codec_read(codec, nid, 0,
568 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200569 if (parm == -1 && codec->bus->rirb_error)
570 return -EIO;
571 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200572 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100573 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100574 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100575 codec_dbg(codec,
576 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200577 nid, i, parm);
578 return 0;
579 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 parm >>= shift;
581 if (range_val) {
582 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200583 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100584 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200585 "invalid dep_range_val %x:%x\n",
586 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100587 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100589 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100590 if (conn_list) {
591 if (conns >= max_conns)
592 return -ENOSPC;
593 conn_list[conns] = n;
594 }
595 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100596 }
597 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100598 if (conn_list) {
599 if (conns >= max_conns)
600 return -ENOSPC;
601 conn_list[conns] = val;
602 }
603 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100605 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 return conns;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200611 * snd_hda_override_conn_list - add/modify the connection-list to cache
612 * @codec: the HDA codec
613 * @nid: NID to parse
614 * @len: number of connection list entries
615 * @list: the list of connection entries
616 *
617 * Add or modify the given connection-list to the cache. If the corresponding
618 * cache already exists, invalidate it and append a new one.
619 *
620 * Returns zero or a negative error code.
621 */
622int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
623 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100625 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200626
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100627 p = lookup_conn_list(codec, nid);
628 if (p) {
629 list_del(&p->list);
630 kfree(p);
631 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200632
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100633 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100635EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200636
637/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200638 * snd_hda_get_conn_index - get the connection index of the given NID
639 * @codec: the HDA codec
640 * @mux: NID containing the list
641 * @nid: NID to select
642 * @recursive: 1 when searching NID recursively, otherwise 0
643 *
644 * Parses the connection list of the widget @mux and checks whether the
645 * widget @nid is present. If it is, return the connection index.
646 * Otherwise it returns -1.
647 */
648int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
649 hda_nid_t nid, int recursive)
650{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100651 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200652 int i, nums;
653
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100654 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200655 for (i = 0; i < nums; i++)
656 if (conn[i] == nid)
657 return i;
658 if (!recursive)
659 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100660 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100661 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 return -1;
663 }
664 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200665 for (i = 0; i < nums; i++) {
666 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
667 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
668 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200669 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
670 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200671 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200672 return -1;
673}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100674EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Mengdong Linf1aa0682013-08-26 21:35:21 -0400676
677/* return DEVLIST_LEN parameter of the given widget */
678static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
679{
680 unsigned int wcaps = get_wcaps(codec, nid);
681 unsigned int parm;
682
683 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
684 get_wcaps_type(wcaps) != AC_WID_PIN)
685 return 0;
686
687 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
688 if (parm == -1 && codec->bus->rirb_error)
689 parm = 0;
690 return parm & AC_DEV_LIST_LEN_MASK;
691}
692
693/**
694 * snd_hda_get_devices - copy device list without cache
695 * @codec: the HDA codec
696 * @nid: NID of the pin to parse
697 * @dev_list: device list array
698 * @max_devices: max. number of devices to store
699 *
700 * Copy the device list. This info is dynamic and so not cached.
701 * Currently called only from hda_proc.c, so not exported.
702 */
703int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
704 u8 *dev_list, int max_devices)
705{
706 unsigned int parm;
707 int i, dev_len, devices;
708
709 parm = get_num_devices(codec, nid);
710 if (!parm) /* not multi-stream capable */
711 return 0;
712
713 dev_len = parm + 1;
714 dev_len = dev_len < max_devices ? dev_len : max_devices;
715
716 devices = 0;
717 while (devices < dev_len) {
718 parm = snd_hda_codec_read(codec, nid, 0,
719 AC_VERB_GET_DEVICE_LIST, devices);
720 if (parm == -1 && codec->bus->rirb_error)
721 break;
722
723 for (i = 0; i < 8; i++) {
724 dev_list[devices] = (u8)parm;
725 parm >>= 4;
726 devices++;
727 if (devices >= dev_len)
728 break;
729 }
730 }
731 return devices;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * snd_hda_queue_unsol_event - add an unsolicited event to queue
736 * @bus: the BUS
737 * @res: unsolicited event (lower 32bit of RIRB entry)
738 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
739 *
740 * Adds the given event to the queue. The events are processed in
741 * the workqueue asynchronously. Call this function in the interrupt
742 * hanlder when RIRB receives an unsolicited event.
743 *
744 * Returns 0 if successful, or a negative error code.
745 */
746int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
747{
748 struct hda_bus_unsolicited *unsol;
749 unsigned int wp;
750
Wang YanQing2195b062013-05-07 11:27:33 +0800751 if (!bus || !bus->workq)
752 return 0;
753
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200754 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200755 unsol = bus->unsol;
756 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758
759 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
760 unsol->wp = wp;
761
762 wp <<= 1;
763 unsol->queue[wp] = res;
764 unsol->queue[wp + 1] = res_ex;
765
Takashi Iwai6acaed32009-01-12 10:09:24 +0100766 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return 0;
769}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100770EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800773 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
David Howellsc4028952006-11-22 14:57:56 +0000775static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
David Howellsc4028952006-11-22 14:57:56 +0000777 struct hda_bus_unsolicited *unsol =
778 container_of(work, struct hda_bus_unsolicited, work);
779 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct hda_codec *codec;
781 unsigned int rp, caddr, res;
782
783 while (unsol->rp != unsol->wp) {
784 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
785 unsol->rp = rp;
786 rp <<= 1;
787 res = unsol->queue[rp];
788 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200789 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 continue;
791 codec = bus->caddr_tbl[caddr & 0x0f];
792 if (codec && codec->patch_ops.unsol_event)
793 codec->patch_ops.unsol_event(codec, res);
794 }
795}
796
797/*
798 * initialize unsolicited queue
799 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200800static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct hda_bus_unsolicited *unsol;
803
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100804 if (bus->unsol) /* already initialized */
805 return 0;
806
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200807 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200808 if (!unsol) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100809 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -ENOMEM;
811 }
David Howellsc4028952006-11-22 14:57:56 +0000812 INIT_WORK(&unsol->work, process_unsol_events);
813 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 bus->unsol = unsol;
815 return 0;
816}
817
818/*
819 * destructor
820 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100821static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200823 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100824 return;
825
826 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100827 if (bus->workq)
828 flush_workqueue(bus->workq);
829 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 kfree(bus->unsol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (bus->ops.private_free)
832 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100833 if (bus->workq)
834 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100839static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Takashi Iwai2565c892014-02-19 11:41:09 +0100841 snd_hda_bus_free(device->device_data);
842 return 0;
843}
844
845static int snd_hda_bus_dev_disconnect(struct snd_device *device)
846{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100848 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/**
853 * snd_hda_bus_new - create a HDA bus
854 * @card: the card entry
855 * @temp: the template for hda_bus information
856 * @busp: the pointer to store the created bus instance
857 *
858 * Returns 0 if successful, or a negative error code.
859 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100860int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200861 const struct hda_bus_template *temp,
862 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct hda_bus *bus;
865 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100866 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100867 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 .dev_free = snd_hda_bus_dev_free,
869 };
870
Takashi Iwaida3cec32008-08-08 17:12:14 +0200871 if (snd_BUG_ON(!temp))
872 return -EINVAL;
873 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (busp)
877 *busp = NULL;
878
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200879 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100881 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -ENOMEM;
883 }
884
885 bus->card = card;
886 bus->private_data = temp->private_data;
887 bus->pci = temp->pci;
888 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100889 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 bus->ops = temp->ops;
891
Ingo Molnar62932df2006-01-16 16:34:20 +0100892 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200893 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 INIT_LIST_HEAD(&bus->codec_list);
895
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100896 snprintf(bus->workq_name, sizeof(bus->workq_name),
897 "hd-audio%d", card->number);
898 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100899 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100900 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100901 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100902 kfree(bus);
903 return -ENOMEM;
904 }
905
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
907 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 snd_hda_bus_free(bus);
909 return err;
910 }
911 if (busp)
912 *busp = bus;
913 return 0;
914}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100915EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100917#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200918#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200919 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200920#else
921#define is_generic_config(codec) 0
922#endif
923
Takashi Iwai645f10c2008-11-28 15:07:37 +0100924#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100925#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
926#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100927#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100928#endif
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * find a matching codec preset
932 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200933static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200934find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100936 struct hda_codec_preset_list *tbl;
937 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200938 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100940 again:
941 mutex_lock(&preset_mutex);
942 list_for_each_entry(tbl, &hda_preset_tables, list) {
943 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100944 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100945 continue;
946 }
947 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100949 if (preset->afg && preset->afg != codec->afg)
950 continue;
951 if (preset->mfg && preset->mfg != codec->mfg)
952 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200953 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200955 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200956 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100957 preset->rev == codec->revision_id)) {
958 mutex_unlock(&preset_mutex);
959 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100963 module_put(tbl->owner);
964 }
965 mutex_unlock(&preset_mutex);
966
967 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100968 if (!mod_requested)
Takashi Iwai66797f32014-10-27 15:15:44 +0100969 request_module("snd-hda-codec-id:%08x",
970 codec->vendor_id);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100971 else
Takashi Iwai66797f32014-10-27 15:15:44 +0100972 request_module("snd-hda-codec-id:%04x*",
973 (codec->vendor_id >> 16) & 0xffff);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100974 mod_requested++;
975 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 return NULL;
978}
979
980/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200981 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200983static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 const struct hda_vendor_id *c;
986 const char *vendor = NULL;
987 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200988 char tmp[16];
989
990 if (codec->vendor_name)
991 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 for (c = hda_vendor_ids; c->id; c++) {
994 if (c->id == vendor_id) {
995 vendor = c->name;
996 break;
997 }
998 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200999 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 sprintf(tmp, "Generic %04x", vendor_id);
1001 vendor = tmp;
1002 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001003 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1004 if (!codec->vendor_name)
1005 return -ENOMEM;
1006
1007 get_chip_name:
1008 if (codec->chip_name)
1009 return 0;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001012 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1013 else {
1014 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1015 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1016 }
1017 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001018 return -ENOMEM;
1019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001023 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001025static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001027 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 hda_nid_t nid;
1029
1030 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1031 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001032 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001033 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001034 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001035 case AC_GRP_AUDIO_FUNCTION:
1036 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001037 codec->afg_function_id = function_id & 0xff;
1038 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001039 break;
1040 case AC_GRP_MODEM_FUNCTION:
1041 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001042 codec->mfg_function_id = function_id & 0xff;
1043 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001044 break;
1045 default:
1046 break;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001052 * read widget caps for each widget and store in cache
1053 */
1054static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1055{
1056 int i;
1057 hda_nid_t nid;
1058
1059 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1060 &codec->start_nid);
1061 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001062 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001063 return -ENOMEM;
1064 nid = codec->start_nid;
1065 for (i = 0; i < codec->num_nodes; i++, nid++)
1066 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1067 AC_PAR_AUDIO_WIDGET_CAP);
1068 return 0;
1069}
1070
Takashi Iwai3be14142009-02-20 14:11:16 +01001071/* read all pin default configurations and save codec->init_pins */
1072static int read_pin_defaults(struct hda_codec *codec)
1073{
1074 int i;
1075 hda_nid_t nid = codec->start_nid;
1076
1077 for (i = 0; i < codec->num_nodes; i++, nid++) {
1078 struct hda_pincfg *pin;
1079 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001080 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001081 if (wid_type != AC_WID_PIN)
1082 continue;
1083 pin = snd_array_new(&codec->init_pins);
1084 if (!pin)
1085 return -ENOMEM;
1086 pin->nid = nid;
1087 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1088 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001089 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1090 AC_VERB_GET_PIN_WIDGET_CONTROL,
1091 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001092 }
1093 return 0;
1094}
1095
1096/* look up the given pin config list and return the item matching with NID */
1097static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1098 struct snd_array *array,
1099 hda_nid_t nid)
1100{
1101 int i;
1102 for (i = 0; i < array->used; i++) {
1103 struct hda_pincfg *pin = snd_array_elem(array, i);
1104 if (pin->nid == nid)
1105 return pin;
1106 }
1107 return NULL;
1108}
1109
Takashi Iwai3be14142009-02-20 14:11:16 +01001110/* set the current pin config value for the given NID.
1111 * the value is cached, and read via snd_hda_codec_get_pincfg()
1112 */
1113int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1114 hda_nid_t nid, unsigned int cfg)
1115{
1116 struct hda_pincfg *pin;
1117
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001118 /* the check below may be invalid when pins are added by a fixup
1119 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1120 * for now
1121 */
1122 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001123 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1124 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001125 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001126
Takashi Iwai3be14142009-02-20 14:11:16 +01001127 pin = look_up_pincfg(codec, list, nid);
1128 if (!pin) {
1129 pin = snd_array_new(list);
1130 if (!pin)
1131 return -ENOMEM;
1132 pin->nid = nid;
1133 }
1134 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001135 return 0;
1136}
1137
Takashi Iwaid5191e52009-11-16 14:58:17 +01001138/**
1139 * snd_hda_codec_set_pincfg - Override a pin default configuration
1140 * @codec: the HDA codec
1141 * @nid: NID to set the pin config
1142 * @cfg: the pin default config value
1143 *
1144 * Override a pin default configuration value in the cache.
1145 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1146 * priority than the real hardware value.
1147 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001148int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1149 hda_nid_t nid, unsigned int cfg)
1150{
Takashi Iwai346ff702009-02-23 09:42:57 +01001151 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001152}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001153EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001154
Takashi Iwaid5191e52009-11-16 14:58:17 +01001155/**
1156 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1157 * @codec: the HDA codec
1158 * @nid: NID to get the pin config
1159 *
1160 * Get the current pin config value of the given pin NID.
1161 * If the pincfg value is cached or overridden via sysfs or driver,
1162 * returns the cached value.
1163 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001164unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1165{
1166 struct hda_pincfg *pin;
1167
Takashi Iwai648a8d22014-02-25 10:38:13 +01001168#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001169 {
1170 unsigned int cfg = 0;
1171 mutex_lock(&codec->user_mutex);
1172 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1173 if (pin)
1174 cfg = pin->cfg;
1175 mutex_unlock(&codec->user_mutex);
1176 if (cfg)
1177 return cfg;
1178 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001179#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001180 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1181 if (pin)
1182 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001183 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1184 if (pin)
1185 return pin->cfg;
1186 return 0;
1187}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001188EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001189
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001190/* remember the current pinctl target value */
1191int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1192 unsigned int val)
1193{
1194 struct hda_pincfg *pin;
1195
1196 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1197 if (!pin)
1198 return -EINVAL;
1199 pin->target = val;
1200 return 0;
1201}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001202EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001203
1204/* return the current pinctl target value */
1205int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1206{
1207 struct hda_pincfg *pin;
1208
1209 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1210 if (!pin)
1211 return 0;
1212 return pin->target;
1213}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001214EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001215
Takashi Iwai92ee6162009-12-27 11:18:59 +01001216/**
1217 * snd_hda_shutup_pins - Shut up all pins
1218 * @codec: the HDA codec
1219 *
1220 * Clear all pin controls to shup up before suspend for avoiding click noise.
1221 * The controls aren't cached so that they can be resumed properly.
1222 */
1223void snd_hda_shutup_pins(struct hda_codec *codec)
1224{
1225 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001226 /* don't shut up pins when unloading the driver; otherwise it breaks
1227 * the default pin setup at the next load of the driver
1228 */
1229 if (codec->bus->shutdown)
1230 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001231 for (i = 0; i < codec->init_pins.used; i++) {
1232 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1233 /* use read here for syncing after issuing each verb */
1234 snd_hda_codec_read(codec, pin->nid, 0,
1235 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1236 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001237 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001238}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001239EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001240
Takashi Iwai2a439522011-07-26 09:52:50 +02001241#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001242/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1243static void restore_shutup_pins(struct hda_codec *codec)
1244{
1245 int i;
1246 if (!codec->pins_shutup)
1247 return;
1248 if (codec->bus->shutdown)
1249 return;
1250 for (i = 0; i < codec->init_pins.used; i++) {
1251 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1252 snd_hda_codec_write(codec, pin->nid, 0,
1253 AC_VERB_SET_PIN_WIDGET_CONTROL,
1254 pin->ctrl);
1255 }
1256 codec->pins_shutup = 0;
1257}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001258#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001259
David Henningsson26a6cb62012-10-09 15:04:21 +02001260static void hda_jackpoll_work(struct work_struct *work)
1261{
1262 struct hda_codec *codec =
1263 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001264
1265 snd_hda_jack_set_dirty_all(codec);
1266 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001267
1268 if (!codec->jackpoll_interval)
1269 return;
1270
David Henningsson26a6cb62012-10-09 15:04:21 +02001271 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1272 codec->jackpoll_interval);
1273}
1274
Takashi Iwai01751f52007-08-10 16:59:39 +02001275static void init_hda_cache(struct hda_cache_rec *cache,
1276 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001277static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001278
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001279/* release all pincfg lists */
1280static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001281{
Takashi Iwai346ff702009-02-23 09:42:57 +01001282 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001283#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001284 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001285#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001286 snd_array_free(&codec->init_pins);
1287}
1288
Takashi Iwai54d17402005-11-21 16:33:22 +01001289/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001290 * audio-converter setup caches
1291 */
1292struct hda_cvt_setup {
1293 hda_nid_t nid;
1294 u8 stream_tag;
1295 u8 channel_id;
1296 u16 format_id;
1297 unsigned char active; /* cvt is currently used */
1298 unsigned char dirty; /* setups should be cleared */
1299};
1300
1301/* get or create a cache entry for the given audio converter NID */
1302static struct hda_cvt_setup *
1303get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1304{
1305 struct hda_cvt_setup *p;
1306 int i;
1307
1308 for (i = 0; i < codec->cvt_setups.used; i++) {
1309 p = snd_array_elem(&codec->cvt_setups, i);
1310 if (p->nid == nid)
1311 return p;
1312 }
1313 p = snd_array_new(&codec->cvt_setups);
1314 if (p)
1315 p->nid = nid;
1316 return p;
1317}
1318
1319/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001320 * Dynamic symbol binding for the codec parsers
1321 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001322
1323#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001324 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001325
1326static void unload_parser(struct hda_codec *codec)
1327{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001328 if (codec->parser)
1329 symbol_put_addr(codec->parser);
1330 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001331}
1332
1333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * codec destructor
1335 */
1336static void snd_hda_codec_free(struct hda_codec *codec)
1337{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001338 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001340 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001341 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001342 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001343#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001344 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001345 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001348 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001349 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001350 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001351 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001352 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 codec->bus->caddr_tbl[codec->addr] = NULL;
1354 if (codec->patch_ops.free)
1355 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001356 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001357 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001358 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001359 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001360 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001361 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 kfree(codec->vendor_name);
1363 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001364 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001365 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001366 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001367 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001370static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1371 hda_nid_t fg, unsigned int power_state);
1372
Takashi Iwaid8193872012-08-31 07:54:38 -07001373static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001374 unsigned int power_state);
1375
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001376static int snd_hda_codec_dev_register(struct snd_device *device)
1377{
1378 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001379 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001380
Takashi Iwaid604b392014-02-28 13:42:09 +01001381 if (err < 0)
1382 return err;
1383 snd_hda_register_beep_device(codec);
1384 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001385}
1386
1387static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1388{
1389 struct hda_codec *codec = device->device_data;
1390
Takashi Iwaid604b392014-02-28 13:42:09 +01001391 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001392 device_del(&codec->dev);
1393 return 0;
1394}
1395
Takashi Iwai2565c892014-02-19 11:41:09 +01001396static int snd_hda_codec_dev_free(struct snd_device *device)
1397{
1398 snd_hda_codec_free(device->device_data);
1399 return 0;
1400}
1401
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001402/* just free the container */
1403static void snd_hda_codec_dev_release(struct device *dev)
1404{
1405 kfree(container_of(dev, struct hda_codec, dev));
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/**
1409 * snd_hda_codec_new - create a HDA codec
1410 * @bus: the bus to assign
1411 * @codec_addr: the codec address
1412 * @codecp: the pointer to store the generated codec
1413 *
1414 * Returns 0 if successful, or a negative error code.
1415 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001416int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001417 unsigned int codec_addr,
1418 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001421 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001422 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001424 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001425 .dev_register = snd_hda_codec_dev_register,
1426 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001427 .dev_free = snd_hda_codec_dev_free,
1428 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Takashi Iwaida3cec32008-08-08 17:12:14 +02001430 if (snd_BUG_ON(!bus))
1431 return -EINVAL;
1432 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001436 dev_err(bus->card->dev,
1437 "address 0x%x is already occupied\n",
1438 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EBUSY;
1440 }
1441
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001442 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001444 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -ENOMEM;
1446 }
1447
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001448 device_initialize(&codec->dev);
1449 codec->dev.parent = &bus->card->card_dev;
1450 codec->dev.class = sound_class;
1451 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001452 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001453 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1454 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001455 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 codec->bus = bus;
1458 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001459 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001460 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001461 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001462 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001463 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001464 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1465 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001466 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001467 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001468 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001469 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001470 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001471 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001472 INIT_LIST_HEAD(&codec->conn_list);
1473
David Henningsson26a6cb62012-10-09 15:04:21 +02001474 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001475 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001476 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Takashi Iwai83012a72012-08-24 18:38:08 +02001478#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001479 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001480 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1481 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1482 * the caller has to power down appropriatley after initialization
1483 * phase.
1484 */
1485 hda_keep_power_on(codec);
1486#endif
1487
Takashi Iwai648a8d22014-02-25 10:38:13 +01001488 snd_hda_sysfs_init(codec);
1489
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001490 if (codec->bus->modelname) {
1491 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1492 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001493 err = -ENODEV;
1494 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001495 }
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001499 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 bus->caddr_tbl[codec_addr] = codec;
1502
Takashi Iwai0ba21762007-04-16 11:29:14 +02001503 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1504 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001505 if (codec->vendor_id == -1)
1506 /* read again, hopefully the access method was corrected
1507 * in the last read...
1508 */
1509 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1510 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001511 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1512 AC_PAR_SUBSYSTEM_ID);
1513 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001516 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001517 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001518 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001519 err = -ENODEV;
1520 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Takashi Iwaid8193872012-08-31 07:54:38 -07001523 fg = codec->afg ? codec->afg : codec->mfg;
1524 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001525 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001526 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001527 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001528 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001529 err = read_pin_defaults(codec);
1530 if (err < 0)
1531 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001532
Takashi Iwai0ba21762007-04-16 11:29:14 +02001533 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001534 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001535 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001537 }
1538
Takashi Iwai83012a72012-08-24 18:38:08 +02001539#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001540 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001541 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001542#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001543 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001544 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001545#ifdef CONFIG_PM
1546 if (!codec->d3_stop_clk || !codec->epss)
1547 bus->power_keep_link_on = 1;
1548#endif
1549
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001550
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001551 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001552 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001553
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001554 snd_hda_codec_proc_new(codec);
1555
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001556 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001557
1558 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1559 codec->subsystem_id, codec->revision_id);
1560 snd_component_add(codec->bus->card, component);
1561
Takashi Iwai2565c892014-02-19 11:41:09 +01001562 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1563 if (err < 0)
1564 goto error;
1565
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001566 if (codecp)
1567 *codecp = codec;
1568 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001569
1570 error:
1571 snd_hda_codec_free(codec);
1572 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001573}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001574EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001575
Mengdong Lina15d05d2013-02-08 17:09:31 -05001576int snd_hda_codec_update_widgets(struct hda_codec *codec)
1577{
1578 hda_nid_t fg;
1579 int err;
1580
1581 /* Assume the function group node does not change,
1582 * only the widget nodes may change.
1583 */
1584 kfree(codec->wcaps);
1585 fg = codec->afg ? codec->afg : codec->mfg;
1586 err = read_widget_caps(codec, fg);
1587 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001588 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001589 return err;
1590 }
1591
1592 snd_array_free(&codec->init_pins);
1593 err = read_pin_defaults(codec);
1594
1595 return err;
1596}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001597EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001598
1599
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001600#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001601/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1602static bool is_likely_hdmi_codec(struct hda_codec *codec)
1603{
1604 hda_nid_t nid = codec->start_nid;
1605 int i;
1606
1607 for (i = 0; i < codec->num_nodes; i++, nid++) {
1608 unsigned int wcaps = get_wcaps(codec, nid);
1609 switch (get_wcaps_type(wcaps)) {
1610 case AC_WID_AUD_IN:
1611 return false; /* HDMI parser supports only HDMI out */
1612 case AC_WID_AUD_OUT:
1613 if (!(wcaps & AC_WCAP_DIGITAL))
1614 return false;
1615 break;
1616 }
1617 }
1618 return true;
1619}
1620#else
1621/* no HDMI codec parser support */
1622#define is_likely_hdmi_codec(codec) false
1623#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1624
Takashi Iwaid5191e52009-11-16 14:58:17 +01001625/**
1626 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1627 * @codec: the HDA codec
1628 *
1629 * Start parsing of the given codec tree and (re-)initialize the whole
1630 * patch instance.
1631 *
1632 * Returns 0 if successful or a negative error code.
1633 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001634int snd_hda_codec_configure(struct hda_codec *codec)
1635{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001636 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001637 int err;
1638
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001639 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001640 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001641 err = get_codec_name(codec);
1642 if (err < 0)
1643 return err;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001646 if (!is_generic_config(codec) && codec->preset)
1647 patch = codec->preset->patch;
1648 if (!patch) {
1649 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001650 if (is_likely_hdmi_codec(codec)) {
1651#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001652 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001653#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1654 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001655#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001656 }
1657 if (!patch) {
1658#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1659 patch = load_parser(codec, snd_hda_parse_generic_codec);
1660#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1661 patch = snd_hda_parse_generic_codec;
1662#endif
1663 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001664 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001665 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001666 return -ENODEV;
1667 }
Takashi Iwai82467612007-07-27 19:15:54 +02001668 }
1669
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001670 err = patch(codec);
1671 if (err < 0) {
1672 unload_parser(codec);
1673 return err;
1674 }
Takashi Iwai82467612007-07-27 19:15:54 +02001675
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001676 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001677 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001678 if (err < 0)
1679 return err;
1680 }
1681
Takashi Iwaif62faed2009-12-23 09:27:51 +01001682 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001683 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001684 snprintf(codec->bus->card->mixername,
1685 sizeof(codec->bus->card->mixername),
1686 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001689EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Takashi Iwaied360812012-08-08 17:12:52 +02001691/* update the stream-id if changed */
1692static void update_pcm_stream_id(struct hda_codec *codec,
1693 struct hda_cvt_setup *p, hda_nid_t nid,
1694 u32 stream_tag, int channel_id)
1695{
1696 unsigned int oldval, newval;
1697
1698 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1699 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1700 newval = (stream_tag << 4) | channel_id;
1701 if (oldval != newval)
1702 snd_hda_codec_write(codec, nid, 0,
1703 AC_VERB_SET_CHANNEL_STREAMID,
1704 newval);
1705 p->stream_tag = stream_tag;
1706 p->channel_id = channel_id;
1707 }
1708}
1709
1710/* update the format-id if changed */
1711static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1712 hda_nid_t nid, int format)
1713{
1714 unsigned int oldval;
1715
1716 if (p->format_id != format) {
1717 oldval = snd_hda_codec_read(codec, nid, 0,
1718 AC_VERB_GET_STREAM_FORMAT, 0);
1719 if (oldval != format) {
1720 msleep(1);
1721 snd_hda_codec_write(codec, nid, 0,
1722 AC_VERB_SET_STREAM_FORMAT,
1723 format);
1724 }
1725 p->format_id = format;
1726 }
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/**
1730 * snd_hda_codec_setup_stream - set up the codec for streaming
1731 * @codec: the CODEC to set up
1732 * @nid: the NID to set up
1733 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1734 * @channel_id: channel id to pass, zero based.
1735 * @format: stream format.
1736 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001737void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1738 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 int channel_id, int format)
1740{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001741 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001742 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001743 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001744 int i;
1745
Takashi Iwai0ba21762007-04-16 11:29:14 +02001746 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001747 return;
1748
Takashi Iwai4e76a882014-02-25 12:21:03 +01001749 codec_dbg(codec,
1750 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1751 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001752 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001753 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001754 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001755
1756 if (codec->pcm_format_first)
1757 update_pcm_format(codec, p, nid, format);
1758 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1759 if (!codec->pcm_format_first)
1760 update_pcm_format(codec, p, nid, format);
1761
Takashi Iwaieb541332010-08-06 13:48:11 +02001762 p->active = 1;
1763 p->dirty = 0;
1764
1765 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001766 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001767 list_for_each_entry(c, &codec->bus->codec_list, list) {
1768 for (i = 0; i < c->cvt_setups.used; i++) {
1769 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001770 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001771 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001772 p->dirty = 1;
1773 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001776EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Takashi Iwaif0cea792010-08-13 11:56:53 +02001778static void really_cleanup_stream(struct hda_codec *codec,
1779 struct hda_cvt_setup *q);
1780
Takashi Iwaid5191e52009-11-16 14:58:17 +01001781/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001782 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001783 * @codec: the CODEC to clean up
1784 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001785 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001786 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001787void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1788 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001789{
Takashi Iwaieb541332010-08-06 13:48:11 +02001790 struct hda_cvt_setup *p;
1791
Takashi Iwai888afa12008-03-18 09:57:50 +01001792 if (!nid)
1793 return;
1794
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001795 if (codec->no_sticky_stream)
1796 do_now = 1;
1797
Takashi Iwai4e76a882014-02-25 12:21:03 +01001798 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001799 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001800 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001801 /* here we just clear the active flag when do_now isn't set;
1802 * actual clean-ups will be done later in
1803 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1804 */
1805 if (do_now)
1806 really_cleanup_stream(codec, p);
1807 else
1808 p->active = 0;
1809 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001810}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001811EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001812
Takashi Iwaieb541332010-08-06 13:48:11 +02001813static void really_cleanup_stream(struct hda_codec *codec,
1814 struct hda_cvt_setup *q)
1815{
1816 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001817 if (q->stream_tag || q->channel_id)
1818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1819 if (q->format_id)
1820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1821);
Takashi Iwaieb541332010-08-06 13:48:11 +02001822 memset(q, 0, sizeof(*q));
1823 q->nid = nid;
1824}
1825
1826/* clean up the all conflicting obsolete streams */
1827static void purify_inactive_streams(struct hda_codec *codec)
1828{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001829 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001830 int i;
1831
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001832 list_for_each_entry(c, &codec->bus->codec_list, list) {
1833 for (i = 0; i < c->cvt_setups.used; i++) {
1834 struct hda_cvt_setup *p;
1835 p = snd_array_elem(&c->cvt_setups, i);
1836 if (p->dirty)
1837 really_cleanup_stream(c, p);
1838 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001839 }
1840}
1841
Takashi Iwai2a439522011-07-26 09:52:50 +02001842#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001843/* clean up all streams; called from suspend */
1844static void hda_cleanup_all_streams(struct hda_codec *codec)
1845{
1846 int i;
1847
1848 for (i = 0; i < codec->cvt_setups.used; i++) {
1849 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1850 if (p->stream_tag)
1851 really_cleanup_stream(codec, p);
1852 }
1853}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001854#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/*
1857 * amp access functions
1858 */
1859
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001860/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001861#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001862#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001863#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1864#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001866#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001869static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001870 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Takashi Iwai01751f52007-08-10 16:59:39 +02001872 memset(cache, 0, sizeof(*cache));
1873 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001874 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001875}
1876
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001877static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001878{
Takashi Iwai603c4012008-07-30 15:01:44 +02001879 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001883static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Takashi Iwai01751f52007-08-10 16:59:39 +02001885 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1886 u16 cur = cache->hash[idx];
1887 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001890 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (info->key == key)
1892 return info;
1893 cur = info->next;
1894 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001895 return NULL;
1896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001898/* query the hash. allocate an entry if not found. */
1899static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1900 u32 key)
1901{
1902 struct hda_cache_head *info = get_hash(cache, key);
1903 if (!info) {
1904 u16 idx, cur;
1905 /* add a new hash entry */
1906 info = snd_array_new(&cache->buf);
1907 if (!info)
1908 return NULL;
1909 cur = snd_array_index(&cache->buf, info);
1910 info->key = key;
1911 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001912 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001913 idx = key % (u16)ARRAY_SIZE(cache->hash);
1914 info->next = cache->hash[idx];
1915 cache->hash[idx] = cur;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return info;
1918}
1919
Takashi Iwai01751f52007-08-10 16:59:39 +02001920/* query and allocate an amp hash entry */
1921static inline struct hda_amp_info *
1922get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1923{
1924 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1925}
1926
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001927/* overwrite the value with the key in the caps hash */
1928static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1929{
1930 struct hda_amp_info *info;
1931
1932 mutex_lock(&codec->hash_mutex);
1933 info = get_alloc_amp_hash(codec, key);
1934 if (!info) {
1935 mutex_unlock(&codec->hash_mutex);
1936 return -EINVAL;
1937 }
1938 info->amp_caps = val;
1939 info->head.val |= INFO_AMP_CAPS;
1940 mutex_unlock(&codec->hash_mutex);
1941 return 0;
1942}
1943
1944/* query the value from the caps hash; if not found, fetch the current
1945 * value from the given function and store in the hash
1946 */
1947static unsigned int
1948query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1949 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1950{
1951 struct hda_amp_info *info;
1952 unsigned int val;
1953
1954 mutex_lock(&codec->hash_mutex);
1955 info = get_alloc_amp_hash(codec, key);
1956 if (!info) {
1957 mutex_unlock(&codec->hash_mutex);
1958 return 0;
1959 }
1960 if (!(info->head.val & INFO_AMP_CAPS)) {
1961 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1962 val = func(codec, nid, dir);
1963 write_caps_hash(codec, key, val);
1964 } else {
1965 val = info->amp_caps;
1966 mutex_unlock(&codec->hash_mutex);
1967 }
1968 return val;
1969}
1970
1971static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1972 int direction)
1973{
1974 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1975 nid = codec->afg;
1976 return snd_hda_param_read(codec, nid,
1977 direction == HDA_OUTPUT ?
1978 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1979}
1980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
1982 * query_amp_caps - query AMP capabilities
1983 * @codec: the HD-auio codec
1984 * @nid: the NID to query
1985 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1986 *
1987 * Query AMP capabilities for the given widget and direction.
1988 * Returns the obtained capability bits.
1989 *
1990 * When cap bits have been already read, this doesn't read again but
1991 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001993u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001995 return query_caps_hash(codec, nid, direction,
1996 HDA_HASH_KEY(nid, direction, 0),
1997 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001999EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Takashi Iwaid5191e52009-11-16 14:58:17 +01002001/**
David Henningsson861a04e2014-09-23 10:38:17 +02002002 * snd_hda_check_amp_caps - query AMP capabilities
2003 * @codec: the HD-audio codec
2004 * @nid: the NID to query
2005 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002006 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02002007 *
2008 * Check whether the widget has the given amp capability for the direction.
2009 */
2010bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2011 int dir, unsigned int bits)
2012{
2013 if (!nid)
2014 return false;
2015 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2016 if (query_amp_caps(codec, nid, dir) & bits)
2017 return true;
2018 return false;
2019}
2020EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2021
2022/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002023 * snd_hda_override_amp_caps - Override the AMP capabilities
2024 * @codec: the CODEC to clean up
2025 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002026 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01002027 * @caps: the capability bits to set
2028 *
2029 * Override the cached AMP caps bits value by the given one.
2030 * This function is useful if the driver needs to adjust the AMP ranges,
2031 * e.g. limit to 0dB, etc.
2032 *
2033 * Returns zero if successful or a negative error code.
2034 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002035int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2036 unsigned int caps)
2037{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002038 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002039}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002040EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002041
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002042static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2043 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002044{
2045 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2046}
2047
Takashi Iwaid5191e52009-11-16 14:58:17 +01002048/**
2049 * snd_hda_query_pin_caps - Query PIN capabilities
2050 * @codec: the HD-auio codec
2051 * @nid: the NID to query
2052 *
2053 * Query PIN capabilities for the given widget.
2054 * Returns the obtained capability bits.
2055 *
2056 * When cap bits have been already read, this doesn't read again but
2057 * returns the cached value.
2058 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002059u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2060{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002061 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002062 read_pin_cap);
2063}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002064EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002065
Wu Fengguang864f92b2009-11-18 12:38:02 +08002066/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002067 * snd_hda_override_pin_caps - Override the pin capabilities
2068 * @codec: the CODEC
2069 * @nid: the NID to override
2070 * @caps: the capability bits to set
2071 *
2072 * Override the cached PIN capabilitiy bits value by the given one.
2073 *
2074 * Returns zero if successful or a negative error code.
2075 */
2076int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2077 unsigned int caps)
2078{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002079 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002080}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002081EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002082
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002083/* read or sync the hash value with the current value;
2084 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002086static struct hda_amp_info *
2087update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002088 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002090 struct hda_amp_info *info;
2091 unsigned int parm, val = 0;
2092 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002094 retry:
2095 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2096 if (!info)
2097 return NULL;
2098 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2099 if (!val_read) {
2100 mutex_unlock(&codec->hash_mutex);
2101 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2102 parm |= direction == HDA_OUTPUT ?
2103 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2104 parm |= index;
2105 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002106 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002107 val &= 0xff;
2108 val_read = true;
2109 mutex_lock(&codec->hash_mutex);
2110 goto retry;
2111 }
2112 info->vol[ch] = val;
2113 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002114 } else if (init_only)
2115 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002116 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002120 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002122static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002123 hda_nid_t nid, int ch, int direction, int index,
2124 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 u32 parm;
2127
2128 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2129 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2130 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002131 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2132 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002133 ; /* set the zero value as a fake mute */
2134 else
2135 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2137}
2138
Takashi Iwaid5191e52009-11-16 14:58:17 +01002139/**
2140 * snd_hda_codec_amp_read - Read AMP value
2141 * @codec: HD-audio codec
2142 * @nid: NID to read the AMP value
2143 * @ch: channel (left=0 or right=1)
2144 * @direction: #HDA_INPUT or #HDA_OUTPUT
2145 * @index: the index value (only for input direction)
2146 *
2147 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Takashi Iwai834be882006-03-01 14:16:17 +01002149int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2150 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002152 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002153 unsigned int val = 0;
2154
2155 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002156 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002157 if (info)
2158 val = info->vol[ch];
2159 mutex_unlock(&codec->hash_mutex);
2160 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002162EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Takashi Iwai280e57d2012-12-14 10:32:21 +01002164static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2165 int direction, int idx, int mask, int val,
2166 bool init_only)
2167{
2168 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002169 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002170 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002171
2172 if (snd_BUG_ON(mask & ~0xff))
2173 mask &= 0xff;
2174 val &= mask;
2175
2176 mutex_lock(&codec->hash_mutex);
2177 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2178 if (!info) {
2179 mutex_unlock(&codec->hash_mutex);
2180 return 0;
2181 }
2182 val |= info->vol[ch] & ~mask;
2183 if (info->vol[ch] == val) {
2184 mutex_unlock(&codec->hash_mutex);
2185 return 0;
2186 }
2187 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002188 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002189 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002190 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002191 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002192 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002193 return 1;
2194}
2195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_codec_amp_update - update the AMP value
2198 * @codec: HD-audio codec
2199 * @nid: NID to read the AMP value
2200 * @ch: channel (left=0 or right=1)
2201 * @direction: #HDA_INPUT or #HDA_OUTPUT
2202 * @idx: the index value (only for input direction)
2203 * @mask: bit mask to set
2204 * @val: the bits value to set
2205 *
2206 * Update the AMP value with a bit mask.
2207 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002208 */
Takashi Iwai834be882006-03-01 14:16:17 +01002209int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2210 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002212 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002214EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Takashi Iwaid5191e52009-11-16 14:58:17 +01002216/**
2217 * snd_hda_codec_amp_stereo - update the AMP stereo values
2218 * @codec: HD-audio codec
2219 * @nid: NID to read the AMP value
2220 * @direction: #HDA_INPUT or #HDA_OUTPUT
2221 * @idx: the index value (only for input direction)
2222 * @mask: bit mask to set
2223 * @val: the bits value to set
2224 *
2225 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2226 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002227 */
2228int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2229 int direction, int idx, int mask, int val)
2230{
2231 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002232
2233 if (snd_BUG_ON(mask & ~0xff))
2234 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002235 for (ch = 0; ch < 2; ch++)
2236 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2237 idx, mask, val);
2238 return ret;
2239}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002240EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002241
Takashi Iwai280e57d2012-12-14 10:32:21 +01002242/* Works like snd_hda_codec_amp_update() but it writes the value only at
2243 * the first access. If the amp was already initialized / updated beforehand,
2244 * this does nothing.
2245 */
2246int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2247 int dir, int idx, int mask, int val)
2248{
2249 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2250}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002251EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002252
2253int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2254 int dir, int idx, int mask, int val)
2255{
2256 int ch, ret = 0;
2257
2258 if (snd_BUG_ON(mask & ~0xff))
2259 mask &= 0xff;
2260 for (ch = 0; ch < 2; ch++)
2261 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2262 idx, mask, val);
2263 return ret;
2264}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002265EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002266
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267/**
2268 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2269 * @codec: HD-audio codec
2270 *
2271 * Resume the all amp commands from the cache.
2272 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002273void snd_hda_codec_resume_amp(struct hda_codec *codec)
2274{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002275 int i;
2276
Takashi Iwaic370dd62012-12-13 18:30:04 +01002277 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002278 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002279 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2280 struct hda_amp_info *buffer;
2281 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002282 hda_nid_t nid;
2283 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002284 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002285
2286 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002287 if (!buffer->head.dirty)
2288 continue;
2289 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002290 info = *buffer;
2291 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002292 if (!key)
2293 continue;
2294 nid = key & 0xff;
2295 idx = (key >> 16) & 0xff;
2296 dir = (key >> 24) & 0xff;
2297 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002298 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002299 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002300 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002301 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2302 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002303 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002304 }
2305 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002306 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002307}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002308EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002310static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2311 unsigned int ofs)
2312{
2313 u32 caps = query_amp_caps(codec, nid, dir);
2314 /* get num steps */
2315 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2316 if (ofs < caps)
2317 caps -= ofs;
2318 return caps;
2319}
2320
Takashi Iwaid5191e52009-11-16 14:58:17 +01002321/**
2322 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002323 * @kcontrol: referred ctl element
2324 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002325 *
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
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002386 * @kcontrol: ctl element
2387 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002388 *
2389 * The control element is supposed to have the private_value field
2390 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2391 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002392int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2393 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2396 hda_nid_t nid = get_amp_nid(kcontrol);
2397 int chs = get_amp_channels(kcontrol);
2398 int dir = get_amp_direction(kcontrol);
2399 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002400 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 long *valp = ucontrol->value.integer.value;
2402
2403 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002404 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002406 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return 0;
2408}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002409EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Takashi Iwaid5191e52009-11-16 14:58:17 +01002411/**
2412 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002413 * @kcontrol: ctl element
2414 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002415 *
2416 * The control element is supposed to have the private_value field
2417 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2418 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002419int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
2422 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2423 hda_nid_t nid = get_amp_nid(kcontrol);
2424 int chs = get_amp_channels(kcontrol);
2425 int dir = get_amp_direction(kcontrol);
2426 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002427 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 long *valp = ucontrol->value.integer.value;
2429 int change = 0;
2430
Takashi Iwaicb53c622007-08-10 17:21:45 +02002431 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002432 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002433 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002434 valp++;
2435 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002436 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002437 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002438 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return change;
2440}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002441EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Takashi Iwaid5191e52009-11-16 14:58:17 +01002443/**
2444 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002445 * @kcontrol: ctl element
2446 * @op_flag: operation flag
2447 * @size: byte size of input TLV
2448 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449 *
2450 * The control element is supposed to have the private_value field
2451 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2452 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002453int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2454 unsigned int size, unsigned int __user *_tlv)
2455{
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2457 hda_nid_t nid = get_amp_nid(kcontrol);
2458 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002459 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002460 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002461 u32 caps, val1, val2;
2462
2463 if (size < 4 * sizeof(unsigned int))
2464 return -ENOMEM;
2465 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002466 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2467 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002468 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002469 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002470 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002471 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002472 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002473 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2474 return -EFAULT;
2475 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2476 return -EFAULT;
2477 if (put_user(val1, _tlv + 2))
2478 return -EFAULT;
2479 if (put_user(val2, _tlv + 3))
2480 return -EFAULT;
2481 return 0;
2482}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002483EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002484
Takashi Iwaid5191e52009-11-16 14:58:17 +01002485/**
2486 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2487 * @codec: HD-audio codec
2488 * @nid: NID of a reference widget
2489 * @dir: #HDA_INPUT or #HDA_OUTPUT
2490 * @tlv: TLV data to be stored, at least 4 elements
2491 *
2492 * Set (static) TLV data for a virtual master volume using the AMP caps
2493 * obtained from the reference NID.
2494 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002495 */
2496void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2497 unsigned int *tlv)
2498{
2499 u32 caps;
2500 int nums, step;
2501
2502 caps = query_amp_caps(codec, nid, dir);
2503 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2504 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2505 step = (step + 1) * 25;
2506 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2507 tlv[1] = 2 * sizeof(unsigned int);
2508 tlv[2] = -nums * step;
2509 tlv[3] = step;
2510}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002511EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002512
2513/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002514static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002515find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002516{
2517 struct snd_ctl_elem_id id;
2518 memset(&id, 0, sizeof(id));
2519 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002520 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002521 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002522 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2523 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002524 strcpy(id.name, name);
2525 return snd_ctl_find_id(codec->bus->card, &id);
2526}
2527
Takashi Iwaid5191e52009-11-16 14:58:17 +01002528/**
2529 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2530 * @codec: HD-audio codec
2531 * @name: ctl id name string
2532 *
2533 * Get the control element with the given id string and IFACE_MIXER.
2534 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002535struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2536 const char *name)
2537{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002538 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002539}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002540EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002541
Takashi Iwaidcda5802012-10-12 17:24:51 +02002542static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002543 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002544{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002545 int i, idx;
2546 /* 16 ctlrs should be large enough */
2547 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2548 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002549 return idx;
2550 }
2551 return -EBUSY;
2552}
2553
Takashi Iwaid5191e52009-11-16 14:58:17 +01002554/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002555 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002556 * @codec: HD-audio codec
2557 * @nid: corresponding NID (optional)
2558 * @kctl: the control element to assign
2559 *
2560 * Add the given control element to an array inside the codec instance.
2561 * All control elements belonging to a codec are supposed to be added
2562 * by this function so that a proper clean-up works at the free or
2563 * reconfiguration time.
2564 *
2565 * If non-zero @nid is passed, the NID is assigned to the control element.
2566 * The assignment is shown in the codec proc file.
2567 *
2568 * snd_hda_ctl_add() checks the control subdev id field whether
2569 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002570 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2571 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002572 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002573int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2574 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002575{
2576 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002577 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002578 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002579
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002580 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002581 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002582 if (nid == 0)
2583 nid = get_amp_nid_(kctl->private_value);
2584 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002585 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2586 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002587 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002588 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002589 err = snd_ctl_add(codec->bus->card, kctl);
2590 if (err < 0)
2591 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002592 item = snd_array_new(&codec->mixers);
2593 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002594 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002595 item->kctl = kctl;
2596 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002597 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002598 return 0;
2599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002600EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002601
Takashi Iwaid5191e52009-11-16 14:58:17 +01002602/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002603 * snd_hda_add_nid - Assign a NID to a control element
2604 * @codec: HD-audio codec
2605 * @nid: corresponding NID (optional)
2606 * @kctl: the control element to assign
2607 * @index: index to kctl
2608 *
2609 * Add the given control element to an array inside the codec instance.
2610 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2611 * NID:KCTL mapping - for example "Capture Source" selector.
2612 */
2613int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2614 unsigned int index, hda_nid_t nid)
2615{
2616 struct hda_nid_item *item;
2617
2618 if (nid > 0) {
2619 item = snd_array_new(&codec->nids);
2620 if (!item)
2621 return -ENOMEM;
2622 item->kctl = kctl;
2623 item->index = index;
2624 item->nid = nid;
2625 return 0;
2626 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002627 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2628 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002629 return -EINVAL;
2630}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002631EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002632
2633/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002634 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2635 * @codec: HD-audio codec
2636 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002637void snd_hda_ctls_clear(struct hda_codec *codec)
2638{
2639 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002640 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002641 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002642 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002643 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002644 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002645}
2646
Takashi Iwaia65d6292009-02-23 16:57:04 +01002647/* pseudo device locking
2648 * toggle card->shutdown to allow/disallow the device access (as a hack)
2649 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002650int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002651{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002652 struct snd_card *card = bus->card;
2653 struct hda_codec *codec;
2654
Takashi Iwaia65d6292009-02-23 16:57:04 +01002655 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002656 if (card->shutdown)
2657 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002658 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002659 if (!list_empty(&card->ctl_files))
2660 goto err_clear;
2661
2662 list_for_each_entry(codec, &bus->codec_list, list) {
2663 int pcm;
2664 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2665 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2666 if (!cpcm->pcm)
2667 continue;
2668 if (cpcm->pcm->streams[0].substream_opened ||
2669 cpcm->pcm->streams[1].substream_opened)
2670 goto err_clear;
2671 }
2672 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002673 spin_unlock(&card->files_lock);
2674 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002675
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002676 err_clear:
2677 card->shutdown = 0;
2678 err_unlock:
2679 spin_unlock(&card->files_lock);
2680 return -EINVAL;
2681}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002682EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002683
2684void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002685{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002686 struct snd_card *card = bus->card;
2687
2688 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002689 spin_lock(&card->files_lock);
2690 card->shutdown = 0;
2691 spin_unlock(&card->files_lock);
2692}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002693EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002694
Takashi Iwaid5191e52009-11-16 14:58:17 +01002695/**
2696 * snd_hda_codec_reset - Clear all objects assigned to the codec
2697 * @codec: HD-audio codec
2698 *
2699 * This frees the all PCM and control elements assigned to the codec, and
2700 * clears the caches and restores the pin default configurations.
2701 *
2702 * When a device is being used, it returns -EBSY. If successfully freed,
2703 * returns zero.
2704 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002705int snd_hda_codec_reset(struct hda_codec *codec)
2706{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002707 struct hda_bus *bus = codec->bus;
2708 struct snd_card *card = bus->card;
2709 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002710
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002711 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002712 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002713
2714 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002715 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002716#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002717 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002718 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002719#endif
2720 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002721 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002722 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002723 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002724 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002725 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002726 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002727 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002728 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002729 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002730 if (codec->patch_ops.free)
2731 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002732 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002733 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002734 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002735 codec->spec = NULL;
2736 free_hda_cache(&codec->amp_cache);
2737 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002738 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2739 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002740 /* free only driver_pins so that init_pins + user_pins are restored */
2741 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002742 snd_array_free(&codec->cvt_setups);
2743 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002744 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002745 codec->num_pcms = 0;
2746 codec->pcm_info = NULL;
2747 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002748 codec->slave_dig_outs = NULL;
2749 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002750 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002751 module_put(codec->owner);
2752 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002753
2754 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002755 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002756 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002757}
2758
Takashi Iwai6194b992014-06-06 18:12:16 +02002759typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002760
2761/* apply the function to all matching slave ctls in the mixer list */
2762static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002763 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002764{
2765 struct hda_nid_item *items;
2766 const char * const *s;
2767 int i, err;
2768
2769 items = codec->mixers.list;
2770 for (i = 0; i < codec->mixers.used; i++) {
2771 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002772 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002773 continue;
2774 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002775 char tmpname[sizeof(sctl->id.name)];
2776 const char *name = *s;
2777 if (suffix) {
2778 snprintf(tmpname, sizeof(tmpname), "%s %s",
2779 name, suffix);
2780 name = tmpname;
2781 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002782 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002783 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002784 if (err)
2785 return err;
2786 break;
2787 }
2788 }
2789 }
2790 return 0;
2791}
2792
Takashi Iwai6194b992014-06-06 18:12:16 +02002793static int check_slave_present(struct hda_codec *codec,
2794 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002795{
2796 return 1;
2797}
2798
Takashi Iwai18478e82012-03-09 17:51:10 +01002799/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002800static int get_kctl_0dB_offset(struct hda_codec *codec,
2801 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002802{
2803 int _tlv[4];
2804 const int *tlv = NULL;
2805 int val = -1;
2806
2807 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2808 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2809 mm_segment_t fs = get_fs();
2810 set_fs(get_ds());
2811 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2812 tlv = _tlv;
2813 set_fs(fs);
2814 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2815 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002816 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2817 int step = tlv[3];
2818 step &= ~TLV_DB_SCALE_MUTE;
2819 if (!step)
2820 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002821 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002822 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002823- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002824 return -1;
2825 }
2826 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002827 val = -tlv[2] / step;
2828 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002829 return val;
2830}
2831
2832/* call kctl->put with the given value(s) */
2833static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2834{
2835 struct snd_ctl_elem_value *ucontrol;
2836 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2837 if (!ucontrol)
2838 return -ENOMEM;
2839 ucontrol->value.integer.value[0] = val;
2840 ucontrol->value.integer.value[1] = val;
2841 kctl->put(kctl, ucontrol);
2842 kfree(ucontrol);
2843 return 0;
2844}
2845
2846/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002847static int init_slave_0dB(struct hda_codec *codec,
2848 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002849{
Takashi Iwai6194b992014-06-06 18:12:16 +02002850 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002851 if (offset > 0)
2852 put_kctl_with_value(slave, offset);
2853 return 0;
2854}
2855
2856/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002857static int init_slave_unmute(struct hda_codec *codec,
2858 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002859{
2860 return put_kctl_with_value(slave, 1);
2861}
2862
Takashi Iwaie8750942014-06-30 14:02:39 +02002863static int add_slave(struct hda_codec *codec,
2864 void *data, struct snd_kcontrol *slave)
2865{
2866 return snd_ctl_add_slave(data, slave);
2867}
2868
Takashi Iwaid5191e52009-11-16 14:58:17 +01002869/**
2870 * snd_hda_add_vmaster - create a virtual master control and add slaves
2871 * @codec: HD-audio codec
2872 * @name: vmaster control name
2873 * @tlv: TLV data (optional)
2874 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002875 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002876 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002877 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002878 *
2879 * Create a virtual master control with the given name. The TLV data
2880 * must be either NULL or a valid data.
2881 *
2882 * @slaves is a NULL-terminated array of strings, each of which is a
2883 * slave control name. All controls with these names are assigned to
2884 * the new virtual master control.
2885 *
2886 * This function returns zero if successful or a negative error code.
2887 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002888int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002889 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002890 const char *suffix, bool init_slave_vol,
2891 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002892{
2893 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002894 int err;
2895
Takashi Iwai29e58532012-03-12 12:25:03 +01002896 if (ctl_ret)
2897 *ctl_ret = NULL;
2898
Takashi Iwai9322ca52012-02-03 14:28:01 +01002899 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002900 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002901 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002902 return 0;
2903 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002904 kctl = snd_ctl_make_virtual_master(name, tlv);
2905 if (!kctl)
2906 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002907 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002908 if (err < 0)
2909 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002910
Takashi Iwaie8750942014-06-30 14:02:39 +02002911 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002912 if (err < 0)
2913 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002914
2915 /* init with master mute & zero volume */
2916 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002917 if (init_slave_vol) {
2918 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002919 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002920 tlv ? init_slave_0dB : init_slave_unmute, &step);
2921 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002922
Takashi Iwai29e58532012-03-12 12:25:03 +01002923 if (ctl_ret)
2924 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002925 return 0;
2926}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002927EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002928
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002929/*
2930 * mute-LED control using vmaster
2931 */
2932static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_info *uinfo)
2934{
2935 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002936 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002937 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002938
Takashi Iwai3ff72212014-10-20 18:17:28 +02002939 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002940}
2941
2942static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_value *ucontrol)
2944{
2945 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2946 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2947 return 0;
2948}
2949
2950static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
2952{
2953 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2954 unsigned int old_mode = hook->mute_mode;
2955
2956 hook->mute_mode = ucontrol->value.enumerated.item[0];
2957 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2958 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2959 if (old_mode == hook->mute_mode)
2960 return 0;
2961 snd_hda_sync_vmaster_hook(hook);
2962 return 1;
2963}
2964
2965static struct snd_kcontrol_new vmaster_mute_mode = {
2966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2967 .name = "Mute-LED Mode",
2968 .info = vmaster_mute_mode_info,
2969 .get = vmaster_mute_mode_get,
2970 .put = vmaster_mute_mode_put,
2971};
2972
2973/*
2974 * Add a mute-LED hook with the given vmaster switch kctl
2975 * "Mute-LED Mode" control is automatically created and associated with
2976 * the given hook.
2977 */
2978int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002979 struct hda_vmaster_mute_hook *hook,
2980 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002981{
2982 struct snd_kcontrol *kctl;
2983
2984 if (!hook->hook || !hook->sw_kctl)
2985 return 0;
2986 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2987 hook->codec = codec;
2988 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002989 if (!expose_enum_ctl)
2990 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002991 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2992 if (!kctl)
2993 return -ENOMEM;
2994 return snd_hda_ctl_add(codec, 0, kctl);
2995}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002996EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002997
2998/*
2999 * Call the hook with the current value for synchronization
3000 * Should be called in init callback
3001 */
3002void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3003{
3004 if (!hook->hook || !hook->codec)
3005 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003006 /* don't call vmaster hook in the destructor since it might have
3007 * been already destroyed
3008 */
3009 if (hook->codec->bus->shutdown)
3010 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003011 switch (hook->mute_mode) {
3012 case HDA_VMUTE_FOLLOW_MASTER:
3013 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3014 break;
3015 default:
3016 hook->hook(hook->codec, hook->mute_mode);
3017 break;
3018 }
3019}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003020EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003021
3022
Takashi Iwaid5191e52009-11-16 14:58:17 +01003023/**
3024 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003025 * @kcontrol: referred ctl element
3026 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003027 *
3028 * The control element is supposed to have the private_value field
3029 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3030 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 int chs = get_amp_channels(kcontrol);
3035
3036 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3037 uinfo->count = chs == 3 ? 2 : 1;
3038 uinfo->value.integer.min = 0;
3039 uinfo->value.integer.max = 1;
3040 return 0;
3041}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003042EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Takashi Iwaid5191e52009-11-16 14:58:17 +01003044/**
3045 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003046 * @kcontrol: ctl element
3047 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003048 *
3049 * The control element is supposed to have the private_value field
3050 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3051 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003052int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3056 hda_nid_t nid = get_amp_nid(kcontrol);
3057 int chs = get_amp_channels(kcontrol);
3058 int dir = get_amp_direction(kcontrol);
3059 int idx = get_amp_index(kcontrol);
3060 long *valp = ucontrol->value.integer.value;
3061
3062 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003063 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003064 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003067 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return 0;
3069}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003070EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Takashi Iwaid5191e52009-11-16 14:58:17 +01003072/**
3073 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003074 * @kcontrol: ctl element
3075 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003076 *
3077 * The control element is supposed to have the private_value field
3078 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3079 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003080int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3081 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3084 hda_nid_t nid = get_amp_nid(kcontrol);
3085 int chs = get_amp_channels(kcontrol);
3086 int dir = get_amp_direction(kcontrol);
3087 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 long *valp = ucontrol->value.integer.value;
3089 int change = 0;
3090
Takashi Iwaicb53c622007-08-10 17:21:45 +02003091 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003092 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003093 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003094 HDA_AMP_MUTE,
3095 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003096 valp++;
3097 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003098 if (chs & 2)
3099 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003100 HDA_AMP_MUTE,
3101 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003102 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003103 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return change;
3105}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003106EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108/*
Takashi Iwai985be542005-11-02 18:26:49 +01003109 * bound volume controls
3110 *
3111 * bind multiple volumes (# indices, from 0)
3112 */
3113
3114#define AMP_VAL_IDX_SHIFT 19
3115#define AMP_VAL_IDX_MASK (0x0f<<19)
3116
Takashi Iwaid5191e52009-11-16 14:58:17 +01003117/**
3118 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003119 * @kcontrol: ctl element
3120 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003121 *
3122 * The control element is supposed to have the private_value field
3123 * set up via HDA_BIND_MUTE*() macros.
3124 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003125int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003127{
3128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3129 unsigned long pval;
3130 int err;
3131
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003132 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003133 pval = kcontrol->private_value;
3134 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3135 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3136 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003137 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003138 return err;
3139}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003140EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003141
Takashi Iwaid5191e52009-11-16 14:58:17 +01003142/**
3143 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003144 * @kcontrol: ctl element
3145 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003146 *
3147 * The control element is supposed to have the private_value field
3148 * set up via HDA_BIND_MUTE*() macros.
3149 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003150int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003152{
3153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3154 unsigned long pval;
3155 int i, indices, err = 0, change = 0;
3156
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003157 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003158 pval = kcontrol->private_value;
3159 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3160 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003161 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3162 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003163 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3164 if (err < 0)
3165 break;
3166 change |= err;
3167 }
3168 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003169 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003170 return err < 0 ? err : change;
3171}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003172EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003173
Takashi Iwaid5191e52009-11-16 14:58:17 +01003174/**
3175 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003176 * @kcontrol: referred ctl element
3177 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003178 *
3179 * The control element is supposed to have the private_value field
3180 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003181 */
3182int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_info *uinfo)
3184{
3185 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3186 struct hda_bind_ctls *c;
3187 int err;
3188
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003189 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003190 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003191 kcontrol->private_value = *c->values;
3192 err = c->ops->info(kcontrol, uinfo);
3193 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003194 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003195 return err;
3196}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003197EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003198
Takashi Iwaid5191e52009-11-16 14:58:17 +01003199/**
3200 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003201 * @kcontrol: ctl element
3202 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003203 *
3204 * The control element is supposed to have the private_value field
3205 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3206 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003207int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3208 struct snd_ctl_elem_value *ucontrol)
3209{
3210 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3211 struct hda_bind_ctls *c;
3212 int err;
3213
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003214 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003215 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003216 kcontrol->private_value = *c->values;
3217 err = c->ops->get(kcontrol, ucontrol);
3218 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003219 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003220 return err;
3221}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003222EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003223
Takashi Iwaid5191e52009-11-16 14:58:17 +01003224/**
3225 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003226 * @kcontrol: ctl element
3227 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003228 *
3229 * The control element is supposed to have the private_value field
3230 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3231 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003232int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3233 struct snd_ctl_elem_value *ucontrol)
3234{
3235 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3236 struct hda_bind_ctls *c;
3237 unsigned long *vals;
3238 int err = 0, change = 0;
3239
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003240 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003241 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003242 for (vals = c->values; *vals; vals++) {
3243 kcontrol->private_value = *vals;
3244 err = c->ops->put(kcontrol, ucontrol);
3245 if (err < 0)
3246 break;
3247 change |= err;
3248 }
3249 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003250 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003251 return err < 0 ? err : change;
3252}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003253EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003254
Takashi Iwaid5191e52009-11-16 14:58:17 +01003255/**
3256 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003257 * @kcontrol: ctl element
3258 * @op_flag: operation flag
3259 * @size: byte size of input TLV
3260 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003261 *
3262 * The control element is supposed to have the private_value field
3263 * set up via HDA_BIND_VOL() macro.
3264 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003265int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3266 unsigned int size, unsigned int __user *tlv)
3267{
3268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3269 struct hda_bind_ctls *c;
3270 int err;
3271
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003272 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003273 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003274 kcontrol->private_value = *c->values;
3275 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3276 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003277 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003278 return err;
3279}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003280EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003281
3282struct hda_ctl_ops snd_hda_bind_vol = {
3283 .info = snd_hda_mixer_amp_volume_info,
3284 .get = snd_hda_mixer_amp_volume_get,
3285 .put = snd_hda_mixer_amp_volume_put,
3286 .tlv = snd_hda_mixer_amp_tlv
3287};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003288EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003289
3290struct hda_ctl_ops snd_hda_bind_sw = {
3291 .info = snd_hda_mixer_amp_switch_info,
3292 .get = snd_hda_mixer_amp_switch_get,
3293 .put = snd_hda_mixer_amp_switch_put,
3294 .tlv = snd_hda_mixer_amp_tlv
3295};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003296EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003297
3298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 * SPDIF out controls
3300 */
3301
Takashi Iwai0ba21762007-04-16 11:29:14 +02003302static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3303 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3306 uinfo->count = 1;
3307 return 0;
3308}
3309
Takashi Iwai0ba21762007-04-16 11:29:14 +02003310static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3314 IEC958_AES0_NONAUDIO |
3315 IEC958_AES0_CON_EMPHASIS_5015 |
3316 IEC958_AES0_CON_NOT_COPYRIGHT;
3317 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3318 IEC958_AES1_CON_ORIGINAL;
3319 return 0;
3320}
3321
Takashi Iwai0ba21762007-04-16 11:29:14 +02003322static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3323 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
3325 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3326 IEC958_AES0_NONAUDIO |
3327 IEC958_AES0_PRO_EMPHASIS_5015;
3328 return 0;
3329}
3330
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003335 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003336 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003338 mutex_lock(&codec->spdif_mutex);
3339 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003340 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3341 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3342 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3343 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003344 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 return 0;
3347}
3348
3349/* convert from SPDIF status bits to HDA SPDIF bits
3350 * bit 0 (DigEn) is always set zero (to be filled later)
3351 */
3352static unsigned short convert_from_spdif_status(unsigned int sbits)
3353{
3354 unsigned short val = 0;
3355
3356 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003357 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003359 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003361 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3362 IEC958_AES0_PRO_EMPHASIS_5015)
3363 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003365 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3366 IEC958_AES0_CON_EMPHASIS_5015)
3367 val |= AC_DIG1_EMPHASIS;
3368 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3369 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003371 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3373 }
3374 return val;
3375}
3376
3377/* convert to SPDIF status bits from HDA SPDIF bits
3378 */
3379static unsigned int convert_to_spdif_status(unsigned short val)
3380{
3381 unsigned int sbits = 0;
3382
Takashi Iwai0ba21762007-04-16 11:29:14 +02003383 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003385 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 sbits |= IEC958_AES0_PROFESSIONAL;
3387 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003388 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3390 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003391 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003393 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003395 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3397 sbits |= val & (0x7f << 8);
3398 }
3399 return sbits;
3400}
3401
Takashi Iwai2f728532008-09-25 16:32:41 +02003402/* set digital convert verbs both for the given NID and its slaves */
3403static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3404 int verb, int val)
3405{
Takashi Iwaidda14412011-05-02 11:29:30 +02003406 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003407
Takashi Iwai9e976972008-11-25 08:17:20 +01003408 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003409 d = codec->slave_dig_outs;
3410 if (!d)
3411 return;
3412 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003413 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003414}
3415
3416static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3417 int dig1, int dig2)
3418{
3419 if (dig1 != -1)
3420 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3421 if (dig2 != -1)
3422 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3423}
3424
Takashi Iwai0ba21762007-04-16 11:29:14 +02003425static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3426 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003429 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003430 struct hda_spdif_out *spdif;
3431 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 unsigned short val;
3433 int change;
3434
Ingo Molnar62932df2006-01-16 16:34:20 +01003435 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003436 spdif = snd_array_elem(&codec->spdif_out, idx);
3437 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003438 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3440 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3441 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003442 val = convert_from_spdif_status(spdif->status);
3443 val |= spdif->ctls & 1;
3444 change = spdif->ctls != val;
3445 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003446 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003447 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003448 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return change;
3450}
3451
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003452#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Takashi Iwai0ba21762007-04-16 11:29:14 +02003454static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3455 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003458 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003459 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003461 mutex_lock(&codec->spdif_mutex);
3462 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003463 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003464 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return 0;
3466}
3467
Stephen Warren74b654c2011-06-01 11:14:18 -06003468static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3469 int dig1, int dig2)
3470{
3471 set_dig_out_convert(codec, nid, dig1, dig2);
3472 /* unmute amp switch (if any) */
3473 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3474 (dig1 & AC_DIG1_ENABLE))
3475 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3476 HDA_AMP_MUTE, 0);
3477}
3478
Takashi Iwai0ba21762007-04-16 11:29:14 +02003479static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3480 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003483 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003484 struct hda_spdif_out *spdif;
3485 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 unsigned short val;
3487 int change;
3488
Ingo Molnar62932df2006-01-16 16:34:20 +01003489 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003490 spdif = snd_array_elem(&codec->spdif_out, idx);
3491 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003492 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003494 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003495 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003496 spdif->ctls = val;
3497 if (change && nid != (u16)-1)
3498 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003499 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return change;
3501}
3502
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003503static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 {
3505 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003507 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 .info = snd_hda_spdif_mask_info,
3509 .get = snd_hda_spdif_cmask_get,
3510 },
3511 {
3512 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003514 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 .info = snd_hda_spdif_mask_info,
3516 .get = snd_hda_spdif_pmask_get,
3517 },
3518 {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003520 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 .info = snd_hda_spdif_mask_info,
3522 .get = snd_hda_spdif_default_get,
3523 .put = snd_hda_spdif_default_put,
3524 },
3525 {
3526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003527 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 .info = snd_hda_spdif_out_switch_info,
3529 .get = snd_hda_spdif_out_switch_get,
3530 .put = snd_hda_spdif_out_switch_put,
3531 },
3532 { } /* end */
3533};
3534
3535/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003536 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003538 * @associated_nid: NID that new ctls associated with
3539 * @cvt_nid: converter NID
3540 * @type: HDA_PCM_TYPE_*
3541 * Creates controls related with the digital output.
3542 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 *
3544 * Returns 0 if successful, or a negative error code.
3545 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003546int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3547 hda_nid_t associated_nid,
3548 hda_nid_t cvt_nid,
3549 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003552 struct snd_kcontrol *kctl;
3553 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003554 int idx = 0;
3555 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003556 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003557 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003559 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003560 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003561 idx = spdif_index;
3562 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003563 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003564 /* suppose a single SPDIF device */
3565 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3566 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3567 if (!kctl)
3568 break;
3569 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003570 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003571 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003572 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003573 if (!bus->primary_dig_out_type)
3574 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003575
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003576 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003577 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003578 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003579 return -EBUSY;
3580 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003581 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003582 if (!spdif)
3583 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3585 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003586 if (!kctl)
3587 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003588 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003589 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003590 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003591 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return err;
3593 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003594 spdif->nid = cvt_nid;
3595 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003596 AC_VERB_GET_DIGI_CONVERT_1, 0);
3597 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return 0;
3599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003600EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003602/* get the hda_spdif_out entry from the given NID
3603 * call within spdif_mutex lock
3604 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003605struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3606 hda_nid_t nid)
3607{
3608 int i;
3609 for (i = 0; i < codec->spdif_out.used; i++) {
3610 struct hda_spdif_out *spdif =
3611 snd_array_elem(&codec->spdif_out, i);
3612 if (spdif->nid == nid)
3613 return spdif;
3614 }
3615 return NULL;
3616}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003617EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003618
Stephen Warren74b654c2011-06-01 11:14:18 -06003619void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3620{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003621 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003622
3623 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003624 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003625 spdif->nid = (u16)-1;
3626 mutex_unlock(&codec->spdif_mutex);
3627}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003628EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003629
3630void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3631{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003632 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003633 unsigned short val;
3634
3635 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003636 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003637 if (spdif->nid != nid) {
3638 spdif->nid = nid;
3639 val = spdif->ctls;
3640 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3641 }
3642 mutex_unlock(&codec->spdif_mutex);
3643}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003644EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003647 * SPDIF sharing with analog output
3648 */
3649static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3650 struct snd_ctl_elem_value *ucontrol)
3651{
3652 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3653 ucontrol->value.integer.value[0] = mout->share_spdif;
3654 return 0;
3655}
3656
3657static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3658 struct snd_ctl_elem_value *ucontrol)
3659{
3660 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3661 mout->share_spdif = !!ucontrol->value.integer.value[0];
3662 return 0;
3663}
3664
3665static struct snd_kcontrol_new spdif_share_sw = {
3666 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3667 .name = "IEC958 Default PCM Playback Switch",
3668 .info = snd_ctl_boolean_mono_info,
3669 .get = spdif_share_sw_get,
3670 .put = spdif_share_sw_put,
3671};
3672
Takashi Iwaid5191e52009-11-16 14:58:17 +01003673/**
3674 * snd_hda_create_spdif_share_sw - create Default PCM switch
3675 * @codec: the HDA codec
3676 * @mout: multi-out instance
3677 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003678int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3679 struct hda_multi_out *mout)
3680{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003681 struct snd_kcontrol *kctl;
3682
Takashi Iwai9a081602008-02-12 18:37:26 +01003683 if (!mout->dig_out_nid)
3684 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003685
3686 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3687 if (!kctl)
3688 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003689 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003690 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003691}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003692EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003693
3694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * SPDIF input
3696 */
3697
3698#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3699
Takashi Iwai0ba21762007-04-16 11:29:14 +02003700static int snd_hda_spdif_in_switch_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
3705 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3706 return 0;
3707}
3708
Takashi Iwai0ba21762007-04-16 11:29:14 +02003709static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3710 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3713 hda_nid_t nid = kcontrol->private_value;
3714 unsigned int val = !!ucontrol->value.integer.value[0];
3715 int change;
3716
Ingo Molnar62932df2006-01-16 16:34:20 +01003717 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003719 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003721 snd_hda_codec_write_cache(codec, nid, 0,
3722 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003724 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return change;
3726}
3727
Takashi Iwai0ba21762007-04-16 11:29:14 +02003728static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3729 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3732 hda_nid_t nid = kcontrol->private_value;
3733 unsigned short val;
3734 unsigned int sbits;
3735
Andrew Paprocki3982d172007-12-19 12:13:44 +01003736 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 sbits = convert_to_spdif_status(val);
3738 ucontrol->value.iec958.status[0] = sbits;
3739 ucontrol->value.iec958.status[1] = sbits >> 8;
3740 ucontrol->value.iec958.status[2] = sbits >> 16;
3741 ucontrol->value.iec958.status[3] = sbits >> 24;
3742 return 0;
3743}
3744
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003745static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 {
3747 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003748 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 .info = snd_hda_spdif_in_switch_info,
3750 .get = snd_hda_spdif_in_switch_get,
3751 .put = snd_hda_spdif_in_switch_put,
3752 },
3753 {
3754 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003756 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 .info = snd_hda_spdif_mask_info,
3758 .get = snd_hda_spdif_in_status_get,
3759 },
3760 { } /* end */
3761};
3762
3763/**
3764 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3765 * @codec: the HDA codec
3766 * @nid: audio in widget NID
3767 *
3768 * Creates controls related with the SPDIF input.
3769 * Called from each patch supporting the SPDIF in.
3770 *
3771 * Returns 0 if successful, or a negative error code.
3772 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003773int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003776 struct snd_kcontrol *kctl;
3777 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003778 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Takashi Iwaidcda5802012-10-12 17:24:51 +02003780 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003781 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003782 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003783 return -EBUSY;
3784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3786 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003787 if (!kctl)
3788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003790 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003791 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return err;
3793 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003794 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003795 snd_hda_codec_read(codec, nid, 0,
3796 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003797 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return 0;
3799}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003800EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003802/*
3803 * command cache
3804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Takashi Iwaic370dd62012-12-13 18:30:04 +01003806/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003807#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3808#define get_cmd_cache_nid(key) ((key) & 0xff)
3809#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3810
3811/**
3812 * snd_hda_codec_write_cache - send a single command with caching
3813 * @codec: the HDA codec
3814 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003815 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003816 * @verb: the verb to send
3817 * @parm: the parameter for the verb
3818 *
3819 * Send a single command without waiting for response.
3820 *
3821 * Returns 0 if successful, or a negative error code.
3822 */
3823int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003824 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003825{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003826 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003827 struct hda_cache_head *c;
3828 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003829 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003830
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003831 cache_only = codec->cached_write;
3832 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003833 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003834 if (err < 0)
3835 return err;
3836 }
3837
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003838 /* parm may contain the verb stuff for get/set amp */
3839 verb = verb | (parm >> 8);
3840 parm &= 0xff;
3841 key = build_cmd_cache_key(nid, verb);
3842 mutex_lock(&codec->bus->cmd_mutex);
3843 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003844 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003845 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003846 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003847 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003848 mutex_unlock(&codec->bus->cmd_mutex);
3849 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003851EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003852
Takashi Iwaid5191e52009-11-16 14:58:17 +01003853/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003854 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3855 * @codec: the HDA codec
3856 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003857 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003858 * @verb: the verb to send
3859 * @parm: the parameter for the verb
3860 *
3861 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3862 * command if the parameter is already identical with the cached value.
3863 * If not, it sends the command and refreshes the cache.
3864 *
3865 * Returns 0 if successful, or a negative error code.
3866 */
3867int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003868 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003869{
3870 struct hda_cache_head *c;
3871 u32 key;
3872
3873 /* parm may contain the verb stuff for get/set amp */
3874 verb = verb | (parm >> 8);
3875 parm &= 0xff;
3876 key = build_cmd_cache_key(nid, verb);
3877 mutex_lock(&codec->bus->cmd_mutex);
3878 c = get_hash(&codec->cmd_cache, key);
3879 if (c && c->val == parm) {
3880 mutex_unlock(&codec->bus->cmd_mutex);
3881 return 0;
3882 }
3883 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003884 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003885}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003886EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003887
3888/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003889 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3890 * @codec: HD-audio codec
3891 *
3892 * Execute all verbs recorded in the command caches to resume.
3893 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003894void snd_hda_codec_resume_cache(struct hda_codec *codec)
3895{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003896 int i;
3897
Takashi Iwaic370dd62012-12-13 18:30:04 +01003898 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003899 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003900 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3901 struct hda_cache_head *buffer;
3902 u32 key;
3903
3904 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3905 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003906 if (!key)
3907 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003908 if (!buffer->dirty)
3909 continue;
3910 buffer->dirty = 0;
3911 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003912 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3913 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003914 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003915 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003916 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003917}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003918EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003919
3920/**
3921 * snd_hda_sequence_write_cache - sequence writes with caching
3922 * @codec: the HDA codec
3923 * @seq: VERB array to send
3924 *
3925 * Send the commands sequentially from the given array.
3926 * Thte commands are recorded on cache for power-save and resume.
3927 * The array must be terminated with NID=0.
3928 */
3929void snd_hda_sequence_write_cache(struct hda_codec *codec,
3930 const struct hda_verb *seq)
3931{
3932 for (; seq->nid; seq++)
3933 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3934 seq->param);
3935}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003936EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003937
Takashi Iwaidc870f32013-01-22 15:24:30 +01003938/**
3939 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3940 * @codec: HD-audio codec
3941 */
3942void snd_hda_codec_flush_cache(struct hda_codec *codec)
3943{
3944 snd_hda_codec_resume_amp(codec);
3945 snd_hda_codec_resume_cache(codec);
3946}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003947EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003948
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003949void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003950 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003951{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003952 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003953 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003954
Takashi Iwaicb53c622007-08-10 17:21:45 +02003955 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003956 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003957 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003958 if (!(wcaps & AC_WCAP_POWER))
3959 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003960 if (codec->power_filter) {
3961 state = codec->power_filter(codec, nid, power_state);
3962 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003963 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003964 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003965 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003966 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003967 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003968}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003969EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003970
3971/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003972 * supported power states check
3973 */
3974static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3975 unsigned int power_state)
3976{
3977 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3978
Mengdong Line037cb42012-08-10 14:11:58 +02003979 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003980 return false;
3981 if (sup & power_state)
3982 return true;
3983 else
3984 return false;
3985}
3986
3987/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003988 * wait until the state is reached, returns the current state
3989 */
3990static unsigned int hda_sync_power_state(struct hda_codec *codec,
3991 hda_nid_t fg,
3992 unsigned int power_state)
3993{
3994 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3995 unsigned int state, actual_state;
3996
3997 for (;;) {
3998 state = snd_hda_codec_read(codec, fg, 0,
3999 AC_VERB_GET_POWER_STATE, 0);
4000 if (state & AC_PWRST_ERROR)
4001 break;
4002 actual_state = (state >> 4) & 0x0f;
4003 if (actual_state == power_state)
4004 break;
4005 if (time_after_eq(jiffies, end_time))
4006 break;
4007 /* wait until the codec reachs to the target state */
4008 msleep(1);
4009 }
4010 return state;
4011}
4012
Takashi Iwai9419ab62013-01-24 17:23:35 +01004013/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004014unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4015 hda_nid_t nid,
4016 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004017{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004018 if (nid == codec->afg || nid == codec->mfg)
4019 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004020 if (power_state == AC_PWRST_D3 &&
4021 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4022 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4023 int eapd = snd_hda_codec_read(codec, nid, 0,
4024 AC_VERB_GET_EAPD_BTLENABLE, 0);
4025 if (eapd & 0x02)
4026 return AC_PWRST_D0;
4027 }
4028 return power_state;
4029}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004030EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004031
Takashi Iwai432c6412012-08-28 09:59:20 -07004032/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004033 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004034 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004035static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004036 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004037{
Takashi Iwaid8193872012-08-31 07:54:38 -07004038 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004039 int count;
4040 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004041 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004042
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004043 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004044 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004045 if (codec->depop_delay < 0)
4046 msleep(codec->epss ? 10 : 100);
4047 else if (codec->depop_delay > 0)
4048 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004049 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004050 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004051
4052 /* repeat power states setting at most 10 times*/
4053 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004054 if (codec->patch_ops.set_power_state)
4055 codec->patch_ops.set_power_state(codec, fg,
4056 power_state);
4057 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004058 state = power_state;
4059 if (codec->power_filter)
4060 state = codec->power_filter(codec, fg, state);
4061 if (state == power_state || power_state != AC_PWRST_D3)
4062 snd_hda_codec_read(codec, fg, flags,
4063 AC_VERB_SET_POWER_STATE,
4064 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004065 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004066 }
4067 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004068 if (!(state & AC_PWRST_ERROR))
4069 break;
4070 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004071
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004072 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004073}
Takashi Iwai54d17402005-11-21 16:33:22 +01004074
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004075/* sync power states of all widgets;
4076 * this is called at the end of codec parsing
4077 */
4078static void sync_power_up_states(struct hda_codec *codec)
4079{
4080 hda_nid_t nid = codec->start_nid;
4081 int i;
4082
Takashi Iwaiba615b82013-03-13 14:47:21 +01004083 /* don't care if no filter is used */
4084 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004085 return;
4086
4087 for (i = 0; i < codec->num_nodes; i++, nid++) {
4088 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004089 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004090 if (!(wcaps & AC_WCAP_POWER))
4091 continue;
4092 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4093 if (target == AC_PWRST_D0)
4094 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004095 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004096 snd_hda_codec_write(codec, nid, 0,
4097 AC_VERB_SET_POWER_STATE, target);
4098 }
4099}
4100
Takashi Iwai648a8d22014-02-25 10:38:13 +01004101#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004102/* execute additional init verbs */
4103static void hda_exec_init_verbs(struct hda_codec *codec)
4104{
4105 if (codec->init_verbs.list)
4106 snd_hda_sequence_write(codec, codec->init_verbs.list);
4107}
4108#else
4109static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4110#endif
4111
Takashi Iwai2a439522011-07-26 09:52:50 +02004112#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004113/*
4114 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004115 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004116 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004117static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004118{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004119 unsigned int state;
4120
Takashi Iwai989c3182012-11-19 14:14:58 +01004121 codec->in_pm = 1;
4122
Takashi Iwaicb53c622007-08-10 17:21:45 +02004123 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004124 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004125 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004126 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004127 /* Cancel delayed work if we aren't currently running from it. */
4128 if (!in_wq)
4129 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004130 spin_lock(&codec->power_lock);
4131 snd_hda_update_power_acct(codec);
4132 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004133 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004134 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004135 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004136 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004137 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004138 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004139}
4140
Takashi Iwaic370dd62012-12-13 18:30:04 +01004141/* mark all entries of cmd and amp caches dirty */
4142static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4143{
4144 int i;
4145 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4146 struct hda_cache_head *cmd;
4147 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4148 cmd->dirty = 1;
4149 }
4150 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4151 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004152 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004153 amp->head.dirty = 1;
4154 }
4155}
4156
Takashi Iwaicb53c622007-08-10 17:21:45 +02004157/*
4158 * kick up codec; used both from PM and power-save
4159 */
4160static void hda_call_codec_resume(struct hda_codec *codec)
4161{
Takashi Iwai989c3182012-11-19 14:14:58 +01004162 codec->in_pm = 1;
4163
Takashi Iwaic370dd62012-12-13 18:30:04 +01004164 hda_mark_cmd_cache_dirty(codec);
4165
Takashi Iwai7f308302012-05-08 16:52:23 +02004166 /* set as if powered on for avoiding re-entering the resume
4167 * in the resume / power-save sequence
4168 */
4169 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004170 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004171 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004172 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004173 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004174 if (codec->patch_ops.resume)
4175 codec->patch_ops.resume(codec);
4176 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004177 if (codec->patch_ops.init)
4178 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004179 snd_hda_codec_resume_amp(codec);
4180 snd_hda_codec_resume_cache(codec);
4181 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004182
4183 if (codec->jackpoll_interval)
4184 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004185 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004186 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004187
4188 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004189 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004190}
Takashi Iwai2a439522011-07-26 09:52:50 +02004191#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004192
Takashi Iwai54d17402005-11-21 16:33:22 +01004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194/**
4195 * snd_hda_build_controls - build mixer controls
4196 * @bus: the BUS
4197 *
4198 * Creates mixer controls for each codec included in the bus.
4199 *
4200 * Returns 0 if successful, otherwise a negative error code.
4201 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004202int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004204 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Takashi Iwai0ba21762007-04-16 11:29:14 +02004206 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004207 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004208 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004209 codec_err(codec,
4210 "cannot build controls for #%d (error %d)\n",
4211 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004212 err = snd_hda_codec_reset(codec);
4213 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004214 codec_err(codec,
4215 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004216 return err;
4217 }
4218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004220 return 0;
4221}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004222EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004224/*
4225 * add standard channel maps if not specified
4226 */
4227static int add_std_chmaps(struct hda_codec *codec)
4228{
4229 int i, str, err;
4230
4231 for (i = 0; i < codec->num_pcms; i++) {
4232 for (str = 0; str < 2; str++) {
4233 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4234 struct hda_pcm_stream *hinfo =
4235 &codec->pcm_info[i].stream[str];
4236 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004237 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004238
4239 if (codec->pcm_info[i].own_chmap)
4240 continue;
4241 if (!pcm || !hinfo->substreams)
4242 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004243 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4244 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004245 hinfo->channels_max,
4246 0, &chmap);
4247 if (err < 0)
4248 return err;
4249 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4250 }
4251 }
4252 return 0;
4253}
4254
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004255/* default channel maps for 2.1 speakers;
4256 * since HD-audio supports only stereo, odd number channels are omitted
4257 */
4258const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4259 { .channels = 2,
4260 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4261 { .channels = 4,
4262 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4263 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4264 { }
4265};
4266EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4267
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004268int snd_hda_codec_build_controls(struct hda_codec *codec)
4269{
4270 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004271 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004272 /* continue to initialize... */
4273 if (codec->patch_ops.init)
4274 err = codec->patch_ops.init(codec);
4275 if (!err && codec->patch_ops.build_controls)
4276 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004277 if (err < 0)
4278 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004279
4280 /* we create chmaps here instead of build_pcms */
4281 err = add_std_chmaps(codec);
4282 if (err < 0)
4283 return err;
4284
David Henningsson26a6cb62012-10-09 15:04:21 +02004285 if (codec->jackpoll_interval)
4286 hda_jackpoll_work(&codec->jackpoll_work.work);
4287 else
4288 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004289 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return 0;
4291}
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293/*
4294 * stream formats
4295 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004296struct hda_rate_tbl {
4297 unsigned int hz;
4298 unsigned int alsa_bits;
4299 unsigned int hda_fmt;
4300};
4301
Takashi Iwai92f10b32010-08-03 14:21:00 +02004302/* rate = base * mult / div */
4303#define HDA_RATE(base, mult, div) \
4304 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4305 (((div) - 1) << AC_FMT_DIV_SHIFT))
4306
Takashi Iwaibefdf312005-08-22 13:57:55 +02004307static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004309
4310 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004311 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4312 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4313 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4314 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4315 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4316 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4317 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4318 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4319 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4320 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4321 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004322#define AC_PAR_PCM_RATE_BITS 11
4323 /* up to bits 10, 384kHZ isn't supported properly */
4324
4325 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004326 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004327
Takashi Iwaibefdf312005-08-22 13:57:55 +02004328 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329};
4330
4331/**
4332 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004333 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 * @rate: the sample rate
4335 * @channels: the number of channels
4336 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4337 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004338 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 *
4340 * Calculate the format bitset from the given rate, channels and th PCM format.
4341 *
4342 * Return zero if invalid.
4343 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004344unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4345 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 unsigned int channels,
4347 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004348 unsigned int maxbps,
4349 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
4351 int i;
4352 unsigned int val = 0;
4353
Takashi Iwaibefdf312005-08-22 13:57:55 +02004354 for (i = 0; rate_bits[i].hz; i++)
4355 if (rate_bits[i].hz == rate) {
4356 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 break;
4358 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004359 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004360 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return 0;
4362 }
4363
4364 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004365 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return 0;
4367 }
4368 val |= channels - 1;
4369
4370 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004371 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004372 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004373 break;
4374 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004375 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 case 20:
4378 case 24:
4379 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004380 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004381 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004383 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004385 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 break;
4387 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004388 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004389 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return 0;
4391 }
4392
Anssi Hannula32c168c2010-08-03 13:28:57 +03004393 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004394 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return val;
4397}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004398EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004400static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4401 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004402{
4403 unsigned int val = 0;
4404 if (nid != codec->afg &&
4405 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4406 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4407 if (!val || val == -1)
4408 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4409 if (!val || val == -1)
4410 return 0;
4411 return val;
4412}
4413
4414static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4415{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004416 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004417 get_pcm_param);
4418}
4419
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004420static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4421 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004422{
4423 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4424 if (!streams || streams == -1)
4425 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4426 if (!streams || streams == -1)
4427 return 0;
4428 return streams;
4429}
4430
4431static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4432{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004433 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004434 get_stream_param);
4435}
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437/**
4438 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4439 * @codec: the HDA codec
4440 * @nid: NID to query
4441 * @ratesp: the pointer to store the detected rate bitflags
4442 * @formatsp: the pointer to store the detected formats
4443 * @bpsp: the pointer to store the detected format widths
4444 *
4445 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4446 * or @bsps argument is ignored.
4447 *
4448 * Returns 0 if successful, otherwise a negative error code.
4449 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004450int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4452{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004453 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004455 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004456 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 if (ratesp) {
4459 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004460 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004462 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004464 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004465 codec_err(codec,
4466 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4467 nid, val,
4468 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004469 return -EIO;
4470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 *ratesp = rates;
4472 }
4473
4474 if (formatsp || bpsp) {
4475 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004476 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004478 streams = query_stream_param(codec, nid);
4479 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 bps = 0;
4483 if (streams & AC_SUPFMT_PCM) {
4484 if (val & AC_SUPPCM_BITS_8) {
4485 formats |= SNDRV_PCM_FMTBIT_U8;
4486 bps = 8;
4487 }
4488 if (val & AC_SUPPCM_BITS_16) {
4489 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4490 bps = 16;
4491 }
4492 if (wcaps & AC_WCAP_DIGITAL) {
4493 if (val & AC_SUPPCM_BITS_32)
4494 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4495 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4496 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4497 if (val & AC_SUPPCM_BITS_24)
4498 bps = 24;
4499 else if (val & AC_SUPPCM_BITS_20)
4500 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004501 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4502 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4504 if (val & AC_SUPPCM_BITS_32)
4505 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 else if (val & AC_SUPPCM_BITS_24)
4507 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004508 else if (val & AC_SUPPCM_BITS_20)
4509 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
4511 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004512#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004513 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004515 if (!bps)
4516 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004517 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004518#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004519 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004520 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 /* temporary hack: we have still no proper support
4522 * for the direct AC3 stream...
4523 */
4524 formats |= SNDRV_PCM_FMTBIT_U8;
4525 bps = 8;
4526 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004527 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004528 codec_err(codec,
4529 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4530 nid, val,
4531 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4532 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004533 return -EIO;
4534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 if (formatsp)
4536 *formatsp = formats;
4537 if (bpsp)
4538 *bpsp = bps;
4539 }
4540
4541 return 0;
4542}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004543EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004546 * snd_hda_is_supported_format - Check the validity of the format
4547 * @codec: HD-audio codec
4548 * @nid: NID to check
4549 * @format: the HD-audio format value to check
4550 *
4551 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 *
4553 * Returns 1 if supported, 0 if not.
4554 */
4555int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4556 unsigned int format)
4557{
4558 int i;
4559 unsigned int val = 0, rate, stream;
4560
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004561 val = query_pcm_param(codec, nid);
4562 if (!val)
4563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004566 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004567 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (val & (1 << i))
4569 break;
4570 return 0;
4571 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004572 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 return 0;
4574
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004575 stream = query_stream_param(codec, nid);
4576 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return 0;
4578
4579 if (stream & AC_SUPFMT_PCM) {
4580 switch (format & 0xf0) {
4581 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004582 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return 0;
4584 break;
4585 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004586 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return 0;
4588 break;
4589 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return 0;
4592 break;
4593 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004594 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return 0;
4596 break;
4597 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004598 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return 0;
4600 break;
4601 default:
4602 return 0;
4603 }
4604 } else {
4605 /* FIXME: check for float32 and AC3? */
4606 }
4607
4608 return 1;
4609}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004610EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612/*
4613 * PCM stuff
4614 */
4615static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004617 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
4619 return 0;
4620}
4621
4622static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4623 struct hda_codec *codec,
4624 unsigned int stream_tag,
4625 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004626 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
4628 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4629 return 0;
4630}
4631
4632static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4633 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004634 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Takashi Iwai888afa12008-03-18 09:57:50 +01004636 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return 0;
4638}
4639
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004640static int set_pcm_default_values(struct hda_codec *codec,
4641 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004643 int err;
4644
Takashi Iwai0ba21762007-04-16 11:29:14 +02004645 /* query support PCM information from the given NID */
4646 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004647 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004648 info->rates ? NULL : &info->rates,
4649 info->formats ? NULL : &info->formats,
4650 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004651 if (err < 0)
4652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654 if (info->ops.open == NULL)
4655 info->ops.open = hda_pcm_default_open_close;
4656 if (info->ops.close == NULL)
4657 info->ops.close = hda_pcm_default_open_close;
4658 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004659 if (snd_BUG_ON(!info->nid))
4660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 info->ops.prepare = hda_pcm_default_prepare;
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004664 if (snd_BUG_ON(!info->nid))
4665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 info->ops.cleanup = hda_pcm_default_cleanup;
4667 }
4668 return 0;
4669}
4670
Takashi Iwaieb541332010-08-06 13:48:11 +02004671/*
4672 * codec prepare/cleanup entries
4673 */
4674int snd_hda_codec_prepare(struct hda_codec *codec,
4675 struct hda_pcm_stream *hinfo,
4676 unsigned int stream,
4677 unsigned int format,
4678 struct snd_pcm_substream *substream)
4679{
4680 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004681 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004682 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4683 if (ret >= 0)
4684 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004685 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004686 return ret;
4687}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004688EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004689
4690void snd_hda_codec_cleanup(struct hda_codec *codec,
4691 struct hda_pcm_stream *hinfo,
4692 struct snd_pcm_substream *substream)
4693{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004694 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004695 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004696 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004697}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004698EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004699
Takashi Iwaid5191e52009-11-16 14:58:17 +01004700/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004701const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4702 "Audio", "SPDIF", "HDMI", "Modem"
4703};
4704
Takashi Iwai176d5332008-07-30 15:01:44 +02004705/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004706 * get the empty PCM device number to assign
4707 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004708static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004709{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004710 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004711 /* assigned to static slots up to dev#10; if more needed, assign
4712 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4713 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004714 static int audio_idx[HDA_PCM_NTYPES][5] = {
4715 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4716 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004717 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004718 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004719 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004720 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004721
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004722 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004723 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004724 return -EINVAL;
4725 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004726
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004727 for (i = 0; audio_idx[type][i] >= 0; i++) {
4728#ifndef CONFIG_SND_DYNAMIC_MINORS
4729 if (audio_idx[type][i] >= 8)
4730 break;
4731#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004732 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4733 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004734 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004735
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004736#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004737 /* non-fixed slots starting from 10 */
4738 for (i = 10; i < 32; i++) {
4739 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4740 return i;
4741 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004742#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004743
Takashi Iwai4e76a882014-02-25 12:21:03 +01004744 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004745 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004746#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004747 dev_warn(bus->card->dev,
4748 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004749#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004750 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004751}
4752
4753/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004754 * attach a new PCM stream
4755 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004756static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004757{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004758 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004759 struct hda_pcm_stream *info;
4760 int stream, err;
4761
Takashi Iwaib91f0802008-11-04 08:43:08 +01004762 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004763 return -EINVAL;
4764 for (stream = 0; stream < 2; stream++) {
4765 info = &pcm->stream[stream];
4766 if (info->substreams) {
4767 err = set_pcm_default_values(codec, info);
4768 if (err < 0)
4769 return err;
4770 }
4771 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004772 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004773}
4774
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004775/* assign all PCMs of the given codec */
4776int snd_hda_codec_build_pcms(struct hda_codec *codec)
4777{
4778 unsigned int pcm;
4779 int err;
4780
4781 if (!codec->num_pcms) {
4782 if (!codec->patch_ops.build_pcms)
4783 return 0;
4784 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004785 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004786 codec_err(codec,
4787 "cannot build PCMs for #%d (error %d)\n",
4788 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004789 err = snd_hda_codec_reset(codec);
4790 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004791 codec_err(codec,
4792 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004793 return err;
4794 }
4795 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004796 }
4797 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4798 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4799 int dev;
4800
4801 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004802 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004803
4804 if (!cpcm->pcm) {
4805 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4806 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004807 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004808 cpcm->device = dev;
4809 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004810 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004811 codec_err(codec,
4812 "cannot attach PCM stream %d for codec #%d\n",
4813 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004814 continue; /* no fatal error */
4815 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004816 }
4817 }
4818 return 0;
4819}
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821/**
4822 * snd_hda_build_pcms - build PCM information
4823 * @bus: the BUS
4824 *
4825 * Create PCM information for each codec included in the bus.
4826 *
4827 * The build_pcms codec patch is requested to set up codec->num_pcms and
4828 * codec->pcm_info properly. The array is referred by the top-level driver
4829 * to create its PCM instances.
4830 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4831 * callback.
4832 *
4833 * At least, substreams, channels_min and channels_max must be filled for
4834 * each stream. substreams = 0 indicates that the stream doesn't exist.
4835 * When rates and/or formats are zero, the supported values are queried
4836 * from the given nid. The nid is used also by the default ops.prepare
4837 * and ops.cleanup callbacks.
4838 *
4839 * The driver needs to call ops.open in its open callback. Similarly,
4840 * ops.close is supposed to be called in the close callback.
4841 * ops.prepare should be called in the prepare or hw_params callback
4842 * with the proper parameters for set up.
4843 * ops.cleanup should be called in hw_free for clean up of streams.
4844 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004845 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004847int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004849 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Takashi Iwai0ba21762007-04-16 11:29:14 +02004851 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004852 int err = snd_hda_codec_build_pcms(codec);
4853 if (err < 0)
4854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
4856 return 0;
4857}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004858EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 * snd_hda_add_new_ctls - create controls from the array
4862 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004863 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 *
4865 * This helper function creates and add new controls in the given array.
4866 * The array must be terminated with an empty entry as terminator.
4867 *
4868 * Returns 0 if successful, or a negative error code.
4869 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004870int snd_hda_add_new_ctls(struct hda_codec *codec,
4871 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004873 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004876 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004877 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004878 if (knew->iface == -1) /* skip this codec private value */
4879 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004880 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004881 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004882 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004883 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004884 if (addr > 0)
4885 kctl->id.device = addr;
4886 if (idx > 0)
4887 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004888 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004889 if (!err)
4890 break;
4891 /* try first with another device index corresponding to
4892 * the codec addr; if it still fails (or it's the
4893 * primary codec), then try another control index
4894 */
4895 if (!addr && codec->addr)
4896 addr = codec->addr;
4897 else if (!idx && !knew->index) {
4898 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004899 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004900 if (idx <= 0)
4901 return err;
4902 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004903 return err;
4904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 }
4906 return 0;
4907}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004908EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Takashi Iwai83012a72012-08-24 18:38:08 +02004910#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004911static void hda_power_work(struct work_struct *work)
4912{
4913 struct hda_codec *codec =
4914 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004915 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004916 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004917
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004918 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004919 if (codec->power_transition > 0) { /* during power-up sequence? */
4920 spin_unlock(&codec->power_lock);
4921 return;
4922 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004923 if (!codec->power_on || codec->power_count) {
4924 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004925 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004926 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004927 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004928 spin_unlock(&codec->power_lock);
4929
Dylan Reidd17344b2012-09-28 15:57:01 -07004930 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004931 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4932 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004933}
4934
4935static void hda_keep_power_on(struct hda_codec *codec)
4936{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004937 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004938 codec->power_count++;
4939 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004940 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004941 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004942 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004943}
4944
Takashi Iwaid5191e52009-11-16 14:58:17 +01004945/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004946void snd_hda_update_power_acct(struct hda_codec *codec)
4947{
4948 unsigned long delta = jiffies - codec->power_jiffies;
4949 if (codec->power_on)
4950 codec->power_on_acct += delta;
4951 else
4952 codec->power_off_acct += delta;
4953 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004954}
4955
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004956/* Transition to powered up, if wait_power_down then wait for a pending
4957 * transition to D3 to complete. A pending D3 transition is indicated
4958 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004959/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004960static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004961{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004962 /* Return if power_on or transitioning to power_on, unless currently
4963 * powering down. */
4964 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004965 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004966 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004967 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004968
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004969 cancel_delayed_work_sync(&codec->power_work);
4970
4971 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004972 /* If the power down delayed work was cancelled above before starting,
4973 * then there is no need to go through power up here.
4974 */
4975 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004976 if (codec->power_transition < 0)
4977 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004978 return;
4979 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004980
Takashi Iwaid66fee52011-08-02 15:39:31 +02004981 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004982 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004983 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004984 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004985 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004986 spin_unlock(&codec->power_lock);
4987
Takashi Iwaicb53c622007-08-10 17:21:45 +02004988 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004989
4990 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004991 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004992}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004993
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004994#define power_save(codec) \
4995 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004996
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004997/* Transition to powered down */
4998static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004999{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005000 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005001 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005002
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005003 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005004 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005005 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005006 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005007 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005008}
5009
5010/**
5011 * snd_hda_power_save - Power-up/down/sync the codec
5012 * @codec: HD-audio codec
5013 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005014 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005015 *
5016 * Change the power-up counter via @delta, and power up or down the hardware
5017 * appropriately. For the power-down, queue to the delayed action.
5018 * Passing zero to @delta means to synchronize the power state.
5019 */
5020void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5021{
5022 spin_lock(&codec->power_lock);
5023 codec->power_count += delta;
5024 trace_hda_power_count(codec);
5025 if (delta > 0)
5026 __snd_hda_power_up(codec, d3wait);
5027 else
5028 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005029 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005030}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005031EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005032
Takashi Iwaid5191e52009-11-16 14:58:17 +01005033/**
5034 * snd_hda_check_amp_list_power - Check the amp list and update the power
5035 * @codec: HD-audio codec
5036 * @check: the object containing an AMP list and the status
5037 * @nid: NID to check / update
5038 *
5039 * Check whether the given NID is in the amp list. If it's in the list,
5040 * check the current AMP status, and update the the power-status according
5041 * to the mute status.
5042 *
5043 * This function is supposed to be set or called from the check_power_status
5044 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005045 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005046int snd_hda_check_amp_list_power(struct hda_codec *codec,
5047 struct hda_loopback_check *check,
5048 hda_nid_t nid)
5049{
Takashi Iwai031024e2011-05-02 11:29:30 +02005050 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005051 int ch, v;
5052
5053 if (!check->amplist)
5054 return 0;
5055 for (p = check->amplist; p->nid; p++) {
5056 if (p->nid == nid)
5057 break;
5058 }
5059 if (!p->nid)
5060 return 0; /* nothing changed */
5061
5062 for (p = check->amplist; p->nid; p++) {
5063 for (ch = 0; ch < 2; ch++) {
5064 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5065 p->idx);
5066 if (!(v & HDA_AMP_MUTE) && v > 0) {
5067 if (!check->power_on) {
5068 check->power_on = 1;
5069 snd_hda_power_up(codec);
5070 }
5071 return 1;
5072 }
5073 }
5074 }
5075 if (check->power_on) {
5076 check->power_on = 0;
5077 snd_hda_power_down(codec);
5078 }
5079 return 0;
5080}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005081EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005084/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005085 * Channel mode helper
5086 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005087
5088/**
5089 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005090 * @codec: the HDA codec
5091 * @uinfo: pointer to get/store the data
5092 * @chmode: channel mode array
5093 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005095int snd_hda_ch_mode_info(struct hda_codec *codec,
5096 struct snd_ctl_elem_info *uinfo,
5097 const struct hda_channel_mode *chmode,
5098 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005099{
5100 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5101 uinfo->count = 1;
5102 uinfo->value.enumerated.items = num_chmodes;
5103 if (uinfo->value.enumerated.item >= num_chmodes)
5104 uinfo->value.enumerated.item = num_chmodes - 1;
5105 sprintf(uinfo->value.enumerated.name, "%dch",
5106 chmode[uinfo->value.enumerated.item].channels);
5107 return 0;
5108}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005109EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005110
Takashi Iwaid5191e52009-11-16 14:58:17 +01005111/**
5112 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005113 * @codec: the HDA codec
5114 * @ucontrol: pointer to get/store the data
5115 * @chmode: channel mode array
5116 * @num_chmodes: channel mode array size
5117 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005118 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005119int snd_hda_ch_mode_get(struct hda_codec *codec,
5120 struct snd_ctl_elem_value *ucontrol,
5121 const struct hda_channel_mode *chmode,
5122 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005123 int max_channels)
5124{
5125 int i;
5126
5127 for (i = 0; i < num_chmodes; i++) {
5128 if (max_channels == chmode[i].channels) {
5129 ucontrol->value.enumerated.item[0] = i;
5130 break;
5131 }
5132 }
5133 return 0;
5134}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005135EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005136
Takashi Iwaid5191e52009-11-16 14:58:17 +01005137/**
5138 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005139 * @codec: the HDA codec
5140 * @ucontrol: pointer to get/store the data
5141 * @chmode: channel mode array
5142 * @num_chmodes: channel mode array size
5143 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005144 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005145int snd_hda_ch_mode_put(struct hda_codec *codec,
5146 struct snd_ctl_elem_value *ucontrol,
5147 const struct hda_channel_mode *chmode,
5148 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005149 int *max_channelsp)
5150{
5151 unsigned int mode;
5152
5153 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005154 if (mode >= num_chmodes)
5155 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005156 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005157 return 0;
5158 /* change the current channel setting */
5159 *max_channelsp = chmode[mode].channels;
5160 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005161 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005162 return 1;
5163}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005164EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166/*
5167 * input MUX helper
5168 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005169
5170/**
5171 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005172 * @imux: imux helper object
5173 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005175int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5176 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
5178 unsigned int index;
5179
5180 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5181 uinfo->count = 1;
5182 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005183 if (!imux->num_items)
5184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 index = uinfo->value.enumerated.item;
5186 if (index >= imux->num_items)
5187 index = imux->num_items - 1;
5188 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5189 return 0;
5190}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005191EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Takashi Iwaid5191e52009-11-16 14:58:17 +01005193/**
5194 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005195 * @codec: the HDA codec
5196 * @imux: imux helper object
5197 * @ucontrol: pointer to get/store the data
5198 * @nid: input mux NID
5199 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005200 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005201int snd_hda_input_mux_put(struct hda_codec *codec,
5202 const struct hda_input_mux *imux,
5203 struct snd_ctl_elem_value *ucontrol,
5204 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 unsigned int *cur_val)
5206{
5207 unsigned int idx;
5208
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005209 if (!imux->num_items)
5210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 idx = ucontrol->value.enumerated.item[0];
5212 if (idx >= imux->num_items)
5213 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005214 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005216 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5217 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 *cur_val = idx;
5219 return 1;
5220}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005221EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
5223
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005224/**
5225 * snd_hda_enum_helper_info - Helper for simple enum ctls
5226 * @kcontrol: ctl element
5227 * @uinfo: pointer to get/store the data
5228 * @num_items: number of enum items
5229 * @texts: enum item string array
5230 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005231 * process kcontrol info callback of a simple string enum array
5232 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5233 */
5234int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5235 struct snd_ctl_elem_info *uinfo,
5236 int num_items, const char * const *texts)
5237{
5238 static const char * const texts_default[] = {
5239 "Disabled", "Enabled"
5240 };
5241
5242 if (!texts || !num_items) {
5243 num_items = 2;
5244 texts = texts_default;
5245 }
5246
Takashi Iwai3ff72212014-10-20 18:17:28 +02005247 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005248}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005249EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005250
5251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 * Multi-channel / digital-out PCM helper functions
5253 */
5254
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005255/* setup SPDIF output stream */
5256static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5257 unsigned int stream_tag, unsigned int format)
5258{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005259 struct hda_spdif_out *spdif;
5260 unsigned int curr_fmt;
5261 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005262
Laurence Darby3bef1c32012-11-03 17:00:06 +00005263 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5264 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5265 AC_VERB_GET_STREAM_FORMAT, 0);
5266 reset = codec->spdif_status_reset &&
5267 (spdif->ctls & AC_DIG1_ENABLE) &&
5268 curr_fmt != format;
5269
5270 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5271 updated */
5272 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005273 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005274 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005275 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005276 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005277 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005278 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005279 for (d = codec->slave_dig_outs; *d; d++)
5280 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5281 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005282 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005283 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005284 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005285 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005286 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005287}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005288
Takashi Iwai2f728532008-09-25 16:32:41 +02005289static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5290{
5291 snd_hda_codec_cleanup_stream(codec, nid);
5292 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005293 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005294 for (d = codec->slave_dig_outs; *d; d++)
5295 snd_hda_codec_cleanup_stream(codec, *d);
5296 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005297}
5298
Takashi Iwaid5191e52009-11-16 14:58:17 +01005299/**
5300 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5301 * @bus: HD-audio bus
5302 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005303void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5304{
5305 struct hda_codec *codec;
5306
5307 if (!bus)
5308 return;
5309 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005310 if (hda_codec_is_power_on(codec) &&
5311 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005312 codec->patch_ops.reboot_notify(codec);
5313 }
5314}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005315EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005316
Takashi Iwaid5191e52009-11-16 14:58:17 +01005317/**
5318 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005319 * @codec: the HDA codec
5320 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005322int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5323 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
Ingo Molnar62932df2006-01-16 16:34:20 +01005325 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005326 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5327 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005328 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005330 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return 0;
5332}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005333EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Takashi Iwaid5191e52009-11-16 14:58:17 +01005335/**
5336 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005337 * @codec: the HDA codec
5338 * @mout: hda_multi_out object
5339 * @stream_tag: stream tag to assign
5340 * @format: format id to assign
5341 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005342 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005343int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5344 struct hda_multi_out *mout,
5345 unsigned int stream_tag,
5346 unsigned int format,
5347 struct snd_pcm_substream *substream)
5348{
5349 mutex_lock(&codec->spdif_mutex);
5350 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5351 mutex_unlock(&codec->spdif_mutex);
5352 return 0;
5353}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005354EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005355
Takashi Iwaid5191e52009-11-16 14:58:17 +01005356/**
5357 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005358 * @codec: the HDA codec
5359 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005360 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005361int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5362 struct hda_multi_out *mout)
5363{
5364 mutex_lock(&codec->spdif_mutex);
5365 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5366 mutex_unlock(&codec->spdif_mutex);
5367 return 0;
5368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005369EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005370
Takashi Iwaid5191e52009-11-16 14:58:17 +01005371/**
5372 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005373 * @codec: the HDA codec
5374 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005376int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5377 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
Ingo Molnar62932df2006-01-16 16:34:20 +01005379 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005381 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return 0;
5383}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005384EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Takashi Iwaid5191e52009-11-16 14:58:17 +01005386/**
5387 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005388 * @codec: the HDA codec
5389 * @mout: hda_multi_out object
5390 * @substream: PCM substream to assign
5391 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005392 *
5393 * Open analog outputs and set up the hw-constraints.
5394 * If the digital outputs can be opened as slave, open the digital
5395 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005397int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5398 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005399 struct snd_pcm_substream *substream,
5400 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Takashi Iwai9a081602008-02-12 18:37:26 +01005402 struct snd_pcm_runtime *runtime = substream->runtime;
5403 runtime->hw.channels_max = mout->max_channels;
5404 if (mout->dig_out_nid) {
5405 if (!mout->analog_rates) {
5406 mout->analog_rates = hinfo->rates;
5407 mout->analog_formats = hinfo->formats;
5408 mout->analog_maxbps = hinfo->maxbps;
5409 } else {
5410 runtime->hw.rates = mout->analog_rates;
5411 runtime->hw.formats = mout->analog_formats;
5412 hinfo->maxbps = mout->analog_maxbps;
5413 }
5414 if (!mout->spdif_rates) {
5415 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5416 &mout->spdif_rates,
5417 &mout->spdif_formats,
5418 &mout->spdif_maxbps);
5419 }
5420 mutex_lock(&codec->spdif_mutex);
5421 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005422 if ((runtime->hw.rates & mout->spdif_rates) &&
5423 (runtime->hw.formats & mout->spdif_formats)) {
5424 runtime->hw.rates &= mout->spdif_rates;
5425 runtime->hw.formats &= mout->spdif_formats;
5426 if (mout->spdif_maxbps < hinfo->maxbps)
5427 hinfo->maxbps = mout->spdif_maxbps;
5428 } else {
5429 mout->share_spdif = 0;
5430 /* FIXME: need notify? */
5431 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005432 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005433 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5436 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5437}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005438EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Takashi Iwaid5191e52009-11-16 14:58:17 +01005440/**
5441 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005442 * @codec: the HDA codec
5443 * @mout: hda_multi_out object
5444 * @stream_tag: stream tag to assign
5445 * @format: format id to assign
5446 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005447 *
5448 * Set up the i/o for analog out.
5449 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005451int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5452 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 unsigned int stream_tag,
5454 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005455 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
Takashi Iwaidda14412011-05-02 11:29:30 +02005457 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005459 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 int i;
5461
Ingo Molnar62932df2006-01-16 16:34:20 +01005462 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005463 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005464 if (mout->dig_out_nid && mout->share_spdif &&
5465 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005467 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5468 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005469 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005471 setup_dig_out_stream(codec, mout->dig_out_nid,
5472 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 } else {
5474 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005475 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 }
5477 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005478 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005481 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5482 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005483 if (!mout->no_share_stream &&
5484 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005486 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5487 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005488 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005489 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5490 if (!mout->no_share_stream && mout->hp_out_nid[i])
5491 snd_hda_codec_setup_stream(codec,
5492 mout->hp_out_nid[i],
5493 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 /* surrounds */
5496 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005497 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005498 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5499 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005500 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005501 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5502 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005504
5505 /* extra surrounds */
5506 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5507 int ch = 0;
5508 if (!mout->extra_out_nid[i])
5509 break;
5510 if (chs >= (i + 1) * 2)
5511 ch = i * 2;
5512 else if (!mout->no_share_stream)
5513 break;
5514 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5515 stream_tag, ch, format);
5516 }
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 return 0;
5519}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005520EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Takashi Iwaid5191e52009-11-16 14:58:17 +01005522/**
5523 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005524 * @codec: the HDA codec
5525 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005527int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5528 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529{
Takashi Iwaidda14412011-05-02 11:29:30 +02005530 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 int i;
5532
5533 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005534 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005536 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005537 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5538 if (mout->hp_out_nid[i])
5539 snd_hda_codec_cleanup_stream(codec,
5540 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005541 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5542 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005543 snd_hda_codec_cleanup_stream(codec,
5544 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005545 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005547 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 mout->dig_out_used = 0;
5549 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005550 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 return 0;
5552}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005553EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Takashi Iwai47408602012-04-20 13:06:53 +02005555/**
5556 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005557 * @codec: the HDA codec
5558 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005559 *
5560 * Guess the suitable VREF pin bits to be set as the pin-control value.
5561 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5562 */
5563unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5564{
5565 unsigned int pincap;
5566 unsigned int oldval;
5567 oldval = snd_hda_codec_read(codec, pin, 0,
5568 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5569 pincap = snd_hda_query_pin_caps(codec, pin);
5570 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5571 /* Exception: if the default pin setup is vref50, we give it priority */
5572 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5573 return AC_PINCTL_VREF_80;
5574 else if (pincap & AC_PINCAP_VREF_50)
5575 return AC_PINCTL_VREF_50;
5576 else if (pincap & AC_PINCAP_VREF_100)
5577 return AC_PINCTL_VREF_100;
5578 else if (pincap & AC_PINCAP_VREF_GRD)
5579 return AC_PINCTL_VREF_GRD;
5580 return AC_PINCTL_VREF_HIZ;
5581}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005582EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005583
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005584/**
5585 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5586 * @codec: the HDA codec
5587 * @pin: referred pin NID
5588 * @val: pin ctl value to audit
5589 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005590unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5591 hda_nid_t pin, unsigned int val)
5592{
5593 static unsigned int cap_lists[][2] = {
5594 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5595 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5596 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5597 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5598 };
5599 unsigned int cap;
5600
5601 if (!val)
5602 return 0;
5603 cap = snd_hda_query_pin_caps(codec, pin);
5604 if (!cap)
5605 return val; /* don't know what to do... */
5606
5607 if (val & AC_PINCTL_OUT_EN) {
5608 if (!(cap & AC_PINCAP_OUT))
5609 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5610 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5611 val &= ~AC_PINCTL_HP_EN;
5612 }
5613
5614 if (val & AC_PINCTL_IN_EN) {
5615 if (!(cap & AC_PINCAP_IN))
5616 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5617 else {
5618 unsigned int vcap, vref;
5619 int i;
5620 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5621 vref = val & AC_PINCTL_VREFEN;
5622 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5623 if (vref == cap_lists[i][0] &&
5624 !(vcap & cap_lists[i][1])) {
5625 if (i == ARRAY_SIZE(cap_lists) - 1)
5626 vref = AC_PINCTL_VREF_HIZ;
5627 else
5628 vref = cap_lists[i + 1][0];
5629 }
5630 }
5631 val &= ~AC_PINCTL_VREFEN;
5632 val |= vref;
5633 }
5634 }
5635
5636 return val;
5637}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005638EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005639
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005640/**
5641 * _snd_hda_pin_ctl - Helper to set pin ctl value
5642 * @codec: the HDA codec
5643 * @pin: referred pin NID
5644 * @val: pin control value to set
5645 * @cached: access over codec pinctl cache or direct write
5646 *
5647 * This function is a helper to set a pin ctl value more safely.
5648 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5649 * value in pin target array via snd_hda_codec_set_pin_target(), then
5650 * actually writes the value via either snd_hda_codec_update_cache() or
5651 * snd_hda_codec_write() depending on @cached flag.
5652 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005653int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5654 unsigned int val, bool cached)
5655{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005656 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005657 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005658 if (cached)
5659 return snd_hda_codec_update_cache(codec, pin, 0,
5660 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5661 else
5662 return snd_hda_codec_write(codec, pin, 0,
5663 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5664}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005665EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005666
Takashi Iwai990061c2010-09-09 22:08:44 +02005667/**
5668 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005669 * @codec: the HDA codec
5670 * @imux: imux helper object
5671 * @label: the name of imux item to assign
5672 * @index: index number of imux item to assign
5673 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005674 *
5675 * When the same label is used already in the existing items, the number
5676 * suffix is appended to the label. This label index number is stored
5677 * to type_idx when non-NULL pointer is given.
5678 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005679int snd_hda_add_imux_item(struct hda_codec *codec,
5680 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005681 int index, int *type_idx)
5682{
5683 int i, label_idx = 0;
5684 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005685 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005686 return -EINVAL;
5687 }
5688 for (i = 0; i < imux->num_items; i++) {
5689 if (!strncmp(label, imux->items[i].label, strlen(label)))
5690 label_idx++;
5691 }
5692 if (type_idx)
5693 *type_idx = label_idx;
5694 if (label_idx > 0)
5695 snprintf(imux->items[imux->num_items].label,
5696 sizeof(imux->items[imux->num_items].label),
5697 "%s %d", label, label_idx);
5698 else
5699 strlcpy(imux->items[imux->num_items].label, label,
5700 sizeof(imux->items[imux->num_items].label));
5701 imux->items[imux->num_items].index = index;
5702 imux->num_items++;
5703 return 0;
5704}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005705EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005706
Takashi Iwai4a471b72005-12-07 13:56:29 +01005707
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708#ifdef CONFIG_PM
5709/*
5710 * power management
5711 */
5712
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005713
5714static void hda_async_suspend(void *data, async_cookie_t cookie)
5715{
5716 hda_call_codec_suspend(data, false);
5717}
5718
5719static void hda_async_resume(void *data, async_cookie_t cookie)
5720{
5721 hda_call_codec_resume(data);
5722}
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724/**
5725 * snd_hda_suspend - suspend the codecs
5726 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 *
5728 * Returns 0 if successful.
5729 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005730int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005732 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005733 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Takashi Iwai0ba21762007-04-16 11:29:14 +02005735 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005736 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005737 if (hda_codec_is_power_on(codec)) {
5738 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005739 async_schedule_domain(hda_async_suspend, codec,
5740 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005741 else
5742 hda_call_codec_suspend(codec, false);
5743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005745
5746 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005747 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return 0;
5750}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005751EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753/**
5754 * snd_hda_resume - resume the codecs
5755 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 *
5757 * Returns 0 if successful.
5758 */
5759int snd_hda_resume(struct hda_bus *bus)
5760{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005761 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005762 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
Takashi Iwai0ba21762007-04-16 11:29:14 +02005764 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005765 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005766 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005767 else
5768 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005770
5771 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005772 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005773
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 return 0;
5775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005776EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005777#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005778
5779/*
5780 * generic arrays
5781 */
5782
Takashi Iwaid5191e52009-11-16 14:58:17 +01005783/**
5784 * snd_array_new - get a new element from the given array
5785 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005786 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005787 * Get a new element from the given array. If it exceeds the
5788 * pre-allocated array size, re-allocate the array.
5789 *
5790 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005791 */
5792void *snd_array_new(struct snd_array *array)
5793{
Takashi Iwai12f17712012-10-10 08:59:14 +02005794 if (snd_BUG_ON(!array->elem_size))
5795 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005796 if (array->used >= array->alloced) {
5797 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005798 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005799 void *nlist;
5800 if (snd_BUG_ON(num >= 4096))
5801 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005802 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005803 if (!nlist)
5804 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005805 array->list = nlist;
5806 array->alloced = num;
5807 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005808 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005809}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005810EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005811
Takashi Iwaid5191e52009-11-16 14:58:17 +01005812/**
5813 * snd_array_free - free the given array elements
5814 * @array: the array object
5815 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005816void snd_array_free(struct snd_array *array)
5817{
5818 kfree(array->list);
5819 array->used = 0;
5820 array->alloced = 0;
5821 array->list = NULL;
5822}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005823EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005824
Takashi Iwaid5191e52009-11-16 14:58:17 +01005825/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005826 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5827 * @pcm: PCM caps bits
5828 * @buf: the string buffer to write
5829 * @buflen: the max buffer length
5830 *
5831 * used by hda_proc.c and hda_eld.c
5832 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005833void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5834{
5835 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5836 int i, j;
5837
5838 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5839 if (pcm & (AC_SUPPCM_BITS_8 << i))
5840 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5841
5842 buf[j] = '\0'; /* necessary when j == 0 */
5843}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005844EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005845
5846MODULE_DESCRIPTION("HDA codec core");
5847MODULE_LICENSE("GPL");